Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-ignatov committed Jul 21, 2022
1 parent 3ad1d83 commit aa5b163
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ export default class Client
body: JSON.stringify(resource),
headers: {
// TODO: Do we need to alternate with "application/json+fhir"?
"Content-Type": "application/json",
"content-type": "application/json",
...(requestOptions || {}).headers
}
});
Expand All @@ -692,7 +692,7 @@ export default class Client
body: JSON.stringify(resource),
headers: {
// TODO: Do we need to alternate with "application/json+fhir"?
"Content-Type": "application/json",
"content-type": "application/json",
...(requestOptions || {}).headers
}
});
Expand Down Expand Up @@ -809,7 +809,7 @@ export default class Client
if (authHeader) {
requestOptions.headers = {
...requestOptions.headers,
Authorization: authHeader
authorization: authHeader
};
}
return requestOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class HttpError extends Error
{
if (!this.response.bodyUsed) {
try {
const type = this.response.headers.get("Content-Type") || "text/plain";
const type = this.response.headers.get("content-type") || "text/plain";
if (type.match(/\bjson\b/i)) {
let body = await this.response.json();
if (body.error) {
Expand Down
26 changes: 24 additions & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ export function responseToJSON(resp: Response): Promise<object|string> {
return resp.text().then(text => text.length ? JSON.parse(text) : "");
}

export function loweCaseKeys<T=Record<string, any> | any[] | undefined>(obj: T): T {

// Can be undefined to signal that this key should be removed
if (!obj) {
return obj as T
}

// Arrays are valid values in case of recursive calls
if (Array.isArray(obj)) {
return obj.map(v => v && typeof v === "object" ? loweCaseKeys(v) : v) as unknown as T;
}

// Plain object
let out: Record<string, any> = {};
Object.keys(obj).forEach(key => {
const lowerKey = key.toLowerCase()
const v = (obj as Record<string, any>)[key]
out[lowerKey] = v && typeof v == "object" ? loweCaseKeys(v) : v;
});
return out as T;
}

/**
* This is our built-in request function. It does a few things by default
* (unless told otherwise):
Expand All @@ -101,12 +123,12 @@ export function request<T = fhirclient.FetchResult>(
...options,
headers: {
accept: "application/json",
...options.headers
...loweCaseKeys(options.headers)
}
})
.then(checkResponse)
.then((res: Response) => {
const type = res.headers.get("Content-Type") + "";
const type = res.headers.get("content-type") + "";
if (type.match(/\bjson\b/i)) {
return responseToJSON(res).then(body => ({ res, body }));
}
Expand Down
4 changes: 2 additions & 2 deletions src/smart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ export async function buildTokenRequest(env: fhirclient.Adapter, code: string, s
// Basic authentication is required, where the username is the app’s
// client_id and the password is the app’s client_secret (see example).
if (clientSecret) {
requestOptions.headers.Authorization = "Basic " + env.btoa(
requestOptions.headers.authorization = "Basic " + env.btoa(
clientId + ":" + clientSecret
);
debug("Using state.clientSecret to construct the authorization header: %s", requestOptions.headers.Authorization);
debug("Using state.clientSecret to construct the authorization header: %s", requestOptions.headers.authorization);
} else if (clientPrivateJwk) {
const clientPrivateKey = await security.importKey(clientPrivateJwk)

Expand Down
28 changes: 14 additions & 14 deletions test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@ describe("FHIR.client", () => {
const client = new Client(env, mockUrl);

mockServer.mock({
headers: { "Content-Type": "text/plain" },
headers: { "content-type": "text/plain" },
status: 200,
body: "This is a text"
});
Expand All @@ -2728,7 +2728,7 @@ describe("FHIR.client", () => {
const goal64 = file.toString("base64");

mockServer.mock({
headers: { "Content-Type": "image/png" },
headers: { "content-type": "image/png" },
status: 200,
file: "json.png"
});
Expand Down Expand Up @@ -3816,7 +3816,7 @@ describe("FHIR.client", () => {
mockServer.mock({
status: 200,
body: resource,
headers: { "Content-Type": "application/json" }
headers: { "content-type": "application/json" }
});
result = await client.create(resource, { includeResponse: true });
expect(result.body).to.equal(resource);
Expand All @@ -3831,7 +3831,7 @@ describe("FHIR.client", () => {
method : "POST",
body : JSON.stringify(resource),
headers: {
"Content-Type": "application/json"
"content-type": "application/json"
}
});

Expand All @@ -3844,7 +3844,7 @@ describe("FHIR.client", () => {
signal: "whatever",
headers: {
"x-custom": "value",
"Content-Type": "application/fhir+json"
"content-type": "application/fhir+json"
}
});
expect(result).to.equal({
Expand All @@ -3854,7 +3854,7 @@ describe("FHIR.client", () => {
signal : "whatever",
headers: {
"x-custom": "value",
"Content-Type": "application/fhir+json"
"content-type": "application/fhir+json"
}
});

Expand All @@ -3872,7 +3872,7 @@ describe("FHIR.client", () => {
body : JSON.stringify(resource),
signal : "whatever",
headers: {
"Content-Type": "application/json"
"content-type": "application/json"
}
});
});
Expand All @@ -3888,7 +3888,7 @@ describe("FHIR.client", () => {
mockServer.mock({
status: 200,
body: resource,
headers: { "Content-Type": "application/json" }
headers: { "content-type": "application/json" }
});
result = await client.update(resource, { includeResponse: true });
expect(result.body).to.equal(resource);
Expand All @@ -3903,7 +3903,7 @@ describe("FHIR.client", () => {
method : "PUT",
body : JSON.stringify(resource),
headers: {
"Content-Type": "application/json"
"content-type": "application/json"
}
});

Expand All @@ -3916,7 +3916,7 @@ describe("FHIR.client", () => {
signal: "whatever",
headers: {
"x-custom": "value",
"Content-Type": "application/fhir+json"
"content-type": "application/fhir+json"
}
});
expect(result).to.equal({
Expand All @@ -3926,7 +3926,7 @@ describe("FHIR.client", () => {
signal: "whatever",
headers: {
"x-custom": "value",
"Content-Type": "application/fhir+json"
"content-type": "application/fhir+json"
}
});

Expand All @@ -3944,7 +3944,7 @@ describe("FHIR.client", () => {
body : JSON.stringify(resource),
signal: "whatever",
headers: {
"Content-Type": "application/json"
"content-type": "application/json"
}
});
});
Expand All @@ -3960,7 +3960,7 @@ describe("FHIR.client", () => {
mockServer.mock({
status: 200,
body: { result: "success" },
headers: { "Content-Type": "application/json" }
headers: { "content-type": "application/json" }
});

result = await client.delete("Patient/2", { includeResponse: true });
Expand Down Expand Up @@ -4012,7 +4012,7 @@ describe("FHIR.client", () => {
mockServer.mock({
status: 200,
body: { result: "success" },
headers: { "Content-Type": "application/json" }
headers: { "content-type": "application/json" }
});

result = await client.patch("Patient/2", [{ op: "remove", path: "/x" }], { includeResponse: true });
Expand Down
2 changes: 1 addition & 1 deletion test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe("Lib", () => {

const result = await lib.request<fhirclient.CombinedFetchResult>(mockUrl, { includeResponse: true });
expect(result.body).to.equal({ result: "success" });
expect(result.response.headers.get("Content-Type")).to.startWith("application/json");
expect(result.response.headers.get("content-type")).to.startWith("application/json");
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/smart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe("smart", () => {
clientSecret: "test-secret"
});

const authz = requestOptions.headers?.['Authorization'] as string;
expect(authz).to.exist;
const authz = requestOptions.headers?.['authorization'] as string;
expect(authz).to.exist();
expect(authz).to.startWith("Basic ")
});

Expand Down

0 comments on commit aa5b163

Please sign in to comment.