From 05b24866472f125f8dae5f1c34c07db3e6b5ccc2 Mon Sep 17 00:00:00 2001 From: Dennis Passway Date: Wed, 17 Aug 2022 13:16:41 +0200 Subject: [PATCH] Add json support for application/problem+json content type --- src/templates/core/fetch/getResponseBody.hbs | 3 ++- src/templates/core/node/getResponseBody.hbs | 3 ++- src/templates/core/xhr/getResponseBody.hbs | 3 ++- test/__snapshots__/index.spec.ts.snap | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/templates/core/fetch/getResponseBody.hbs b/src/templates/core/fetch/getResponseBody.hbs index 083cd16e..556c1f03 100644 --- a/src/templates/core/fetch/getResponseBody.hbs +++ b/src/templates/core/fetch/getResponseBody.hbs @@ -3,7 +3,8 @@ const getResponseBody = async (response: Response): Promise => { try { const contentType = response.headers.get('Content-Type'); if (contentType) { - const isJSON = contentType.toLowerCase().startsWith('application/json'); + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); if (isJSON) { return await response.json(); } else { diff --git a/src/templates/core/node/getResponseBody.hbs b/src/templates/core/node/getResponseBody.hbs index 083cd16e..556c1f03 100644 --- a/src/templates/core/node/getResponseBody.hbs +++ b/src/templates/core/node/getResponseBody.hbs @@ -3,7 +3,8 @@ const getResponseBody = async (response: Response): Promise => { try { const contentType = response.headers.get('Content-Type'); if (contentType) { - const isJSON = contentType.toLowerCase().startsWith('application/json'); + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); if (isJSON) { return await response.json(); } else { diff --git a/src/templates/core/xhr/getResponseBody.hbs b/src/templates/core/xhr/getResponseBody.hbs index 596733a8..84a662ef 100644 --- a/src/templates/core/xhr/getResponseBody.hbs +++ b/src/templates/core/xhr/getResponseBody.hbs @@ -3,7 +3,8 @@ const getResponseBody = (xhr: XMLHttpRequest): any => { try { const contentType = xhr.getResponseHeader('Content-Type'); if (contentType) { - const isJSON = contentType.toLowerCase().startsWith('application/json'); + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); if (isJSON) { return JSON.parse(xhr.responseText); } else { diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index cb3405f2..f10b9282 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -463,7 +463,8 @@ const getResponseBody = async (response: Response): Promise => { try { const contentType = response.headers.get('Content-Type'); if (contentType) { - const isJSON = contentType.toLowerCase().startsWith('application/json'); + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); if (isJSON) { return await response.json(); } else { @@ -3522,7 +3523,8 @@ const getResponseBody = async (response: Response): Promise => { try { const contentType = response.headers.get('Content-Type'); if (contentType) { - const isJSON = contentType.toLowerCase().startsWith('application/json'); + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); if (isJSON) { return await response.json(); } else {