@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,121 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { makeRequest } from "../makeRequest";
|
|
3
|
+
|
|
4
|
+
global.fetch = vi.fn() as any;
|
|
5
|
+
|
|
6
|
+
describe("makeRequest", () => {
|
|
7
|
+
it("should return success response for a successful POST request", async () => {
|
|
8
|
+
const mockResponse = { message: "Resource created successfully" };
|
|
9
|
+
const mockBody = { name: "New Resource" };
|
|
10
|
+
|
|
11
|
+
(fetch as any).mockResolvedValueOnce({
|
|
12
|
+
ok: true,
|
|
13
|
+
status: 201,
|
|
14
|
+
json: async () => mockResponse,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const response = await makeRequest(
|
|
18
|
+
"POST",
|
|
19
|
+
"https://api.example.com/resource",
|
|
20
|
+
{ "Content-Type": "application/json" },
|
|
21
|
+
mockBody
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
expect(response).toEqual({
|
|
25
|
+
success: true,
|
|
26
|
+
status: 201,
|
|
27
|
+
message: "Resource created successfully",
|
|
28
|
+
response: mockResponse,
|
|
29
|
+
cause: null,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return failed response for a failed PUT request", async () => {
|
|
34
|
+
const mockErrorResponse = { error: "Invalid data" };
|
|
35
|
+
const mockBody = { name: "" };
|
|
36
|
+
|
|
37
|
+
(fetch as any).mockResolvedValueOnce({
|
|
38
|
+
ok: false,
|
|
39
|
+
status: 400,
|
|
40
|
+
json: async () => mockErrorResponse,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const response = await makeRequest(
|
|
44
|
+
"PUT",
|
|
45
|
+
"https://api.example.com/resource",
|
|
46
|
+
{ "Content-Type": "application/json" },
|
|
47
|
+
mockBody
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(response.success).toBe(false);
|
|
51
|
+
expect(response.status).toBe(400);
|
|
52
|
+
expect(response.response).toEqual(mockErrorResponse);
|
|
53
|
+
expect(response.message).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should handle network errors gracefully", async () => {
|
|
57
|
+
(fetch as any).mockRejectedValueOnce(new Error("Network Error"));
|
|
58
|
+
|
|
59
|
+
const response = await makeRequest(
|
|
60
|
+
"GET",
|
|
61
|
+
"https://api.example.com/resource",
|
|
62
|
+
{ "Content-Type": "application/json" }
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(response).toEqual({
|
|
66
|
+
success: false,
|
|
67
|
+
status: 0,
|
|
68
|
+
message: "Network error or request failed",
|
|
69
|
+
response: null,
|
|
70
|
+
cause: "Network Error",
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should handle invalid JSON response gracefully", async () => {
|
|
75
|
+
(fetch as any).mockResolvedValueOnce({
|
|
76
|
+
ok: true,
|
|
77
|
+
status: 200,
|
|
78
|
+
json: async () => {
|
|
79
|
+
throw new Error("Invalid JSON");
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const response = await makeRequest(
|
|
84
|
+
"GET",
|
|
85
|
+
"https://api.example.com/resource",
|
|
86
|
+
{ "Content-Type": "application/json" }
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
expect(response).toEqual({
|
|
90
|
+
success: true,
|
|
91
|
+
status: 200,
|
|
92
|
+
message: "Request successful",
|
|
93
|
+
response: null,
|
|
94
|
+
cause: null,
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should send headers and body correctly for a DELETE request", async () => {
|
|
99
|
+
const mockResponse = { message: "Resource deleted successfully" };
|
|
100
|
+
|
|
101
|
+
(fetch as any).mockResolvedValueOnce({
|
|
102
|
+
ok: true,
|
|
103
|
+
status: 204,
|
|
104
|
+
json: async () => mockResponse,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const response = await makeRequest(
|
|
108
|
+
"DELETE",
|
|
109
|
+
"https://api.example.com/resource",
|
|
110
|
+
{ Authorization: "Bearer token" }
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
expect(response).toEqual({
|
|
114
|
+
success: true,
|
|
115
|
+
status: 204,
|
|
116
|
+
message: "Resource deleted successfully",
|
|
117
|
+
response: mockResponse,
|
|
118
|
+
cause: null,
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { patchRequest } from "../patchRequest";
|
|
3
|
+
|
|
4
|
+
global.fetch = vi.fn() as any;
|
|
5
|
+
|
|
6
|
+
describe("patchRequest", () => {
|
|
7
|
+
it("should return success response for a successful patch request", async () => {
|
|
8
|
+
const mockResponse = { message: "Resource updated successfully" };
|
|
9
|
+
const mockBody = { name: "Updated Name" };
|
|
10
|
+
|
|
11
|
+
(fetch as any).mockResolvedValueOnce({
|
|
12
|
+
ok: true,
|
|
13
|
+
status: 200,
|
|
14
|
+
json: async () => mockResponse,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const response = await patchRequest(
|
|
18
|
+
"https://api.example.com/resource",
|
|
19
|
+
{ "Content-Type": "application/json" },
|
|
20
|
+
mockBody
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
expect(response).toEqual({
|
|
24
|
+
success: true,
|
|
25
|
+
status: 200,
|
|
26
|
+
message: "Resource updated successfully",
|
|
27
|
+
response: mockResponse,
|
|
28
|
+
cause: null,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return failed response for a failed patch request", async () => {
|
|
33
|
+
const mockErrorResponse = { error: "Resource not found" };
|
|
34
|
+
const mockBody = { name: "Updated Name" };
|
|
35
|
+
|
|
36
|
+
(fetch as any).mockResolvedValueOnce({
|
|
37
|
+
ok: false,
|
|
38
|
+
status: 404,
|
|
39
|
+
json: async () => mockErrorResponse,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const response = await patchRequest(
|
|
43
|
+
"https://api.example.com/resource",
|
|
44
|
+
{ "Content-Type": "application/json" },
|
|
45
|
+
mockBody
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(response.success).toBe(false);
|
|
49
|
+
expect(response.status).toBe(404);
|
|
50
|
+
expect(response.response).toEqual(mockErrorResponse);
|
|
51
|
+
expect(response.message).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should handle network errors gracefully", async () => {
|
|
55
|
+
const mockBody = { name: "Updated Name" };
|
|
56
|
+
|
|
57
|
+
(fetch as any).mockRejectedValueOnce(new Error("Network Error"));
|
|
58
|
+
|
|
59
|
+
const response = await patchRequest(
|
|
60
|
+
"https://api.example.com/resource",
|
|
61
|
+
{ "Content-Type": "application/json" },
|
|
62
|
+
mockBody
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(response).toEqual({
|
|
66
|
+
success: false,
|
|
67
|
+
status: 0,
|
|
68
|
+
message: "Network error or request failed",
|
|
69
|
+
response: null,
|
|
70
|
+
cause: "Network Error",
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should handle invalid JSON response gracefully", async () => {
|
|
75
|
+
const mockBody = { name: "Updated Name" };
|
|
76
|
+
|
|
77
|
+
(fetch as any).mockResolvedValueOnce({
|
|
78
|
+
ok: true,
|
|
79
|
+
status: 200,
|
|
80
|
+
json: async () => {
|
|
81
|
+
throw new Error("Invalid JSON");
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const response = await patchRequest(
|
|
86
|
+
"https://api.example.com/resource",
|
|
87
|
+
{ "Content-Type": "application/json" },
|
|
88
|
+
mockBody
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(response).toEqual({
|
|
92
|
+
success: true,
|
|
93
|
+
status: 200,
|
|
94
|
+
message: "Resource patched successfully",
|
|
95
|
+
response: null,
|
|
96
|
+
cause: null,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should send headers and body correctly", async () => {
|
|
101
|
+
const mockResponse = { message: "Resource updated successfully" };
|
|
102
|
+
const mockBody = { name: "Updated Name" };
|
|
103
|
+
const mockHeaders = { Authorization: "Bearer token" };
|
|
104
|
+
|
|
105
|
+
(fetch as any).mockResolvedValueOnce({
|
|
106
|
+
ok: true,
|
|
107
|
+
status: 200,
|
|
108
|
+
json: async () => mockResponse,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const response = await patchRequest(
|
|
112
|
+
"https://api.example.com/resource",
|
|
113
|
+
mockHeaders,
|
|
114
|
+
mockBody
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(response).toEqual({
|
|
118
|
+
success: true,
|
|
119
|
+
status: 200,
|
|
120
|
+
message: "Resource updated successfully",
|
|
121
|
+
response: mockResponse,
|
|
122
|
+
cause: null,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { postRequest } from "../postRequest";
|
|
3
|
+
|
|
4
|
+
global.fetch = vi.fn() as any;
|
|
5
|
+
|
|
6
|
+
describe("postRequest", () => {
|
|
7
|
+
it("should return success response for a successful POST request", async () => {
|
|
8
|
+
const mockResponse = { message: "Resource created successfully" };
|
|
9
|
+
const mockBody = { name: "New Resource" };
|
|
10
|
+
|
|
11
|
+
(fetch as any).mockResolvedValueOnce({
|
|
12
|
+
ok: true,
|
|
13
|
+
status: 201,
|
|
14
|
+
json: async () => mockResponse,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const response = await postRequest(
|
|
18
|
+
"https://api.example.com/resource",
|
|
19
|
+
{ "Content-Type": "application/json" },
|
|
20
|
+
mockBody
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
expect(response).toEqual({
|
|
24
|
+
success: true,
|
|
25
|
+
status: 201,
|
|
26
|
+
message: "Resource created successfully",
|
|
27
|
+
response: mockResponse,
|
|
28
|
+
cause: null,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return failed response for a failed POST request", async () => {
|
|
33
|
+
const mockErrorResponse = { error: "Invalid data" };
|
|
34
|
+
const mockBody = { name: "" };
|
|
35
|
+
|
|
36
|
+
(fetch as any).mockResolvedValueOnce({
|
|
37
|
+
ok: false,
|
|
38
|
+
status: 400,
|
|
39
|
+
json: async () => mockErrorResponse,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const response = await postRequest(
|
|
43
|
+
"https://api.example.com/resource",
|
|
44
|
+
{ "Content-Type": "application/json" },
|
|
45
|
+
mockBody
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(response.success).toBe(false);
|
|
49
|
+
expect(response.status).toBe(400);
|
|
50
|
+
expect(response.response).toEqual(mockErrorResponse);
|
|
51
|
+
expect(response.message).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should handle network errors gracefully", async () => {
|
|
55
|
+
const mockBody = { name: "New Resource" };
|
|
56
|
+
|
|
57
|
+
(fetch as any).mockRejectedValueOnce(new Error("Network Error"));
|
|
58
|
+
|
|
59
|
+
const response = await postRequest(
|
|
60
|
+
"https://api.example.com/resource",
|
|
61
|
+
{ "Content-Type": "application/json" },
|
|
62
|
+
mockBody
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(response).toEqual({
|
|
66
|
+
success: false,
|
|
67
|
+
status: 0,
|
|
68
|
+
message: "Network error or request failed",
|
|
69
|
+
response: null,
|
|
70
|
+
cause: "Network Error",
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should handle invalid JSON response gracefully", async () => {
|
|
75
|
+
const mockBody = { name: "New Resource" };
|
|
76
|
+
|
|
77
|
+
(fetch as any).mockResolvedValueOnce({
|
|
78
|
+
ok: true,
|
|
79
|
+
status: 201,
|
|
80
|
+
json: async () => {
|
|
81
|
+
throw new Error("Invalid JSON");
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const response = await postRequest(
|
|
86
|
+
"https://api.example.com/resource",
|
|
87
|
+
{ "Content-Type": "application/json" },
|
|
88
|
+
mockBody
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(response).toEqual({
|
|
92
|
+
success: true,
|
|
93
|
+
status: 201,
|
|
94
|
+
message: "Resource created successfully",
|
|
95
|
+
response: null,
|
|
96
|
+
cause: null,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should send headers and body correctly", async () => {
|
|
101
|
+
const mockResponse = { message: "Resource created successfully" };
|
|
102
|
+
const mockBody = { name: "New Resource" };
|
|
103
|
+
const mockHeaders = { Authorization: "Bearer token" };
|
|
104
|
+
|
|
105
|
+
(fetch as any).mockResolvedValueOnce({
|
|
106
|
+
ok: true,
|
|
107
|
+
status: 201,
|
|
108
|
+
json: async () => mockResponse,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const response = await postRequest(
|
|
112
|
+
"https://api.example.com/resource",
|
|
113
|
+
mockHeaders,
|
|
114
|
+
mockBody
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(response).toEqual({
|
|
118
|
+
success: true,
|
|
119
|
+
status: 201,
|
|
120
|
+
message: "Resource created successfully",
|
|
121
|
+
response: mockResponse,
|
|
122
|
+
cause: null,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { putRequest } from "../putRequest";
|
|
3
|
+
|
|
4
|
+
global.fetch = vi.fn() as any;
|
|
5
|
+
|
|
6
|
+
describe("putRequest", () => {
|
|
7
|
+
it("should return success response for a successful PUT request", async () => {
|
|
8
|
+
const mockResponse = { message: "Resource updated successfully" };
|
|
9
|
+
const mockBody = { name: "Updated Resource" };
|
|
10
|
+
|
|
11
|
+
(fetch as any).mockResolvedValueOnce({
|
|
12
|
+
ok: true,
|
|
13
|
+
status: 200,
|
|
14
|
+
json: async () => mockResponse,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const response = await putRequest(
|
|
18
|
+
"https://api.example.com/resource",
|
|
19
|
+
{ "Content-Type": "application/json" },
|
|
20
|
+
mockBody
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
expect(response).toEqual({
|
|
24
|
+
success: true,
|
|
25
|
+
status: 200,
|
|
26
|
+
message: "Resource updated successfully",
|
|
27
|
+
response: mockResponse,
|
|
28
|
+
cause: null,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return failed response for a failed PUT request", async () => {
|
|
33
|
+
const mockErrorResponse = { error: "Invalid data" };
|
|
34
|
+
const mockBody = { name: "" };
|
|
35
|
+
|
|
36
|
+
(fetch as any).mockResolvedValueOnce({
|
|
37
|
+
ok: false,
|
|
38
|
+
status: 400,
|
|
39
|
+
json: async () => mockErrorResponse,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const response = await putRequest(
|
|
43
|
+
"https://api.example.com/resource",
|
|
44
|
+
{ "Content-Type": "application/json" },
|
|
45
|
+
mockBody
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(response.success).toBe(false);
|
|
49
|
+
expect(response.status).toBe(400);
|
|
50
|
+
expect(response.response).toEqual(mockErrorResponse);
|
|
51
|
+
expect(response.message).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should handle network errors gracefully", async () => {
|
|
55
|
+
const mockBody = { name: "Updated Resource" };
|
|
56
|
+
|
|
57
|
+
(fetch as any).mockRejectedValueOnce(new Error("Network Error"));
|
|
58
|
+
|
|
59
|
+
const response = await putRequest(
|
|
60
|
+
"https://api.example.com/resource",
|
|
61
|
+
{ "Content-Type": "application/json" },
|
|
62
|
+
mockBody
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(response).toEqual({
|
|
66
|
+
success: false,
|
|
67
|
+
status: 0,
|
|
68
|
+
message: "Network error or request failed",
|
|
69
|
+
response: null,
|
|
70
|
+
cause: "Network Error",
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should handle invalid JSON response gracefully", async () => {
|
|
75
|
+
const mockBody = { name: "Updated Resource" };
|
|
76
|
+
|
|
77
|
+
(fetch as any).mockResolvedValueOnce({
|
|
78
|
+
ok: true,
|
|
79
|
+
status: 200,
|
|
80
|
+
json: async () => {
|
|
81
|
+
throw new Error("Invalid JSON");
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const response = await putRequest(
|
|
86
|
+
"https://api.example.com/resource",
|
|
87
|
+
{ "Content-Type": "application/json" },
|
|
88
|
+
mockBody
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(response).toEqual({
|
|
92
|
+
success: true,
|
|
93
|
+
status: 200,
|
|
94
|
+
message: "Resource updated successfully",
|
|
95
|
+
response: null,
|
|
96
|
+
cause: null,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should send headers and body correctly", async () => {
|
|
101
|
+
const mockResponse = { message: "Resource updated successfully" };
|
|
102
|
+
const mockBody = { name: "Updated Resource" };
|
|
103
|
+
const mockHeaders = { Authorization: "Bearer token" };
|
|
104
|
+
|
|
105
|
+
(fetch as any).mockResolvedValueOnce({
|
|
106
|
+
ok: true,
|
|
107
|
+
status: 200,
|
|
108
|
+
json: async () => mockResponse,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const response = await putRequest(
|
|
112
|
+
"https://api.example.com/resource",
|
|
113
|
+
mockHeaders,
|
|
114
|
+
mockBody
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(response).toEqual({
|
|
118
|
+
success: true,
|
|
119
|
+
status: 200,
|
|
120
|
+
message: "Resource updated successfully",
|
|
121
|
+
response: mockResponse,
|
|
122
|
+
cause: null,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a DELETE 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 DELETE request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} [body] - Optional body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function deleteRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body?: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("DELETE", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { deleteRequest };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a GET request to the specified URL with optional headers.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the GET request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
async function getRequest<T = any>(
|
|
14
|
+
url: string,
|
|
15
|
+
headers: HeadersInit = {}
|
|
16
|
+
): Promise<ApiResponseDTO<T>> {
|
|
17
|
+
return makeRequest("GET", url, headers);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { getRequest };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
3
|
+
|
|
4
|
+
type ConfigProps = {
|
|
5
|
+
status: number;
|
|
6
|
+
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "ERROR";
|
|
7
|
+
request: string;
|
|
8
|
+
response: string;
|
|
9
|
+
token: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sends a request to the inbox flow API with the provided configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param config - The configuration object for the request.
|
|
16
|
+
* @param config.status - The HTTP status code associated with the request.
|
|
17
|
+
* @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", "PATCH", or "ERROR".
|
|
18
|
+
* @param config.request - The request payload or details as a string.
|
|
19
|
+
* @param config.response - The response payload or details as a string.
|
|
20
|
+
* @param config.token - The token associated with the request for authentication or identification.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
|
|
24
|
+
* - If the configuration is not available, the function will return early without performing any action.
|
|
25
|
+
* - In a development environment (`NODE_ENV === "development"`), the function will also return early.
|
|
26
|
+
* - The request is sent as a POST request to the inbox API URL with the provided configuration details.
|
|
27
|
+
* - If an error occurs during the request, it will be logged using the `httpDebug` service.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const config = {
|
|
32
|
+
* status: 200,
|
|
33
|
+
* method: "POST",
|
|
34
|
+
* request: JSON.stringify({ key: "value" }),
|
|
35
|
+
* response: JSON.stringify({ success: true }),
|
|
36
|
+
* token: "example-token",
|
|
37
|
+
* };
|
|
38
|
+
*
|
|
39
|
+
* await inboxFlowRequest(config);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
async function inboxFlowRequest(config: ConfigProps) {
|
|
44
|
+
const inboxFlowInstance = InboxFlowInstance.getInboxConfig();
|
|
45
|
+
if (!inboxFlowInstance) return;
|
|
46
|
+
|
|
47
|
+
const { inboxChannelId, inboxUserToken, inboxApiUrl } = inboxFlowInstance;
|
|
48
|
+
|
|
49
|
+
const { status, method, request, response, token } = config;
|
|
50
|
+
|
|
51
|
+
if (process.env.NODE_ENV === "development") return;
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const body = JSON.stringify({
|
|
55
|
+
status,
|
|
56
|
+
channelId: inboxChannelId,
|
|
57
|
+
method,
|
|
58
|
+
token,
|
|
59
|
+
request,
|
|
60
|
+
response,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await fetch(inboxApiUrl, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
body,
|
|
66
|
+
headers: {
|
|
67
|
+
"Content-Type": "application/json",
|
|
68
|
+
Authorization: `Bearer ${inboxUserToken}`,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
} catch (err) {
|
|
72
|
+
httpDebug("inboxFlowRequest", "Error sending inbox flow request", err);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { inboxFlowRequest };
|