Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createReadableStreamResponse (content: object, status?: number): Response {
// const stream = new Readable({ objectMode: true });
const stream = new RepeatingReadable(content);
stream.push(JSON.stringify(content));
const response = new Response(stream, { status: status ? status : 200 });
return response;
}
it('fetch response error with status', async () => {
const headers = new Headers();
headers.append('content-type', 'text/plain; charset=utf-8');
const errorResponse500 = new Response('500 Internal Server Error', {
status: 500,
headers,
});
await failureLogger()(errorResponse500);
expect(logger.error.mock.calls).toHaveLength(1);
assertErrorLog(logger.error.mock.calls[0][0]);
const errorResponse404 = new Response('404 Not Found', {
status: 404,
headers,
});
await failureLogger()(errorResponse404);
expect(logger.error.mock.calls).toHaveLength(1);
expect(logger.warn.mock.calls).toHaveLength(1);
assertErrorLog(logger.warn.mock.calls[0][0]);
});
export const mockResponse = (body, headers?): Response =>
(new nodeFetch.Response(
JSON.stringify(body),
headers && {
headers: new nodeFetch.Headers(headers)
}
) as unknown) as Response;
function error(err, status = 500, extraHeaders = {}) {
const headers = new Headers({
'content-type': 'application/json',
...extraHeaders
});
if (typeof err === 'string') {
err = { message: err, code: 'ENOTSUPPLIED' };
}
if (isDev()) {
err.trace = new Error().stack;
}
const r = new Response(JSON.stringify(err), { status, headers });
return r;
}
function empty(status = 204, headers = {}) {
return new Response('', { status, headers });
}
function bytes(stream, status = 200, extraHeaders = {}) {
const headers = new Headers({
'content-type': 'application/octet-stream',
...extraHeaders
});
const r = new Response(stream, { status, headers });
return r;
}
function text(body, status = 200, extraHeaders = {}) {
const headers = new Headers({
'content-type': 'text/plain',
...extraHeaders
});
const r = new Response(body, { status, headers });
return r;
}
function authneeded(message, status = 401, extraHeaders = {}) {
const headers = new Headers({
'www-authenticate': 'bearer',
'content-type': 'application/json',
...extraHeaders
});
if (typeof message === 'string') {
message = { message, code: 'authneeded' };
}
const r = new Response(JSON.stringify(message), { status, headers });
return r;
}