Skip to content

Commit

Permalink
feat: http-utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Dec 14, 2023
1 parent a2165cf commit de4cfe6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
20 changes: 16 additions & 4 deletions packages/spacecat-shared-utils/src/http-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@

import { Response } from '@adobe/fetch';

function createResponse(body, status = 200) {
/**
* Creates a response with a JSON body. Defaults to 200 status.
* @param {object} body - JSON body.
* @param {number} status - Optional status code.
* @param {object} headers - Optional headers.
* @return {Response} Response.
*/
export function createResponse(body, status = 200, headers = {}) {
return new Response(
JSON.stringify(body),
{
headers: { 'content-type': 'application/json' },
headers: { 'content-type': 'application/json; charset=utf-8', ...headers },
status,
},
);
}

export function ok(body) {
return createResponse(body, 200);
}

export function noContent(body) {
return createResponse(body, 204);
}

export function badRequest(message) {
return createResponse({ message }, 400);
}
Expand All @@ -35,5 +45,7 @@ export function notFound(message) {
}

export function internalServerError(message) {
return createResponse({ message }, 500);
return createResponse({ message }, 500, {
'x-error': `internal server error: ${message}`,
});
}
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {

export {
ok,
noContent,
badRequest,
notFound,
internalServerError,
Expand Down
13 changes: 7 additions & 6 deletions packages/spacecat-shared-utils/test/http-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,33 @@ describe('http-utils', () => {
const body = { key: 'value' };
const response = ok(body);
expect(response.status).to.equal(200);
expect(response.headers.get('content-type')).to.equal('application/json');
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8');
const respJson = await response.json();
expect(respJson).to.eql(body);
});

it('badRequest should return a 400 response with JSON body', async () => {
const response = badRequest('Bad Request');
expect(response.status).to.equal(400);
expect(response.headers.get('content-type')).to.equal('application/json');
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8');
const respJson = await response.json();
expect(respJson).to.eql({ message: 'Bad Request' });
});

it('notFound return a 404 response with JSON body', async () => {
const response = notFound('Not Found');
expect(response.status).to.equal(404);
expect(response.headers.get('content-type')).to.equal('application/json');
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8');
const respJson = await response.json();
expect(respJson).to.eql({ message: 'Not Found' });
});

it('internalServerError should return a 500 response with JSON body', async () => {
const response = internalServerError('Internal Server Error');
const response = internalServerError('uh oh');
expect(response.status).to.equal(500);
expect(response.headers.get('content-type')).to.equal('application/json');
expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8');
expect(response.headers.get('x-error')).to.equal('internal server error: uh oh');
const respJson = await response.json();
expect(respJson).to.eql({ message: 'Internal Server Error' });
expect(respJson).to.eql({ message: 'uh oh' });
});
});
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Index Exports', () => {
'resolveSecretsName',
'dateAfterDays',
'ok',
'noContent',
'badRequest',
'notFound',
'internalServerError',
Expand Down

0 comments on commit de4cfe6

Please sign in to comment.