11import { 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" ;
33import { 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
66export 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-
3832export 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
7063export 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
8982export 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
109102export const deleteResource = async (
110- client : AidboxClient ,
103+ client : AidboxClientR5 ,
111104 resourceType : string ,
112105 id : string ,
113106) => {
0 commit comments