diff --git a/src/__tests__/responses.test.ts b/src/__tests__/responses.test.ts index 96eb910..abf4947 100644 --- a/src/__tests__/responses.test.ts +++ b/src/__tests__/responses.test.ts @@ -1,6 +1,7 @@ import { HttpStatusCodeEnum, ResponseErrorPreconditionFailed, + ResponseErrorUnauthorized, ResponseSuccessJson, } from "../responses"; @@ -35,6 +36,18 @@ describe("ResponseSuccessJson", () => { }); }); +describe("ResponseErrorUnauthorized", () => { + const anErrorDetail = "Invalid credentials"; + it("should return the standard response", () => { + expect(ResponseErrorUnauthorized(anErrorDetail)).toMatchObject( + expect.objectContaining({ + kind: "IResponseErrorUnauthorized", + detail: `Unauthorized: ${anErrorDetail}`, + }) + ); + }); +}); + describe("ResponseErrorForbiddenNotAuthorized", () => { it("should return the standard response", () => { expect(getResponseErrorForbiddenNotAuthorized()).toMatchObject( diff --git a/src/responses.ts b/src/responses.ts index 1e64e84..a112b8f 100644 --- a/src/responses.ts +++ b/src/responses.ts @@ -351,6 +351,27 @@ export const ResponseErrorFromValidationErrors = return ResponseErrorValidation(`Invalid ${type.name}`, detail); }; +/** + * Interface for 401 unauthorized + */ +export type IResponseErrorUnauthorized = + IResponse<"IResponseErrorUnauthorized"> & { readonly detail: string }; + +/** + * Returns an unauthorized error response with status code 401. + */ +export const ResponseErrorUnauthorized = ( + detail: string +): IResponseErrorUnauthorized => ({ + ...ResponseErrorGeneric( + HttpStatusCodeEnum.HTTP_STATUS_401, + "Unauthorized", + detail + ), + detail: `Unauthorized: ${detail}`, + kind: "IResponseErrorUnauthorized", +}); + /** * The user is not allowed here. */