@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
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
3
|
+
import { inboxFlowRequest } from "./inboxFlowRequest";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Makes an HTTP request using the Fetch API and returns a standardized response.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The expected type of the response data.
|
|
9
|
+
* @param method - The HTTP method to use for the request. Supported methods are:
|
|
10
|
+
* - "POST": Create a new resource.
|
|
11
|
+
* - "PUT": Update an existing resource.
|
|
12
|
+
* - "DELETE": Remove a resource.
|
|
13
|
+
* - "PATCH": Partially update a resource.
|
|
14
|
+
* - "GET": Retrieve a resource.
|
|
15
|
+
* @param url - The URL to which the request is sent.
|
|
16
|
+
* @param headers - Optional headers to include in the request. Defaults to an empty object.
|
|
17
|
+
* @param body - Optional body to include in the request. Should be serializable to JSON.
|
|
18
|
+
* @returns A promise that resolves to an `ApiResponseDTO<T>` object containing:
|
|
19
|
+
* - `success`: A boolean indicating whether the request was successful.
|
|
20
|
+
* - `status`: The HTTP status code of the response.
|
|
21
|
+
* - `message`: A message describing the result of the request.
|
|
22
|
+
* - `response`: The parsed JSON response data, or `null` if parsing fails.
|
|
23
|
+
* - `cause`: Additional error information, if applicable.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { makeRequest } from "./makeRequest";
|
|
28
|
+
*
|
|
29
|
+
* async function fetchData() {
|
|
30
|
+
* const response = await makeRequest("GET", "https://api.example.com/data");
|
|
31
|
+
* if (response.success) {
|
|
32
|
+
* console.log("Data:", response.response);
|
|
33
|
+
* } else {
|
|
34
|
+
* console.error("Error:", response.message);
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
async function makeRequest<T = any>(
|
|
41
|
+
method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET",
|
|
42
|
+
url: string,
|
|
43
|
+
headers: HeadersInit = {},
|
|
44
|
+
body?: any
|
|
45
|
+
): Promise<ApiResponseDTO<T>> {
|
|
46
|
+
const successMessage = {
|
|
47
|
+
POST: "Resource created successfully",
|
|
48
|
+
PUT: "Resource updated successfully",
|
|
49
|
+
DELETE: "Resource deleted successfully",
|
|
50
|
+
PATCH: "Resource patched successfully",
|
|
51
|
+
GET: "Request successful",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(url, {
|
|
56
|
+
method,
|
|
57
|
+
headers: {
|
|
58
|
+
...headers,
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
},
|
|
61
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const status = response.status;
|
|
65
|
+
|
|
66
|
+
let data: any = null;
|
|
67
|
+
try {
|
|
68
|
+
data = await response.json();
|
|
69
|
+
} catch {
|
|
70
|
+
data = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
inboxFlowRequest({
|
|
74
|
+
method,
|
|
75
|
+
status,
|
|
76
|
+
request: JSON.stringify(response.headers),
|
|
77
|
+
response: JSON.stringify(data),
|
|
78
|
+
token: (headers as any)?.Authorization || "TOKEN_NOT_FOUND",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
return {
|
|
83
|
+
success: false,
|
|
84
|
+
status,
|
|
85
|
+
message: data?.message || response.statusText || "Request failed",
|
|
86
|
+
response: data,
|
|
87
|
+
cause: null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
success: true,
|
|
93
|
+
status,
|
|
94
|
+
message: data?.message || successMessage[method],
|
|
95
|
+
response: data,
|
|
96
|
+
cause: null,
|
|
97
|
+
};
|
|
98
|
+
} catch (err) {
|
|
99
|
+
inboxFlowRequest({
|
|
100
|
+
method,
|
|
101
|
+
request: JSON.stringify(headers),
|
|
102
|
+
response: String(err),
|
|
103
|
+
status: 500,
|
|
104
|
+
token: "TOKEN_NOT_FOUND",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
status: 0,
|
|
110
|
+
message: "Network error or request failed",
|
|
111
|
+
response: null,
|
|
112
|
+
cause: err instanceof Error ? err.message : String(err),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { makeRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function patchRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("PATCH", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { patchRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function postRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("POST", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { postRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function putRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("PUT", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { putRequest };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { ApiInstance } from "../apiInstance";
|
|
3
|
+
import { getRequest } from "../../api/getRequest";
|
|
4
|
+
import { postRequest } from "../../api/postRequest";
|
|
5
|
+
import { putRequest } from "../../api/putRequest";
|
|
6
|
+
import { patchRequest } from "../../api/patchRequest";
|
|
7
|
+
import { deleteRequest } from "../../api/deleteRequest";
|
|
8
|
+
|
|
9
|
+
vi.mock("../../api/getRequest");
|
|
10
|
+
vi.mock("../../api/postRequest");
|
|
11
|
+
vi.mock("../../api/putRequest");
|
|
12
|
+
vi.mock("../../api/patchRequest");
|
|
13
|
+
vi.mock("../../api/deleteRequest");
|
|
14
|
+
|
|
15
|
+
describe("ApiInstance", () => {
|
|
16
|
+
const baseUrl = "https://api.example.com";
|
|
17
|
+
const baseHeaders = { "Content-Type": "application/json" };
|
|
18
|
+
const baseToken = "base-token";
|
|
19
|
+
|
|
20
|
+
const apiInstance = new ApiInstance({
|
|
21
|
+
baseUrl,
|
|
22
|
+
baseHeaders,
|
|
23
|
+
baseToken,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should send a GET request with correct headers and URL", async () => {
|
|
27
|
+
const mockResponse = { success: true, data: "GET response" };
|
|
28
|
+
(getRequest as any).mockResolvedValueOnce(mockResponse);
|
|
29
|
+
|
|
30
|
+
const response = await apiInstance.GET("/resource", {});
|
|
31
|
+
|
|
32
|
+
expect(getRequest).toHaveBeenCalledWith(
|
|
33
|
+
`${baseUrl}/resource`,
|
|
34
|
+
expect.objectContaining({
|
|
35
|
+
Authorization: `Bearer ${baseToken}`,
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
expect(response).toEqual(mockResponse);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should send a POST request with correct headers, URL, and body", async () => {
|
|
43
|
+
const mockResponse = { success: true, data: "POST response" };
|
|
44
|
+
const mockBody = { name: "New Resource" };
|
|
45
|
+
(postRequest as any).mockResolvedValueOnce(mockResponse);
|
|
46
|
+
|
|
47
|
+
const response = await apiInstance.POST("/resource", { body: mockBody });
|
|
48
|
+
|
|
49
|
+
expect(postRequest).toHaveBeenCalledWith(
|
|
50
|
+
`${baseUrl}/resource`,
|
|
51
|
+
expect.objectContaining({
|
|
52
|
+
Authorization: `Bearer ${baseToken}`,
|
|
53
|
+
"Content-Type": "application/json",
|
|
54
|
+
}),
|
|
55
|
+
mockBody
|
|
56
|
+
);
|
|
57
|
+
expect(response).toEqual(mockResponse);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should send a PUT request with correct headers, URL, and body", async () => {
|
|
61
|
+
const mockResponse = { success: true, data: "PUT response" };
|
|
62
|
+
const mockBody = { name: "Updated Resource" };
|
|
63
|
+
(putRequest as any).mockResolvedValueOnce(mockResponse);
|
|
64
|
+
|
|
65
|
+
const response = await apiInstance.PUT("/resource", { body: mockBody });
|
|
66
|
+
|
|
67
|
+
expect(putRequest).toHaveBeenCalledWith(
|
|
68
|
+
`${baseUrl}/resource`,
|
|
69
|
+
expect.objectContaining({
|
|
70
|
+
Authorization: `Bearer ${baseToken}`,
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
}),
|
|
73
|
+
mockBody
|
|
74
|
+
);
|
|
75
|
+
expect(response).toEqual(mockResponse);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should send a PATCH request with correct headers, URL, and body", async () => {
|
|
79
|
+
const mockResponse = { success: true, data: "PATCH response" };
|
|
80
|
+
const mockBody = { name: "Partially Updated Resource" };
|
|
81
|
+
(patchRequest as any).mockResolvedValueOnce(mockResponse);
|
|
82
|
+
|
|
83
|
+
const response = await apiInstance.PATCH("/resource", { body: mockBody });
|
|
84
|
+
|
|
85
|
+
expect(patchRequest).toHaveBeenCalledWith(
|
|
86
|
+
`${baseUrl}/resource`,
|
|
87
|
+
expect.objectContaining({
|
|
88
|
+
Authorization: `Bearer ${baseToken}`,
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
}),
|
|
91
|
+
mockBody
|
|
92
|
+
);
|
|
93
|
+
expect(response).toEqual(mockResponse);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should send a DELETE request with correct headers, URL, and body", async () => {
|
|
97
|
+
const mockResponse = { success: true, data: "DELETE response" };
|
|
98
|
+
const mockBody = { id: 1 };
|
|
99
|
+
(deleteRequest as any).mockResolvedValueOnce(mockResponse);
|
|
100
|
+
|
|
101
|
+
const response = await apiInstance.DELETE("/resource", { body: mockBody });
|
|
102
|
+
|
|
103
|
+
expect(deleteRequest).toHaveBeenCalledWith(
|
|
104
|
+
`${baseUrl}/resource`,
|
|
105
|
+
expect.objectContaining({
|
|
106
|
+
Authorization: `Bearer ${baseToken}`,
|
|
107
|
+
"Content-Type": "application/json",
|
|
108
|
+
}),
|
|
109
|
+
mockBody
|
|
110
|
+
);
|
|
111
|
+
expect(response).toEqual(mockResponse);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should override base token with provided token", async () => {
|
|
115
|
+
const mockResponse = { success: true, data: "GET response with token" };
|
|
116
|
+
(getRequest as any).mockResolvedValueOnce(mockResponse);
|
|
117
|
+
|
|
118
|
+
const response = await apiInstance.GET("/resource", { token: "new-token" });
|
|
119
|
+
|
|
120
|
+
expect(getRequest).toHaveBeenCalledWith(
|
|
121
|
+
`${baseUrl}/resource`,
|
|
122
|
+
expect.objectContaining({
|
|
123
|
+
Authorization: `Bearer new-token`,
|
|
124
|
+
"Content-Type": "application/json",
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
expect(response).toEqual(mockResponse);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { InboxFlowInstance } from "../inboxFlowInstance";
|
|
3
|
+
|
|
4
|
+
describe("InboxFlowInstance", () => {
|
|
5
|
+
it("should set the inbox configuration if not already set", () => {
|
|
6
|
+
const inboxConfig = {
|
|
7
|
+
inboxChannelId: "channel-123",
|
|
8
|
+
inboxUserToken: "user-token-abc",
|
|
9
|
+
inboxApiUrl: "https://custom-inbox-api.com",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
InboxFlowInstance.setInboxConfig(inboxConfig);
|
|
13
|
+
|
|
14
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
15
|
+
expect(config).toEqual(inboxConfig);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should not overwrite the inbox configuration if already set", () => {
|
|
19
|
+
const initialConfig = {
|
|
20
|
+
inboxChannelId: "channel-123",
|
|
21
|
+
inboxUserToken: "user-token-abc",
|
|
22
|
+
inboxApiUrl: "https://custom-inbox-api.com",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const newConfig = {
|
|
26
|
+
inboxChannelId: "channel-456",
|
|
27
|
+
inboxUserToken: "user-token-def",
|
|
28
|
+
inboxApiUrl: "https://another-inbox-api.com",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
InboxFlowInstance.setInboxConfig(initialConfig);
|
|
32
|
+
InboxFlowInstance.setInboxConfig(newConfig);
|
|
33
|
+
|
|
34
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
35
|
+
expect(config).toEqual(initialConfig);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should return undefined if no configuration is set", () => {
|
|
39
|
+
InboxFlowInstance.resetInboxConfig();
|
|
40
|
+
|
|
41
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
42
|
+
expect(config).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should use the default API URL if none is provided", () => {
|
|
46
|
+
const inboxConfig = {
|
|
47
|
+
inboxChannelId: "channel-123",
|
|
48
|
+
inboxUserToken: "user-token-abc",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
52
|
+
|
|
53
|
+
InboxFlowInstance.setInboxConfig(inboxConfig);
|
|
54
|
+
|
|
55
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
56
|
+
expect(config).toEqual({
|
|
57
|
+
...inboxConfig,
|
|
58
|
+
inboxApiUrl: defaultInboxURL,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { deleteRequest } from "../api/deleteRequest";
|
|
2
|
+
import { getRequest } from "../api/getRequest";
|
|
3
|
+
import { patchRequest } from "../api/patchRequest";
|
|
4
|
+
import { postRequest } from "../api/postRequest";
|
|
5
|
+
import { putRequest } from "../api/putRequest";
|
|
6
|
+
|
|
7
|
+
type ApiAinstanceContructorProps = {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
baseHeaders?: HeadersInit;
|
|
10
|
+
baseToken?: string | null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type ApiRequestDataWithoutBodyProps = {
|
|
14
|
+
headers?: HeadersInit;
|
|
15
|
+
token?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type ApiRequestDataWithBodyProps = {
|
|
19
|
+
body?: any;
|
|
20
|
+
headers?: HeadersInit;
|
|
21
|
+
token?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Class representing an API instance to handle HTTP requests with base configurations.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
class ApiInstance {
|
|
29
|
+
private baseUrl: string;
|
|
30
|
+
private baseHeaders?: HeadersInit;
|
|
31
|
+
private baseToken?: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates an instance of ApiInstance.
|
|
35
|
+
* @param props - The configuration properties for the API instance.
|
|
36
|
+
* @param props.baseUrl - The base URL for the API.
|
|
37
|
+
* @param props.baseHeaders - Optional base headers to include in all requests.
|
|
38
|
+
* @param props.baseToken - Optional base token for authorization.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
constructor(props: ApiAinstanceContructorProps) {
|
|
42
|
+
this.baseUrl = props.baseUrl;
|
|
43
|
+
this.baseHeaders = props.baseHeaders || undefined;
|
|
44
|
+
this.baseToken = props.baseToken || undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Generates the full URL by appending the route to the base URL.
|
|
49
|
+
* @param route - The route to append to the base URL.
|
|
50
|
+
* @returns The full URL as a string.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
private generateURL(route: string) {
|
|
54
|
+
return this.baseUrl + route;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Generates the headers for a request by merging base headers, provided headers, and tokens.
|
|
59
|
+
* @param initHeaders - Initial headers to include in the request.
|
|
60
|
+
* @param token - Optional token to override the base token.
|
|
61
|
+
* @returns The merged headers as a HeadersInit object.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
private generateHeaders(
|
|
65
|
+
initHeaders: HeadersInit,
|
|
66
|
+
token?: string
|
|
67
|
+
): HeadersInit {
|
|
68
|
+
let headers: HeadersInit = {};
|
|
69
|
+
if (this.baseToken) headers = { Authorization: `Bearer ${this.baseToken}` };
|
|
70
|
+
if (this.baseHeaders) headers = { ...headers, ...this.baseHeaders };
|
|
71
|
+
|
|
72
|
+
if (initHeaders) headers = { ...headers, ...initHeaders };
|
|
73
|
+
if (token) headers = { ...headers, Authorization: `Bearer ${token}` };
|
|
74
|
+
|
|
75
|
+
return headers;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sends a GET request to the specified route.
|
|
80
|
+
* @param route - The API route to send the GET request to.
|
|
81
|
+
* @param data - The request data, including optional headers and token.
|
|
82
|
+
* @returns The API response data.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
async GET(route: string, data?: ApiRequestDataWithoutBodyProps) {
|
|
86
|
+
const url = this.generateURL(route);
|
|
87
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
88
|
+
return await getRequest(url, headers);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sends a POST request to the specified route.
|
|
93
|
+
* @param route - The API route to send the POST request to.
|
|
94
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
95
|
+
* @returns The API response data.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
async POST(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
99
|
+
const url = this.generateURL(route);
|
|
100
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
101
|
+
const body = data?.body;
|
|
102
|
+
return await postRequest(url, headers, body);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Sends a PUT request to the specified route.
|
|
107
|
+
* @param route - The API route to send the PUT request to.
|
|
108
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
109
|
+
* @returns The API response data.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
async PUT(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
113
|
+
const url = this.generateURL(route);
|
|
114
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
115
|
+
const body = data?.body;
|
|
116
|
+
return await putRequest(url, headers, body);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sends a PATCH request to the specified route.
|
|
121
|
+
* @param route - The API route to send the PATCH request to.
|
|
122
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
123
|
+
* @returns The API response data.
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
async PATCH(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
127
|
+
const url = this.generateURL(route);
|
|
128
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
129
|
+
const body = data?.body;
|
|
130
|
+
return await patchRequest(url, headers, body);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sends a DELETE request to the specified route.
|
|
135
|
+
* @param route - The API route to send the DELETE request to.
|
|
136
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
137
|
+
* @returns The API response data.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
async DELETE(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
141
|
+
const url = this.generateURL(route);
|
|
142
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
143
|
+
const body = data?.body;
|
|
144
|
+
return await deleteRequest(url, headers, body);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { ApiInstance };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
type InboxConfigProps = {
|
|
2
|
+
inboxChannelId: string;
|
|
3
|
+
inboxUserToken: string;
|
|
4
|
+
inboxApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type SetInboxConfigProps = {
|
|
8
|
+
inboxChannelId: string;
|
|
9
|
+
inboxUserToken: string;
|
|
10
|
+
inboxApiUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
15
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
16
|
+
* user token, and API URL.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class InboxFlowInstance {
|
|
20
|
+
private static inboxConfig?: InboxConfigProps;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
24
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
25
|
+
* the method will return early without making any changes.
|
|
26
|
+
*
|
|
27
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
28
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
29
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
30
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
31
|
+
* a default URL will be used.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
static setInboxConfig(inboxConfig: SetInboxConfigProps) {
|
|
35
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
36
|
+
if (!!this.inboxConfig) return;
|
|
37
|
+
|
|
38
|
+
this.inboxConfig = {
|
|
39
|
+
inboxChannelId: inboxConfig.inboxChannelId,
|
|
40
|
+
inboxUserToken: inboxConfig.inboxUserToken,
|
|
41
|
+
inboxApiUrl: inboxConfig.inboxApiUrl || defaultInboxURL,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
47
|
+
*
|
|
48
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
49
|
+
* or `undefined` if no configuration has been initialized.
|
|
50
|
+
*/
|
|
51
|
+
static getInboxConfig(): InboxConfigProps | undefined {
|
|
52
|
+
return this.inboxConfig;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Resets the inbox configuration to `undefined`.
|
|
57
|
+
* This method can be used to clear the current configuration.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
static resetInboxConfig() {
|
|
61
|
+
this.inboxConfig = undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { InboxFlowInstance };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { BadGateway } from "../badGateway";
|
|
3
|
+
|
|
4
|
+
describe("BadGateway", () => {
|
|
5
|
+
it("should create an instance with the correct properties", () => {
|
|
6
|
+
const message = "Bad Gateway error occurred";
|
|
7
|
+
const cause = { reason: "Upstream server is down" };
|
|
8
|
+
const error = new BadGateway(message, cause);
|
|
9
|
+
|
|
10
|
+
expect(error.body).toEqual({ name: "BadGateway", message });
|
|
11
|
+
expect(error.status).toBe(502);
|
|
12
|
+
expect(error.statusText).toBe(message);
|
|
13
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should create an instance without a cause", () => {
|
|
17
|
+
const message = "Bad Gateway error occurred";
|
|
18
|
+
const error = new BadGateway(message);
|
|
19
|
+
|
|
20
|
+
expect(error.body).toEqual({ name: "BadGateway", message });
|
|
21
|
+
expect(error.status).toBe(502);
|
|
22
|
+
expect(error.statusText).toBe(message);
|
|
23
|
+
expect(error.cause).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return a Response object from toResponse", () => {
|
|
27
|
+
const message = "Bad Gateway error occurred";
|
|
28
|
+
const error = new BadGateway(message);
|
|
29
|
+
const response = error.toResponse();
|
|
30
|
+
|
|
31
|
+
expect(response.status).toBe(502);
|
|
32
|
+
expect(response.statusText).toBe(message);
|
|
33
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
34
|
+
response.json().then((body) => {
|
|
35
|
+
expect(body).toEqual({ name: "BadGateway", message });
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should return a Response object from toJson", () => {
|
|
40
|
+
const message = "Bad Gateway error occurred";
|
|
41
|
+
const error = new BadGateway(message);
|
|
42
|
+
const response = error.toJson();
|
|
43
|
+
|
|
44
|
+
expect(response.status).toBe(502);
|
|
45
|
+
expect(response.statusText).toBe(message);
|
|
46
|
+
response.json().then((body) => {
|
|
47
|
+
expect(body).toEqual({ name: "BadGateway", message });
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { BadRequest } from "../badRequest";
|
|
3
|
+
|
|
4
|
+
describe("BadRequest", () => {
|
|
5
|
+
it("should create an instance with the correct properties", () => {
|
|
6
|
+
const message = "Invalid request data";
|
|
7
|
+
const cause = { field: "email", error: "Invalid format" };
|
|
8
|
+
const error = new BadRequest(message, cause);
|
|
9
|
+
|
|
10
|
+
expect(error.body).toEqual({ name: "BadRequest", message });
|
|
11
|
+
expect(error.status).toBe(400);
|
|
12
|
+
expect(error.statusText).toBe(message);
|
|
13
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should create an instance without a cause", () => {
|
|
17
|
+
const message = "Invalid request data";
|
|
18
|
+
const error = new BadRequest(message);
|
|
19
|
+
|
|
20
|
+
expect(error.body).toEqual({ name: "BadRequest", message });
|
|
21
|
+
expect(error.status).toBe(400);
|
|
22
|
+
expect(error.statusText).toBe(message);
|
|
23
|
+
expect(error.cause).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return a Response object from toResponse", async () => {
|
|
27
|
+
const message = "Invalid request data";
|
|
28
|
+
const error = new BadRequest(message);
|
|
29
|
+
const response = error.toResponse();
|
|
30
|
+
|
|
31
|
+
expect(response.status).toBe(400);
|
|
32
|
+
expect(response.statusText).toBe(message);
|
|
33
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
34
|
+
|
|
35
|
+
const body = await response.json();
|
|
36
|
+
expect(body).toEqual({ name: "BadRequest", message });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should return a Response object from toJson", async () => {
|
|
40
|
+
const message = "Invalid request data";
|
|
41
|
+
const error = new BadRequest(message);
|
|
42
|
+
const response = error.toJson();
|
|
43
|
+
|
|
44
|
+
expect(response.status).toBe(400);
|
|
45
|
+
expect(response.statusText).toBe(message);
|
|
46
|
+
|
|
47
|
+
const body = await response.json();
|
|
48
|
+
expect(body).toEqual({ name: "BadRequest", message });
|
|
49
|
+
});
|
|
50
|
+
});
|