@arkyn/server 2.0.1-beta.1 → 2.0.1-beta.3

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 (136) hide show
  1. package/dist/http/badResponses/badGateway.d.ts +11 -0
  2. package/dist/http/badResponses/badGateway.d.ts.map +1 -0
  3. package/dist/http/badResponses/badGateway.js +29 -0
  4. package/dist/http/badResponses/badRequest.d.ts +11 -0
  5. package/dist/http/badResponses/badRequest.d.ts.map +1 -0
  6. package/dist/http/badResponses/badRequest.js +29 -0
  7. package/dist/http/badResponses/conflict.d.ts +11 -0
  8. package/dist/http/badResponses/conflict.d.ts.map +1 -0
  9. package/dist/http/badResponses/conflict.js +29 -0
  10. package/dist/http/badResponses/forbidden.d.ts +11 -0
  11. package/dist/http/badResponses/forbidden.d.ts.map +1 -0
  12. package/dist/http/badResponses/forbidden.js +29 -0
  13. package/dist/http/badResponses/notFound.d.ts +11 -0
  14. package/dist/http/badResponses/notFound.d.ts.map +1 -0
  15. package/dist/http/badResponses/notFound.js +29 -0
  16. package/dist/http/badResponses/notImplemented.d.ts +11 -0
  17. package/dist/http/badResponses/notImplemented.d.ts.map +1 -0
  18. package/dist/http/badResponses/notImplemented.js +29 -0
  19. package/dist/http/badResponses/serverError.d.ts +11 -0
  20. package/dist/http/badResponses/serverError.d.ts.map +1 -0
  21. package/dist/http/badResponses/serverError.js +29 -0
  22. package/dist/http/badResponses/unauthorized.d.ts +11 -0
  23. package/dist/http/badResponses/unauthorized.d.ts.map +1 -0
  24. package/dist/http/badResponses/unauthorized.js +29 -0
  25. package/dist/http/badResponses/unprocessableEntity.d.ts +16 -0
  26. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -0
  27. package/dist/http/badResponses/unprocessableEntity.js +33 -0
  28. package/dist/http/httpDebug.d.ts +3 -0
  29. package/dist/http/httpDebug.d.ts.map +1 -0
  30. package/dist/http/httpDebug.js +15 -0
  31. package/dist/http/successResponses/created.d.ts +11 -0
  32. package/dist/http/successResponses/created.d.ts.map +1 -0
  33. package/dist/http/successResponses/created.js +29 -0
  34. package/dist/http/successResponses/found.d.ts +11 -0
  35. package/dist/http/successResponses/found.d.ts.map +1 -0
  36. package/dist/http/successResponses/found.js +29 -0
  37. package/dist/http/successResponses/noContent.d.ts +10 -0
  38. package/dist/http/successResponses/noContent.d.ts.map +1 -0
  39. package/dist/http/successResponses/noContent.js +27 -0
  40. package/dist/http/successResponses/success.d.ts +11 -0
  41. package/dist/http/successResponses/success.d.ts.map +1 -0
  42. package/dist/http/successResponses/success.js +29 -0
  43. package/dist/http/successResponses/updated.d.ts +11 -0
  44. package/dist/http/successResponses/updated.d.ts.map +1 -0
  45. package/dist/http/successResponses/updated.js +29 -0
  46. package/dist/index.d.ts +16 -13
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +16 -17
  49. package/dist/services/decodeRequestBody.d.ts +17 -0
  50. package/dist/services/decodeRequestBody.d.ts.map +1 -0
  51. package/dist/services/{extractJsonFromRequest.js → decodeRequestBody.js} +16 -3
  52. package/dist/services/errorHandler.d.ts +3 -0
  53. package/dist/services/errorHandler.d.ts.map +1 -0
  54. package/dist/services/errorHandler.js +51 -0
  55. package/dist/services/formParse.d.ts +40 -1
  56. package/dist/services/formParse.d.ts.map +1 -1
  57. package/dist/services/formParse.js +40 -1
  58. package/dist/services/getScopedParams.d.ts +26 -1
  59. package/dist/services/getScopedParams.d.ts.map +1 -1
  60. package/dist/services/getScopedParams.js +26 -2
  61. package/dist/services/sendFileToS3.d.ts +50 -18
  62. package/dist/services/sendFileToS3.d.ts.map +1 -1
  63. package/dist/services/sendFileToS3.js +73 -31
  64. package/package.json +2 -1
  65. package/src/http/badResponses/badGateway.ts +36 -0
  66. package/src/http/badResponses/badRequest.ts +36 -0
  67. package/src/http/badResponses/conflict.ts +36 -0
  68. package/src/http/badResponses/forbidden.ts +36 -0
  69. package/src/http/badResponses/notFound.ts +36 -0
  70. package/src/http/badResponses/notImplemented.ts +36 -0
  71. package/src/http/badResponses/serverError.ts +36 -0
  72. package/src/http/badResponses/unauthorized.ts +36 -0
  73. package/src/http/badResponses/unprocessableEntity.ts +48 -0
  74. package/src/http/httpDebug.ts +31 -0
  75. package/src/http/successResponses/created.ts +35 -0
  76. package/src/http/successResponses/found.ts +35 -0
  77. package/src/http/successResponses/noContent.ts +33 -0
  78. package/src/http/successResponses/success.ts +35 -0
  79. package/src/http/successResponses/updated.ts +35 -0
  80. package/src/index.ts +16 -18
  81. package/src/services/decodeRequestBody.ts +46 -0
  82. package/src/services/errorHandler.ts +55 -0
  83. package/src/services/formParse.ts +42 -1
  84. package/src/services/getScopedParams.ts +29 -2
  85. package/src/services/sendFileToS3.ts +81 -56
  86. package/dist/helpers/globalErrorHandler.d.ts +0 -3
  87. package/dist/helpers/globalErrorHandler.d.ts.map +0 -1
  88. package/dist/helpers/globalErrorHandler.js +0 -53
  89. package/dist/httpBadResponses/badRequest.d.ts +0 -9
  90. package/dist/httpBadResponses/badRequest.d.ts.map +0 -1
  91. package/dist/httpBadResponses/badRequest.js +0 -23
  92. package/dist/httpBadResponses/conflict.d.ts +0 -9
  93. package/dist/httpBadResponses/conflict.d.ts.map +0 -1
  94. package/dist/httpBadResponses/conflict.js +0 -23
  95. package/dist/httpBadResponses/forbidden.d.ts +0 -9
  96. package/dist/httpBadResponses/forbidden.d.ts.map +0 -1
  97. package/dist/httpBadResponses/forbidden.js +0 -23
  98. package/dist/httpBadResponses/notFound.d.ts +0 -9
  99. package/dist/httpBadResponses/notFound.d.ts.map +0 -1
  100. package/dist/httpBadResponses/notFound.js +0 -23
  101. package/dist/httpBadResponses/serverError.d.ts +0 -9
  102. package/dist/httpBadResponses/serverError.d.ts.map +0 -1
  103. package/dist/httpBadResponses/serverError.js +0 -23
  104. package/dist/httpBadResponses/unauthorized.d.ts +0 -9
  105. package/dist/httpBadResponses/unauthorized.d.ts.map +0 -1
  106. package/dist/httpBadResponses/unauthorized.js +0 -23
  107. package/dist/httpBadResponses/unprocessableEntity.d.ts +0 -17
  108. package/dist/httpBadResponses/unprocessableEntity.d.ts.map +0 -1
  109. package/dist/httpBadResponses/unprocessableEntity.js +0 -30
  110. package/dist/httpResponses/created.d.ts +0 -10
  111. package/dist/httpResponses/created.d.ts.map +0 -1
  112. package/dist/httpResponses/created.js +0 -30
  113. package/dist/httpResponses/noContent.d.ts +0 -9
  114. package/dist/httpResponses/noContent.d.ts.map +0 -1
  115. package/dist/httpResponses/noContent.js +0 -28
  116. package/dist/httpResponses/success.d.ts +0 -10
  117. package/dist/httpResponses/success.d.ts.map +0 -1
  118. package/dist/httpResponses/success.js +0 -30
  119. package/dist/httpResponses/updated.d.ts +0 -10
  120. package/dist/httpResponses/updated.d.ts.map +0 -1
  121. package/dist/httpResponses/updated.js +0 -30
  122. package/dist/services/extractJsonFromRequest.d.ts +0 -3
  123. package/dist/services/extractJsonFromRequest.d.ts.map +0 -1
  124. package/src/helpers/globalErrorHandler.ts +0 -64
  125. package/src/httpBadResponses/badRequest.ts +0 -29
  126. package/src/httpBadResponses/conflict.ts +0 -29
  127. package/src/httpBadResponses/forbidden.ts +0 -29
  128. package/src/httpBadResponses/notFound.ts +0 -29
  129. package/src/httpBadResponses/serverError.ts +0 -29
  130. package/src/httpBadResponses/unauthorized.ts +0 -29
  131. package/src/httpBadResponses/unprocessableEntity.ts +0 -43
  132. package/src/httpResponses/created.ts +0 -35
  133. package/src/httpResponses/noContent.ts +0 -33
  134. package/src/httpResponses/success.ts +0 -35
  135. package/src/httpResponses/updated.ts +0 -35
  136. package/src/services/extractJsonFromRequest.ts +0 -30
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class BadGateway {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 502;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "BadGateway", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("BadGateway", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { BadGateway };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class BadRequest {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 400;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "BadRequest", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("BadRequest", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { BadRequest };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class Conflict {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 409;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "Conflict", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("Conflict", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { Conflict };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class Forbidden {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 402;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "Forbidden", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("Forbidden", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { Forbidden };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class NotFound {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 404;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "NotFound", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("NotFound", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { NotFound };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class NotImplemented {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 501;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "NotImplemented", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("NotImplemented", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { NotImplemented };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class ServerError {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 500;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "ServerError", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("ServerError", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { ServerError };
@@ -0,0 +1,36 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ class Unauthorized {
4
+ body: any;
5
+ cause?: any;
6
+ status: number = 401;
7
+ statusText: string;
8
+
9
+ constructor(message: string, cause?: any) {
10
+ this.body = { name: "Unauthorized", message: message };
11
+ this.statusText = message;
12
+ this.cause = cause ? JSON.stringify(cause) : undefined;
13
+ httpDebug("Unauthorized", this.body, this.cause);
14
+ }
15
+
16
+ toResponse() {
17
+ const responseInit: ResponseInit = {
18
+ headers: { "Content-Type": "application/json" },
19
+ status: this.status,
20
+ statusText: this.statusText,
21
+ };
22
+
23
+ return new Response(JSON.stringify(this.body), responseInit);
24
+ }
25
+
26
+ toJson() {
27
+ const responseInit: ResponseInit = {
28
+ status: this.status,
29
+ statusText: this.statusText,
30
+ };
31
+
32
+ return Response.json(this.body, responseInit);
33
+ }
34
+ }
35
+
36
+ export { Unauthorized };
@@ -0,0 +1,48 @@
1
+ import { httpDebug } from "../httpDebug";
2
+
3
+ type UnprocessableEntityProps = {
4
+ data?: any;
5
+ fieldErrors?: Record<string, string>;
6
+ fields?: Record<string, string>;
7
+ message?: string;
8
+ };
9
+
10
+ class UnprocessableEntity {
11
+ body: any;
12
+ status: number = 422;
13
+ statusText: string;
14
+
15
+ constructor(props: UnprocessableEntityProps) {
16
+ this.statusText = props.message || "Unprocessable Entity";
17
+ this.body = {
18
+ name: "UnprocessableEntity",
19
+ message: props.message || null,
20
+ data: props.data,
21
+ fieldErrors: props.fieldErrors,
22
+ fields: props.fields,
23
+ };
24
+
25
+ httpDebug("UnprocessableEntity", this.body);
26
+ }
27
+
28
+ toResponse() {
29
+ const responseInit: ResponseInit = {
30
+ headers: { "Content-Type": "application/json" },
31
+ status: this.status,
32
+ statusText: this.statusText,
33
+ };
34
+
35
+ return new Response(JSON.stringify(this.body), responseInit);
36
+ }
37
+
38
+ toJson() {
39
+ const responseInit: ResponseInit = {
40
+ status: this.status,
41
+ statusText: this.statusText,
42
+ };
43
+
44
+ return Response.json(this.body, responseInit);
45
+ }
46
+ }
47
+
48
+ export { UnprocessableEntity };
@@ -0,0 +1,31 @@
1
+ function httpDebug(name: string, body: any, cause?: any) {
2
+ const isDebugMode =
3
+ process.env.NODE_ENV === "development" ||
4
+ process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
5
+
6
+ if (isDebugMode) {
7
+ const reset = "\x1b[0m";
8
+ const cyan = "\x1b[36m";
9
+
10
+ const error = new Error();
11
+ const stackLines = error.stack?.split("\n") || [];
12
+
13
+ let callerLine = "Unknown caller";
14
+ for (const line of stackLines) {
15
+ if (
16
+ !line.includes("httpDebug") &&
17
+ !line.includes("BadGateway.<anonymous>")
18
+ ) {
19
+ callerLine = line.trim();
20
+ break;
21
+ }
22
+ }
23
+
24
+ console.log(`${cyan}[ARKYN-DEBUG]${reset} ${name} initialized`);
25
+ console.log(`${cyan}[ARKYN-DEBUG]${reset} Caller:`, callerLine);
26
+ console.log(`${cyan}[ARKYN-DEBUG]${reset} Body:`, body);
27
+ if (cause) console.log(`[ARKYN-DEBUG] Cause:`, cause);
28
+ }
29
+ }
30
+
31
+ export { httpDebug };
@@ -0,0 +1,35 @@
1
+ class Created<T> {
2
+ body: T;
3
+ headers: ResponseInit["headers"];
4
+ status: number;
5
+ statusText: string;
6
+
7
+ constructor(body: T, init?: ResponseInit) {
8
+ this.body = body;
9
+ this.headers = init?.headers || {};
10
+ this.status = init?.status || 201;
11
+ this.statusText = init?.statusText || "Resource created successfully";
12
+ }
13
+
14
+ toResponse() {
15
+ const responseInit: ResponseInit = {
16
+ headers: { "Content-Type": "application/json", ...this.headers },
17
+ status: this.status,
18
+ statusText: this.statusText,
19
+ };
20
+
21
+ return new Response(JSON.stringify(this.body), responseInit);
22
+ }
23
+
24
+ toJson() {
25
+ const responseInit: ResponseInit = {
26
+ headers: this.headers,
27
+ status: this.status,
28
+ statusText: this.statusText,
29
+ };
30
+
31
+ return Response.json(this.body, responseInit);
32
+ }
33
+ }
34
+
35
+ export { Created };
@@ -0,0 +1,35 @@
1
+ class Found<T> {
2
+ body: T;
3
+ headers: ResponseInit["headers"];
4
+ status: number;
5
+ statusText: string;
6
+
7
+ constructor(body: T, init?: ResponseInit) {
8
+ this.body = body;
9
+ this.headers = init?.headers || {};
10
+ this.status = init?.status || 302;
11
+ this.statusText = init?.statusText || "Found";
12
+ }
13
+
14
+ toResponse() {
15
+ const responseInit: ResponseInit = {
16
+ headers: { "Content-Type": "application/json", ...this.headers },
17
+ status: this.status,
18
+ statusText: this.statusText,
19
+ };
20
+
21
+ return new Response(JSON.stringify(this.body), responseInit);
22
+ }
23
+
24
+ toJson() {
25
+ const responseInit: ResponseInit = {
26
+ headers: this.headers,
27
+ status: this.status,
28
+ statusText: this.statusText,
29
+ };
30
+
31
+ return Response.json(this.body, responseInit);
32
+ }
33
+ }
34
+
35
+ export { Found };
@@ -0,0 +1,33 @@
1
+ class NoContent {
2
+ headers: ResponseInit["headers"];
3
+ status: number;
4
+ statusText: string;
5
+
6
+ constructor(init?: ResponseInit) {
7
+ this.headers = init?.headers || {};
8
+ this.status = init?.status || 204;
9
+ this.statusText = init?.statusText ?? "No content";
10
+ }
11
+
12
+ toResponse() {
13
+ const responseInit: ResponseInit = {
14
+ headers: this.headers,
15
+ status: this.status,
16
+ statusText: this.statusText,
17
+ };
18
+
19
+ return new Response(null, responseInit);
20
+ }
21
+
22
+ toJson() {
23
+ const responseInit: ResponseInit = {
24
+ headers: this.headers,
25
+ status: this.status,
26
+ statusText: this.statusText,
27
+ };
28
+
29
+ return Response.json(null, responseInit);
30
+ }
31
+ }
32
+
33
+ export { NoContent };
@@ -0,0 +1,35 @@
1
+ class Success<T> {
2
+ body: T;
3
+ headers: ResponseInit["headers"];
4
+ status: number;
5
+ statusText: string;
6
+
7
+ constructor(body: T, init?: ResponseInit) {
8
+ this.body = body;
9
+ this.headers = init?.headers || {};
10
+ this.status = init?.status || 200;
11
+ this.statusText = init?.statusText ?? "OK";
12
+ }
13
+
14
+ toResponse() {
15
+ const responseInit: ResponseInit = {
16
+ headers: { "Content-Type": "application/json", ...this.headers },
17
+ status: this.status,
18
+ statusText: this.statusText,
19
+ };
20
+
21
+ return new Response(JSON.stringify(this.body), responseInit);
22
+ }
23
+
24
+ toJson() {
25
+ const responseInit: ResponseInit = {
26
+ headers: this.headers,
27
+ status: this.status,
28
+ statusText: this.statusText,
29
+ };
30
+
31
+ return Response.json(this.body, responseInit);
32
+ }
33
+ }
34
+
35
+ export { Success };
@@ -0,0 +1,35 @@
1
+ class Updated<T> {
2
+ body: T;
3
+ headers: ResponseInit["headers"];
4
+ status: number;
5
+ statusText: string;
6
+
7
+ constructor(body: T, init?: ResponseInit) {
8
+ this.body = body;
9
+ this.headers = init?.headers || {};
10
+ this.status = init?.status || 200;
11
+ this.statusText = init?.statusText || "Resource updated successfully";
12
+ }
13
+
14
+ toResponse() {
15
+ const responseInit: ResponseInit = {
16
+ headers: { "Content-Type": "application/json", ...this.headers },
17
+ status: this.status,
18
+ statusText: this.statusText,
19
+ };
20
+
21
+ return new Response(JSON.stringify(this.body), responseInit);
22
+ }
23
+
24
+ toJson() {
25
+ const responseInit: ResponseInit = {
26
+ headers: this.headers,
27
+ status: this.status,
28
+ statusText: this.statusText,
29
+ };
30
+
31
+ return Response.json(this.body, responseInit);
32
+ }
33
+ }
34
+
35
+ export { Updated };
package/src/index.ts CHANGED
@@ -1,23 +1,21 @@
1
- // http bad responses
2
- export { BadRequestError } from "./httpBadResponses/badRequest";
3
- export { ConflictError } from "./httpBadResponses/conflict";
4
- export { ForbiddenError } from "./httpBadResponses/forbidden";
5
- export { NotFoundError } from "./httpBadResponses/notFound";
6
- export { ServerError } from "./httpBadResponses/serverError";
7
- export { UnauthorizedError } from "./httpBadResponses/unauthorized";
8
- export { UnprocessableEntityError } from "./httpBadResponses/unprocessableEntity";
1
+ export { BadGateway } from "./http/badResponses/badGateway";
2
+ export { BadRequest } from "./http/badResponses/badRequest";
3
+ export { Conflict } from "./http/badResponses/conflict";
4
+ export { Forbidden } from "./http/badResponses/forbidden";
5
+ export { NotFound } from "./http/badResponses/notFound";
6
+ export { NotImplemented } from "./http/badResponses/notImplemented";
7
+ export { ServerError } from "./http/badResponses/serverError";
8
+ export { Unauthorized } from "./http/badResponses/unauthorized";
9
+ export { UnprocessableEntity } from "./http/badResponses/unprocessableEntity";
9
10
 
10
- // http responses
11
- export { Created } from "./httpResponses/created";
12
- export { NoContent } from "./httpResponses/noContent";
13
- export { Success } from "./httpResponses/success";
14
- export { Updated } from "./httpResponses/updated";
11
+ export { Created } from "./http/successResponses/created";
12
+ export { Found } from "./http/successResponses/found";
13
+ export { NoContent } from "./http/successResponses/noContent";
14
+ export { Success } from "./http/successResponses/success";
15
+ export { Updated } from "./http/successResponses/updated";
15
16
 
16
- // helpers
17
- export { globalErrorHandler } from "./helpers/globalErrorHandler";
18
-
19
- // services
20
- export { extractJsonFromRequest } from "./services/extractJsonFromRequest";
17
+ export { decodeRequestBody } from "./services/decodeRequestBody";
18
+ export { errorHandler } from "./services/errorHandler";
21
19
  export { formParse } from "./services/formParse";
22
20
  export { getScopedParams } from "./services/getScopedParams";
23
21
  export { sendFileToS3 } from "./services/sendFileToS3";
@@ -0,0 +1,46 @@
1
+ import type { DecodeRequestBodyFunction } from "@arkyn/types";
2
+
3
+ /**
4
+ * Decodes the body of an incoming request into a JavaScript object.
5
+ *
6
+ * This function attempts to parse the request body in the following order:
7
+ * 1. Tries to parse the body as JSON.
8
+ * 2. If JSON parsing fails, attempts to parse the body as URL-encoded form data.
9
+ * 3. If both parsing attempts fail, logs the errors and returns an empty object.
10
+ *
11
+ * @param req - The incoming request object containing the body to decode.
12
+ * @returns A promise that resolves to the decoded data as a JavaScript object.
13
+ *
14
+ * @throws Logs errors to the console if the request body cannot be read or parsed.
15
+ */
16
+
17
+ const decodeRequestBody: DecodeRequestBodyFunction = async (req) => {
18
+ let data: any;
19
+
20
+ try {
21
+ const arrayBuffer = await req.arrayBuffer();
22
+ const text = new TextDecoder().decode(arrayBuffer);
23
+
24
+ try {
25
+ data = JSON.parse(text);
26
+ } catch (jsonError) {
27
+ try {
28
+ const formData = new URLSearchParams(text);
29
+ data = Object.fromEntries(formData.entries());
30
+ } catch (formDataError) {
31
+ console.error("Failed to extract data from request:", {
32
+ jsonError,
33
+ formDataError,
34
+ });
35
+ data = {};
36
+ }
37
+ }
38
+ } catch (error) {
39
+ console.error("Failed to read request body:", error);
40
+ data = {};
41
+ }
42
+
43
+ return data;
44
+ };
45
+
46
+ export { decodeRequestBody };