Skip to content

Commit

Permalink
Updating eslintrc (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: fzhao99 <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 836844a commit 09795b4
Show file tree
Hide file tree
Showing 14 changed files with 2,764 additions and 539 deletions.
3 changes: 2 additions & 1 deletion query-connector/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"publicOnly": true
}
]
],
"@typescript-eslint/no-explicit-any": "error"
},
"overrides": [
{
Expand Down
3,069 changes: 2,623 additions & 446 deletions query-connector/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion query-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"classnames": "^2.5.1",
"date-fns": "^3.4.0",
"dotenv": "^16.4.5",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-jsx-a11y": "^6.10.1",
"eslint-plugin-requirejs": "^4.0.1",
"fhirpath": "3.10.4",
"html-react-parser": "^5.1.4",
"jest-fixed-jsdom": "^0.0.4",
Expand Down Expand Up @@ -68,7 +72,7 @@
"autoprefixer": "^10.0.1",
"aws-sdk-client-mock": "^3.0.1",
"chai": "^4.3.10",
"eslint": "^8.56.0",
"eslint": "^8.57.1",
"eslint-config-next": "14.0.3",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^48.2.3",
Expand Down
10 changes: 9 additions & 1 deletion query-connector/src/app/CustomQuery.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
interface CustomQuerySpec {
labCodes?: string[];
snomedCodes?: string[];
rxnormCodes?: string[];
classTypeCodes?: string[];
hasSecondEncounterQuery?: boolean;
}

/**
* A Data Class designed to store and manipulate various code values used
* to create a fully customized FHIR query. The class holds instance variables
Expand Down Expand Up @@ -35,7 +43,7 @@ export class CustomQuery {
* @param jsonSpec A JSON Object containing four code fields to load.
* @param patientId The ID of the patient to build into query strings.
*/
constructor(jsonSpec: any, patientId: string) {
constructor(jsonSpec: CustomQuerySpec, patientId: string) {
try {
this.labCodes = jsonSpec?.labCodes || [];
this.snomedCodes = jsonSpec?.snomedCodes || [];
Expand Down
7 changes: 5 additions & 2 deletions query-connector/src/app/api/query/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ export async function POST(request: NextRequest) {
const OperationOutcome = await handleRequestError(diagnostics_message);
return NextResponse.json(OperationOutcome);
}
} catch (error: any) {
const diagnostics_message = `${error.message}`;
} catch (error: unknown) {
let diagnostics_message = "An error occurred.";
if (error instanceof Error) {
diagnostics_message = `${error.message}`;
}
const OperationOutcome = await handleRequestError(diagnostics_message);
return NextResponse.json(OperationOutcome);
}
Expand Down
40 changes: 39 additions & 1 deletion query-connector/src/app/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import {
Patient,
Observation,
DiagnosticReport,
Condition,
Encounter,
Medication,
MedicationAdministration,
MedicationRequest,
} from "fhir/r4";
/**
* The use cases that can be used in the app
*/
Expand Down Expand Up @@ -324,5 +334,33 @@ export interface ValueSetDisplay {
medications: ValueSet[];
conditions: ValueSet[];
}

export type DibbsValueSetType = keyof ValueSetDisplay;

// Define the type guard for FHIR resources
// Define the FHIR Resource types
export type FhirResource =
| Patient
| Observation
| DiagnosticReport
| Condition
| Encounter
| Medication
| MedicationAdministration
| MedicationRequest;

/**
* A type guard function that checks if the given resource is a valid FHIR resource.
* This ensures the resource has a `resourceType` field and is one of the allowed
* resource types (Patient, Observation, DiagnosticReport, Condition, etc.).
* @param resource - The resource to check.
* @returns True if the resource is a valid FHIR resource, false otherwise.
*/
// Define a type guard to check if the object is a FHIR resource
export function isFhirResource(resource: unknown): resource is FhirResource {
return (
resource !== null &&
typeof resource === "object" &&
resource !== undefined &&
"resourceType" in resource
);
}
2 changes: 1 addition & 1 deletion query-connector/src/app/format-service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export function formatMRN(identifier: Identifier[]): JSX.Element {
* @param dateString - The date string to be formatted. formatDate will also be able to take 'yyyymmdd' as input.
* @returns - The formatted date string, "" if input date was invalid, or undefined if the input date is falsy.
*/
export const formatDate = (dateString?: string): string | undefined => {
export const formatDate = (dateString?: string | null): string | undefined => {
if (dateString) {
let date = new Date(dateString);

Expand Down
65 changes: 33 additions & 32 deletions query-connector/src/app/query-service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"use server";
import fetch from "node-fetch";
import {
Patient,
Observation,
DiagnosticReport,
Condition,
Encounter,
Medication,
MedicationAdministration,
MedicationRequest,
Bundle,
} from "fhir/r4";
import { Bundle, DomainResource } from "fhir/r4";

import FHIRClient from "./fhir-servers";
import { USE_CASES, FHIR_SERVERS, ValueSet } from "./constants";
import {
USE_CASES,
FHIR_SERVERS,
ValueSet,
isFhirResource,
FhirResource,
} from "./constants";

import { CustomQuery } from "./CustomQuery";
import { GetPhoneQueryFormats } from "./format-service";
import { formatValueSetsAsQuerySpec } from "./format-service";
Expand All @@ -22,14 +19,13 @@ import { formatValueSetsAsQuerySpec } from "./format-service";
* The query response when the request source is from the Viewer UI.
*/
export type QueryResponse = {
Patient?: Patient[];
Observation?: Observation[];
DiagnosticReport?: DiagnosticReport[];
Condition?: Condition[];
Encounter?: Encounter[];
Medication?: Medication[];
MedicationAdministration?: MedicationAdministration[];
MedicationRequest?: MedicationRequest[];
[R in FhirResource as R["resourceType"]]?: R[];
};

// workaround types to get around a typescript compilation issue
type SuperSetFhirResource = DomainResource | FhirResource;
type SuperSetQueryResponse = {
[R in SuperSetFhirResource as R["resourceType"]]?: R[];
};

export type APIQueryResponse = Bundle;
Expand Down Expand Up @@ -228,23 +224,23 @@ async function generalizedQuery(
*/
export async function parseFhirSearch(
response: fetch.Response | Array<fetch.Response>,
queryResponse: QueryResponse = {},
queryResponse: SuperSetQueryResponse = {},
): Promise<QueryResponse> {
let resourceArray: any[] = [];
let resourceArray: SuperSetFhirResource[] = [];

// Process the responses
// Process the responses and flatten them
if (Array.isArray(response)) {
for (const r of response) {
resourceArray = resourceArray.concat(await processResponse(r));
}
resourceArray = (
await Promise.all(response.map(processFhirResponse))
).flat();
} else {
resourceArray = await processResponse(response);
resourceArray = await processFhirResponse(response);
}

// Add resources to queryResponse
for (const resource of resourceArray) {
const resourceType = resource.resourceType as keyof QueryResponse;
if (!queryResponse[resourceType]) {
const resourceType = resource.resourceType;
if (!(resourceType in queryResponse)) {
queryResponse[resourceType] = [resource];
} else {
queryResponse[resourceType]!.push(resource);
Expand All @@ -259,14 +255,19 @@ export async function parseFhirSearch(
* @param response - The response from the FHIR server.
* @returns - The array of resources from the response.
*/
export async function processResponse(
export async function processFhirResponse(
response: fetch.Response,
): Promise<any[]> {
let resourceArray: any[] = [];
): Promise<FhirResource[]> {
let resourceArray: FhirResource[] = [];
if (response.status === 200) {
const body = await response.json();
if (body.entry) {
for (const entry of body.entry) {
if (!isFhirResource(entry.resource)) {
console.error(
"Entry in FHIR resource response parsing was of unexpected shape",
);
}
resourceArray.push(entry.resource);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ type Lengthwise = {
length: number;
};

function isLengthwise(thing: any): thing is Lengthwise {
return thing?.length !== undefined;
function isLengthwise(thing: unknown): thing is Lengthwise {
return typeof thing === "object" && thing !== null && "length" in thing;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion query-connector/src/app/tests/shared_utils/readJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as fs from "fs";
* @param filePath The relative string path to the file.
* @returns A JSON object of the string representation of the file.
*/
export function readJsonFile(filePath: string): any {
export function readJsonFile(filePath: string): unknown {
try {
const data = fs.readFileSync(filePath, "utf-8");
return JSON.parse(data);
Expand Down
8 changes: 4 additions & 4 deletions query-connector/src/app/tests/unit/format-service.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ describe("formatDate", () => {
});

it("should return N/A if provided date is undefined", () => {
const inputDate = undefined;
const inputDate: string | undefined = undefined;

const result = formatDate(inputDate as any);
const result = formatDate(inputDate);
expect(result).toBeUndefined();
});

it("should return N/A if provided date is null", () => {
const inputDate = null;
const inputDate: string | null = null;

const result = formatDate(inputDate as any);
const result = formatDate(inputDate);
expect(result).toBeUndefined();
});
});
Expand Down
Loading

0 comments on commit 09795b4

Please sign in to comment.