Skip to content

Commit

Permalink
Add overload to standardForbiddenResponse that takes an entity (#232)
Browse files Browse the repository at this point in the history
Closes #231
  • Loading branch information
chrisrohr authored Mar 22, 2024
1 parent 8f23ab1 commit 261feb8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
13 changes: 13 additions & 0 deletions __tests__/express/kiwi-standard-responses-express.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/express/kiwi-standard-responses-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -227,6 +240,7 @@ export const KiwiStandardResponsesExpress = {
standardErrorResponse,
standardUnauthorizedResponse,
standardForbiddenResponse,
standardForbiddenResponseWithEntity,
standardNotFoundResponse,
standardBadRequestResponse,
};

0 comments on commit 261feb8

Please sign in to comment.