@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,86 @@
|
|
|
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
|
+
vi.mock("../../api/getRequest");
|
|
9
|
+
vi.mock("../../api/postRequest");
|
|
10
|
+
vi.mock("../../api/putRequest");
|
|
11
|
+
vi.mock("../../api/patchRequest");
|
|
12
|
+
vi.mock("../../api/deleteRequest");
|
|
13
|
+
describe("ApiInstance", () => {
|
|
14
|
+
const baseUrl = "https://api.example.com";
|
|
15
|
+
const baseHeaders = { "Content-Type": "application/json" };
|
|
16
|
+
const baseToken = "base-token";
|
|
17
|
+
const apiInstance = new ApiInstance({
|
|
18
|
+
baseUrl,
|
|
19
|
+
baseHeaders,
|
|
20
|
+
baseToken,
|
|
21
|
+
});
|
|
22
|
+
it("should send a GET request with correct headers and URL", async () => {
|
|
23
|
+
const mockResponse = { success: true, data: "GET response" };
|
|
24
|
+
getRequest.mockResolvedValueOnce(mockResponse);
|
|
25
|
+
const response = await apiInstance.GET("/resource", {});
|
|
26
|
+
expect(getRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
27
|
+
Authorization: `Bearer ${baseToken}`,
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
}));
|
|
30
|
+
expect(response).toEqual(mockResponse);
|
|
31
|
+
});
|
|
32
|
+
it("should send a POST request with correct headers, URL, and body", async () => {
|
|
33
|
+
const mockResponse = { success: true, data: "POST response" };
|
|
34
|
+
const mockBody = { name: "New Resource" };
|
|
35
|
+
postRequest.mockResolvedValueOnce(mockResponse);
|
|
36
|
+
const response = await apiInstance.POST("/resource", { body: mockBody });
|
|
37
|
+
expect(postRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
38
|
+
Authorization: `Bearer ${baseToken}`,
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
}), mockBody);
|
|
41
|
+
expect(response).toEqual(mockResponse);
|
|
42
|
+
});
|
|
43
|
+
it("should send a PUT request with correct headers, URL, and body", async () => {
|
|
44
|
+
const mockResponse = { success: true, data: "PUT response" };
|
|
45
|
+
const mockBody = { name: "Updated Resource" };
|
|
46
|
+
putRequest.mockResolvedValueOnce(mockResponse);
|
|
47
|
+
const response = await apiInstance.PUT("/resource", { body: mockBody });
|
|
48
|
+
expect(putRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
49
|
+
Authorization: `Bearer ${baseToken}`,
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
}), mockBody);
|
|
52
|
+
expect(response).toEqual(mockResponse);
|
|
53
|
+
});
|
|
54
|
+
it("should send a PATCH request with correct headers, URL, and body", async () => {
|
|
55
|
+
const mockResponse = { success: true, data: "PATCH response" };
|
|
56
|
+
const mockBody = { name: "Partially Updated Resource" };
|
|
57
|
+
patchRequest.mockResolvedValueOnce(mockResponse);
|
|
58
|
+
const response = await apiInstance.PATCH("/resource", { body: mockBody });
|
|
59
|
+
expect(patchRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
60
|
+
Authorization: `Bearer ${baseToken}`,
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
}), mockBody);
|
|
63
|
+
expect(response).toEqual(mockResponse);
|
|
64
|
+
});
|
|
65
|
+
it("should send a DELETE request with correct headers, URL, and body", async () => {
|
|
66
|
+
const mockResponse = { success: true, data: "DELETE response" };
|
|
67
|
+
const mockBody = { id: 1 };
|
|
68
|
+
deleteRequest.mockResolvedValueOnce(mockResponse);
|
|
69
|
+
const response = await apiInstance.DELETE("/resource", { body: mockBody });
|
|
70
|
+
expect(deleteRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
71
|
+
Authorization: `Bearer ${baseToken}`,
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
}), mockBody);
|
|
74
|
+
expect(response).toEqual(mockResponse);
|
|
75
|
+
});
|
|
76
|
+
it("should override base token with provided token", async () => {
|
|
77
|
+
const mockResponse = { success: true, data: "GET response with token" };
|
|
78
|
+
getRequest.mockResolvedValueOnce(mockResponse);
|
|
79
|
+
const response = await apiInstance.GET("/resource", { token: "new-token" });
|
|
80
|
+
expect(getRequest).toHaveBeenCalledWith(`${baseUrl}/resource`, expect.objectContaining({
|
|
81
|
+
Authorization: `Bearer new-token`,
|
|
82
|
+
"Content-Type": "application/json",
|
|
83
|
+
}));
|
|
84
|
+
expect(response).toEqual(mockResponse);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inboxFlowInstance.spec.d.ts","sourceRoot":"","sources":["../../../src/config/__test__/inboxFlowInstance.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { InboxFlowInstance } from "../inboxFlowInstance";
|
|
3
|
+
describe("InboxFlowInstance", () => {
|
|
4
|
+
it("should set the inbox configuration if not already set", () => {
|
|
5
|
+
const inboxConfig = {
|
|
6
|
+
inboxChannelId: "channel-123",
|
|
7
|
+
inboxUserToken: "user-token-abc",
|
|
8
|
+
inboxApiUrl: "https://custom-inbox-api.com",
|
|
9
|
+
};
|
|
10
|
+
InboxFlowInstance.setInboxConfig(inboxConfig);
|
|
11
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
12
|
+
expect(config).toEqual(inboxConfig);
|
|
13
|
+
});
|
|
14
|
+
it("should not overwrite the inbox configuration if already set", () => {
|
|
15
|
+
const initialConfig = {
|
|
16
|
+
inboxChannelId: "channel-123",
|
|
17
|
+
inboxUserToken: "user-token-abc",
|
|
18
|
+
inboxApiUrl: "https://custom-inbox-api.com",
|
|
19
|
+
};
|
|
20
|
+
const newConfig = {
|
|
21
|
+
inboxChannelId: "channel-456",
|
|
22
|
+
inboxUserToken: "user-token-def",
|
|
23
|
+
inboxApiUrl: "https://another-inbox-api.com",
|
|
24
|
+
};
|
|
25
|
+
InboxFlowInstance.setInboxConfig(initialConfig);
|
|
26
|
+
InboxFlowInstance.setInboxConfig(newConfig);
|
|
27
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
28
|
+
expect(config).toEqual(initialConfig);
|
|
29
|
+
});
|
|
30
|
+
it("should return undefined if no configuration is set", () => {
|
|
31
|
+
InboxFlowInstance.resetInboxConfig();
|
|
32
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
33
|
+
expect(config).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
it("should use the default API URL if none is provided", () => {
|
|
36
|
+
const inboxConfig = {
|
|
37
|
+
inboxChannelId: "channel-123",
|
|
38
|
+
inboxUserToken: "user-token-abc",
|
|
39
|
+
};
|
|
40
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
41
|
+
InboxFlowInstance.setInboxConfig(inboxConfig);
|
|
42
|
+
const config = InboxFlowInstance.getInboxConfig();
|
|
43
|
+
expect(config).toEqual({
|
|
44
|
+
...inboxConfig,
|
|
45
|
+
inboxApiUrl: defaultInboxURL,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
type ApiAinstanceContructorProps = {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
baseHeaders?: HeadersInit;
|
|
4
|
+
baseToken?: string | null;
|
|
5
|
+
};
|
|
6
|
+
type ApiRequestDataWithoutBodyProps = {
|
|
7
|
+
headers?: HeadersInit;
|
|
8
|
+
token?: string;
|
|
9
|
+
};
|
|
10
|
+
type ApiRequestDataWithBodyProps = {
|
|
11
|
+
body?: any;
|
|
12
|
+
headers?: HeadersInit;
|
|
13
|
+
token?: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Class representing an API instance to handle HTTP requests with base configurations.
|
|
17
|
+
*/
|
|
18
|
+
declare class ApiInstance {
|
|
19
|
+
private baseUrl;
|
|
20
|
+
private baseHeaders?;
|
|
21
|
+
private baseToken?;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of ApiInstance.
|
|
24
|
+
* @param props - The configuration properties for the API instance.
|
|
25
|
+
* @param props.baseUrl - The base URL for the API.
|
|
26
|
+
* @param props.baseHeaders - Optional base headers to include in all requests.
|
|
27
|
+
* @param props.baseToken - Optional base token for authorization.
|
|
28
|
+
*/
|
|
29
|
+
constructor(props: ApiAinstanceContructorProps);
|
|
30
|
+
/**
|
|
31
|
+
* Generates the full URL by appending the route to the base URL.
|
|
32
|
+
* @param route - The route to append to the base URL.
|
|
33
|
+
* @returns The full URL as a string.
|
|
34
|
+
*/
|
|
35
|
+
private generateURL;
|
|
36
|
+
/**
|
|
37
|
+
* Generates the headers for a request by merging base headers, provided headers, and tokens.
|
|
38
|
+
* @param initHeaders - Initial headers to include in the request.
|
|
39
|
+
* @param token - Optional token to override the base token.
|
|
40
|
+
* @returns The merged headers as a HeadersInit object.
|
|
41
|
+
*/
|
|
42
|
+
private generateHeaders;
|
|
43
|
+
/**
|
|
44
|
+
* Sends a GET request to the specified route.
|
|
45
|
+
* @param route - The API route to send the GET request to.
|
|
46
|
+
* @param data - The request data, including optional headers and token.
|
|
47
|
+
* @returns The API response data.
|
|
48
|
+
*/
|
|
49
|
+
GET(route: string, data?: ApiRequestDataWithoutBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
|
|
50
|
+
/**
|
|
51
|
+
* Sends a POST request to the specified route.
|
|
52
|
+
* @param route - The API route to send the POST request to.
|
|
53
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
54
|
+
* @returns The API response data.
|
|
55
|
+
*/
|
|
56
|
+
POST(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
|
|
57
|
+
/**
|
|
58
|
+
* Sends a PUT request to the specified route.
|
|
59
|
+
* @param route - The API route to send the PUT request to.
|
|
60
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
61
|
+
* @returns The API response data.
|
|
62
|
+
*/
|
|
63
|
+
PUT(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
|
|
64
|
+
/**
|
|
65
|
+
* Sends a PATCH request to the specified route.
|
|
66
|
+
* @param route - The API route to send the PATCH request to.
|
|
67
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
68
|
+
* @returns The API response data.
|
|
69
|
+
*/
|
|
70
|
+
PATCH(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
|
|
71
|
+
/**
|
|
72
|
+
* Sends a DELETE request to the specified route.
|
|
73
|
+
* @param route - The API route to send the DELETE request to.
|
|
74
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
75
|
+
* @returns The API response data.
|
|
76
|
+
*/
|
|
77
|
+
DELETE(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
|
|
78
|
+
}
|
|
79
|
+
export { ApiInstance };
|
|
80
|
+
//# sourceMappingURL=apiInstance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apiInstance.d.ts","sourceRoot":"","sources":["../../src/config/apiInstance.ts"],"names":[],"mappings":"AAMA,KAAK,2BAA2B,GAAG;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AAEH,cAAM,WAAW;IACf,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAC,CAAc;IAClC,OAAO,CAAC,SAAS,CAAC,CAAS;IAE3B;;;;;;OAMG;gBAES,KAAK,EAAE,2BAA2B;IAM9C;;;;OAIG;IAEH,OAAO,CAAC,WAAW;IAInB;;;;;OAKG;IAEH,OAAO,CAAC,eAAe;IAcvB;;;;;OAKG;IAEG,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,8BAA8B;IAM9D;;;;;OAKG;IAEG,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO5D;;;;;OAKG;IAEG,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO3D;;;;;OAKG;IAEG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO7D;;;;;OAKG;IAEG,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;CAM/D;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
* Class representing an API instance to handle HTTP requests with base configurations.
|
|
8
|
+
*/
|
|
9
|
+
class ApiInstance {
|
|
10
|
+
baseUrl;
|
|
11
|
+
baseHeaders;
|
|
12
|
+
baseToken;
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of ApiInstance.
|
|
15
|
+
* @param props - The configuration properties for the API instance.
|
|
16
|
+
* @param props.baseUrl - The base URL for the API.
|
|
17
|
+
* @param props.baseHeaders - Optional base headers to include in all requests.
|
|
18
|
+
* @param props.baseToken - Optional base token for authorization.
|
|
19
|
+
*/
|
|
20
|
+
constructor(props) {
|
|
21
|
+
this.baseUrl = props.baseUrl;
|
|
22
|
+
this.baseHeaders = props.baseHeaders || undefined;
|
|
23
|
+
this.baseToken = props.baseToken || undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generates the full URL by appending the route to the base URL.
|
|
27
|
+
* @param route - The route to append to the base URL.
|
|
28
|
+
* @returns The full URL as a string.
|
|
29
|
+
*/
|
|
30
|
+
generateURL(route) {
|
|
31
|
+
return this.baseUrl + route;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generates the headers for a request by merging base headers, provided headers, and tokens.
|
|
35
|
+
* @param initHeaders - Initial headers to include in the request.
|
|
36
|
+
* @param token - Optional token to override the base token.
|
|
37
|
+
* @returns The merged headers as a HeadersInit object.
|
|
38
|
+
*/
|
|
39
|
+
generateHeaders(initHeaders, token) {
|
|
40
|
+
let headers = {};
|
|
41
|
+
if (this.baseToken)
|
|
42
|
+
headers = { Authorization: `Bearer ${this.baseToken}` };
|
|
43
|
+
if (this.baseHeaders)
|
|
44
|
+
headers = { ...headers, ...this.baseHeaders };
|
|
45
|
+
if (initHeaders)
|
|
46
|
+
headers = { ...headers, ...initHeaders };
|
|
47
|
+
if (token)
|
|
48
|
+
headers = { ...headers, Authorization: `Bearer ${token}` };
|
|
49
|
+
return headers;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sends a GET request to the specified route.
|
|
53
|
+
* @param route - The API route to send the GET request to.
|
|
54
|
+
* @param data - The request data, including optional headers and token.
|
|
55
|
+
* @returns The API response data.
|
|
56
|
+
*/
|
|
57
|
+
async GET(route, data) {
|
|
58
|
+
const url = this.generateURL(route);
|
|
59
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
60
|
+
return await getRequest(url, headers);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Sends a POST request to the specified route.
|
|
64
|
+
* @param route - The API route to send the POST request to.
|
|
65
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
66
|
+
* @returns The API response data.
|
|
67
|
+
*/
|
|
68
|
+
async POST(route, data) {
|
|
69
|
+
const url = this.generateURL(route);
|
|
70
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
71
|
+
const body = data?.body;
|
|
72
|
+
return await postRequest(url, headers, body);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Sends a PUT request to the specified route.
|
|
76
|
+
* @param route - The API route to send the PUT request to.
|
|
77
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
78
|
+
* @returns The API response data.
|
|
79
|
+
*/
|
|
80
|
+
async PUT(route, data) {
|
|
81
|
+
const url = this.generateURL(route);
|
|
82
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
83
|
+
const body = data?.body;
|
|
84
|
+
return await putRequest(url, headers, body);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Sends a PATCH request to the specified route.
|
|
88
|
+
* @param route - The API route to send the PATCH request to.
|
|
89
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
90
|
+
* @returns The API response data.
|
|
91
|
+
*/
|
|
92
|
+
async PATCH(route, data) {
|
|
93
|
+
const url = this.generateURL(route);
|
|
94
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
95
|
+
const body = data?.body;
|
|
96
|
+
return await patchRequest(url, headers, body);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Sends a DELETE request to the specified route.
|
|
100
|
+
* @param route - The API route to send the DELETE request to.
|
|
101
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
102
|
+
* @returns The API response data.
|
|
103
|
+
*/
|
|
104
|
+
async DELETE(route, data) {
|
|
105
|
+
const url = this.generateURL(route);
|
|
106
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
107
|
+
const body = data?.body;
|
|
108
|
+
return await deleteRequest(url, headers, body);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export { ApiInstance };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type InboxConfigProps = {
|
|
2
|
+
inboxChannelId: string;
|
|
3
|
+
inboxUserToken: string;
|
|
4
|
+
inboxApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
type SetInboxConfigProps = {
|
|
7
|
+
inboxChannelId: string;
|
|
8
|
+
inboxUserToken: string;
|
|
9
|
+
inboxApiUrl?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
13
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
14
|
+
* user token, and API URL.
|
|
15
|
+
*/
|
|
16
|
+
declare class InboxFlowInstance {
|
|
17
|
+
private static inboxConfig?;
|
|
18
|
+
/**
|
|
19
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
20
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
21
|
+
* the method will return early without making any changes.
|
|
22
|
+
*
|
|
23
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
24
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
25
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
26
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
27
|
+
* a default URL will be used.
|
|
28
|
+
*/
|
|
29
|
+
static setInboxConfig(inboxConfig: SetInboxConfigProps): void;
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
32
|
+
*
|
|
33
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
34
|
+
* or `undefined` if no configuration has been initialized.
|
|
35
|
+
*/
|
|
36
|
+
static getInboxConfig(): InboxConfigProps | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Resets the inbox configuration to `undefined`.
|
|
39
|
+
* This method can be used to clear the current configuration.
|
|
40
|
+
*/
|
|
41
|
+
static resetInboxConfig(): void;
|
|
42
|
+
}
|
|
43
|
+
export { InboxFlowInstance };
|
|
44
|
+
//# sourceMappingURL=inboxFlowInstance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inboxFlowInstance.d.ts","sourceRoot":"","sources":["../../src/config/inboxFlowInstance.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AAEH,cAAM,iBAAiB;IACrB,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAmB;IAE9C;;;;;;;;;;OAUG;IAEH,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,mBAAmB;IAWtD;;;;;OAKG;IACH,MAAM,CAAC,cAAc,IAAI,gBAAgB,GAAG,SAAS;IAIrD;;;OAGG;IAEH,MAAM,CAAC,gBAAgB;CAGxB;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
3
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
4
|
+
* user token, and API URL.
|
|
5
|
+
*/
|
|
6
|
+
class InboxFlowInstance {
|
|
7
|
+
static inboxConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
10
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
11
|
+
* the method will return early without making any changes.
|
|
12
|
+
*
|
|
13
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
14
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
15
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
16
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
17
|
+
* a default URL will be used.
|
|
18
|
+
*/
|
|
19
|
+
static setInboxConfig(inboxConfig) {
|
|
20
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
21
|
+
if (!!this.inboxConfig)
|
|
22
|
+
return;
|
|
23
|
+
this.inboxConfig = {
|
|
24
|
+
inboxChannelId: inboxConfig.inboxChannelId,
|
|
25
|
+
inboxUserToken: inboxConfig.inboxUserToken,
|
|
26
|
+
inboxApiUrl: inboxConfig.inboxApiUrl || defaultInboxURL,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
31
|
+
*
|
|
32
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
33
|
+
* or `undefined` if no configuration has been initialized.
|
|
34
|
+
*/
|
|
35
|
+
static getInboxConfig() {
|
|
36
|
+
return this.inboxConfig;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resets the inbox configuration to `undefined`.
|
|
40
|
+
* This method can be used to clear the current configuration.
|
|
41
|
+
*/
|
|
42
|
+
static resetInboxConfig() {
|
|
43
|
+
this.inboxConfig = undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export { InboxFlowInstance };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"badGateway.spec.d.ts","sourceRoot":"","sources":["../../../../src/http/badResponses/__test__/badGateway.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { BadGateway } from "../badGateway";
|
|
3
|
+
describe("BadGateway", () => {
|
|
4
|
+
it("should create an instance with the correct properties", () => {
|
|
5
|
+
const message = "Bad Gateway error occurred";
|
|
6
|
+
const cause = { reason: "Upstream server is down" };
|
|
7
|
+
const error = new BadGateway(message, cause);
|
|
8
|
+
expect(error.body).toEqual({ name: "BadGateway", message });
|
|
9
|
+
expect(error.status).toBe(502);
|
|
10
|
+
expect(error.statusText).toBe(message);
|
|
11
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
12
|
+
});
|
|
13
|
+
it("should create an instance without a cause", () => {
|
|
14
|
+
const message = "Bad Gateway error occurred";
|
|
15
|
+
const error = new BadGateway(message);
|
|
16
|
+
expect(error.body).toEqual({ name: "BadGateway", message });
|
|
17
|
+
expect(error.status).toBe(502);
|
|
18
|
+
expect(error.statusText).toBe(message);
|
|
19
|
+
expect(error.cause).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it("should return a Response object from toResponse", () => {
|
|
22
|
+
const message = "Bad Gateway error occurred";
|
|
23
|
+
const error = new BadGateway(message);
|
|
24
|
+
const response = error.toResponse();
|
|
25
|
+
expect(response.status).toBe(502);
|
|
26
|
+
expect(response.statusText).toBe(message);
|
|
27
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
28
|
+
response.json().then((body) => {
|
|
29
|
+
expect(body).toEqual({ name: "BadGateway", message });
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
it("should return a Response object from toJson", () => {
|
|
33
|
+
const message = "Bad Gateway error occurred";
|
|
34
|
+
const error = new BadGateway(message);
|
|
35
|
+
const response = error.toJson();
|
|
36
|
+
expect(response.status).toBe(502);
|
|
37
|
+
expect(response.statusText).toBe(message);
|
|
38
|
+
response.json().then((body) => {
|
|
39
|
+
expect(body).toEqual({ name: "BadGateway", message });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"badRequest.spec.d.ts","sourceRoot":"","sources":["../../../../src/http/badResponses/__test__/badRequest.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { BadRequest } from "../badRequest";
|
|
3
|
+
describe("BadRequest", () => {
|
|
4
|
+
it("should create an instance with the correct properties", () => {
|
|
5
|
+
const message = "Invalid request data";
|
|
6
|
+
const cause = { field: "email", error: "Invalid format" };
|
|
7
|
+
const error = new BadRequest(message, cause);
|
|
8
|
+
expect(error.body).toEqual({ name: "BadRequest", message });
|
|
9
|
+
expect(error.status).toBe(400);
|
|
10
|
+
expect(error.statusText).toBe(message);
|
|
11
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
12
|
+
});
|
|
13
|
+
it("should create an instance without a cause", () => {
|
|
14
|
+
const message = "Invalid request data";
|
|
15
|
+
const error = new BadRequest(message);
|
|
16
|
+
expect(error.body).toEqual({ name: "BadRequest", message });
|
|
17
|
+
expect(error.status).toBe(400);
|
|
18
|
+
expect(error.statusText).toBe(message);
|
|
19
|
+
expect(error.cause).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it("should return a Response object from toResponse", async () => {
|
|
22
|
+
const message = "Invalid request data";
|
|
23
|
+
const error = new BadRequest(message);
|
|
24
|
+
const response = error.toResponse();
|
|
25
|
+
expect(response.status).toBe(400);
|
|
26
|
+
expect(response.statusText).toBe(message);
|
|
27
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
28
|
+
const body = await response.json();
|
|
29
|
+
expect(body).toEqual({ name: "BadRequest", message });
|
|
30
|
+
});
|
|
31
|
+
it("should return a Response object from toJson", async () => {
|
|
32
|
+
const message = "Invalid request data";
|
|
33
|
+
const error = new BadRequest(message);
|
|
34
|
+
const response = error.toJson();
|
|
35
|
+
expect(response.status).toBe(400);
|
|
36
|
+
expect(response.statusText).toBe(message);
|
|
37
|
+
const body = await response.json();
|
|
38
|
+
expect(body).toEqual({ name: "BadRequest", message });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conflict.spec.d.ts","sourceRoot":"","sources":["../../../../src/http/badResponses/__test__/conflict.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Conflict } from "../conflict";
|
|
3
|
+
describe("Conflict", () => {
|
|
4
|
+
it("should create an instance with the correct properties", () => {
|
|
5
|
+
const message = "Conflict occurred";
|
|
6
|
+
const cause = { reason: "Duplicate entry" };
|
|
7
|
+
const error = new Conflict(message, cause);
|
|
8
|
+
expect(error.body).toEqual({ name: "Conflict", message });
|
|
9
|
+
expect(error.status).toBe(409);
|
|
10
|
+
expect(error.statusText).toBe(message);
|
|
11
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
12
|
+
});
|
|
13
|
+
it("should create an instance without a cause", () => {
|
|
14
|
+
const message = "Conflict occurred";
|
|
15
|
+
const error = new Conflict(message);
|
|
16
|
+
expect(error.body).toEqual({ name: "Conflict", message });
|
|
17
|
+
expect(error.status).toBe(409);
|
|
18
|
+
expect(error.statusText).toBe(message);
|
|
19
|
+
expect(error.cause).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it("should return a Response object from toResponse", async () => {
|
|
22
|
+
const message = "Conflict occurred";
|
|
23
|
+
const error = new Conflict(message);
|
|
24
|
+
const response = error.toResponse();
|
|
25
|
+
expect(response.status).toBe(409);
|
|
26
|
+
expect(response.statusText).toBe(message);
|
|
27
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
28
|
+
const body = await response.json();
|
|
29
|
+
expect(body).toEqual({ name: "Conflict", message });
|
|
30
|
+
});
|
|
31
|
+
it("should return a Response object from toJson", async () => {
|
|
32
|
+
const message = "Conflict occurred";
|
|
33
|
+
const error = new Conflict(message);
|
|
34
|
+
const response = error.toJson();
|
|
35
|
+
expect(response.status).toBe(409);
|
|
36
|
+
expect(response.statusText).toBe(message);
|
|
37
|
+
const body = await response.json();
|
|
38
|
+
expect(body).toEqual({ name: "Conflict", message });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forbidden.spec.d.ts","sourceRoot":"","sources":["../../../../src/http/badResponses/__test__/forbidden.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Forbidden } from "../forbidden";
|
|
3
|
+
describe("Forbidden", () => {
|
|
4
|
+
it("should create an instance with the correct properties", () => {
|
|
5
|
+
const message = "Access is forbidden";
|
|
6
|
+
const cause = { reason: "Insufficient permissions" };
|
|
7
|
+
const error = new Forbidden(message, cause);
|
|
8
|
+
expect(error.body).toEqual({ name: "Forbidden", message });
|
|
9
|
+
expect(error.status).toBe(403);
|
|
10
|
+
expect(error.statusText).toBe(message);
|
|
11
|
+
expect(error.cause).toBe(JSON.stringify(cause));
|
|
12
|
+
});
|
|
13
|
+
it("should create an instance without a cause", () => {
|
|
14
|
+
const message = "Access is forbidden";
|
|
15
|
+
const error = new Forbidden(message);
|
|
16
|
+
expect(error.body).toEqual({ name: "Forbidden", message });
|
|
17
|
+
expect(error.status).toBe(403);
|
|
18
|
+
expect(error.statusText).toBe(message);
|
|
19
|
+
expect(error.cause).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it("should return a Response object from toResponse", async () => {
|
|
22
|
+
const message = "Access is forbidden";
|
|
23
|
+
const error = new Forbidden(message);
|
|
24
|
+
const response = error.toResponse();
|
|
25
|
+
expect(response.status).toBe(403);
|
|
26
|
+
expect(response.statusText).toBe(message);
|
|
27
|
+
expect(response.headers.get("Content-Type")).toBe("application/json");
|
|
28
|
+
const body = await response.json();
|
|
29
|
+
expect(body).toEqual({ name: "Forbidden", message });
|
|
30
|
+
});
|
|
31
|
+
it("should return a Response object from toJson", async () => {
|
|
32
|
+
const message = "Access is forbidden";
|
|
33
|
+
const error = new Forbidden(message);
|
|
34
|
+
const response = error.toJson();
|
|
35
|
+
expect(response.status).toBe(403);
|
|
36
|
+
expect(response.statusText).toBe(message);
|
|
37
|
+
const body = await response.json();
|
|
38
|
+
expect(body).toEqual({ name: "Forbidden", message });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notFound.spec.d.ts","sourceRoot":"","sources":["../../../../src/http/badResponses/__test__/notFound.spec.ts"],"names":[],"mappings":""}
|