Skip to content

Commit

Permalink
fix(backend): Access the correct headers to determine the Content-Typ…
Browse files Browse the repository at this point in the history
…e returned by the Response (#1469)
  • Loading branch information
anagstef authored Jul 12, 2023
1 parent 8786bf5 commit e303684
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-monkeys-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fix the headers checked to determine the Response Content-Type
5 changes: 3 additions & 2 deletions packages/backend/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ClerkAPIError, ClerkAPIErrorJSON } from '@clerk/types';
import deepmerge from 'deepmerge';
import snakecaseKeys from 'snakecase-keys';

import { API_URL, API_VERSION, USER_AGENT } from '../constants';
import { API_URL, API_VERSION, constants, USER_AGENT } from '../constants';
// DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js
// For more information refer to https://sinonjs.org/how-to/stub-dependency/
import runtime from '../runtime';
Expand Down Expand Up @@ -128,7 +128,8 @@ export function buildRequest(options: CreateBackendApiOptions) {
}

// TODO: Parse JSON or Text response based on a response header
const isJSONResponse = headers['Content-Type'] === 'application/json';
const isJSONResponse =
res?.headers && res.headers?.get(constants.Headers.ContentType) === constants.ContentTypes.Json;
const data = await (isJSONResponse ? res.json() : res.text());

if (!res.ok) {
Expand Down
16 changes: 7 additions & 9 deletions packages/backend/src/util/mockFetch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { constants } from '../constants';

export function jsonOk(body: unknown, status = 200) {
// Mock response object that satisfies the window.Response interface
const mockResponse = {
ok: true,
status,
headers: {
'Content-type': 'application/json',
},
headers: { get: mockHeadersGet },
json() {
return Promise.resolve(body);
},
Expand All @@ -19,9 +19,7 @@ export function jsonNotOk(body: unknown) {
const mockResponse = {
ok: false,
status: 422,
headers: {
'Content-type': 'application/json',
},
headers: { get: mockHeadersGet },
json() {
return Promise.resolve(body);
},
Expand All @@ -35,13 +33,13 @@ export function jsonError(body: unknown, status = 500) {
const mockResponse = {
ok: false,
status: status,
headers: {
'Content-type': 'application/json',
},
headers: { get: mockHeadersGet },
json() {
return Promise.resolve(body);
},
};

return Promise.resolve(mockResponse);
}

const mockHeadersGet = (key: string) => (key === constants.Headers.ContentType ? constants.ContentTypes.Json : null);

0 comments on commit e303684

Please sign in to comment.