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