From de4cfe6a692c4cbd0f8284cc5eb24316c690884b Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 14 Dec 2023 10:05:41 +0100 Subject: [PATCH] feat: http-utils functions --- .../spacecat-shared-utils/src/http-utils.js | 20 +++++++++++++++---- packages/spacecat-shared-utils/src/index.js | 1 + .../test/http-utils.test.js | 13 ++++++------ .../spacecat-shared-utils/test/index.test.js | 1 + 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/spacecat-shared-utils/src/http-utils.js b/packages/spacecat-shared-utils/src/http-utils.js index 76c344d8..5a0dc25a 100644 --- a/packages/spacecat-shared-utils/src/http-utils.js +++ b/packages/spacecat-shared-utils/src/http-utils.js @@ -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); } @@ -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}`, + }); } diff --git a/packages/spacecat-shared-utils/src/index.js b/packages/spacecat-shared-utils/src/index.js index 7560c947..a7c8036a 100644 --- a/packages/spacecat-shared-utils/src/index.js +++ b/packages/spacecat-shared-utils/src/index.js @@ -29,6 +29,7 @@ export { export { ok, + noContent, badRequest, notFound, internalServerError, diff --git a/packages/spacecat-shared-utils/test/http-utils.test.js b/packages/spacecat-shared-utils/test/http-utils.test.js index a0b18ad8..3450bb98 100644 --- a/packages/spacecat-shared-utils/test/http-utils.test.js +++ b/packages/spacecat-shared-utils/test/http-utils.test.js @@ -20,7 +20,7 @@ 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); }); @@ -28,7 +28,7 @@ describe('http-utils', () => { 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' }); }); @@ -36,16 +36,17 @@ describe('http-utils', () => { 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' }); }); }); diff --git a/packages/spacecat-shared-utils/test/index.test.js b/packages/spacecat-shared-utils/test/index.test.js index 2f532eca..3f7bc8da 100644 --- a/packages/spacecat-shared-utils/test/index.test.js +++ b/packages/spacecat-shared-utils/test/index.test.js @@ -33,6 +33,7 @@ describe('Index Exports', () => { 'resolveSecretsName', 'dateAfterDays', 'ok', + 'noContent', 'badRequest', 'notFound', 'internalServerError',