@arkyn/server 2.0.1 → 2.0.2

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.
Files changed (117) hide show
  1. package/package.json +1 -1
  2. package/dist/api/__test__/deleteRequest.spec.d.ts +0 -2
  3. package/dist/api/__test__/deleteRequest.spec.d.ts.map +0 -1
  4. package/dist/api/__test__/deleteRequest.spec.js +0 -84
  5. package/dist/api/__test__/getRequest.spec.d.ts +0 -2
  6. package/dist/api/__test__/getRequest.spec.d.ts.map +0 -1
  7. package/dist/api/__test__/getRequest.spec.js +0 -62
  8. package/dist/api/__test__/inboxFlowRequest.spec.d.ts +0 -2
  9. package/dist/api/__test__/inboxFlowRequest.spec.d.ts.map +0 -1
  10. package/dist/api/__test__/inboxFlowRequest.spec.js +0 -96
  11. package/dist/api/__test__/makeRequest.spec.d.ts +0 -2
  12. package/dist/api/__test__/makeRequest.spec.d.ts.map +0 -1
  13. package/dist/api/__test__/makeRequest.spec.js +0 -80
  14. package/dist/api/__test__/patchRequest.spec.d.ts +0 -2
  15. package/dist/api/__test__/patchRequest.spec.d.ts.map +0 -1
  16. package/dist/api/__test__/patchRequest.spec.js +0 -84
  17. package/dist/api/__test__/postRequest.spec.d.ts +0 -2
  18. package/dist/api/__test__/postRequest.spec.d.ts.map +0 -1
  19. package/dist/api/__test__/postRequest.spec.js +0 -84
  20. package/dist/api/__test__/putRequest.spec.d.ts +0 -2
  21. package/dist/api/__test__/putRequest.spec.d.ts.map +0 -1
  22. package/dist/api/__test__/putRequest.spec.js +0 -84
  23. package/dist/config/__test__/apiInstance.spec.d.ts +0 -2
  24. package/dist/config/__test__/apiInstance.spec.d.ts.map +0 -1
  25. package/dist/config/__test__/apiInstance.spec.js +0 -86
  26. package/dist/config/__test__/inboxFlowInstance.spec.d.ts +0 -2
  27. package/dist/config/__test__/inboxFlowInstance.spec.d.ts.map +0 -1
  28. package/dist/config/__test__/inboxFlowInstance.spec.js +0 -48
  29. package/dist/http/badResponses/__test__/badGateway.spec.d.ts +0 -2
  30. package/dist/http/badResponses/__test__/badGateway.spec.d.ts.map +0 -1
  31. package/dist/http/badResponses/__test__/badGateway.spec.js +0 -42
  32. package/dist/http/badResponses/__test__/badRequest.spec.d.ts +0 -2
  33. package/dist/http/badResponses/__test__/badRequest.spec.d.ts.map +0 -1
  34. package/dist/http/badResponses/__test__/badRequest.spec.js +0 -40
  35. package/dist/http/badResponses/__test__/conflict.spec.d.ts +0 -2
  36. package/dist/http/badResponses/__test__/conflict.spec.d.ts.map +0 -1
  37. package/dist/http/badResponses/__test__/conflict.spec.js +0 -40
  38. package/dist/http/badResponses/__test__/forbidden.spec.d.ts +0 -2
  39. package/dist/http/badResponses/__test__/forbidden.spec.d.ts.map +0 -1
  40. package/dist/http/badResponses/__test__/forbidden.spec.js +0 -40
  41. package/dist/http/badResponses/__test__/notFound.spec.d.ts +0 -2
  42. package/dist/http/badResponses/__test__/notFound.spec.d.ts.map +0 -1
  43. package/dist/http/badResponses/__test__/notFound.spec.js +0 -40
  44. package/dist/http/badResponses/__test__/notImplemented.spec.d.ts +0 -2
  45. package/dist/http/badResponses/__test__/notImplemented.spec.d.ts.map +0 -1
  46. package/dist/http/badResponses/__test__/notImplemented.spec.js +0 -40
  47. package/dist/http/badResponses/__test__/serverError.spec.d.ts +0 -2
  48. package/dist/http/badResponses/__test__/serverError.spec.d.ts.map +0 -1
  49. package/dist/http/badResponses/__test__/serverError.spec.js +0 -40
  50. package/dist/http/badResponses/__test__/unauthorized.spec.d.ts +0 -2
  51. package/dist/http/badResponses/__test__/unauthorized.spec.d.ts.map +0 -1
  52. package/dist/http/badResponses/__test__/unauthorized.spec.js +0 -40
  53. package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts +0 -2
  54. package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts.map +0 -1
  55. package/dist/http/badResponses/__test__/unprocessableEntity.spec.js +0 -66
  56. package/dist/http/successResponses/__test__/created.spec.d.ts +0 -2
  57. package/dist/http/successResponses/__test__/created.spec.d.ts.map +0 -1
  58. package/dist/http/successResponses/__test__/created.spec.js +0 -57
  59. package/dist/http/successResponses/__test__/found.spec.d.ts +0 -2
  60. package/dist/http/successResponses/__test__/found.spec.d.ts.map +0 -1
  61. package/dist/http/successResponses/__test__/found.spec.js +0 -57
  62. package/dist/http/successResponses/__test__/noContent.spec.d.ts +0 -2
  63. package/dist/http/successResponses/__test__/noContent.spec.d.ts.map +0 -1
  64. package/dist/http/successResponses/__test__/noContent.spec.js +0 -40
  65. package/dist/http/successResponses/__test__/success.spec.d.ts +0 -2
  66. package/dist/http/successResponses/__test__/success.spec.d.ts.map +0 -1
  67. package/dist/http/successResponses/__test__/success.spec.js +0 -60
  68. package/dist/http/successResponses/__test__/updated.spec.d.ts +0 -2
  69. package/dist/http/successResponses/__test__/updated.spec.d.ts.map +0 -1
  70. package/dist/http/successResponses/__test__/updated.spec.js +0 -60
  71. package/dist/services/__test__/decodeRequestBody.spec.d.ts +0 -2
  72. package/dist/services/__test__/decodeRequestBody.spec.d.ts.map +0 -1
  73. package/dist/services/__test__/decodeRequestBody.spec.js +0 -39
  74. package/dist/services/__test__/errorHandler.spec.d.ts +0 -2
  75. package/dist/services/__test__/errorHandler.spec.d.ts.map +0 -1
  76. package/dist/services/__test__/errorHandler.spec.js +0 -115
  77. package/dist/services/__test__/formParse.spec.d.ts +0 -2
  78. package/dist/services/__test__/formParse.spec.d.ts.map +0 -1
  79. package/dist/services/__test__/formParse.spec.js +0 -78
  80. package/dist/services/__test__/getCaller.spec.d.ts +0 -2
  81. package/dist/services/__test__/getCaller.spec.d.ts.map +0 -1
  82. package/dist/services/__test__/getCaller.spec.js +0 -60
  83. package/dist/services/__test__/getScopedParams.spec.d.ts +0 -2
  84. package/dist/services/__test__/getScopedParams.spec.d.ts.map +0 -1
  85. package/dist/services/__test__/getScopedParams.spec.js +0 -51
  86. package/dist/services/__test__/schemaValidator.spec.d.ts +0 -2
  87. package/dist/services/__test__/schemaValidator.spec.d.ts.map +0 -1
  88. package/dist/services/__test__/schemaValidator.spec.js +0 -89
  89. package/src/api/__test__/deleteRequest.spec.ts +0 -107
  90. package/src/api/__test__/getRequest.spec.ts +0 -76
  91. package/src/api/__test__/inboxFlowRequest.spec.ts +0 -125
  92. package/src/api/__test__/makeRequest.spec.ts +0 -121
  93. package/src/api/__test__/patchRequest.spec.ts +0 -125
  94. package/src/api/__test__/postRequest.spec.ts +0 -125
  95. package/src/api/__test__/putRequest.spec.ts +0 -125
  96. package/src/config/__test__/apiInstance.spec.ts +0 -129
  97. package/src/config/__test__/inboxFlowInstance.spec.ts +0 -61
  98. package/src/http/badResponses/__test__/badGateway.spec.ts +0 -50
  99. package/src/http/badResponses/__test__/badRequest.spec.ts +0 -50
  100. package/src/http/badResponses/__test__/conflict.spec.ts +0 -50
  101. package/src/http/badResponses/__test__/forbidden.spec.ts +0 -50
  102. package/src/http/badResponses/__test__/notFound.spec.ts +0 -50
  103. package/src/http/badResponses/__test__/notImplemented.spec.ts +0 -50
  104. package/src/http/badResponses/__test__/serverError.spec.ts +0 -50
  105. package/src/http/badResponses/__test__/unauthorized.spec.ts +0 -50
  106. package/src/http/badResponses/__test__/unprocessableEntity.spec.ts +0 -76
  107. package/src/http/successResponses/__test__/created.spec.ts +0 -65
  108. package/src/http/successResponses/__test__/found.spec.ts +0 -65
  109. package/src/http/successResponses/__test__/noContent.spec.ts +0 -49
  110. package/src/http/successResponses/__test__/success.spec.ts +0 -68
  111. package/src/http/successResponses/__test__/updated.spec.ts +0 -68
  112. package/src/services/__test__/decodeRequestBody.spec.ts +0 -51
  113. package/src/services/__test__/errorHandler.spec.ts +0 -133
  114. package/src/services/__test__/formParse.spec.ts +0 -96
  115. package/src/services/__test__/getCaller.spec.ts +0 -78
  116. package/src/services/__test__/getScopedParams.spec.ts +0 -68
  117. package/src/services/__test__/schemaValidator.spec.ts +0 -106
