Skip to content

Commit 1b69f06

Browse files
committed
Passing headers to resolve refs requests #184
1 parent 2a1c320 commit 1b69f06

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/Client.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,26 @@ async function contextualize(
6666
* @param refId
6767
* @param cache A map to store the resolved refs
6868
* @param client The client instance
69-
* @param [signal] The `AbortSignal` if any
69+
* @param requestOptions Only signal and headers are currently used if provided
7070
* @returns The resolved reference
7171
* @private
7272
*/
7373
function getRef(
7474
refId: string,
7575
cache: Record<string, any>,
7676
client: Client,
77-
signal?: AbortSignal
77+
requestOptions: RequestInit
7878
): Promise<fhirclient.JsonObject> {
7979
if (!cache[refId]) {
8080

81+
const { signal, headers } = requestOptions;
82+
8183
// Note that we set cache[refId] immediately! When the promise is
8284
// settled it will be updated. This is to avoid a ref being fetched
8385
// twice because some of these requests are executed in parallel.
8486
cache[refId] = client.request({
8587
url: refId,
88+
headers,
8689
signal
8790
}).then(res => {
8891
cache[refId] = res;
@@ -106,15 +109,15 @@ function resolveRef(
106109
graph: boolean,
107110
cache: fhirclient.JsonObject,
108111
client: Client,
109-
signal?: AbortSignal
112+
requestOptions: fhirclient.RequestOptions
110113
) {
111114
const node = getPath(obj, path);
112115
if (node) {
113116
const isArray = Array.isArray(node);
114117
return Promise.all(makeArray(node).filter(Boolean).map((item, i) => {
115118
const ref = item.reference;
116119
if (ref) {
117-
return getRef(ref, cache, client, signal).then(sub => {
120+
return getRef(ref, cache, client, requestOptions).then(sub => {
118121
if (graph) {
119122
if (isArray) {
120123
if (path.indexOf("..") > -1) {
@@ -150,7 +153,7 @@ function resolveRefs(
150153
fhirOptions: fhirclient.FhirOptions,
151154
cache: fhirclient.JsonObject,
152155
client: Client,
153-
signal?: AbortSignal
156+
requestOptions: fhirclient.RequestOptions
154157
) {
155158

156159
// 1. Sanitize paths, remove any invalid ones
@@ -191,7 +194,7 @@ function resolveRefs(
191194
Object.keys(groups).sort().forEach(len => {
192195
const group = groups[len];
193196
task = task.then(() => Promise.all(group.map((path: string) => {
194-
return resolveRef(obj, path, !!fhirOptions.graph, cache, client, signal);
197+
return resolveRef(obj, path, !!fhirOptions.graph, cache, client, requestOptions);
195198
})));
196199
});
197200
return task;
@@ -898,7 +901,7 @@ export default class Client
898901
options,
899902
_resolvedRefs,
900903
this,
901-
signal
904+
requestOptions
902905
)));
903906
}
904907
else {
@@ -907,7 +910,7 @@ export default class Client
907910
options,
908911
_resolvedRefs,
909912
this,
910-
signal
913+
requestOptions
911914
);
912915
}
913916

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ declare namespace fhirclient {
338338
/**
339339
* Supported PKCE Code challenge methods
340340
*/
341-
codeChallengeMethods: string[];
341+
codeChallengeMethods: string[];
342342
}
343343

344344
/**

test/Client.test.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,9 +1651,7 @@ describe("FHIR.client", () => {
16511651

16521652
describe ("can resolve nested refs", () => {
16531653
crossPlatformTest(async (env) => {
1654-
const client = new Client(env, {
1655-
serverUrl: mockUrl
1656-
});
1654+
const client = new Client(env, { serverUrl: mockUrl });
16571655

16581656
// This is how the user had defined the list, If it works properly,
16591657
// the request function should resolve them in different order:
@@ -1665,49 +1663,57 @@ describe("FHIR.client", () => {
16651663
"encounter"
16661664
];
16671665

1666+
function createHandler(json) {
1667+
return function handler(req, res, next) {
1668+
try {
1669+
expect(req.headers['x-custom-header'], "Custom headers not sent on ref requests").to.equal('someCustomKey');
1670+
res.json(json)
1671+
} catch (ex) {
1672+
next(ex)
1673+
}
1674+
}
1675+
}
1676+
16681677
// 1. Observation
16691678
// this request should be sent first!
16701679
mockServer.mock({
1671-
headers: { "content-type": "application/json" },
1672-
status: 200,
1673-
body: {
1680+
handler: createHandler({
16741681
resourceType: "Observation",
16751682
encounter: { reference: "encounter/1" },
16761683
subject: { reference: "subject/1" }
1677-
}
1684+
})
16781685
});
16791686

16801687
// 2. Patient (Observation.subject)
16811688
// this request should be sent second (even though it might
16821689
// reply after #3)
16831690
mockServer.mock({
1684-
headers: { "content-type": "application/json" },
1685-
status: 200,
1686-
body: { resourceType: "Patient" }
1691+
handler: createHandler({ resourceType: "Patient" })
16871692
});
16881693

16891694
// 3. Encounter
16901695
// this request should be sent third (even though it might
16911696
// reply before #2)
16921697
mockServer.mock({
1693-
headers: { "content-type": "application/json" },
1694-
status: 200,
1695-
body: {
1698+
handler: createHandler({
16961699
resourceType: "Encounter",
16971700
serviceProvider: { reference: "Organization/1" }
1698-
}
1701+
})
16991702
});
17001703

17011704
// 4. Organization (Encounter.serviceProvider)
17021705
// this request should be sent AFTER we have handled the response
17031706
// from #3!
17041707
mockServer.mock({
1705-
headers: { "content-type": "application/json" },
1706-
status: 200,
1707-
body: { resourceType: "Organization" }
1708+
handler: createHandler({ resourceType: "Organization" })
17081709
});
17091710

1710-
const result = await client.request("Observation/id", {
1711+
const result = await client.request({
1712+
url: "Observation/id",
1713+
headers: {
1714+
'X-Custom-Header': 'someCustomKey',
1715+
}
1716+
}, {
17111717
resolveReferences: refsToResolve
17121718
});
17131719

0 commit comments

Comments
 (0)