1- import type { AidboxClient } from "@health-samurai/aidbox-client" ;
2-
3- export type Resource = {
4- resourceType : string ;
5- id ?: string ;
6- [ key : string ] : unknown ;
7- } ;
1+ import { parseOperationOutcome } from "@aidbox-ui/api/utils" ;
2+ import type { Bundle , Resource , OperationOutcome } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core" ;
3+ import { isOperationOutcome } from "@aidbox-ui/fhir-types/hl7-fhir-r5-core" ;
4+ import type { AidboxClient , AidboxResponse } from "@health-samurai/aidbox-client" ;
85
96export const fetchResource = async (
107 client : AidboxClient ,
@@ -24,13 +21,6 @@ export const fetchResource = async (
2421 return raw ;
2522} ;
2623
27- export interface HistoryBundle {
28- resourceType : "Bundle" ;
29- type : "history" ;
30- total : number ;
31- entry : HistoryEntry [ ] ;
32- }
33-
3424export type HistoryEntryResource = {
3525 meta : {
3626 versionId : string ;
@@ -49,22 +39,32 @@ export const fetchResourceHistory = async (
4939 client : AidboxClient ,
5040 resourceType : string ,
5141 id : string ,
52- ) => {
53- const raw = (
54- await client . aidboxRequest < HistoryBundle > ( {
55- method : "GET" ,
56- url : `/fhir/${ resourceType } /${ id } /_history` ,
57- headers : {
58- "Content-Type" : "application/json" ,
59- Accept : "application/json" ,
60- } ,
61- params : [
62- [ "_page" , "1" ] ,
63- [ "_count" , "100" ] ,
64- ] ,
65- } )
66- ) . response . body ;
67- return raw as HistoryBundle ;
42+ ) : Promise < Bundle > => {
43+ const res : AidboxResponse < Bundle > = await client . aidboxRequest < Bundle > ( {
44+ method : "GET" ,
45+ url : `/fhir/${ resourceType } /${ id } /_history` ,
46+ headers : {
47+ "Content-Type" : "application/json" ,
48+ Accept : "application/json" ,
49+ } ,
50+ params : [
51+ [ "_page" , "1" ] ,
52+ [ "_count" , "100" ] ,
53+ ] ,
54+ } ) ;
55+ const response = res . response ;
56+
57+ const body = response . body ;
58+
59+ if ( isOperationOutcome ( body ) )
60+ throw new Error (
61+ parseOperationOutcome ( body )
62+ . map ( ( { expression, diagnostics } ) => `${ expression } : ${ diagnostics } ` )
63+ . join ( "; " ) ,
64+ { cause : body } ,
65+ ) ;
66+
67+ return body ;
6868} ;
6969
7070export const createResource = async (
0 commit comments