@@ -1,51 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { getScopedParams } from "../getScopedParams";
3
- describe("getScopedParams", () => {
4
- it("should return all query parameters when no scope is provided", () => {
5
- const request = {
6
- url: "https://example.com?key1=value1&key2=value2",
7
- };
8
- const params = getScopedParams(request);
9
- expect(params.get("key1")).toBe("value1");
10
- expect(params.get("key2")).toBe("value2");
11
- expect(params.toString()).toBe("key1=value1&key2=value2");
12
- });
13
- it("should return only scoped query parameters when a scope is provided", () => {
14
- const request = {
15
- url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3",
16
- };
17
- const params = getScopedParams(request, "scope");
18
- expect(params.get("key1")).toBe("value1");
19
- expect(params.get("key2")).toBe("value2");
20
- expect(params.get("key3")).toBeNull();
21
- expect(params.toString()).toBe("key1=value1&key2=value2");
22
- });
23
- it("should return an empty URLSearchParams object if no parameters match the scope", () => {
24
- const request = {
25
- url: "https://example.com?key1=value1&key2=value2",
26
- };
27
- const params = getScopedParams(request, "scope");
28
- expect(params.toString()).toBe("");
29
- });
30
- it("should handle URLs with no query parameters", () => {
31
- const request = { url: "https://example.com" };
32
- const params = getScopedParams(request);
33
- expect(params.toString()).toBe("");
34
- });
35
- it("should handle URLs with query parameters but no matching scope", () => {
36
- const request = {
37
- url: "https://example.com?other:key1=value1&other:key2=value2",
38
- };
39
- const params = getScopedParams(request, "scope");
40
- expect(params.toString()).toBe("");
41
- });
42
- it("should handle special characters in query parameter keys and values", () => {
43
- const request = {
44
- url: "https://example.com?scope:key+1=value+1&scope:key+2=value+2",
45
- };
46
- const params = getScopedParams(request, "scope");
47
- expect(params.get("key 1")).toBe("value 1");
48
- expect(params.get("key 2")).toBe("value 2");
49
- expect(params.toString()).toBe("key+1=value+1&key+2=value+2");
50
- });
51
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=schemaValidator.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schemaValidator.spec.d.ts","sourceRoot":"","sources":["../../../src/services/__test__/schemaValidator.spec.ts"],"names":[],"mappings":""}
@@ -1,89 +0,0 @@
1
- import { z } from "zod";
2
- import { SchemaValidator } from "../schemaValidator";
3
- import { httpDebug } from "../httpDebug";
4
- import { ServerError } from "../../http/badResponses/serverError";
5
- import { UnprocessableEntity } from "../../http/badResponses/unprocessableEntity";
6
- import { describe, expect, it, vi } from "vitest";
7
- vi.mock("../getCaller", () => ({
8
- getCaller: vi.fn(() => ({
9
- callerInfo: "mockedCallerInfo",
10
- functionName: "mockedFunctionName",
11
- })),
12
- }));
13
- vi.mock("../httpDebug", () => ({
14
- httpDebug: vi.fn(),
15
- }));
16
- describe("SchemaValidator", () => {
17
- const schema = z.object({
18
- name: z.string().min(1, "Name is required"),
19
- age: z.number().min(18, "Must be at least 18"),
20
- });
21
- const validator = new SchemaValidator(schema);
22
- describe("constructor", () => {
23
- it("should initialize with caller info and function name", () => {
24
- expect(validator.callerInfo).toBe("mockedCallerInfo");
25
- expect(validator.functionName).toBe("mockedFunctionName");
26
- });
27
- });
28
- describe("isValid", () => {
29
- it("should return true for valid data", () => {
30
- const data = { name: "John", age: 25 };
31
- expect(validator.isValid(data)).toBe(true);
32
- });
33
- it("should return false for invalid data", () => {
34
- const data = { name: "", age: 17 };
35
- expect(validator.isValid(data)).toBe(false);
36
- });
37
- });
38
- describe("safeValidate", () => {
39
- it("should return success for valid data", () => {
40
- const data = { name: "John", age: 25 };
41
- const result = validator.safeValidate(data);
42
- expect(result.success).toBe(true);
43
- expect(result.data).toEqual(data);
44
- });
45
- it("should return failure for invalid data", () => {
46
- const data = { name: "", age: 17 };
47
- const result = validator.safeValidate(data);
48
- expect(result.success).toBe(false);
49
- if (!result.success)
50
- expect(result.error.issues).toHaveLength(2);
51
- });
52
- });
53
- describe("validate", () => {
54
- it("should return parsed data for valid input", () => {
55
- const data = { name: "John", age: 25 };
56
- expect(validator.validate(data)).toEqual(data);
57
- });
58
- it("should throw ServerError for invalid input", () => {
59
- const data = { name: "", age: 17 };
60
- expect(() => validator.validate(data)).toThrow(ServerError);
61
- });
62
- });
63
- describe("formValidate", () => {
64
- it("should return parsed data for valid input", () => {
65
- const data = { name: "John", age: 25 };
66
- expect(validator.formValidate(data)).toEqual(data);
67
- });
68
- it("should throw UnprocessableEntity for invalid input", () => {
69
- const data = { name: "", age: 17 };
70
- expect(() => validator.formValidate(data)).toThrow(UnprocessableEntity);
71
- });
72
- it("should call httpDebug with formParsed data on validation failure", () => {
73
- const data = { name: "", age: 17 };
74
- try {
75
- validator.formValidate(data);
76
- }
77
- catch (error) {
78
- // Ignore the error
79
- }
80
- expect(httpDebug).toHaveBeenCalledWith("UnprocessableEntity", expect.objectContaining({
81
- success: false,
82
- fieldErrors: {
83
- name: "Name is required",
84
- age: "Must be at least 18",
85
- },
86
- }));
87
- });
88
- });
89
- });
@@ -1,107 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { deleteRequest } from "../deleteRequest";
3
-
4
- global.fetch = vi.fn() as any;
5
-
6
- describe("deleteRequest", () => {
7
- it("should return success response for a successfully delete request", async () => {
8
- const mockResponse = { message: "Resource deleted successfully" };
9
- (fetch as any).mockResolvedValueOnce({
10
- ok: true,
11
- status: 200,
12
- json: async () => mockResponse,
13
- });
14
-
15
- const response = await deleteRequest("https://api.example.com/resource");
16
-
17
- expect(response).toEqual({
18
- success: true,
19
- status: 200,
20
- message: "Resource deleted successfully",
21
- response: mockResponse,
22
- cause: null,
23
- });
24
- });
25
-
26
- it("should return failed response for a failed delete request", async () => {
27
- const mockErrorResponse = { error: "Resource not found" };
28
-
29
- (fetch as any).mockResolvedValueOnce({
30
- ok: false,
31
- status: 404,
32
- json: async () => mockErrorResponse,
33
- });
34
-
35
- const response = await deleteRequest("https://api.example.com/resource");
36
-
37
- expect(response.success).toBe(false);
38
- expect(response.status).toBe(404);
39
- expect(response.response).toEqual(mockErrorResponse);
40
- expect(response.message).toBeDefined();
41
- });
42
-
43
- it("should handle network errors gracefully", async () => {
44
- (fetch as any).mockRejectedValueOnce(new Error("Network Error"));
45
-
46
- const response = await deleteRequest("https://api.example.com/resource");
47
-
48
- expect(response).toEqual({
49
- success: false,
50
- status: 0,
51
- message: "Network error or request failed",
52
- response: null,
53
- cause: "Network Error",
54
- });
55
- });
56
-
57
- it("should handle invalid JSON response gracefully", async () => {
58
- (fetch as any).mockResolvedValueOnce({
59
- ok: true,
60
- status: 200,
61
- json: async () => {
62
- throw new Error("Invalid JSON");
63
- },
64
- });
65
-
66
- const response = await deleteRequest("https://api.example.com/resource");
67
-
68
- expect(response).toEqual({
69
- success: true,
70
- status: 200,
71
- message: "Resource deleted successfully",
72
- response: null,
73
- cause: null,
74
- });
75
- });
76
-
77
- it("should send a body with the delete request if provided", async () => {
78
- const mockResponse = { message: "Resource deleted successfully" };
79
- const mockBody = { reason: "No longer needed" };
80
-
81
- (fetch as any).mockResolvedValueOnce({
82
- ok: true,
83
- status: 200,
84
- json: async () => mockResponse,
85
- });
86
-
87
- const response = await deleteRequest(
88
- "https://api.example.com/resource",
89
- { "Content-Type": "application/json" },
90
- mockBody
91
- );
92
-
93
- expect(fetch).toHaveBeenCalledWith("https://api.example.com/resource", {
94
- method: "DELETE",
95
- headers: { "Content-Type": "application/json" },
96
- body: JSON.stringify(mockBody),
97
- });
98
-
99
- expect(response).toEqual({
100
- success: true,
101
- status: 200,
102
- message: "Resource deleted successfully",
103
- response: mockResponse,
104
- cause: null,
105
- });
106
- });
107
- });
@@ -1,76 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { getRequest } from "../getRequest";
3
-
4
- global.fetch = vi.fn() as any;
5
-
6
- describe("getRequest", () => {
7
- it("should return success response for a successful request", async () => {
8
- const mockResponse = { message: "Request successful", data: { id: 1 } };
9
- (fetch as any).mockResolvedValueOnce({
10
- ok: true,
11
- status: 200,
12
- json: async () => mockResponse,
13
- });
14
-
15
- const response = await getRequest("https://api.example.com/resource");
16
-
17
- expect(response).toEqual({
18
- success: true,
19
- status: 200,
20
- message: "Request successful",
21
- response: mockResponse,
22
- cause: null,
23
- });
24
- });
25
-
26
- it("should return failed response for a failed request", async () => {
27
- const mockErrorResponse = { error: "Not Found" };
28
-
29
- (fetch as any).mockResolvedValueOnce({
30
- ok: false,
31
- status: 404,
32
- json: async () => mockErrorResponse,
33
- });
34
-
35
- const response = await getRequest("https://api.example.com/resource");
36
-
37
- expect(response.success).toBe(false);
38
- expect(response.status).toBe(404);
39
- expect(response.response).toEqual(mockErrorResponse);
40
- expect(response.message).toBeDefined();
41
- });
42
-
43
- it("should handle network errors gracefully", async () => {
44
- (fetch as any).mockRejectedValueOnce(new Error("Network Error"));
45
-
46
- const response = await getRequest("https://api.example.com/resource");
47
-
48
- expect(response).toEqual({
49
- success: false,
50
- status: 0,
51
- message: "Network error or request failed",
52
- response: null,
53
- cause: "Network Error",
54
- });
55
- });
56
-
57
- it("should handle invalid JSON response gracefully", async () => {
58
- (fetch as any).mockResolvedValueOnce({
59
- ok: true,
60
- status: 200,
61
- json: async () => {
62
- throw new Error("Invalid JSON");
63
- },
64
- });
65
-
66
- const response = await getRequest("https://api.example.com/resource");
67
-
68
- expect(response).toEqual({
69
- success: true,
70
- status: 200,
71
- message: "Request successful",
72
- response: null,
73
- cause: null,
74
- });
75
- });
76
- });
@@ -1,125 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { inboxFlowRequest } from "../inboxFlowRequest";
3
- import { InboxFlowInstance } from "../../config/inboxFlowInstance";
4
- import { httpDebug } from "../../services/httpDebug";
5
-
6
- vi.mock("../../config/inboxFlowInstance");
7
- vi.mock("../../services/httpDebug");
8
-
9
- global.fetch = vi.fn() as any;
10
-
11
- describe("inboxFlowRequest", () => {
12
- beforeEach(() => {
13
- vi.clearAllMocks();
14
- });
15
-
16
- it("should send a POST request with correct body and headers", async () => {
17
- const mockConfig = {
18
- status: 200,
19
- method: "POST" as any,
20
- request: "Request data",
21
- response: "Response data",
22
- token: "test-token",
23
- };
24
-
25
- const mockInboxConfig = {
26
- inboxChannelId: "channel-id",
27
- inboxUserToken: "user-token",
28
- inboxApiUrl: "https://api.example.com/inbox",
29
- };
30
-
31
- (InboxFlowInstance.getInboxConfig as any).mockReturnValue(mockInboxConfig);
32
-
33
- (fetch as any).mockResolvedValueOnce({
34
- ok: true,
35
- status: 200,
36
- });
37
-
38
- await inboxFlowRequest(mockConfig);
39
-
40
- expect(fetch).toHaveBeenCalledWith(mockInboxConfig.inboxApiUrl, {
41
- method: "POST",
42
- body: JSON.stringify({
43
- status: mockConfig.status,
44
- channelId: mockInboxConfig.inboxChannelId,
45
- method: mockConfig.method,
46
- token: mockConfig.token,
47
- request: mockConfig.request,
48
- response: mockConfig.response,
49
- }),
50
- headers: {
51
- "Content-Type": "application/json",
52
- Authorization: `Bearer ${mockInboxConfig.inboxUserToken}`,
53
- },
54
- });
55
- });
56
-
57
- it("should not send a request if inbox configuration is not set", async () => {
58
- (InboxFlowInstance.getInboxConfig as any).mockReturnValue(undefined);
59
-
60
- await inboxFlowRequest({
61
- status: 200,
62
- method: "POST",
63
- request: "Request data",
64
- response: "Response data",
65
- token: "test-token",
66
- });
67
-
68
- expect(fetch).not.toHaveBeenCalled();
69
- });
70
-
71
- it("should handle fetch errors gracefully", async () => {
72
- const mockConfig = {
73
- status: 500,
74
- method: "ERROR" as any,
75
- request: "Request data",
76
- response: "Error response",
77
- token: "test-token",
78
- };
79
-
80
- const mockInboxConfig = {
81
- inboxChannelId: "channel-id",
82
- inboxUserToken: "user-token",
83
- inboxApiUrl: "https://api.example.com/inbox",
84
- };
85
-
86
- (InboxFlowInstance.getInboxConfig as any).mockReturnValue(mockInboxConfig);
87
-
88
- (fetch as any).mockRejectedValueOnce(new Error("Network Error"));
89
-
90
- await inboxFlowRequest(mockConfig);
91
-
92
- expect(httpDebug).toHaveBeenCalledWith(
93
- "inboxFlowRequest",
94
- "Error sending inbox flow request",
95
- expect.any(Error)
96
- );
97
- });
98
-
99
- it("should not send a request in development environment", async () => {
100
- const originalEnv = process.env.NODE_ENV;
101
- process.env.NODE_ENV = "development";
102
-
103
- const mockConfig = {
104
- status: 200,
105
- method: "POST" as any,
106
- request: "Request data",
107
- response: "Response data",
108
- token: "test-token",
109
- };
110
-
111
- const mockInboxConfig = {
112
- inboxChannelId: "channel-id",
113
- inboxUserToken: "user-token",
114
- inboxApiUrl: "https://api.example.com/inbox",
115
- };
116
-
117
- (InboxFlowInstance.getInboxConfig as any).mockReturnValue(mockInboxConfig);
118
-
119
- await inboxFlowRequest(mockConfig);
120
-
121
- expect(fetch).not.toHaveBeenCalled();
122
-
123
- process.env.NODE_ENV = originalEnv;
124
- });
125
- });
@@ -1,121 +0,0 @@
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
- });