From 261feb8698d1825dc4d8f159ce8a02a59d20b51b Mon Sep 17 00:00:00 2001 From: Chris Rohr <51920+chrisrohr@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:26:00 -0400 Subject: [PATCH] Add overload to standardForbiddenResponse that takes an entity (#232) Closes #231 --- .../express/kiwi-standard-responses-express.t.ts | 13 +++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/express/kiwi-standard-responses-express.ts | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/__tests__/express/kiwi-standard-responses-express.t.ts b/__tests__/express/kiwi-standard-responses-express.t.ts index 5ed77d9..b462a7b 100644 --- a/__tests__/express/kiwi-standard-responses-express.t.ts +++ b/__tests__/express/kiwi-standard-responses-express.t.ts @@ -181,6 +181,19 @@ describe("KiwiStandardResponses", () => { }); }); + describe("standardForbiddenResponseWithEntity", () => { + it("should setup a standard 403 response", () => { + KiwiStandardResponsesExpress.standardForbiddenResponseWithEntity(res, { + foo: "bar", + }); + + expect(res.status).toHaveBeenCalledWith(403); + expect(res.json).toHaveBeenCalledWith({ + foo: "bar", + }); + }); + }); + describe("standardErrorResponse", () => { it("should setup a standard error response with given status", () => { KiwiStandardResponsesExpress.standardErrorResponse(res, 500, "Whoops"); diff --git a/package-lock.json b/package-lock.json index 015814a..4ce45dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kiwiproject/kiwi-js", - "version": "0.10.0", + "version": "0.11.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@kiwiproject/kiwi-js", - "version": "0.10.0", + "version": "0.11.0", "dependencies": { "express": "4.18.3" }, diff --git a/package.json b/package.json index d2e9a08..c7e3d16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kiwiproject/kiwi-js", - "version": "0.10.0", + "version": "0.11.0", "description": "KiwiJS is a utility library. It contains a variety of utilities that we have built over time and find useful. Most of these utilities are ports from the Java Kiwi library.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/express/kiwi-standard-responses-express.ts b/src/express/kiwi-standard-responses-express.ts index 964f683..d1134e3 100644 --- a/src/express/kiwi-standard-responses-express.ts +++ b/src/express/kiwi-standard-responses-express.ts @@ -171,6 +171,19 @@ const standardForbiddenResponse = (res: Response, errorMessage: string) => { res.status(403).json(new ErrorResponse(errorMessage).toMap()); }; +/** + * Returns a 403 Forbidden response containing a provided entity. + * + * @param res the Express Response + * @param entity the entity to use + */ +const standardForbiddenResponseWithEntity = ( + res: Response, + entity: unknown, +) => { + res.status(403).json(entity); +}; + /** * Returns a response having the given status and an {@link ErrorResponse} entity which uses {@code errorMessage} * as the detailed error message. @@ -227,6 +240,7 @@ export const KiwiStandardResponsesExpress = { standardErrorResponse, standardUnauthorizedResponse, standardForbiddenResponse, + standardForbiddenResponseWithEntity, standardNotFoundResponse, standardBadRequestResponse, };