Skip to content

Commit 05b2486

Browse files
committed
Add json support for application/problem+json content type
1 parent 0eb57b8 commit 05b2486

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/templates/core/fetch/getResponseBody.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const getResponseBody = async (response: Response): Promise<any> => {
33
try {
44
const contentType = response.headers.get('Content-Type');
55
if (contentType) {
6-
const isJSON = contentType.toLowerCase().startsWith('application/json');
6+
const jsonTypes = ['application/json', 'application/problem+json']
7+
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
78
if (isJSON) {
89
return await response.json();
910
} else {

src/templates/core/node/getResponseBody.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const getResponseBody = async (response: Response): Promise<any> => {
33
try {
44
const contentType = response.headers.get('Content-Type');
55
if (contentType) {
6-
const isJSON = contentType.toLowerCase().startsWith('application/json');
6+
const jsonTypes = ['application/json', 'application/problem+json']
7+
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
78
if (isJSON) {
89
return await response.json();
910
} else {

src/templates/core/xhr/getResponseBody.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const getResponseBody = (xhr: XMLHttpRequest): any => {
33
try {
44
const contentType = xhr.getResponseHeader('Content-Type');
55
if (contentType) {
6-
const isJSON = contentType.toLowerCase().startsWith('application/json');
6+
const jsonTypes = ['application/json', 'application/problem+json']
7+
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
78
if (isJSON) {
89
return JSON.parse(xhr.responseText);
910
} else {

test/__snapshots__/index.spec.ts.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ const getResponseBody = async (response: Response): Promise<any> => {
463463
try {
464464
const contentType = response.headers.get('Content-Type');
465465
if (contentType) {
466-
const isJSON = contentType.toLowerCase().startsWith('application/json');
466+
const jsonTypes = ['application/json', 'application/problem+json']
467+
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
467468
if (isJSON) {
468469
return await response.json();
469470
} else {
@@ -3522,7 +3523,8 @@ const getResponseBody = async (response: Response): Promise<any> => {
35223523
try {
35233524
const contentType = response.headers.get('Content-Type');
35243525
if (contentType) {
3525-
const isJSON = contentType.toLowerCase().startsWith('application/json');
3526+
const jsonTypes = ['application/json', 'application/problem+json']
3527+
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
35263528
if (isJSON) {
35273529
return await response.json();
35283530
} else {

0 commit comments

Comments
 (0)