Skip to content

Commit 2ebda89

Browse files
andreyorstryukzak
andcommitted
WIP migrate to Bundle type
Co-authored-by: Aleksandr Penskoi <alexandr.penskoi@health-samurai.io>
1 parent de1ebac commit 2ebda89

File tree

19 files changed

+226
-215
lines changed

19 files changed

+226
-215
lines changed

src/AidboxClient.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import type * as Aidbox from "@health-samurai/aidbox-client";
22
import { makeClient } from "@health-samurai/aidbox-client";
33
import { redirect } from "@tanstack/react-router";
44
import * as React from "react";
5+
import type { Bundle, OperationOutcome } from "./fhir-types/hl7-fhir-r5-core";
6+
7+
export type AidboxClientR5 = Aidbox.AidboxClient<Bundle, OperationOutcome>;
58

69
export const AidboxClientContext = React.createContext<
7-
Aidbox.AidboxClient | undefined
10+
AidboxClientR5 | undefined
811
>(undefined);
912

1013
export type AidboxClientProviderProps = {
@@ -29,7 +32,7 @@ export function AidboxClientProvider({
2932
baseurl,
3033
children,
3134
}: AidboxClientProviderProps): React.JSX.Element {
32-
const client = makeClient({
35+
const client = makeClient<Bundle, OperationOutcome>({
3336
baseurl,
3437
onRawResponseHook: makeAuthHandler(baseurl),
3538
});
@@ -41,9 +44,7 @@ export function AidboxClientProvider({
4144
);
4245
}
4346

44-
export function useAidboxClient(
45-
aidboxClient?: Aidbox.AidboxClient,
46-
): Aidbox.AidboxClient {
47+
export function useAidboxClient(aidboxClient?: AidboxClientR5): AidboxClientR5 {
4748
const client = React.useContext(AidboxClientContext);
4849

4950
if (aidboxClient) return aidboxClient;

src/api/utils.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,30 @@ import { AidboxClientError } from "@health-samurai/aidbox-client";
66
import * as HSComp from "@health-samurai/react-components";
77
import type { MutationFunctionContext } from "@tanstack/react-query";
88

9+
export function toastError(expression: string, diagnostics: string) {
10+
HSComp.toast.error(
11+
<div className="text-left">
12+
<b>{expression}</b>
13+
<p>{diagnostics}</p>
14+
</div>,
15+
{
16+
position: "bottom-right",
17+
style: {
18+
margin: "1rem",
19+
backgroundColor: "var(--destructive)",
20+
color: "var(--accent)",
21+
},
22+
},
23+
);
24+
}
25+
926
export function parseOperationOutcome(
1027
oo: OperationOutcome,
1128
): { expression: string; diagnostics: string }[] {
1229
const issues = oo.issue;
1330

1431
return issues.flatMap((issue) => {
32+
console.log(issue);
1533
if (typeof issue !== "object" || issue === null) {
1634
return [];
1735
}
@@ -26,11 +44,14 @@ export function parseOperationOutcome(
2644
? issue.diagnostics
2745
: null;
2846

29-
if (expression === null || diagnostics === null) {
47+
if (expression === null && diagnostics === null) {
3048
return [];
3149
}
3250

33-
return { expression, diagnostics };
51+
return {
52+
expression: expression || "Error",
53+
diagnostics: diagnostics || "unknown error",
54+
};
3455
});
3556
}
3657

@@ -40,20 +61,7 @@ export function toastOperationOutcome(oo: OperationOutcome) {
4061
throw new Error("Invalid OperationOutcome", { cause: oo });
4162

4263
issues.forEach(({ expression, diagnostics }) => {
43-
HSComp.toast.error(
44-
<div className="text-left">
45-
<b>{expression}</b>
46-
<p>{diagnostics}</p>
47-
</div>,
48-
{
49-
position: "bottom-right",
50-
style: {
51-
margin: "1rem",
52-
backgroundColor: "var(--destructive)",
53-
color: "var(--accent)",
54-
},
55-
},
56-
);
64+
toastError(expression, diagnostics);
5765
});
5866
}
5967

src/components/ResourceBrowser/browser.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { useLocalStorage } from "@aidbox-ui/hooks/useLocalStorage";
2-
import type * as AidboxTypes from "@health-samurai/aidbox-client";
32
import * as HSComp from "@health-samurai/react-components";
43
import { useQuery } from "@tanstack/react-query";
54
import { useNavigate } from "@tanstack/react-router";
65
import { Pin } from "lucide-react";
76
import { memo, useMemo, useState } from "react";
8-
import { useAidboxClient } from "../../AidboxClient";
7+
import { type AidboxClientR5, useAidboxClient } from "../../AidboxClient";
98

109
type ResourceRow = {
1110
resourceType: string;
@@ -183,7 +182,7 @@ type ResourceData = {
183182
stats: Record<string, ResourceDataStats>;
184183
};
185184

186-
function useResourceData(client: AidboxTypes.AidboxClient) {
185+
function useResourceData(client: AidboxClientR5) {
187186
return useQuery<ResourceData>({
188187
queryKey: ["resource-browser-resources"],
189188
queryFn: async () => {

src/components/ResourceBrowser/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type * as AidboxTypes from "@health-samurai/aidbox-client";
21
import * as HSComp from "@health-samurai/react-components";
32
import * as ReactQuery from "@tanstack/react-query";
43
import * as Router from "@tanstack/react-router";
54
import * as Lucide from "lucide-react";
65
import * as React from "react";
6+
import type { AidboxClientR5 } from "../../AidboxClient";
77
import * as Humanize from "../../humanize";
88
import * as Utils from "../../utils";
99
import type * as VDTypes from "../ViewDefinition/types";
@@ -130,7 +130,7 @@ export const ResourcesTabHeader = ({
130130
};
131131

132132
const fetchSchemas = async (
133-
client: AidboxTypes.AidboxClient,
133+
client: AidboxClientR5,
134134
resourceType: string,
135135
): Promise<Record<string, Schema> | undefined> => {
136136
const response = await client.aidboxRawRequest({
@@ -153,7 +153,7 @@ const fetchSchemas = async (
153153
};
154154

155155
const fetchDefaultSchema = async (
156-
client: AidboxTypes.AidboxClient,
156+
client: AidboxClientR5,
157157
resourceType: string,
158158
): Promise<Schema | undefined> => {
159159
const schemas = await fetchSchemas(client, resourceType);

src/components/ResourceBrowser/types.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Snapshot } from "@aidbox-ui/humanize";
2-
import type { AidboxClient } from "@health-samurai/aidbox-client";
2+
import type { AidboxClientR5 } from "../../AidboxClient";
33

44
export interface ResourcesPageProps {
5-
client: AidboxClient;
5+
client: AidboxClientR5;
66
resourceType: string;
77
}
88

src/components/ResourceEditor/action.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { defaultToastPlacement } from "@aidbox-ui/components/config";
22
import type { Resource } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core";
3-
import type * as AidboxTypes from "@health-samurai/aidbox-client";
43
import * as HSComp from "@health-samurai/react-components";
54
import { useMutation } from "@tanstack/react-query";
65
import * as Router from "@tanstack/react-router";
76
import * as YAML from "js-yaml";
7+
import type { AidboxClientR5 } from "../../AidboxClient";
88
import * as Utils from "../../api/utils";
99
import { createResource, deleteResource, updateResource } from "./api";
1010
import type { EditorMode } from "./types";
@@ -20,7 +20,7 @@ export const SaveButton = ({
2020
id: string | undefined;
2121
resource: string;
2222
mode: EditorMode;
23-
client: AidboxTypes.AidboxClient;
23+
client: AidboxClientR5;
2424
}) => {
2525
const navigate = Router.useNavigate();
2626
const mutation = useMutation({
@@ -64,7 +64,7 @@ export const DeleteButton = ({
6464
}: {
6565
resourceType: string;
6666
id: string;
67-
client: AidboxTypes.AidboxClient;
67+
client: AidboxClientR5;
6868
}) => {
6969
const navigate = Router.useNavigate();
7070
const mutation = useMutation({

src/components/ResourceEditor/api.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
import { parseOperationOutcome } from "@aidbox-ui/api/utils";
2-
import type { Bundle, Resource, OperationOutcome } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core";
2+
import type { Bundle, Resource } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core";
33
import { isOperationOutcome } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core";
4-
import type { AidboxClient, AidboxResponse } from "@health-samurai/aidbox-client";
4+
import type { AidboxClientR5 } from "../../AidboxClient";
55

66
export const fetchResource = async (
7-
client: AidboxClient,
7+
client: AidboxClientR5,
88
resourceType: string,
99
id: string,
10-
) => {
11-
const raw = (
12-
await client.aidboxRequest<Resource>({
13-
method: "GET",
14-
url: `/fhir/${resourceType}/${id}`,
15-
headers: {
16-
"Content-Type": "application/json",
17-
Accept: "application/json",
18-
},
19-
})
20-
).response.body;
21-
return raw;
22-
};
10+
): Promise<Resource> => {
11+
const {
12+
response: { body },
13+
} = await client.aidboxRequest<Resource>({
14+
method: "GET",
15+
url: `/fhir/${resourceType}/${id}`,
16+
headers: {
17+
"Content-Type": "application/json",
18+
Accept: "application/json",
19+
},
20+
});
2321

24-
export type HistoryEntryResource = {
25-
meta: {
26-
versionId: string;
27-
lastUpdated: string;
28-
};
29-
resourceType: string;
30-
id: string;
22+
if (isOperationOutcome(body))
23+
throw new Error(
24+
parseOperationOutcome(body)
25+
.map(({ expression, diagnostics }) => `${expression}: ${diagnostics}`)
26+
.join("; "),
27+
{ cause: body },
28+
);
29+
else return body;
3130
};
3231

33-
export interface HistoryEntry {
34-
resource: HistoryEntryResource;
35-
response: { status: string };
36-
}
37-
3832
export const fetchResourceHistory = async (
39-
client: AidboxClient,
33+
client: AidboxClientR5,
4034
resourceType: string,
4135
id: string,
4236
): Promise<Bundle> => {
43-
const res: AidboxResponse<Bundle> = await client.aidboxRequest<Bundle>({
37+
const {
38+
response: { body },
39+
} = await client.aidboxRequest<Bundle>({
4440
method: "GET",
4541
url: `/fhir/${resourceType}/${id}/_history`,
4642
headers: {
@@ -52,9 +48,6 @@ export const fetchResourceHistory = async (
5248
["_count", "100"],
5349
],
5450
});
55-
const response = res.response;
56-
57-
const body = response.body;
5851

5952
if (isOperationOutcome(body))
6053
throw new Error(
@@ -68,7 +61,7 @@ export const fetchResourceHistory = async (
6861
};
6962

7063
export const createResource = async (
71-
client: AidboxClient,
64+
client: AidboxClientR5,
7265
resourceType: string,
7366
resource: Resource,
7467
) => {
@@ -87,7 +80,7 @@ export const createResource = async (
8780
};
8881

8982
export const updateResource = async (
90-
client: AidboxClient,
83+
client: AidboxClientR5,
9184
resourceType: string,
9285
id: string,
9386
resource: Resource,
@@ -107,7 +100,7 @@ export const updateResource = async (
107100
};
108101

109102
export const deleteResource = async (
110-
client: AidboxClient,
103+
client: AidboxClientR5,
111104
resourceType: string,
112105
id: string,
113106
) => {

0 commit comments

Comments
 (0)