Skip to content

feat(http-utils): add HTTP 202 Accepted response helper #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/spacecat-shared-http-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export declare function ok(body?: string): Response;

export declare function created(body: object): Response;

export declare function accepted(body: object): Response;

export declare function noContent(headers?: object): Response;

export declare function badRequest(message?: string, headers?: object): Response;
Expand Down
4 changes: 4 additions & 0 deletions packages/spacecat-shared-http-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export function created(body) {
return createResponse(body, 201);
}

export function accepted(body) {
return createResponse(body, 202);
}

export function noContent(headers = {}) {
return createResponse('', 204, headers);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/spacecat-shared-http-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { expect } from 'chai';
import {
ok, badRequest, notFound, internalServerError,
noContent, found, created, createResponse, unauthorized, forbidden,
accepted,
} from '../src/index.js';

async function testMethod(response, expectedCode, expectedBody) {
Expand Down Expand Up @@ -59,6 +60,12 @@ describe('HTTP Response Functions', () => {
await testMethod(response, 201, body);
});

it('accepted should return a 202 ACCEPTED response with custom body', async () => {
const body = { status: 'ACCEPTED' };
const response = await accepted(body);
await testMethod(response, 202, body);
});

it('noContent should return a 204 No Content response with default headers', async () => {
const response = await noContent();
expect(response.status).to.equal(204);
Expand Down