@arkyn/server 1.9.1 → 1.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +24 -0
- package/README.md +116 -0
- package/dist/api/__test__/deleteRequest.spec.d.ts +2 -0
- package/dist/api/__test__/deleteRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/deleteRequest.spec.js +84 -0
- package/dist/api/__test__/getRequest.spec.d.ts +2 -0
- package/dist/api/__test__/getRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/getRequest.spec.js +62 -0
- package/dist/api/__test__/inboxFlowRequest.spec.d.ts +2 -0
- package/dist/api/__test__/inboxFlowRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/inboxFlowRequest.spec.js +96 -0
- package/dist/api/__test__/makeRequest.spec.d.ts +2 -0
- package/dist/api/__test__/makeRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/makeRequest.spec.js +80 -0
- package/dist/api/__test__/patchRequest.spec.d.ts +2 -0
- package/dist/api/__test__/patchRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/patchRequest.spec.js +84 -0
- package/dist/api/__test__/postRequest.spec.d.ts +2 -0
- package/dist/api/__test__/postRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/postRequest.spec.js +84 -0
- package/dist/api/__test__/putRequest.spec.d.ts +2 -0
- package/dist/api/__test__/putRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/putRequest.spec.js +84 -0
- package/dist/api/deleteRequest.d.ts +13 -0
- package/dist/api/deleteRequest.d.ts.map +1 -0
- package/dist/api/deleteRequest.js +14 -0
- package/dist/api/getRequest.d.ts +12 -0
- package/dist/api/getRequest.d.ts.map +1 -0
- package/dist/api/getRequest.js +13 -0
- package/dist/api/inboxFlowRequest.d.ts +40 -0
- package/dist/api/inboxFlowRequest.d.ts.map +1 -0
- package/dist/api/inboxFlowRequest.js +63 -0
- package/dist/api/makeRequest.d.ts +38 -0
- package/dist/api/makeRequest.d.ts.map +1 -0
- package/dist/api/makeRequest.js +103 -0
- package/dist/api/patchRequest.d.ts +13 -0
- package/dist/api/patchRequest.d.ts.map +1 -0
- package/dist/api/patchRequest.js +14 -0
- package/dist/api/postRequest.d.ts +13 -0
- package/dist/api/postRequest.d.ts.map +1 -0
- package/dist/api/postRequest.js +14 -0
- package/dist/api/putRequest.d.ts +13 -0
- package/dist/api/putRequest.d.ts.map +1 -0
- package/dist/api/putRequest.js +14 -0
- package/dist/config/__test__/apiInstance.spec.d.ts +2 -0
- package/dist/config/__test__/apiInstance.spec.d.ts.map +1 -0
- package/dist/config/__test__/apiInstance.spec.js +86 -0
- package/dist/config/__test__/inboxFlowInstance.spec.d.ts +2 -0
- package/dist/config/__test__/inboxFlowInstance.spec.d.ts.map +1 -0
- package/dist/config/__test__/inboxFlowInstance.spec.js +48 -0
- package/dist/config/apiInstance.d.ts +80 -0
- package/dist/config/apiInstance.d.ts.map +1 -0
- package/dist/config/apiInstance.js +111 -0
- package/dist/config/inboxFlowInstance.d.ts +44 -0
- package/dist/config/inboxFlowInstance.d.ts.map +1 -0
- package/dist/config/inboxFlowInstance.js +46 -0
- package/dist/http/badResponses/__test__/badGateway.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/badGateway.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/badGateway.spec.js +42 -0
- package/dist/http/badResponses/__test__/badRequest.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/badRequest.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/badRequest.spec.js +40 -0
- package/dist/http/badResponses/__test__/conflict.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/conflict.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/conflict.spec.js +40 -0
- package/dist/http/badResponses/__test__/forbidden.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/forbidden.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/forbidden.spec.js +40 -0
- package/dist/http/badResponses/__test__/notFound.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/notFound.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/notFound.spec.js +40 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.js +40 -0
- package/dist/http/badResponses/__test__/serverError.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/serverError.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/serverError.spec.js +40 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.js +40 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.js +66 -0
- package/dist/http/badResponses/badGateway.d.ts +34 -0
- package/dist/http/badResponses/badGateway.d.ts.map +1 -0
- package/dist/http/badResponses/badGateway.js +52 -0
- package/dist/http/badResponses/badRequest.d.ts +34 -0
- package/dist/http/badResponses/badRequest.d.ts.map +1 -0
- package/dist/http/badResponses/badRequest.js +52 -0
- package/dist/http/badResponses/conflict.d.ts +34 -0
- package/dist/http/badResponses/conflict.d.ts.map +1 -0
- package/dist/http/badResponses/conflict.js +52 -0
- package/dist/http/badResponses/forbidden.d.ts +34 -0
- package/dist/http/badResponses/forbidden.d.ts.map +1 -0
- package/dist/http/badResponses/forbidden.js +52 -0
- package/dist/http/badResponses/notFound.d.ts +34 -0
- package/dist/http/badResponses/notFound.d.ts.map +1 -0
- package/dist/http/badResponses/notFound.js +52 -0
- package/dist/http/badResponses/notImplemented.d.ts +34 -0
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -0
- package/dist/http/badResponses/notImplemented.js +52 -0
- package/dist/http/badResponses/serverError.d.ts +34 -0
- package/dist/http/badResponses/serverError.d.ts.map +1 -0
- package/dist/http/badResponses/serverError.js +52 -0
- package/dist/http/badResponses/unauthorized.d.ts +34 -0
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -0
- package/dist/http/badResponses/unauthorized.js +52 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts +43 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -0
- package/dist/http/badResponses/unprocessableEntity.js +60 -0
- package/dist/http/successResponses/__test__/created.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/created.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/created.spec.js +57 -0
- package/dist/http/successResponses/__test__/found.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/found.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/found.spec.js +57 -0
- package/dist/http/successResponses/__test__/noContent.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/noContent.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/noContent.spec.js +40 -0
- package/dist/http/successResponses/__test__/success.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/success.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/success.spec.js +60 -0
- package/dist/http/successResponses/__test__/updated.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/updated.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/updated.spec.js +60 -0
- package/dist/http/successResponses/created.d.ts +36 -0
- package/dist/http/successResponses/created.d.ts.map +1 -0
- package/dist/http/successResponses/created.js +54 -0
- package/dist/http/successResponses/found.d.ts +39 -0
- package/dist/http/successResponses/found.d.ts.map +1 -0
- package/dist/http/successResponses/found.js +57 -0
- package/dist/http/successResponses/noContent.d.ts +25 -0
- package/dist/http/successResponses/noContent.d.ts.map +1 -0
- package/dist/http/successResponses/noContent.js +35 -0
- package/dist/http/successResponses/success.d.ts +36 -0
- package/dist/http/successResponses/success.d.ts.map +1 -0
- package/dist/http/successResponses/success.js +54 -0
- package/dist/http/successResponses/updated.d.ts +36 -0
- package/dist/http/successResponses/updated.d.ts.map +1 -0
- package/dist/http/successResponses/updated.js +54 -0
- package/dist/index.d.ts +22 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -15
- package/dist/services/__test__/decodeRequestBody.spec.d.ts +2 -0
- package/dist/services/__test__/decodeRequestBody.spec.d.ts.map +1 -0
- package/dist/services/__test__/decodeRequestBody.spec.js +39 -0
- package/dist/services/__test__/errorHandler.spec.d.ts +2 -0
- package/dist/services/__test__/errorHandler.spec.d.ts.map +1 -0
- package/dist/services/__test__/errorHandler.spec.js +115 -0
- package/dist/services/__test__/formParse.spec.d.ts +2 -0
- package/dist/services/__test__/formParse.spec.d.ts.map +1 -0
- package/dist/services/__test__/formParse.spec.js +78 -0
- package/dist/services/__test__/getCaller.spec.d.ts +2 -0
- package/dist/services/__test__/getCaller.spec.d.ts.map +1 -0
- package/dist/services/__test__/getCaller.spec.js +60 -0
- package/dist/services/__test__/getScopedParams.spec.d.ts +2 -0
- package/dist/services/__test__/getScopedParams.spec.d.ts.map +1 -0
- package/dist/services/__test__/getScopedParams.spec.js +51 -0
- package/dist/services/__test__/schemaValidator.spec.d.ts +2 -0
- package/dist/services/__test__/schemaValidator.spec.d.ts.map +1 -0
- package/dist/services/__test__/schemaValidator.spec.js +89 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
- package/dist/services/decodeErrorMessageFromRequest.js +30 -0
- package/dist/services/decodeRequestBody.d.ts +17 -0
- package/dist/services/decodeRequestBody.d.ts.map +1 -0
- package/dist/services/decodeRequestBody.js +38 -0
- package/dist/services/errorHandler.d.ts +44 -0
- package/dist/services/errorHandler.d.ts.map +1 -0
- package/dist/services/errorHandler.js +93 -0
- package/dist/services/formParse.d.ts +40 -1
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +44 -3
- package/dist/services/getCaller.d.ts +17 -0
- package/dist/services/getCaller.d.ts.map +1 -0
- package/dist/services/getCaller.js +45 -0
- package/dist/services/getScopedParams.d.ts +26 -1
- package/dist/services/getScopedParams.d.ts.map +1 -1
- package/dist/services/getScopedParams.js +26 -2
- package/dist/services/httpDebug.d.ts +35 -0
- package/dist/services/httpDebug.d.ts.map +1 -0
- package/dist/services/httpDebug.js +56 -0
- package/dist/services/schemaValidator.d.ts +13 -0
- package/dist/services/schemaValidator.d.ts.map +1 -0
- package/dist/services/schemaValidator.js +51 -0
- package/dist/services/sendFileToS3.d.ts +50 -18
- package/dist/services/sendFileToS3.d.ts.map +1 -1
- package/dist/services/sendFileToS3.js +73 -31
- package/package.json +14 -6
- package/src/api/__test__/deleteRequest.spec.ts +107 -0
- package/src/api/__test__/getRequest.spec.ts +76 -0
- package/src/api/__test__/inboxFlowRequest.spec.ts +125 -0
- package/src/api/__test__/makeRequest.spec.ts +121 -0
- package/src/api/__test__/patchRequest.spec.ts +125 -0
- package/src/api/__test__/postRequest.spec.ts +125 -0
- package/src/api/__test__/putRequest.spec.ts +125 -0
- package/src/api/deleteRequest.ts +22 -0
- package/src/api/getRequest.ts +20 -0
- package/src/api/inboxFlowRequest.ts +76 -0
- package/src/api/makeRequest.ts +117 -0
- package/src/api/patchRequest.ts +22 -0
- package/src/api/postRequest.ts +22 -0
- package/src/api/putRequest.ts +22 -0
- package/src/config/__test__/apiInstance.spec.ts +129 -0
- package/src/config/__test__/inboxFlowInstance.spec.ts +61 -0
- package/src/config/apiInstance.ts +148 -0
- package/src/config/inboxFlowInstance.ts +65 -0
- package/src/http/badResponses/__test__/badGateway.spec.ts +50 -0
- package/src/http/badResponses/__test__/badRequest.spec.ts +50 -0
- package/src/http/badResponses/__test__/conflict.spec.ts +50 -0
- package/src/http/badResponses/__test__/forbidden.spec.ts +50 -0
- package/src/http/badResponses/__test__/notFound.spec.ts +50 -0
- package/src/http/badResponses/__test__/notImplemented.spec.ts +50 -0
- package/src/http/badResponses/__test__/serverError.spec.ts +50 -0
- package/src/http/badResponses/__test__/unauthorized.spec.ts +50 -0
- package/src/http/badResponses/__test__/unprocessableEntity.spec.ts +76 -0
- package/src/http/badResponses/badGateway.ts +63 -0
- package/src/http/badResponses/badRequest.ts +63 -0
- package/src/http/badResponses/conflict.ts +63 -0
- package/src/http/badResponses/forbidden.ts +63 -0
- package/src/http/badResponses/notFound.ts +63 -0
- package/src/http/badResponses/notImplemented.ts +63 -0
- package/src/http/badResponses/serverError.ts +63 -0
- package/src/http/badResponses/unauthorized.ts +63 -0
- package/src/http/badResponses/unprocessableEntity.ts +79 -0
- package/src/http/successResponses/__test__/created.spec.ts +65 -0
- package/src/http/successResponses/__test__/found.spec.ts +65 -0
- package/src/http/successResponses/__test__/noContent.spec.ts +49 -0
- package/src/http/successResponses/__test__/success.spec.ts +68 -0
- package/src/http/successResponses/__test__/updated.spec.ts +68 -0
- package/src/http/successResponses/created.ts +64 -0
- package/src/http/successResponses/found.ts +67 -0
- package/src/http/successResponses/noContent.ts +42 -0
- package/src/http/successResponses/success.ts +64 -0
- package/src/http/successResponses/updated.ts +64 -0
- package/src/index.ts +25 -16
- package/src/services/__test__/decodeRequestBody.spec.ts +51 -0
- package/src/services/__test__/errorHandler.spec.ts +133 -0
- package/src/services/__test__/formParse.spec.ts +96 -0
- package/src/services/__test__/getCaller.spec.ts +78 -0
- package/src/services/__test__/getScopedParams.spec.ts +68 -0
- package/src/services/__test__/schemaValidator.spec.ts +106 -0
- package/src/services/decodeErrorMessageFromRequest.ts +36 -0
- package/src/services/decodeRequestBody.ts +42 -0
- package/src/services/errorHandler.ts +99 -0
- package/src/services/formParse.ts +46 -6
- package/src/services/getCaller.ts +58 -0
- package/src/services/getScopedParams.ts +29 -2
- package/src/services/httpDebug.ts +64 -0
- package/src/services/schemaValidator.ts +66 -0
- package/src/services/sendFileToS3.ts +81 -56
- package/vitest.config.ts +5 -0
- package/dist/helpers/globalErrorHandler.d.ts +0 -3
- package/dist/helpers/globalErrorHandler.d.ts.map +0 -1
- package/dist/helpers/globalErrorHandler.js +0 -53
- package/dist/httpBadResponses/badRequest.d.ts +0 -9
- package/dist/httpBadResponses/badRequest.d.ts.map +0 -1
- package/dist/httpBadResponses/badRequest.js +0 -23
- package/dist/httpBadResponses/conflict.d.ts +0 -9
- package/dist/httpBadResponses/conflict.d.ts.map +0 -1
- package/dist/httpBadResponses/conflict.js +0 -23
- package/dist/httpBadResponses/forbidden.d.ts +0 -9
- package/dist/httpBadResponses/forbidden.d.ts.map +0 -1
- package/dist/httpBadResponses/forbidden.js +0 -23
- package/dist/httpBadResponses/notFound.d.ts +0 -9
- package/dist/httpBadResponses/notFound.d.ts.map +0 -1
- package/dist/httpBadResponses/notFound.js +0 -23
- package/dist/httpBadResponses/serverError.d.ts +0 -9
- package/dist/httpBadResponses/serverError.d.ts.map +0 -1
- package/dist/httpBadResponses/serverError.js +0 -23
- package/dist/httpBadResponses/unauthorized.d.ts +0 -9
- package/dist/httpBadResponses/unauthorized.d.ts.map +0 -1
- package/dist/httpBadResponses/unauthorized.js +0 -23
- package/dist/httpBadResponses/unprocessableEntity.d.ts +0 -17
- package/dist/httpBadResponses/unprocessableEntity.d.ts.map +0 -1
- package/dist/httpBadResponses/unprocessableEntity.js +0 -30
- package/dist/httpResponses/created.d.ts +0 -10
- package/dist/httpResponses/created.d.ts.map +0 -1
- package/dist/httpResponses/created.js +0 -30
- package/dist/httpResponses/noContent.d.ts +0 -9
- package/dist/httpResponses/noContent.d.ts.map +0 -1
- package/dist/httpResponses/noContent.js +0 -28
- package/dist/httpResponses/success.d.ts +0 -10
- package/dist/httpResponses/success.d.ts.map +0 -1
- package/dist/httpResponses/success.js +0 -30
- package/dist/httpResponses/updated.d.ts +0 -10
- package/dist/httpResponses/updated.d.ts.map +0 -1
- package/dist/httpResponses/updated.js +0 -30
- package/dist/services/extractJsonFromRequest.d.ts +0 -3
- package/dist/services/extractJsonFromRequest.d.ts.map +0 -1
- package/dist/services/extractJsonFromRequest.js +0 -29
- package/src/helpers/globalErrorHandler.ts +0 -64
- package/src/httpBadResponses/badRequest.ts +0 -29
- package/src/httpBadResponses/conflict.ts +0 -29
- package/src/httpBadResponses/forbidden.ts +0 -29
- package/src/httpBadResponses/notFound.ts +0 -29
- package/src/httpBadResponses/serverError.ts +0 -29
- package/src/httpBadResponses/unauthorized.ts +0 -29
- package/src/httpBadResponses/unprocessableEntity.ts +0 -43
- package/src/httpResponses/created.ts +0 -35
- package/src/httpResponses/noContent.ts +0 -33
- package/src/httpResponses/success.ts +0 -35
- package/src/httpResponses/updated.ts +0 -35
- package/src/services/extractJsonFromRequest.ts +0 -30
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { httpDebug } from "../../services/httpDebug";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents an HTTP error response with a status code of 401 (Unauthorized).
|
|
5
|
+
* This class is used to standardize the structure of an "Unauthorized" error response,
|
|
6
|
+
* including the response body, headers, status, and status text.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
class Unauthorized {
|
|
10
|
+
body: any;
|
|
11
|
+
cause?: any;
|
|
12
|
+
status: number = 401;
|
|
13
|
+
statusText: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `Unauthorized` class.
|
|
17
|
+
*
|
|
18
|
+
* @param message - A descriptive message explaining why the request is unauthorized.
|
|
19
|
+
* @param cause - Optional additional information about the cause of the error.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
constructor(message: string, cause?: any) {
|
|
23
|
+
this.body = { name: "Unauthorized", message: message };
|
|
24
|
+
this.statusText = message;
|
|
25
|
+
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
26
|
+
httpDebug("Unauthorized", this.body, this.cause);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Converts the `Unauthorized` instance into a `Response` object with a JSON body.
|
|
31
|
+
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
+
*
|
|
33
|
+
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
toResponse(): Response {
|
|
37
|
+
const responseInit: ResponseInit = {
|
|
38
|
+
headers: { "Content-Type": "application/json" },
|
|
39
|
+
status: this.status,
|
|
40
|
+
statusText: this.statusText,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `Unauthorized` instance into a `Response` object using the `Response.json` method.
|
|
48
|
+
* This method is an alternative to `toResponse` for generating JSON error responses.
|
|
49
|
+
*
|
|
50
|
+
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
toJson(): Response {
|
|
54
|
+
const responseInit: ResponseInit = {
|
|
55
|
+
status: this.status,
|
|
56
|
+
statusText: this.statusText,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return Response.json(this.body, responseInit);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { Unauthorized };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { httpDebug } from "../../services/httpDebug";
|
|
2
|
+
|
|
3
|
+
type UnprocessableEntityProps = {
|
|
4
|
+
data?: any;
|
|
5
|
+
fieldErrors?: Record<string, string>;
|
|
6
|
+
fields?: Record<string, string>;
|
|
7
|
+
message?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Represents an HTTP error response with a status code of 422 (Unprocessable Entity).
|
|
12
|
+
* This class is used to standardize the structure of an "Unprocessable Entity" error response,
|
|
13
|
+
* including the response body, headers, status, and status text.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
class UnprocessableEntity {
|
|
17
|
+
body: any;
|
|
18
|
+
status: number = 422;
|
|
19
|
+
statusText: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates an instance of the `UnprocessableEntity` class.
|
|
23
|
+
*
|
|
24
|
+
* @param props - An object containing details about the error, such as:
|
|
25
|
+
* - `data`: Additional data related to the error.
|
|
26
|
+
* - `fieldErrors`: A record of field-specific error messages.
|
|
27
|
+
* - `fields`: A record of field values that caused the error.
|
|
28
|
+
* - `message`: A descriptive message explaining the error.
|
|
29
|
+
* @param enableDebug - A boolean indicating whether to enable debug logging for this error.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
constructor(props: UnprocessableEntityProps, enableDebug = false) {
|
|
33
|
+
this.statusText = props.message || "Unprocessable Entity";
|
|
34
|
+
this.body = {
|
|
35
|
+
name: "UnprocessableEntity",
|
|
36
|
+
message: props.message || null,
|
|
37
|
+
data: props.data,
|
|
38
|
+
fieldErrors: props.fieldErrors,
|
|
39
|
+
fields: props.fields,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
enableDebug && httpDebug("UnprocessableEntity", this.body);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Converts the `UnprocessableEntity` instance into a `Response` object with a JSON body.
|
|
47
|
+
* This method ensures the response has the appropriate headers, status, and status text.
|
|
48
|
+
*
|
|
49
|
+
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
toResponse(): Response {
|
|
53
|
+
const responseInit: ResponseInit = {
|
|
54
|
+
headers: { "Content-Type": "application/json" },
|
|
55
|
+
status: this.status,
|
|
56
|
+
statusText: this.statusText,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return new Response(JSON.stringify(this.body), responseInit);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Converts the `UnprocessableEntity` instance into a `Response` object using the `Response.json` method.
|
|
64
|
+
* This method is an alternative to `toResponse` for generating JSON error responses.
|
|
65
|
+
*
|
|
66
|
+
* @returns A `Response` object with the JSON body and response metadata.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
toJson(): Response {
|
|
70
|
+
const responseInit: ResponseInit = {
|
|
71
|
+
status: this.status,
|
|
72
|
+
statusText: this.statusText,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return Response.json(this.body, responseInit);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { UnprocessableEntity };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Created } from "../created";
|
|
3
|
+
|
|
4
|
+
describe("Created", () => {
|
|
5
|
+
it("should create a response with default values", () => {
|
|
6
|
+
const body = { message: "Resource created" };
|
|
7
|
+
const created = new Created(body);
|
|
8
|
+
|
|
9
|
+
const response = created.toResponse();
|
|
10
|
+
expect(response.status).toBe(201);
|
|
11
|
+
expect(response.statusText).toBe("Resource created successfully");
|
|
12
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
13
|
+
response.json().then((data) => {
|
|
14
|
+
expect(data).toEqual(body);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should create a response with custom values", () => {
|
|
19
|
+
const body = { message: "Custom resource created" };
|
|
20
|
+
const init = {
|
|
21
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
22
|
+
status: 202,
|
|
23
|
+
statusText: "Accepted",
|
|
24
|
+
};
|
|
25
|
+
const created = new Created(body, init);
|
|
26
|
+
|
|
27
|
+
const response = created.toResponse();
|
|
28
|
+
expect(response.status).toBe(202);
|
|
29
|
+
expect(response.statusText).toBe("Accepted");
|
|
30
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
31
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
32
|
+
response.json().then((data) => {
|
|
33
|
+
expect(data).toEqual(body);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should create a JSON response with default values", () => {
|
|
38
|
+
const body = { message: "Resource created" };
|
|
39
|
+
const created = new Created(body);
|
|
40
|
+
|
|
41
|
+
const response = created.toJson();
|
|
42
|
+
expect(response.status).toBe(201);
|
|
43
|
+
expect(response.statusText).toBe("Resource created successfully");
|
|
44
|
+
response.json().then((data) => {
|
|
45
|
+
expect(data).toEqual(body);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should create a JSON response with custom values", () => {
|
|
50
|
+
const body = { message: "Custom resource created" };
|
|
51
|
+
const init = {
|
|
52
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
53
|
+
status: 202,
|
|
54
|
+
statusText: "Accepted",
|
|
55
|
+
};
|
|
56
|
+
const created = new Created(body, init);
|
|
57
|
+
|
|
58
|
+
const response = created.toJson();
|
|
59
|
+
expect(response.status).toBe(202);
|
|
60
|
+
expect(response.statusText).toBe("Accepted");
|
|
61
|
+
response.json().then((data) => {
|
|
62
|
+
expect(data).toEqual(body);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Found } from "../found";
|
|
3
|
+
|
|
4
|
+
describe("Found", () => {
|
|
5
|
+
it("should create a response with default values", () => {
|
|
6
|
+
const body = { message: "Resource found" };
|
|
7
|
+
const found = new Found(body);
|
|
8
|
+
|
|
9
|
+
const response = found.toResponse();
|
|
10
|
+
expect(response.status).toBe(302);
|
|
11
|
+
expect(response.statusText).toBe("Found");
|
|
12
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
13
|
+
response.json().then((data) => {
|
|
14
|
+
expect(data).toEqual(body);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should create a response with custom values", () => {
|
|
19
|
+
const body = { message: "Custom resource found" };
|
|
20
|
+
const init = {
|
|
21
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
22
|
+
status: 301,
|
|
23
|
+
statusText: "Moved Permanently",
|
|
24
|
+
};
|
|
25
|
+
const found = new Found(body, init);
|
|
26
|
+
|
|
27
|
+
const response = found.toResponse();
|
|
28
|
+
expect(response.status).toBe(301);
|
|
29
|
+
expect(response.statusText).toBe("Moved Permanently");
|
|
30
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
31
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
32
|
+
response.json().then((data) => {
|
|
33
|
+
expect(data).toEqual(body);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should create a JSON response with default values", () => {
|
|
38
|
+
const body = { message: "Resource found" };
|
|
39
|
+
const found = new Found(body);
|
|
40
|
+
|
|
41
|
+
const response = found.toJson();
|
|
42
|
+
expect(response.status).toBe(302);
|
|
43
|
+
expect(response.statusText).toBe("Found");
|
|
44
|
+
response.json().then((data) => {
|
|
45
|
+
expect(data).toEqual(body);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should create a JSON response with custom values", () => {
|
|
50
|
+
const body = { message: "Custom resource found" };
|
|
51
|
+
const init = {
|
|
52
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
53
|
+
status: 301,
|
|
54
|
+
statusText: "Moved Permanently",
|
|
55
|
+
};
|
|
56
|
+
const found = new Found(body, init);
|
|
57
|
+
|
|
58
|
+
const response = found.toJson();
|
|
59
|
+
expect(response.status).toBe(301);
|
|
60
|
+
expect(response.statusText).toBe("Moved Permanently");
|
|
61
|
+
response.json().then((data) => {
|
|
62
|
+
expect(data).toEqual(body);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NoContent } from "../noContent";
|
|
3
|
+
|
|
4
|
+
describe("NoContent", () => {
|
|
5
|
+
it("should create a NoContent instance with default values", () => {
|
|
6
|
+
const noContent = new NoContent();
|
|
7
|
+
|
|
8
|
+
expect(noContent.status).toBe(204);
|
|
9
|
+
expect(noContent.statusText).toBe("No content");
|
|
10
|
+
expect(noContent.headers).toEqual({});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should create a NoContent instance with custom values", () => {
|
|
14
|
+
const customInit = {
|
|
15
|
+
headers: { "Content-Type": "application/json" },
|
|
16
|
+
status: 200,
|
|
17
|
+
statusText: "Custom Status",
|
|
18
|
+
};
|
|
19
|
+
const noContent = new NoContent(customInit);
|
|
20
|
+
|
|
21
|
+
expect(noContent.status).toBe(200);
|
|
22
|
+
expect(noContent.statusText).toBe("Custom Status");
|
|
23
|
+
expect(noContent.headers).toEqual(customInit.headers);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return a Response object with default values from toResponse", () => {
|
|
27
|
+
const noContent = new NoContent();
|
|
28
|
+
const response = noContent.toResponse();
|
|
29
|
+
|
|
30
|
+
expect(response.status).toBe(204);
|
|
31
|
+
expect(response.statusText).toBe("No content");
|
|
32
|
+
expect(response.headers.get("Content-Type")).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should return a Response object with custom values from toResponse", () => {
|
|
36
|
+
const customInit = {
|
|
37
|
+
headers: { "Content-Type": "application/json" },
|
|
38
|
+
status: 200,
|
|
39
|
+
statusText: "Custom Status",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const noContent = new NoContent(customInit);
|
|
43
|
+
const response = noContent.toResponse();
|
|
44
|
+
|
|
45
|
+
expect(response.status).toBe(200);
|
|
46
|
+
expect(response.statusText).toBe("Custom Status");
|
|
47
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Success } from "../success";
|
|
3
|
+
|
|
4
|
+
describe("Success", () => {
|
|
5
|
+
it("should create a response with default values", () => {
|
|
6
|
+
const body = { message: "Success" };
|
|
7
|
+
const success = new Success(body);
|
|
8
|
+
|
|
9
|
+
const response = success.toResponse();
|
|
10
|
+
expect(response.status).toBe(200);
|
|
11
|
+
expect(response.statusText).toBe("OK");
|
|
12
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
13
|
+
return response.json().then((data) => {
|
|
14
|
+
expect(data).toEqual(body);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should create a response with custom values", () => {
|
|
19
|
+
const body = { message: "Custom Success" };
|
|
20
|
+
const init = {
|
|
21
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
22
|
+
status: 201,
|
|
23
|
+
statusText: "Created",
|
|
24
|
+
};
|
|
25
|
+
const success = new Success(body, init);
|
|
26
|
+
|
|
27
|
+
const response = success.toResponse();
|
|
28
|
+
expect(response.status).toBe(201);
|
|
29
|
+
expect(response.statusText).toBe("Created");
|
|
30
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
31
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
32
|
+
return response.json().then((data) => {
|
|
33
|
+
expect(data).toEqual(body);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should create a JSON response with default values using toJson", () => {
|
|
38
|
+
const body = { message: "Success" };
|
|
39
|
+
const success = new Success(body);
|
|
40
|
+
|
|
41
|
+
const response = success.toJson();
|
|
42
|
+
expect(response.status).toBe(200);
|
|
43
|
+
expect(response.statusText).toBe("OK");
|
|
44
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
45
|
+
return response.json().then((data) => {
|
|
46
|
+
expect(data).toEqual(body);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should create a JSON response with custom values using toJson", () => {
|
|
51
|
+
const body = { message: "Custom Success" };
|
|
52
|
+
const init = {
|
|
53
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
54
|
+
status: 201,
|
|
55
|
+
statusText: "Created",
|
|
56
|
+
};
|
|
57
|
+
const success = new Success(body, init);
|
|
58
|
+
|
|
59
|
+
const response = success.toJson();
|
|
60
|
+
expect(response.status).toBe(201);
|
|
61
|
+
expect(response.statusText).toBe("Created");
|
|
62
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
63
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
64
|
+
return response.json().then((data) => {
|
|
65
|
+
expect(data).toEqual(body);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Updated } from "../updated";
|
|
3
|
+
|
|
4
|
+
describe("Updated", () => {
|
|
5
|
+
it("should create a response with default values", () => {
|
|
6
|
+
const body = { message: "Resource updated" };
|
|
7
|
+
const updated = new Updated(body);
|
|
8
|
+
|
|
9
|
+
const response = updated.toResponse();
|
|
10
|
+
expect(response.status).toBe(200);
|
|
11
|
+
expect(response.statusText).toBe("Resource updated successfully");
|
|
12
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
13
|
+
return response.json().then((data) => {
|
|
14
|
+
expect(data).toEqual(body);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should create a response with custom values", () => {
|
|
19
|
+
const body = { message: "Custom update" };
|
|
20
|
+
const init = {
|
|
21
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
22
|
+
status: 202,
|
|
23
|
+
statusText: "Accepted",
|
|
24
|
+
};
|
|
25
|
+
const updated = new Updated(body, init);
|
|
26
|
+
|
|
27
|
+
const response = updated.toResponse();
|
|
28
|
+
expect(response.status).toBe(202);
|
|
29
|
+
expect(response.statusText).toBe("Accepted");
|
|
30
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
31
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
32
|
+
return response.json().then((data) => {
|
|
33
|
+
expect(data).toEqual(body);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should create a JSON response with default values using toJson", () => {
|
|
38
|
+
const body = { message: "Resource updated" };
|
|
39
|
+
const updated = new Updated(body);
|
|
40
|
+
|
|
41
|
+
const response = updated.toJson();
|
|
42
|
+
expect(response.status).toBe(200);
|
|
43
|
+
expect(response.statusText).toBe("Resource updated successfully");
|
|
44
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
45
|
+
return response.json().then((data) => {
|
|
46
|
+
expect(data).toEqual(body);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should create a JSON response with custom values using toJson", () => {
|
|
51
|
+
const body = { message: "Custom update" };
|
|
52
|
+
const init = {
|
|
53
|
+
headers: { "X-Custom-Header": "CustomValue" },
|
|
54
|
+
status: 202,
|
|
55
|
+
statusText: "Accepted",
|
|
56
|
+
};
|
|
57
|
+
const updated = new Updated(body, init);
|
|
58
|
+
|
|
59
|
+
const response = updated.toJson();
|
|
60
|
+
expect(response.status).toBe(202);
|
|
61
|
+
expect(response.statusText).toBe("Accepted");
|
|
62
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
63
|
+
expect(response.headers.get("X-Custom-Header")).toBe("CustomValue");
|
|
64
|
+
return response.json().then((data) => {
|
|
65
|
+
expect(data).toEqual(body);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful HTTP response with a status code of 201 (Created).
|
|
3
|
+
* This class is used to standardize the structure of a "Created" response,
|
|
4
|
+
* including the response body, headers, status, and status text.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the response body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
class Created<T> {
|
|
10
|
+
body: T;
|
|
11
|
+
headers: ResponseInit["headers"];
|
|
12
|
+
status: number;
|
|
13
|
+
statusText: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `Created` class.
|
|
17
|
+
*
|
|
18
|
+
* @param body - The response body to be included in the HTTP response.
|
|
19
|
+
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
constructor(body: T, init?: ResponseInit) {
|
|
23
|
+
this.body = body;
|
|
24
|
+
this.headers = init?.headers || {};
|
|
25
|
+
this.status = init?.status || 201;
|
|
26
|
+
this.statusText = init?.statusText || "Resource created successfully";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Converts the `Created` instance into a `Response` object with a JSON body.
|
|
31
|
+
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
+
*
|
|
33
|
+
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
toResponse(): Response {
|
|
37
|
+
const responseInit: ResponseInit = {
|
|
38
|
+
headers: { "Content-Type": "application/json", ...this.headers },
|
|
39
|
+
status: this.status,
|
|
40
|
+
statusText: this.statusText,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `Created` instance into a `Response` object using the `Response.json` method.
|
|
48
|
+
* This method is an alternative to `toResponse` for generating JSON responses.
|
|
49
|
+
*
|
|
50
|
+
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
toJson(): Response {
|
|
54
|
+
const responseInit: ResponseInit = {
|
|
55
|
+
headers: this.headers,
|
|
56
|
+
status: this.status,
|
|
57
|
+
statusText: this.statusText,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return Response.json(this.body, responseInit);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { Created };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an HTTP 302 Found response.
|
|
3
|
+
* This class is used to create a standardized HTTP response with a status of 302 (Found),
|
|
4
|
+
* including optional headers and a response body.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the response body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
class Found<T> {
|
|
10
|
+
body: T;
|
|
11
|
+
headers: ResponseInit["headers"];
|
|
12
|
+
status: number;
|
|
13
|
+
statusText: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `Found` class.
|
|
17
|
+
*
|
|
18
|
+
* @param body - The body of the response.
|
|
19
|
+
* @param init - Optional initialization object to set headers, status, and statusText.
|
|
20
|
+
* - `headers`: Additional headers to include in the response.
|
|
21
|
+
* - `status`: HTTP status code (default is 302).
|
|
22
|
+
* - `statusText`: HTTP status text (default is "Found").
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
constructor(body: T, init?: ResponseInit) {
|
|
26
|
+
this.body = body;
|
|
27
|
+
this.headers = init?.headers || {};
|
|
28
|
+
this.status = init?.status || 302;
|
|
29
|
+
this.statusText = init?.statusText || "Found";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Converts the `Found` instance into a `Response` object.
|
|
34
|
+
* This method serializes the response body as JSON and includes the appropriate headers.
|
|
35
|
+
*
|
|
36
|
+
* @returns A `Response` object with the serialized JSON body and the specified headers, status, and statusText.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
toResponse() {
|
|
40
|
+
const responseInit: ResponseInit = {
|
|
41
|
+
headers: { "Content-Type": "application/json", ...this.headers },
|
|
42
|
+
status: this.status,
|
|
43
|
+
statusText: this.statusText,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
return new Response(JSON.stringify(this.body), responseInit);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Converts the `Found` instance into a JSON response using `Response.json`.
|
|
51
|
+
* This method is an alternative to `toResponse` for creating JSON responses.
|
|
52
|
+
*
|
|
53
|
+
* @returns A `Response` object with the JSON body and the specified headers, status, and statusText.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
toJson() {
|
|
57
|
+
const responseInit: ResponseInit = {
|
|
58
|
+
headers: this.headers,
|
|
59
|
+
status: this.status,
|
|
60
|
+
statusText: this.statusText,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return Response.json(this.body, responseInit);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { Found };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful HTTP response with a status code of 204 (No Content).
|
|
3
|
+
* This class is used to standardize the structure of a "No Content" response,
|
|
4
|
+
* including headers, status, and status text.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class NoContent {
|
|
8
|
+
headers: ResponseInit["headers"];
|
|
9
|
+
status: number;
|
|
10
|
+
statusText: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance of the `NoContent` class.
|
|
14
|
+
*
|
|
15
|
+
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
constructor(init?: ResponseInit) {
|
|
19
|
+
this.headers = init?.headers || {};
|
|
20
|
+
this.status = init?.status || 204;
|
|
21
|
+
this.statusText = init?.statusText ?? "No content";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Converts the `NoContent` instance into a `Response` object.
|
|
26
|
+
* This method ensures the response has the appropriate headers, status, and status text.
|
|
27
|
+
*
|
|
28
|
+
* @returns A `Response` object with no body and response metadata.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
toResponse(): Response {
|
|
32
|
+
const responseInit: ResponseInit = {
|
|
33
|
+
headers: this.headers,
|
|
34
|
+
status: this.status,
|
|
35
|
+
statusText: this.statusText,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return new Response(null, responseInit);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { NoContent };
|