Skip to content

Commit

Permalink
feat(route): support json param in route.fulfill
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike committed Oct 22, 2023
1 parent c3b160a commit b03b0ef
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/WS/route/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,50 @@ export default class Route {
body,
contentType,
headers,
json,
path,
response,
status,
}: {
body?: string | ArrayBufferLike | Blob | ArrayBufferView | null;
contentType?: string;
headers?: Record<string, string>;
json?: unknown;
path?: string;
response?: Response;
status?: number;
} = {}) {
this.assertNotHandled();
let fulfillBody = body;

let fulfillBody;
if (json !== undefined) {
if (body !== undefined) {
throw new Error('Can specify either body or json parameters');
}
fulfillBody = JSON.stringify(fulfillBody);
} else {
fulfillBody = body;
}

if (fulfillBody === undefined) {
const responseAB = await response?.clone().arrayBuffer();
if (responseAB && responseAB.byteLength > 0) {
fulfillBody = responseAB;
}
}

let fulfillContentType = contentType;
if (fulfillContentType === undefined && json !== undefined) {
fulfillContentType = 'application/json';
}

const hasBody = fulfillBody !== undefined && fulfillBody !== null;
this.ws.send(createRouteMeta({
action: 'fulfill',
id: this.id,
resolveID: this.resolveID,
hasBody,
contentType: contentType ?? response?.headers.get('Content-Type') ?? undefined,
contentType: fulfillContentType,
headers: headers ?? (
response && [...response.headers]
.reduce((acc: Record<string, string>, [key, value]) => {
Expand Down

0 comments on commit b03b0ef

Please sign in to comment.