From e3036848d19a48935129aec2fe50003518a3aa53 Mon Sep 17 00:00:00 2001 From: Stefanos Anagnostou Date: Wed, 12 Jul 2023 14:13:38 +0300 Subject: [PATCH] fix(backend): Access the correct headers to determine the Content-Type returned by the Response (#1469) --- .changeset/four-monkeys-smell.md | 5 +++++ packages/backend/src/api/request.ts | 5 +++-- packages/backend/src/util/mockFetch.ts | 16 +++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 .changeset/four-monkeys-smell.md diff --git a/.changeset/four-monkeys-smell.md b/.changeset/four-monkeys-smell.md new file mode 100644 index 0000000000..5d10311a5f --- /dev/null +++ b/.changeset/four-monkeys-smell.md @@ -0,0 +1,5 @@ +--- +'@clerk/backend': patch +--- + +Fix the headers checked to determine the Response Content-Type diff --git a/packages/backend/src/api/request.ts b/packages/backend/src/api/request.ts index 9f5b2556d5..130d47c059 100644 --- a/packages/backend/src/api/request.ts +++ b/packages/backend/src/api/request.ts @@ -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'; @@ -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) { diff --git a/packages/backend/src/util/mockFetch.ts b/packages/backend/src/util/mockFetch.ts index 6d49be88de..387c18cbd4 100644 --- a/packages/backend/src/util/mockFetch.ts +++ b/packages/backend/src/util/mockFetch.ts @@ -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); }, @@ -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); }, @@ -35,9 +33,7 @@ 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); }, @@ -45,3 +41,5 @@ export function jsonError(body: unknown, status = 500) { return Promise.resolve(mockResponse); } + +const mockHeadersGet = (key: string) => (key === constants.Headers.ContentType ? constants.ContentTypes.Json : null);