Comparing version 0.2.1 to 0.2.2
@@ -9,6 +9,10 @@ import { HttpHandler } from 'msw'; | ||
type ResponseCode = number; | ||
/** code, headers, serialized response */ | ||
type ResponseData = [ResponseCode, { | ||
[k: string]: string; | ||
}, string | undefined]; | ||
/** code, headers, response body */ | ||
type ResponseData = [ | ||
ResponseCode, | ||
{ | ||
[k: string]: string; | ||
}, | ||
string | BodyInit | undefined | ||
]; | ||
/** e.g. "/movies/:id" */ | ||
@@ -15,0 +19,0 @@ type Shorthand = string; |
@@ -181,13 +181,17 @@ // lib/msw-config.ts | ||
return new HttpResponse(null, init); | ||
} else if (accept.includes("json")) { | ||
return HttpResponse.json(JSON.parse(responseBody), init); | ||
} else if (accept.includes("text")) { | ||
return HttpResponse.text(responseBody, init); | ||
} else { | ||
try { | ||
const json = JSON.parse(responseBody); | ||
return HttpResponse.json(json, init); | ||
} catch (e) { | ||
} else if (typeof responseBody === "string") { | ||
if (accept.includes("json")) { | ||
return HttpResponse.json(JSON.parse(responseBody), init); | ||
} else if (accept.includes("text")) { | ||
return HttpResponse.text(responseBody, init); | ||
} else { | ||
try { | ||
const json = JSON.parse(responseBody); | ||
return HttpResponse.json(json, init); | ||
} catch (e) { | ||
return HttpResponse.text(responseBody, init); | ||
} | ||
} | ||
} else { | ||
return new HttpResponse(responseBody, init); | ||
} | ||
@@ -194,0 +198,0 @@ }); |
@@ -12,4 +12,8 @@ import { http, HttpHandler, HttpResponse, delay } from 'msw'; | ||
/** code, headers, serialized response */ | ||
type ResponseData = [ResponseCode, { [k: string]: string }, string | undefined]; | ||
/** code, headers, response body */ | ||
type ResponseData = [ | ||
ResponseCode, | ||
{ [k: string]: string }, | ||
string | BodyInit | undefined, | ||
]; | ||
@@ -253,15 +257,21 @@ /** e.g. "/movies/:id" */ | ||
const accept = request.headers?.get('accept')?.toLowerCase() || ''; | ||
if (!responseBody) { | ||
return new HttpResponse(null, init); | ||
} else if (accept.includes('json')) { | ||
return HttpResponse.json(JSON.parse(responseBody), init); | ||
} else if (accept.includes('text')) { | ||
return HttpResponse.text(responseBody, init); | ||
} else { | ||
try { | ||
const json = JSON.parse(responseBody); | ||
return HttpResponse.json(json, init); | ||
} catch (e) { | ||
} else if (typeof responseBody === 'string') { | ||
if (accept.includes('json')) { | ||
return HttpResponse.json(JSON.parse(responseBody), init); | ||
} else if (accept.includes('text')) { | ||
return HttpResponse.text(responseBody, init); | ||
} else { | ||
try { | ||
const json = JSON.parse(responseBody); | ||
return HttpResponse.json(json, init); | ||
} catch (e) { | ||
return HttpResponse.text(responseBody, init); | ||
} | ||
} | ||
} else { | ||
// Could be a ReadableStream, Blob, ArrayBuffer, etc. | ||
return new HttpResponse(responseBody, init); | ||
} | ||
@@ -268,0 +278,0 @@ }); |
{ | ||
"name": "mirage-msw", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Allow mirage to use MSW (Mock Service Worker) as the interceptor", | ||
@@ -5,0 +5,0 @@ "main": "dist/msw-config.cjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53496
1398