@arkyn/server 2.0.1-beta.8 → 2.0.1
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 +23 -0
- package/dist/http/badResponses/badGateway.d.ts.map +1 -1
- package/dist/http/badResponses/badGateway.js +24 -1
- package/dist/http/badResponses/badRequest.d.ts +23 -0
- package/dist/http/badResponses/badRequest.d.ts.map +1 -1
- package/dist/http/badResponses/badRequest.js +24 -1
- package/dist/http/badResponses/conflict.d.ts +23 -0
- package/dist/http/badResponses/conflict.d.ts.map +1 -1
- package/dist/http/badResponses/conflict.js +24 -1
- package/dist/http/badResponses/forbidden.d.ts +23 -0
- package/dist/http/badResponses/forbidden.d.ts.map +1 -1
- package/dist/http/badResponses/forbidden.js +25 -2
- package/dist/http/badResponses/notFound.d.ts +23 -0
- package/dist/http/badResponses/notFound.d.ts.map +1 -1
- package/dist/http/badResponses/notFound.js +24 -1
- package/dist/http/badResponses/notImplemented.d.ts +23 -0
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
- package/dist/http/badResponses/notImplemented.js +24 -1
- package/dist/http/badResponses/serverError.d.ts +23 -0
- package/dist/http/badResponses/serverError.d.ts.map +1 -1
- package/dist/http/badResponses/serverError.js +24 -1
- package/dist/http/badResponses/unauthorized.d.ts +23 -0
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
- package/dist/http/badResponses/unauthorized.js +24 -1
- package/dist/http/badResponses/unprocessableEntity.d.ts +28 -1
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
- package/dist/http/badResponses/unprocessableEntity.js +30 -3
- 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 +25 -0
- package/dist/http/successResponses/created.d.ts.map +1 -1
- package/dist/http/successResponses/created.js +25 -0
- package/dist/http/successResponses/found.d.ts +28 -0
- package/dist/http/successResponses/found.d.ts.map +1 -1
- package/dist/http/successResponses/found.js +28 -0
- package/dist/http/successResponses/noContent.d.ts +16 -1
- package/dist/http/successResponses/noContent.d.ts.map +1 -1
- package/dist/http/successResponses/noContent.js +16 -8
- package/dist/http/successResponses/success.d.ts +25 -0
- package/dist/http/successResponses/success.d.ts.map +1 -1
- package/dist/http/successResponses/success.js +25 -0
- package/dist/http/successResponses/updated.d.ts +25 -0
- package/dist/http/successResponses/updated.d.ts.map +1 -1
- package/dist/http/successResponses/updated.js +25 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- 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.map +1 -1
- package/dist/services/decodeRequestBody.js +12 -16
- package/dist/services/errorHandler.d.ts +42 -1
- package/dist/services/errorHandler.d.ts.map +1 -1
- package/dist/services/errorHandler.js +42 -0
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +4 -2
- 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/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/package.json +13 -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 +29 -2
- package/src/http/badResponses/badRequest.ts +29 -2
- package/src/http/badResponses/conflict.ts +29 -2
- package/src/http/badResponses/forbidden.ts +30 -3
- package/src/http/badResponses/notFound.ts +29 -2
- package/src/http/badResponses/notImplemented.ts +29 -2
- package/src/http/badResponses/serverError.ts +29 -2
- package/src/http/badResponses/unauthorized.ts +29 -2
- package/src/http/badResponses/unprocessableEntity.ts +33 -2
- 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 +31 -2
- package/src/http/successResponses/found.ts +32 -0
- package/src/http/successResponses/noContent.ts +20 -11
- package/src/http/successResponses/success.ts +31 -2
- package/src/http/successResponses/updated.ts +31 -2
- package/src/index.ts +9 -1
- 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 +11 -15
- package/src/services/errorHandler.ts +45 -1
- package/src/services/formParse.ts +4 -5
- package/src/services/getCaller.ts +39 -21
- package/src/services/httpDebug.ts +46 -5
- package/vitest.config.ts +5 -0
- package/dist/http/httpDebug.d.ts +0 -3
- package/dist/http/httpDebug.d.ts.map +0 -1
- package/dist/http/httpDebug.js +0 -15
|
@@ -1,32 +1,41 @@
|
|
|
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
|
+
|
|
1
7
|
class NoContent {
|
|
2
8
|
headers: ResponseInit["headers"];
|
|
3
9
|
status: number;
|
|
4
10
|
statusText: string;
|
|
5
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
|
+
|
|
6
18
|
constructor(init?: ResponseInit) {
|
|
7
19
|
this.headers = init?.headers || {};
|
|
8
20
|
this.status = init?.status || 204;
|
|
9
21
|
this.statusText = init?.statusText ?? "No content";
|
|
10
22
|
}
|
|
11
23
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
*/
|
|
18
30
|
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
toJson() {
|
|
31
|
+
toResponse(): Response {
|
|
23
32
|
const responseInit: ResponseInit = {
|
|
24
33
|
headers: this.headers,
|
|
25
34
|
status: this.status,
|
|
26
35
|
statusText: this.statusText,
|
|
27
36
|
};
|
|
28
37
|
|
|
29
|
-
return Response
|
|
38
|
+
return new Response(null, responseInit);
|
|
30
39
|
}
|
|
31
40
|
}
|
|
32
41
|
|
|
@@ -1,9 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful HTTP response with a status code of 200 (OK).
|
|
3
|
+
* This class is used to standardize the structure of a "Success" response,
|
|
4
|
+
* including the response body, headers, status, and status text.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the response body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
1
9
|
class Success<T> {
|
|
2
10
|
body: T;
|
|
3
11
|
headers: ResponseInit["headers"];
|
|
4
12
|
status: number;
|
|
5
13
|
statusText: string;
|
|
6
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `Success` 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
|
+
|
|
7
22
|
constructor(body: T, init?: ResponseInit) {
|
|
8
23
|
this.body = body;
|
|
9
24
|
this.headers = init?.headers || {};
|
|
@@ -11,7 +26,14 @@ class Success<T> {
|
|
|
11
26
|
this.statusText = init?.statusText ?? "OK";
|
|
12
27
|
}
|
|
13
28
|
|
|
14
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Converts the `Success` 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 {
|
|
15
37
|
const responseInit: ResponseInit = {
|
|
16
38
|
headers: { "Content-Type": "application/json", ...this.headers },
|
|
17
39
|
status: this.status,
|
|
@@ -21,7 +43,14 @@ class Success<T> {
|
|
|
21
43
|
return new Response(JSON.stringify(this.body), responseInit);
|
|
22
44
|
}
|
|
23
45
|
|
|
24
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `Success` 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 {
|
|
25
54
|
const responseInit: ResponseInit = {
|
|
26
55
|
headers: this.headers,
|
|
27
56
|
status: this.status,
|
|
@@ -1,9 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful HTTP response with a status code of 200 (OK) or other custom status codes.
|
|
3
|
+
* This class is used to standardize the structure of an "Updated" response,
|
|
4
|
+
* including the response body, headers, status, and status text.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the response body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
1
9
|
class Updated<T> {
|
|
2
10
|
body: T;
|
|
3
11
|
headers: ResponseInit["headers"];
|
|
4
12
|
status: number;
|
|
5
13
|
statusText: string;
|
|
6
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `Updated` 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
|
+
|
|
7
22
|
constructor(body: T, init?: ResponseInit) {
|
|
8
23
|
this.body = body;
|
|
9
24
|
this.headers = init?.headers || {};
|
|
@@ -11,7 +26,14 @@ class Updated<T> {
|
|
|
11
26
|
this.statusText = init?.statusText || "Resource updated successfully";
|
|
12
27
|
}
|
|
13
28
|
|
|
14
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Converts the `Updated` 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 {
|
|
15
37
|
const responseInit: ResponseInit = {
|
|
16
38
|
headers: { "Content-Type": "application/json", ...this.headers },
|
|
17
39
|
status: this.status,
|
|
@@ -21,7 +43,14 @@ class Updated<T> {
|
|
|
21
43
|
return new Response(JSON.stringify(this.body), responseInit);
|
|
22
44
|
}
|
|
23
45
|
|
|
24
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `Updated` 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 {
|
|
25
54
|
const responseInit: ResponseInit = {
|
|
26
55
|
headers: this.headers,
|
|
27
56
|
status: this.status,
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// config
|
|
2
|
+
export { ApiInstance } from "./config/apiInstance";
|
|
3
|
+
export { InboxFlowInstance } from "./config/inboxFlowInstance";
|
|
4
|
+
|
|
5
|
+
// http bad responses
|
|
1
6
|
export { BadGateway } from "./http/badResponses/badGateway";
|
|
2
7
|
export { BadRequest } from "./http/badResponses/badRequest";
|
|
3
8
|
export { Conflict } from "./http/badResponses/conflict";
|
|
@@ -8,17 +13,20 @@ export { ServerError } from "./http/badResponses/serverError";
|
|
|
8
13
|
export { Unauthorized } from "./http/badResponses/unauthorized";
|
|
9
14
|
export { UnprocessableEntity } from "./http/badResponses/unprocessableEntity";
|
|
10
15
|
|
|
16
|
+
// http success responses
|
|
11
17
|
export { Created } from "./http/successResponses/created";
|
|
12
18
|
export { Found } from "./http/successResponses/found";
|
|
13
19
|
export { NoContent } from "./http/successResponses/noContent";
|
|
14
20
|
export { Success } from "./http/successResponses/success";
|
|
15
21
|
export { Updated } from "./http/successResponses/updated";
|
|
16
22
|
|
|
23
|
+
// services
|
|
24
|
+
export { decodeErrorMessageFromRequest } from "./services/decodeErrorMessageFromRequest";
|
|
17
25
|
export { decodeRequestBody } from "./services/decodeRequestBody";
|
|
18
26
|
export { errorHandler } from "./services/errorHandler";
|
|
19
27
|
export { formParse } from "./services/formParse";
|
|
20
28
|
export { getCaller } from "./services/getCaller";
|
|
21
29
|
export { getScopedParams } from "./services/getScopedParams";
|
|
22
30
|
export { httpDebug } from "./services/httpDebug";
|
|
23
|
-
export { sendFileToS3 } from "./services/sendFileToS3";
|
|
24
31
|
export { SchemaValidator } from "./services/schemaValidator";
|
|
32
|
+
export { sendFileToS3 } from "./services/sendFileToS3";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { decodeRequestBody } from "../decodeRequestBody";
|
|
2
|
+
import { BadRequest } from "../../http/badResponses/badRequest";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
describe("decodeRequestBody", () => {
|
|
6
|
+
it("should decode the request body as JSON", async () => {
|
|
7
|
+
const mockRequest = {
|
|
8
|
+
arrayBuffer: async () =>
|
|
9
|
+
new TextEncoder().encode(JSON.stringify({ key: "value" })).buffer,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const result = await decodeRequestBody(mockRequest as any);
|
|
13
|
+
expect(result).toEqual({ key: "value" });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should decode the request body as URL-encoded form data", async () => {
|
|
17
|
+
const mockRequest = {
|
|
18
|
+
arrayBuffer: async () =>
|
|
19
|
+
new TextEncoder().encode("key=value&anotherKey=anotherValue").buffer,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const result = await decodeRequestBody(mockRequest as any);
|
|
23
|
+
expect(result).toEqual({ key: "value", anotherKey: "anotherValue" });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should throw BadRequest if the text is neither valid JSON nor URL-encoded", async () => {
|
|
27
|
+
const mockRequest = {
|
|
28
|
+
arrayBuffer: async () => new TextEncoder().encode("{invalidJson").buffer,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
await expect(decodeRequestBody(mockRequest as any)).rejects.toThrow();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should throw BadRequest if the text is not a valid format for URLSearchParams", async () => {
|
|
35
|
+
const mockRequest = {
|
|
36
|
+
arrayBuffer: async () => new TextEncoder().encode("invalidFormat").buffer,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
await expect(decodeRequestBody(mockRequest as any)).rejects.toThrow();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should throw BadRequest if an error occurs while reading the body", async () => {
|
|
43
|
+
const mockRequest = {
|
|
44
|
+
arrayBuffer: async () => {
|
|
45
|
+
throw new Error("Error reading the body");
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
await expect(decodeRequestBody(mockRequest as any)).rejects.toThrow();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { errorHandler } from "../errorHandler";
|
|
2
|
+
import { Found } from "../../http/successResponses/found";
|
|
3
|
+
import { Created } from "../../http/successResponses/created";
|
|
4
|
+
import { Updated } from "../../http/successResponses/updated";
|
|
5
|
+
import { Success } from "../../http/successResponses/success";
|
|
6
|
+
import { NoContent } from "../../http/successResponses/noContent";
|
|
7
|
+
|
|
8
|
+
import { BadGateway } from "../../http/badResponses/badGateway";
|
|
9
|
+
import { BadRequest } from "../../http/badResponses/badRequest";
|
|
10
|
+
import { Conflict } from "../../http/badResponses/conflict";
|
|
11
|
+
import { Forbidden } from "../../http/badResponses/forbidden";
|
|
12
|
+
import { NotFound } from "../../http/badResponses/notFound";
|
|
13
|
+
import { NotImplemented } from "../../http/badResponses/notImplemented";
|
|
14
|
+
import { ServerError } from "../../http/badResponses/serverError";
|
|
15
|
+
import { Unauthorized } from "../../http/badResponses/unauthorized";
|
|
16
|
+
import { UnprocessableEntity } from "../../http/badResponses/unprocessableEntity";
|
|
17
|
+
import { describe, expect, it } from "vitest";
|
|
18
|
+
|
|
19
|
+
describe("errorHandler", () => {
|
|
20
|
+
describe("Success Responses", () => {
|
|
21
|
+
it("should handle Found response", () => {
|
|
22
|
+
const found = new Found("Resource found");
|
|
23
|
+
const response = errorHandler(found);
|
|
24
|
+
expect(response.status).toBe(found.toResponse().status);
|
|
25
|
+
expect(response.statusText).toBe(found.toResponse().statusText);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should handle Created response", () => {
|
|
29
|
+
const created = new Created("Resource created");
|
|
30
|
+
const response = errorHandler(created);
|
|
31
|
+
expect(response.status).toBe(created.toResponse().status);
|
|
32
|
+
expect(response.statusText).toBe(created.toResponse().statusText);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should handle Updated response", () => {
|
|
36
|
+
const updated = new Updated("Resource updated");
|
|
37
|
+
const response = errorHandler(updated);
|
|
38
|
+
expect(response.status).toBe(updated.toResponse().status);
|
|
39
|
+
expect(response.statusText).toBe(updated.toResponse().statusText);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should handle Success response", () => {
|
|
43
|
+
const success = new Success("Operation successful");
|
|
44
|
+
const response = errorHandler(success);
|
|
45
|
+
expect(response.status).toBe(success.toResponse().status);
|
|
46
|
+
expect(response.statusText).toBe(success.toResponse().statusText);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should handle NoContent response", () => {
|
|
50
|
+
const noContent = new NoContent();
|
|
51
|
+
const response = errorHandler(noContent);
|
|
52
|
+
expect(response.status).toBe(noContent.toResponse().status);
|
|
53
|
+
expect(response.statusText).toBe(noContent.toResponse().statusText);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("Error Responses", () => {
|
|
58
|
+
it("should handle BadGateway error", () => {
|
|
59
|
+
const badGateway = new BadGateway("Bad gateway error");
|
|
60
|
+
const response = errorHandler(badGateway);
|
|
61
|
+
expect(response.status).toBe(badGateway.toResponse().status);
|
|
62
|
+
expect(response.statusText).toBe(badGateway.toResponse().statusText);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle BadRequest error", () => {
|
|
66
|
+
const badRequest = new BadRequest("Bad request error");
|
|
67
|
+
const response = errorHandler(badRequest);
|
|
68
|
+
expect(response.status).toBe(badRequest.toResponse().status);
|
|
69
|
+
expect(response.statusText).toBe(badRequest.toResponse().statusText);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should handle Conflict error", () => {
|
|
73
|
+
const conflict = new Conflict("Conflict error");
|
|
74
|
+
const response = errorHandler(conflict);
|
|
75
|
+
expect(response.status).toBe(conflict.toResponse().status);
|
|
76
|
+
expect(response.statusText).toBe(conflict.toResponse().statusText);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should handle Forbidden error", () => {
|
|
80
|
+
const forbidden = new Forbidden("Forbidden error");
|
|
81
|
+
const response = errorHandler(forbidden);
|
|
82
|
+
expect(response.status).toBe(forbidden.toResponse().status);
|
|
83
|
+
expect(response.statusText).toBe(forbidden.toResponse().statusText);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should handle NotFound error", () => {
|
|
87
|
+
const notFound = new NotFound("Not found error");
|
|
88
|
+
const response = errorHandler(notFound);
|
|
89
|
+
expect(response.status).toBe(notFound.toResponse().status);
|
|
90
|
+
expect(response.statusText).toBe(notFound.toResponse().statusText);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should handle NotImplemented error", () => {
|
|
94
|
+
const notImplemented = new NotImplemented("Not implemented error");
|
|
95
|
+
const response = errorHandler(notImplemented);
|
|
96
|
+
expect(response.status).toBe(notImplemented.toResponse().status);
|
|
97
|
+
expect(response.statusText).toBe(notImplemented.toResponse().statusText);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should handle ServerError error", () => {
|
|
101
|
+
const serverError = new ServerError("Server error");
|
|
102
|
+
const response = errorHandler(serverError);
|
|
103
|
+
expect(response.status).toBe(serverError.toResponse().status);
|
|
104
|
+
expect(response.statusText).toBe(serverError.toResponse().statusText);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should handle Unauthorized error", () => {
|
|
108
|
+
const unauthorized = new Unauthorized("Unauthorized error");
|
|
109
|
+
const response = errorHandler(unauthorized);
|
|
110
|
+
expect(response.status).toBe(unauthorized.toResponse().status);
|
|
111
|
+
expect(response.statusText).toBe(unauthorized.toResponse().statusText);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should handle UnprocessableEntity error", () => {
|
|
115
|
+
const unprocessableEntity = new UnprocessableEntity({});
|
|
116
|
+
const response = errorHandler(unprocessableEntity);
|
|
117
|
+
expect(response.status).toBe(unprocessableEntity.toResponse().status);
|
|
118
|
+
expect(response.statusText).toBe(
|
|
119
|
+
unprocessableEntity.toResponse().statusText
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe("Unknown Error", () => {
|
|
125
|
+
it("should return undefined for unknown error types", () => {
|
|
126
|
+
const unknownError = new Error("Unknown error");
|
|
127
|
+
const serverError = new ServerError("Server error");
|
|
128
|
+
const response = errorHandler(unknownError);
|
|
129
|
+
expect(response.status).toBe(serverError.toResponse().status);
|
|
130
|
+
expect(response.statusText).toBe(serverError.toResponse().statusText);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { formParse } from "../formParse";
|
|
4
|
+
|
|
5
|
+
describe("formParse", () => {
|
|
6
|
+
it("should return success and parsed data when validation passes", () => {
|
|
7
|
+
const schema = z.object({
|
|
8
|
+
name: z.string().min(1, "Name is required"),
|
|
9
|
+
age: z.number().min(18, "Must be at least 18"),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const formData = { name: "John", age: 25 };
|
|
13
|
+
|
|
14
|
+
const result = formParse([formData, schema]);
|
|
15
|
+
|
|
16
|
+
expect(result.success).toBe(true);
|
|
17
|
+
expect(result).toHaveProperty("data");
|
|
18
|
+
if (result.success) expect(result.data).toEqual(formData);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should return field errors when validation fails", () => {
|
|
22
|
+
const schema = z.object({
|
|
23
|
+
name: z.string().min(1, "Name is required"),
|
|
24
|
+
age: z.number().min(18, "Must be at least 18"),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const formData = { name: "", age: 17 };
|
|
28
|
+
|
|
29
|
+
const result = formParse([formData, schema]);
|
|
30
|
+
|
|
31
|
+
expect(result.success).toBe(false);
|
|
32
|
+
expect(result).toHaveProperty("fieldErrors");
|
|
33
|
+
|
|
34
|
+
if (!result.success) {
|
|
35
|
+
expect(result.fieldErrors).toEqual({
|
|
36
|
+
name: "Name is required",
|
|
37
|
+
age: "Must be at least 18",
|
|
38
|
+
});
|
|
39
|
+
expect(result.fields).toEqual(formData);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle missing fields gracefully", () => {
|
|
44
|
+
const schema = z.object({
|
|
45
|
+
name: z
|
|
46
|
+
.string({ required_error: "Name is required" })
|
|
47
|
+
.min(1, "Name is required"),
|
|
48
|
+
age: z.number().min(18, "Must be at least 18"),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const formData = { age: 20 }; // Missing "name"
|
|
52
|
+
|
|
53
|
+
const result = formParse([formData, schema]);
|
|
54
|
+
|
|
55
|
+
expect(result.success).toBe(false);
|
|
56
|
+
if (!result.success) {
|
|
57
|
+
expect(result.fieldErrors).toEqual({ name: "Name is required" });
|
|
58
|
+
expect(result.fields).toEqual(formData);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should return success for an empty schema", () => {
|
|
63
|
+
const schema = z.object({});
|
|
64
|
+
const formData = {};
|
|
65
|
+
|
|
66
|
+
const result = formParse([formData, schema]);
|
|
67
|
+
|
|
68
|
+
expect(result.success).toBe(true);
|
|
69
|
+
expect(result).toHaveProperty("data");
|
|
70
|
+
if (result.success) expect(result.data).toEqual(formData);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should handle nested schemas correctly", () => {
|
|
74
|
+
const schema = z.object({
|
|
75
|
+
user: z.object({
|
|
76
|
+
name: z.string().min(1, "Name is required"),
|
|
77
|
+
age: z.number().min(18, "Must be at least 18"),
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const formData = {
|
|
82
|
+
user: { name: "", age: 16 },
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const result = formParse([formData, schema]);
|
|
86
|
+
|
|
87
|
+
expect(result.success).toBe(false);
|
|
88
|
+
if (!result.success) {
|
|
89
|
+
expect(result.fieldErrors).toEqual({
|
|
90
|
+
"user.name": "Name is required",
|
|
91
|
+
"user.age": "Must be at least 18",
|
|
92
|
+
});
|
|
93
|
+
expect(result.fields).toEqual(formData);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getCaller } from "../getCaller";
|
|
3
|
+
|
|
4
|
+
describe("getCaller", () => {
|
|
5
|
+
it("should return the correct caller function name and file path", () => {
|
|
6
|
+
function testFunction() {
|
|
7
|
+
return getCaller();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const result = testFunction();
|
|
11
|
+
|
|
12
|
+
expect(result.functionName).toBe("testFunction");
|
|
13
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should handle calls from anonymous functions", () => {
|
|
17
|
+
const result = (() => getCaller())();
|
|
18
|
+
|
|
19
|
+
expect(result.functionName).toBe("Unknown function");
|
|
20
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should return the correct name for nested functions", () => {
|
|
24
|
+
function outerFunction() {
|
|
25
|
+
function innerFunction() {
|
|
26
|
+
return getCaller();
|
|
27
|
+
}
|
|
28
|
+
return innerFunction();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = outerFunction();
|
|
32
|
+
|
|
33
|
+
expect(result.functionName).toBe("innerFunction");
|
|
34
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return method name when called from object method", () => {
|
|
38
|
+
const obj = {
|
|
39
|
+
method() {
|
|
40
|
+
return getCaller();
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const result = obj.method();
|
|
45
|
+
|
|
46
|
+
expect(result.functionName).toMatch(/method|Object.method/);
|
|
47
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should return method name when called from class method", () => {
|
|
51
|
+
class TestClass {
|
|
52
|
+
call() {
|
|
53
|
+
return getCaller();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const instance = new TestClass();
|
|
58
|
+
const result = instance.call();
|
|
59
|
+
|
|
60
|
+
expect(result.functionName).toMatch(/call|TestClass.call/);
|
|
61
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should return the correct caller for indirect calls", () => {
|
|
65
|
+
function intermediary(fn: () => any) {
|
|
66
|
+
return fn();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function actualCaller() {
|
|
70
|
+
return getCaller();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const result = intermediary(actualCaller);
|
|
74
|
+
|
|
75
|
+
expect(result.functionName).toBe("actualCaller");
|
|
76
|
+
expect(result.callerInfo).toContain("getCaller.spec.ts");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getScopedParams } from "../getScopedParams";
|
|
3
|
+
|
|
4
|
+
describe("getScopedParams", () => {
|
|
5
|
+
it("should return all query parameters when no scope is provided", () => {
|
|
6
|
+
const request = {
|
|
7
|
+
url: "https://example.com?key1=value1&key2=value2",
|
|
8
|
+
} as Request;
|
|
9
|
+
|
|
10
|
+
const params = getScopedParams(request);
|
|
11
|
+
|
|
12
|
+
expect(params.get("key1")).toBe("value1");
|
|
13
|
+
expect(params.get("key2")).toBe("value2");
|
|
14
|
+
expect(params.toString()).toBe("key1=value1&key2=value2");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return only scoped query parameters when a scope is provided", () => {
|
|
18
|
+
const request = {
|
|
19
|
+
url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3",
|
|
20
|
+
} as Request;
|
|
21
|
+
|
|
22
|
+
const params = getScopedParams(request, "scope");
|
|
23
|
+
|
|
24
|
+
expect(params.get("key1")).toBe("value1");
|
|
25
|
+
expect(params.get("key2")).toBe("value2");
|
|
26
|
+
expect(params.get("key3")).toBeNull();
|
|
27
|
+
expect(params.toString()).toBe("key1=value1&key2=value2");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return an empty URLSearchParams object if no parameters match the scope", () => {
|
|
31
|
+
const request = {
|
|
32
|
+
url: "https://example.com?key1=value1&key2=value2",
|
|
33
|
+
} as Request;
|
|
34
|
+
|
|
35
|
+
const params = getScopedParams(request, "scope");
|
|
36
|
+
|
|
37
|
+
expect(params.toString()).toBe("");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should handle URLs with no query parameters", () => {
|
|
41
|
+
const request = { url: "https://example.com" } as Request;
|
|
42
|
+
const params = getScopedParams(request);
|
|
43
|
+
|
|
44
|
+
expect(params.toString()).toBe("");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should handle URLs with query parameters but no matching scope", () => {
|
|
48
|
+
const request = {
|
|
49
|
+
url: "https://example.com?other:key1=value1&other:key2=value2",
|
|
50
|
+
} as Request;
|
|
51
|
+
|
|
52
|
+
const params = getScopedParams(request, "scope");
|
|
53
|
+
|
|
54
|
+
expect(params.toString()).toBe("");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should handle special characters in query parameter keys and values", () => {
|
|
58
|
+
const request = {
|
|
59
|
+
url: "https://example.com?scope:key+1=value+1&scope:key+2=value+2",
|
|
60
|
+
} as Request;
|
|
61
|
+
|
|
62
|
+
const params = getScopedParams(request, "scope");
|
|
63
|
+
|
|
64
|
+
expect(params.get("key 1")).toBe("value 1");
|
|
65
|
+
expect(params.get("key 2")).toBe("value 2");
|
|
66
|
+
expect(params.toString()).toBe("key+1=value+1&key+2=value+2");
|
|
67
|
+
});
|
|
68
|
+
});
|