@arkyn/server 2.0.1-beta.1 → 2.0.1-beta.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.
- package/dist/http/badResponses/badGateway.d.ts +11 -0
- package/dist/http/badResponses/badGateway.d.ts.map +1 -0
- package/dist/http/badResponses/badGateway.js +29 -0
- package/dist/http/badResponses/badRequest.d.ts +11 -0
- package/dist/http/badResponses/badRequest.d.ts.map +1 -0
- package/dist/http/badResponses/badRequest.js +29 -0
- package/dist/http/badResponses/conflict.d.ts +11 -0
- package/dist/http/badResponses/conflict.d.ts.map +1 -0
- package/dist/http/badResponses/conflict.js +29 -0
- package/dist/http/badResponses/forbidden.d.ts +11 -0
- package/dist/http/badResponses/forbidden.d.ts.map +1 -0
- package/dist/http/badResponses/forbidden.js +29 -0
- package/dist/http/badResponses/notFound.d.ts +11 -0
- package/dist/http/badResponses/notFound.d.ts.map +1 -0
- package/dist/http/badResponses/notFound.js +29 -0
- package/dist/http/badResponses/notImplemented.d.ts +11 -0
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -0
- package/dist/http/badResponses/notImplemented.js +29 -0
- package/dist/http/badResponses/serverError.d.ts +11 -0
- package/dist/http/badResponses/serverError.d.ts.map +1 -0
- package/dist/http/badResponses/serverError.js +29 -0
- package/dist/http/badResponses/unauthorized.d.ts +11 -0
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -0
- package/dist/http/badResponses/unauthorized.js +29 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts +16 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -0
- package/dist/http/badResponses/unprocessableEntity.js +33 -0
- package/dist/http/httpDebug.d.ts +3 -0
- package/dist/http/httpDebug.d.ts.map +1 -0
- package/dist/http/httpDebug.js +15 -0
- package/dist/http/successResponses/created.d.ts +11 -0
- package/dist/http/successResponses/created.d.ts.map +1 -0
- package/dist/http/successResponses/created.js +29 -0
- package/dist/http/successResponses/found.d.ts +11 -0
- package/dist/http/successResponses/found.d.ts.map +1 -0
- package/dist/http/successResponses/found.js +29 -0
- package/dist/http/successResponses/noContent.d.ts +10 -0
- package/dist/http/successResponses/noContent.d.ts.map +1 -0
- package/dist/http/successResponses/noContent.js +27 -0
- package/dist/http/successResponses/success.d.ts +11 -0
- package/dist/http/successResponses/success.d.ts.map +1 -0
- package/dist/http/successResponses/success.js +29 -0
- package/dist/http/successResponses/updated.d.ts +11 -0
- package/dist/http/successResponses/updated.d.ts.map +1 -0
- package/dist/http/successResponses/updated.js +29 -0
- package/dist/index.d.ts +16 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -17
- package/dist/services/decodeRequestBody.d.ts +17 -0
- package/dist/services/decodeRequestBody.d.ts.map +1 -0
- package/dist/services/{extractJsonFromRequest.js → decodeRequestBody.js} +16 -3
- package/dist/services/errorHandler.d.ts +3 -0
- package/dist/services/errorHandler.d.ts.map +1 -0
- package/dist/services/errorHandler.js +51 -0
- package/dist/services/formParse.d.ts +40 -1
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +40 -1
- package/dist/services/getScopedParams.d.ts +26 -1
- package/dist/services/getScopedParams.d.ts.map +1 -1
- package/dist/services/getScopedParams.js +26 -2
- package/dist/services/sendFileToS3.d.ts +50 -18
- package/dist/services/sendFileToS3.d.ts.map +1 -1
- package/dist/services/sendFileToS3.js +73 -31
- package/package.json +2 -1
- package/src/http/badResponses/badGateway.ts +36 -0
- package/src/http/badResponses/badRequest.ts +36 -0
- package/src/http/badResponses/conflict.ts +36 -0
- package/src/http/badResponses/forbidden.ts +36 -0
- package/src/http/badResponses/notFound.ts +36 -0
- package/src/http/badResponses/notImplemented.ts +36 -0
- package/src/http/badResponses/serverError.ts +36 -0
- package/src/http/badResponses/unauthorized.ts +36 -0
- package/src/http/badResponses/unprocessableEntity.ts +48 -0
- package/src/http/httpDebug.ts +18 -0
- package/src/http/successResponses/created.ts +35 -0
- package/src/http/successResponses/found.ts +35 -0
- package/src/http/successResponses/noContent.ts +33 -0
- package/src/http/successResponses/success.ts +35 -0
- package/src/http/successResponses/updated.ts +35 -0
- package/src/index.ts +16 -18
- package/src/services/decodeRequestBody.ts +46 -0
- package/src/services/errorHandler.ts +55 -0
- package/src/services/formParse.ts +42 -1
- package/src/services/getScopedParams.ts +29 -2
- package/src/services/sendFileToS3.ts +81 -56
- package/dist/helpers/globalErrorHandler.d.ts +0 -3
- package/dist/helpers/globalErrorHandler.d.ts.map +0 -1
- package/dist/helpers/globalErrorHandler.js +0 -53
- package/dist/httpBadResponses/badRequest.d.ts +0 -9
- package/dist/httpBadResponses/badRequest.d.ts.map +0 -1
- package/dist/httpBadResponses/badRequest.js +0 -23
- package/dist/httpBadResponses/conflict.d.ts +0 -9
- package/dist/httpBadResponses/conflict.d.ts.map +0 -1
- package/dist/httpBadResponses/conflict.js +0 -23
- package/dist/httpBadResponses/forbidden.d.ts +0 -9
- package/dist/httpBadResponses/forbidden.d.ts.map +0 -1
- package/dist/httpBadResponses/forbidden.js +0 -23
- package/dist/httpBadResponses/notFound.d.ts +0 -9
- package/dist/httpBadResponses/notFound.d.ts.map +0 -1
- package/dist/httpBadResponses/notFound.js +0 -23
- package/dist/httpBadResponses/serverError.d.ts +0 -9
- package/dist/httpBadResponses/serverError.d.ts.map +0 -1
- package/dist/httpBadResponses/serverError.js +0 -23
- package/dist/httpBadResponses/unauthorized.d.ts +0 -9
- package/dist/httpBadResponses/unauthorized.d.ts.map +0 -1
- package/dist/httpBadResponses/unauthorized.js +0 -23
- package/dist/httpBadResponses/unprocessableEntity.d.ts +0 -17
- package/dist/httpBadResponses/unprocessableEntity.d.ts.map +0 -1
- package/dist/httpBadResponses/unprocessableEntity.js +0 -30
- package/dist/httpResponses/created.d.ts +0 -10
- package/dist/httpResponses/created.d.ts.map +0 -1
- package/dist/httpResponses/created.js +0 -30
- package/dist/httpResponses/noContent.d.ts +0 -9
- package/dist/httpResponses/noContent.d.ts.map +0 -1
- package/dist/httpResponses/noContent.js +0 -28
- package/dist/httpResponses/success.d.ts +0 -10
- package/dist/httpResponses/success.d.ts.map +0 -1
- package/dist/httpResponses/success.js +0 -30
- package/dist/httpResponses/updated.d.ts +0 -10
- package/dist/httpResponses/updated.d.ts.map +0 -1
- package/dist/httpResponses/updated.js +0 -30
- package/dist/services/extractJsonFromRequest.d.ts +0 -3
- package/dist/services/extractJsonFromRequest.d.ts.map +0 -1
- package/src/helpers/globalErrorHandler.ts +0 -64
- package/src/httpBadResponses/badRequest.ts +0 -29
- package/src/httpBadResponses/conflict.ts +0 -29
- package/src/httpBadResponses/forbidden.ts +0 -29
- package/src/httpBadResponses/notFound.ts +0 -29
- package/src/httpBadResponses/serverError.ts +0 -29
- package/src/httpBadResponses/unauthorized.ts +0 -29
- package/src/httpBadResponses/unprocessableEntity.ts +0 -43
- package/src/httpResponses/created.ts +0 -35
- package/src/httpResponses/noContent.ts +0 -33
- package/src/httpResponses/success.ts +0 -35
- package/src/httpResponses/updated.ts +0 -35
- 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,18 @@
|
|
|
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 pathname = new URL(import.meta.url).pathname;
|
|
11
|
+
console.log(`${cyan}[ARKYN-DEBUG]${reset} ${name} initialized`);
|
|
12
|
+
console.log(`${cyan}[ARKYN-DEBUG]${reset} Pathname:`, pathname);
|
|
13
|
+
console.log(`${cyan}[ARKYN-DEBUG]${reset} Body:`, body);
|
|
14
|
+
if (cause) console.log(`[ARKYN-DEBUG] Cause:`, cause);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
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
|
-
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
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
|
-
|
|
11
|
-
export {
|
|
12
|
-
export { NoContent } from "./
|
|
13
|
-
export { Success } from "./
|
|
14
|
-
export { Updated } from "./
|
|
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
|
-
|
|
17
|
-
export {
|
|
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 };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { BadGateway } from "../http/badResponses/badGateway";
|
|
2
|
+
import { BadRequest } from "../http/badResponses/badRequest";
|
|
3
|
+
import { Conflict } from "../http/badResponses/conflict";
|
|
4
|
+
import { Forbidden } from "../http/badResponses/forbidden";
|
|
5
|
+
import { NotFound } from "../http/badResponses/notFound";
|
|
6
|
+
import { NotImplemented } from "../http/badResponses/notImplemented";
|
|
7
|
+
import { ServerError } from "../http/badResponses/serverError";
|
|
8
|
+
import { Unauthorized } from "../http/badResponses/unauthorized";
|
|
9
|
+
import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
|
|
10
|
+
|
|
11
|
+
import { Created } from "../http/successResponses/created";
|
|
12
|
+
import { Found } from "../http/successResponses/found";
|
|
13
|
+
import { NoContent } from "../http/successResponses/noContent";
|
|
14
|
+
import { Success } from "../http/successResponses/success";
|
|
15
|
+
import { Updated } from "../http/successResponses/updated";
|
|
16
|
+
|
|
17
|
+
function errorHandler(error: any) {
|
|
18
|
+
switch (true) {
|
|
19
|
+
case error instanceof Response:
|
|
20
|
+
return error;
|
|
21
|
+
case error instanceof Found:
|
|
22
|
+
return error.toResponse();
|
|
23
|
+
case error instanceof Created:
|
|
24
|
+
return error.toResponse();
|
|
25
|
+
case error instanceof Updated:
|
|
26
|
+
return error.toResponse();
|
|
27
|
+
case error instanceof Success:
|
|
28
|
+
return error.toResponse();
|
|
29
|
+
case error instanceof NoContent:
|
|
30
|
+
return error.toResponse();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
switch (true) {
|
|
34
|
+
case error instanceof BadGateway:
|
|
35
|
+
return error.toResponse();
|
|
36
|
+
case error instanceof BadRequest:
|
|
37
|
+
return error.toResponse();
|
|
38
|
+
case error instanceof Conflict:
|
|
39
|
+
return error.toResponse();
|
|
40
|
+
case error instanceof Forbidden:
|
|
41
|
+
return error.toResponse();
|
|
42
|
+
case error instanceof NotFound:
|
|
43
|
+
return error.toResponse();
|
|
44
|
+
case error instanceof NotImplemented:
|
|
45
|
+
return error.toResponse();
|
|
46
|
+
case error instanceof ServerError:
|
|
47
|
+
return error.toResponse();
|
|
48
|
+
case error instanceof Unauthorized:
|
|
49
|
+
return error.toResponse();
|
|
50
|
+
case error instanceof UnprocessableEntity:
|
|
51
|
+
return error.toResponse();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { errorHandler };
|