@arkyn/server 3.0.1-beta.3 → 3.0.1-beta.30
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/README.md +368 -65
- package/dist/api/arkynLogRequest.d.ts.map +1 -1
- package/dist/api/arkynLogRequest.js +3 -4
- package/dist/api/makeRequest.d.ts +1 -1
- package/dist/api/makeRequest.d.ts.map +1 -1
- package/dist/api/makeRequest.js +3 -5
- package/dist/config/apiInstance.d.ts +17 -17
- package/dist/config/apiInstance.d.ts.map +1 -1
- package/dist/config/apiInstance.js +15 -15
- package/dist/config/arkynLogInstance.d.ts +2 -2
- package/dist/config/arkynLogInstance.d.ts.map +1 -1
- package/dist/config/arkynLogInstance.js +6 -5
- package/dist/mapper/arkynLogRequestMapper.js +2 -2
- package/dist/services/formParse.d.ts +3 -3
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +8 -5
- package/dist/services/measureRouteExecution.d.ts +3 -0
- package/dist/services/measureRouteExecution.d.ts.map +1 -0
- package/dist/services/measureRouteExecution.js +24 -0
- package/dist/services/schemaValidator.d.ts +3 -3
- package/dist/services/schemaValidator.d.ts.map +1 -1
- package/dist/services/schemaValidator.js +1 -1
- package/package.json +32 -21
- package/src/api/arkynLogRequest.ts +0 -112
- package/src/api/deleteRequest.ts +0 -22
- package/src/api/getRequest.ts +0 -20
- package/src/api/makeRequest.ts +0 -120
- package/src/api/patchRequest.ts +0 -22
- package/src/api/postRequest.ts +0 -22
- package/src/api/putRequest.ts +0 -22
- package/src/config/apiInstance.ts +0 -148
- package/src/config/arkynLogInstance.ts +0 -68
- package/src/http/badResponses/badGateway.ts +0 -63
- package/src/http/badResponses/badRequest.ts +0 -63
- package/src/http/badResponses/conflict.ts +0 -63
- package/src/http/badResponses/forbidden.ts +0 -63
- package/src/http/badResponses/notFound.ts +0 -63
- package/src/http/badResponses/notImplemented.ts +0 -63
- package/src/http/badResponses/serverError.ts +0 -63
- package/src/http/badResponses/unauthorized.ts +0 -63
- package/src/http/badResponses/unprocessableEntity.ts +0 -79
- package/src/http/successResponses/created.ts +0 -64
- package/src/http/successResponses/found.ts +0 -67
- package/src/http/successResponses/noContent.ts +0 -42
- package/src/http/successResponses/success.ts +0 -64
- package/src/http/successResponses/updated.ts +0 -64
- package/src/index.ts +0 -31
- package/src/mapper/arkynLogRequestMapper.ts +0 -73
- package/src/services/decodeErrorMessageFromRequest.ts +0 -36
- package/src/services/decodeRequestBody.ts +0 -43
- package/src/services/errorHandler.ts +0 -99
- package/src/services/formParse.ts +0 -83
- package/src/services/getCaller.ts +0 -82
- package/src/services/getScopedParams.ts +0 -43
- package/src/services/httpDebug.ts +0 -61
- package/src/services/schemaValidator.ts +0 -66
- package/src/types/ApiResponseDTO.ts +0 -19
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -5
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { httpDebug } from "../../services/httpDebug";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Represents an HTTP error response with a status code of 401 (Unauthorized).
|
|
5
|
-
* This class is used to standardize the structure of an "Unauthorized" error response,
|
|
6
|
-
* including the response body, headers, status, and status text.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
class Unauthorized {
|
|
10
|
-
body: any;
|
|
11
|
-
cause?: any;
|
|
12
|
-
status: number = 401;
|
|
13
|
-
statusText: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an instance of the `Unauthorized` class.
|
|
17
|
-
*
|
|
18
|
-
* @param message - A descriptive message explaining why the request is unauthorized.
|
|
19
|
-
* @param cause - Optional additional information about the cause of the error.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
constructor(message: string, cause?: any) {
|
|
23
|
-
this.body = { name: "Unauthorized", message: message };
|
|
24
|
-
this.statusText = message;
|
|
25
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
26
|
-
httpDebug("Unauthorized", this.body, this.cause);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Converts the `Unauthorized` instance into a `Response` object with a JSON body.
|
|
31
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
-
*
|
|
33
|
-
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
toResponse(): Response {
|
|
37
|
-
const responseInit: ResponseInit = {
|
|
38
|
-
headers: { "Content-Type": "application/json" },
|
|
39
|
-
status: this.status,
|
|
40
|
-
statusText: this.statusText,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Converts the `Unauthorized` instance into a `Response` object using the `Response.json` method.
|
|
48
|
-
* This method is an alternative to `toResponse` for generating JSON error responses.
|
|
49
|
-
*
|
|
50
|
-
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
toJson(): Response {
|
|
54
|
-
const responseInit: ResponseInit = {
|
|
55
|
-
status: this.status,
|
|
56
|
-
statusText: this.statusText,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
return Response.json(this.body, responseInit);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export { Unauthorized };
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { httpDebug } from "../../services/httpDebug";
|
|
2
|
-
|
|
3
|
-
type UnprocessableEntityProps = {
|
|
4
|
-
data?: any;
|
|
5
|
-
fieldErrors?: Record<string, string>;
|
|
6
|
-
fields?: Record<string, string>;
|
|
7
|
-
message?: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Represents an HTTP error response with a status code of 422 (Unprocessable Entity).
|
|
12
|
-
* This class is used to standardize the structure of an "Unprocessable Entity" error response,
|
|
13
|
-
* including the response body, headers, status, and status text.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
class UnprocessableEntity {
|
|
17
|
-
body: any;
|
|
18
|
-
status: number = 422;
|
|
19
|
-
statusText: string;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Creates an instance of the `UnprocessableEntity` class.
|
|
23
|
-
*
|
|
24
|
-
* @param props - An object containing details about the error, such as:
|
|
25
|
-
* - `data`: Additional data related to the error.
|
|
26
|
-
* - `fieldErrors`: A record of field-specific error messages.
|
|
27
|
-
* - `fields`: A record of field values that caused the error.
|
|
28
|
-
* - `message`: A descriptive message explaining the error.
|
|
29
|
-
* @param enableDebug - A boolean indicating whether to enable debug logging for this error.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
constructor(props: UnprocessableEntityProps, enableDebug = false) {
|
|
33
|
-
this.statusText = props.message || "Unprocessable Entity";
|
|
34
|
-
this.body = {
|
|
35
|
-
name: "UnprocessableEntity",
|
|
36
|
-
message: props.message || null,
|
|
37
|
-
data: props.data,
|
|
38
|
-
fieldErrors: props.fieldErrors,
|
|
39
|
-
fields: props.fields,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
enableDebug && httpDebug("UnprocessableEntity", this.body);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Converts the `UnprocessableEntity` instance into a `Response` object with a JSON body.
|
|
47
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
48
|
-
*
|
|
49
|
-
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
toResponse(): Response {
|
|
53
|
-
const responseInit: ResponseInit = {
|
|
54
|
-
headers: { "Content-Type": "application/json" },
|
|
55
|
-
status: this.status,
|
|
56
|
-
statusText: this.statusText,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Converts the `UnprocessableEntity` instance into a `Response` object using the `Response.json` method.
|
|
64
|
-
* This method is an alternative to `toResponse` for generating JSON error responses.
|
|
65
|
-
*
|
|
66
|
-
* @returns A `Response` object with the JSON body and response metadata.
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
toJson(): Response {
|
|
70
|
-
const responseInit: ResponseInit = {
|
|
71
|
-
status: this.status,
|
|
72
|
-
statusText: this.statusText,
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
return Response.json(this.body, responseInit);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export { UnprocessableEntity };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a successful HTTP response with a status code of 201 (Created).
|
|
3
|
-
* This class is used to standardize the structure of a "Created" response,
|
|
4
|
-
* including the response body, headers, status, and status text.
|
|
5
|
-
*
|
|
6
|
-
* @template T - The type of the response body.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
class Created<T> {
|
|
10
|
-
body: T;
|
|
11
|
-
headers: ResponseInit["headers"];
|
|
12
|
-
status: number;
|
|
13
|
-
statusText: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an instance of the `Created` class.
|
|
17
|
-
*
|
|
18
|
-
* @param body - The response body to be included in the HTTP response.
|
|
19
|
-
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
constructor(body: T, init?: ResponseInit) {
|
|
23
|
-
this.body = body;
|
|
24
|
-
this.headers = init?.headers || {};
|
|
25
|
-
this.status = init?.status || 201;
|
|
26
|
-
this.statusText = init?.statusText || "Resource created successfully";
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Converts the `Created` instance into a `Response` object with a JSON body.
|
|
31
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
-
*
|
|
33
|
-
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
toResponse(): Response {
|
|
37
|
-
const responseInit: ResponseInit = {
|
|
38
|
-
headers: { "Content-Type": "application/json", ...this.headers },
|
|
39
|
-
status: this.status,
|
|
40
|
-
statusText: this.statusText,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Converts the `Created` instance into a `Response` object using the `Response.json` method.
|
|
48
|
-
* This method is an alternative to `toResponse` for generating JSON responses.
|
|
49
|
-
*
|
|
50
|
-
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
toJson(): Response {
|
|
54
|
-
const responseInit: ResponseInit = {
|
|
55
|
-
headers: this.headers,
|
|
56
|
-
status: this.status,
|
|
57
|
-
statusText: this.statusText,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
return Response.json(this.body, responseInit);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { Created };
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents an HTTP 302 Found response.
|
|
3
|
-
* This class is used to create a standardized HTTP response with a status of 302 (Found),
|
|
4
|
-
* including optional headers and a response body.
|
|
5
|
-
*
|
|
6
|
-
* @template T - The type of the response body.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
class Found<T> {
|
|
10
|
-
body: T;
|
|
11
|
-
headers: ResponseInit["headers"];
|
|
12
|
-
status: number;
|
|
13
|
-
statusText: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an instance of the `Found` class.
|
|
17
|
-
*
|
|
18
|
-
* @param body - The body of the response.
|
|
19
|
-
* @param init - Optional initialization object to set headers, status, and statusText.
|
|
20
|
-
* - `headers`: Additional headers to include in the response.
|
|
21
|
-
* - `status`: HTTP status code (default is 302).
|
|
22
|
-
* - `statusText`: HTTP status text (default is "Found").
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
constructor(body: T, init?: ResponseInit) {
|
|
26
|
-
this.body = body;
|
|
27
|
-
this.headers = init?.headers || {};
|
|
28
|
-
this.status = init?.status || 302;
|
|
29
|
-
this.statusText = init?.statusText || "Found";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Converts the `Found` instance into a `Response` object.
|
|
34
|
-
* This method serializes the response body as JSON and includes the appropriate headers.
|
|
35
|
-
*
|
|
36
|
-
* @returns A `Response` object with the serialized JSON body and the specified headers, status, and statusText.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
toResponse() {
|
|
40
|
-
const responseInit: ResponseInit = {
|
|
41
|
-
headers: { "Content-Type": "application/json", ...this.headers },
|
|
42
|
-
status: this.status,
|
|
43
|
-
statusText: this.statusText,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Converts the `Found` instance into a JSON response using `Response.json`.
|
|
51
|
-
* This method is an alternative to `toResponse` for creating JSON responses.
|
|
52
|
-
*
|
|
53
|
-
* @returns A `Response` object with the JSON body and the specified headers, status, and statusText.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
toJson() {
|
|
57
|
-
const responseInit: ResponseInit = {
|
|
58
|
-
headers: this.headers,
|
|
59
|
-
status: this.status,
|
|
60
|
-
statusText: this.statusText,
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
return Response.json(this.body, responseInit);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export { Found };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a successful HTTP response with a status code of 204 (No Content).
|
|
3
|
-
* This class is used to standardize the structure of a "No Content" response,
|
|
4
|
-
* including headers, status, and status text.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
class NoContent {
|
|
8
|
-
headers: ResponseInit["headers"];
|
|
9
|
-
status: number;
|
|
10
|
-
statusText: string;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Creates an instance of the `NoContent` class.
|
|
14
|
-
*
|
|
15
|
-
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
constructor(init?: ResponseInit) {
|
|
19
|
-
this.headers = init?.headers || {};
|
|
20
|
-
this.status = init?.status || 204;
|
|
21
|
-
this.statusText = init?.statusText ?? "No content";
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Converts the `NoContent` instance into a `Response` object.
|
|
26
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
27
|
-
*
|
|
28
|
-
* @returns A `Response` object with no body and response metadata.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
toResponse(): Response {
|
|
32
|
-
const responseInit: ResponseInit = {
|
|
33
|
-
headers: this.headers,
|
|
34
|
-
status: this.status,
|
|
35
|
-
statusText: this.statusText,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
return new Response(null, responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export { NoContent };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a successful HTTP response with a status code of 200 (OK).
|
|
3
|
-
* This class is used to standardize the structure of a "Success" response,
|
|
4
|
-
* including the response body, headers, status, and status text.
|
|
5
|
-
*
|
|
6
|
-
* @template T - The type of the response body.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
class Success<T> {
|
|
10
|
-
body: T;
|
|
11
|
-
headers: ResponseInit["headers"];
|
|
12
|
-
status: number;
|
|
13
|
-
statusText: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an instance of the `Success` class.
|
|
17
|
-
*
|
|
18
|
-
* @param body - The response body to be included in the HTTP response.
|
|
19
|
-
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
constructor(body: T, init?: ResponseInit) {
|
|
23
|
-
this.body = body;
|
|
24
|
-
this.headers = init?.headers || {};
|
|
25
|
-
this.status = init?.status || 200;
|
|
26
|
-
this.statusText = init?.statusText ?? "OK";
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Converts the `Success` instance into a `Response` object with a JSON body.
|
|
31
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
-
*
|
|
33
|
-
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
toResponse(): Response {
|
|
37
|
-
const responseInit: ResponseInit = {
|
|
38
|
-
headers: { "Content-Type": "application/json", ...this.headers },
|
|
39
|
-
status: this.status,
|
|
40
|
-
statusText: this.statusText,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Converts the `Success` instance into a `Response` object using the `Response.json` method.
|
|
48
|
-
* This method is an alternative to `toResponse` for generating JSON responses.
|
|
49
|
-
*
|
|
50
|
-
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
toJson(): Response {
|
|
54
|
-
const responseInit: ResponseInit = {
|
|
55
|
-
headers: this.headers,
|
|
56
|
-
status: this.status,
|
|
57
|
-
statusText: this.statusText,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
return Response.json(this.body, responseInit);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { Success };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a successful HTTP response with a status code of 200 (OK) or other custom status codes.
|
|
3
|
-
* This class is used to standardize the structure of an "Updated" response,
|
|
4
|
-
* including the response body, headers, status, and status text.
|
|
5
|
-
*
|
|
6
|
-
* @template T - The type of the response body.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
class Updated<T> {
|
|
10
|
-
body: T;
|
|
11
|
-
headers: ResponseInit["headers"];
|
|
12
|
-
status: number;
|
|
13
|
-
statusText: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an instance of the `Updated` class.
|
|
17
|
-
*
|
|
18
|
-
* @param body - The response body to be included in the HTTP response.
|
|
19
|
-
* @param init - Optional initialization object for customizing headers, status, and status text.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
constructor(body: T, init?: ResponseInit) {
|
|
23
|
-
this.body = body;
|
|
24
|
-
this.headers = init?.headers || {};
|
|
25
|
-
this.status = init?.status || 200;
|
|
26
|
-
this.statusText = init?.statusText || "Resource updated successfully";
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Converts the `Updated` instance into a `Response` object with a JSON body.
|
|
31
|
-
* This method ensures the response has the appropriate headers, status, and status text.
|
|
32
|
-
*
|
|
33
|
-
* @returns A `Response` object with the serialized JSON body and response metadata.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
toResponse(): Response {
|
|
37
|
-
const responseInit: ResponseInit = {
|
|
38
|
-
headers: { "Content-Type": "application/json", ...this.headers },
|
|
39
|
-
status: this.status,
|
|
40
|
-
statusText: this.statusText,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Converts the `Updated` instance into a `Response` object using the `Response.json` method.
|
|
48
|
-
* This method is an alternative to `toResponse` for generating JSON responses.
|
|
49
|
-
*
|
|
50
|
-
* @returns A `Response` object with the JSON body and response metadata.
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
toJson(): Response {
|
|
54
|
-
const responseInit: ResponseInit = {
|
|
55
|
-
headers: this.headers,
|
|
56
|
-
status: this.status,
|
|
57
|
-
statusText: this.statusText,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
return Response.json(this.body, responseInit);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { Updated };
|
package/src/index.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// config
|
|
2
|
-
export { ApiInstance } from "./config/apiInstance";
|
|
3
|
-
export { ArkynLogInstance } from "./config/arkynLogInstance";
|
|
4
|
-
|
|
5
|
-
// http bad responses
|
|
6
|
-
export { BadGateway } from "./http/badResponses/badGateway";
|
|
7
|
-
export { BadRequest } from "./http/badResponses/badRequest";
|
|
8
|
-
export { Conflict } from "./http/badResponses/conflict";
|
|
9
|
-
export { Forbidden } from "./http/badResponses/forbidden";
|
|
10
|
-
export { NotFound } from "./http/badResponses/notFound";
|
|
11
|
-
export { NotImplemented } from "./http/badResponses/notImplemented";
|
|
12
|
-
export { ServerError } from "./http/badResponses/serverError";
|
|
13
|
-
export { Unauthorized } from "./http/badResponses/unauthorized";
|
|
14
|
-
export { UnprocessableEntity } from "./http/badResponses/unprocessableEntity";
|
|
15
|
-
|
|
16
|
-
// http success responses
|
|
17
|
-
export { Created } from "./http/successResponses/created";
|
|
18
|
-
export { Found } from "./http/successResponses/found";
|
|
19
|
-
export { NoContent } from "./http/successResponses/noContent";
|
|
20
|
-
export { Success } from "./http/successResponses/success";
|
|
21
|
-
export { Updated } from "./http/successResponses/updated";
|
|
22
|
-
|
|
23
|
-
// services
|
|
24
|
-
export { decodeErrorMessageFromRequest } from "./services/decodeErrorMessageFromRequest";
|
|
25
|
-
export { decodeRequestBody } from "./services/decodeRequestBody";
|
|
26
|
-
export { errorHandler } from "./services/errorHandler";
|
|
27
|
-
export { formParse } from "./services/formParse";
|
|
28
|
-
export { getCaller } from "./services/getCaller";
|
|
29
|
-
export { getScopedParams } from "./services/getScopedParams";
|
|
30
|
-
export { httpDebug } from "./services/httpDebug";
|
|
31
|
-
export { SchemaValidator } from "./services/schemaValidator";
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
type InputProps = {
|
|
2
|
-
status: number;
|
|
3
|
-
url: string;
|
|
4
|
-
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
5
|
-
requestHeaders: HeadersInit;
|
|
6
|
-
responseHeaders: HeadersInit;
|
|
7
|
-
requestBody: any;
|
|
8
|
-
elapsedTime: number;
|
|
9
|
-
responseBody: any;
|
|
10
|
-
queryParams: URLSearchParams;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
type OutputProps = {
|
|
14
|
-
rawUrl: string;
|
|
15
|
-
status: number;
|
|
16
|
-
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
17
|
-
token: string | null;
|
|
18
|
-
elapsedTime: number;
|
|
19
|
-
requestHeaders: Record<string, string>;
|
|
20
|
-
requestBody: Record<string, string>;
|
|
21
|
-
queryParams: Record<string, string>;
|
|
22
|
-
responseHeaders: Record<string, string>;
|
|
23
|
-
responseBody: any;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
class ArkynLogRequestMapper {
|
|
27
|
-
private static mapHeaders(headers: HeadersInit): Record<string, string> {
|
|
28
|
-
if (headers instanceof Headers) {
|
|
29
|
-
return Object.fromEntries(headers.entries());
|
|
30
|
-
} else if (typeof headers === "object") {
|
|
31
|
-
return Object.entries(headers).reduce((acc, [key, value]) => {
|
|
32
|
-
if (typeof value === "string") {
|
|
33
|
-
acc[key] = value;
|
|
34
|
-
} else if (Array.isArray(value)) {
|
|
35
|
-
acc[key] = value.join(", ");
|
|
36
|
-
} else {
|
|
37
|
-
acc[key] = JSON.stringify(value);
|
|
38
|
-
}
|
|
39
|
-
return acc;
|
|
40
|
-
}, {} as Record<string, string>);
|
|
41
|
-
}
|
|
42
|
-
return {};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private static mapQueryParams(
|
|
46
|
-
queryParams: URLSearchParams
|
|
47
|
-
): Record<string, string> {
|
|
48
|
-
const params: Record<string, string> = {};
|
|
49
|
-
|
|
50
|
-
queryParams.forEach((value, key) => {
|
|
51
|
-
params[key] = value;
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return params;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
static handle(props: InputProps): OutputProps {
|
|
58
|
-
return {
|
|
59
|
-
rawUrl: props.url,
|
|
60
|
-
status: props.status,
|
|
61
|
-
method: props.method,
|
|
62
|
-
token: null,
|
|
63
|
-
elapsedTime: props.elapsedTime,
|
|
64
|
-
requestHeaders: this.mapHeaders(props.requestHeaders),
|
|
65
|
-
requestBody: props.requestBody,
|
|
66
|
-
queryParams: this.mapQueryParams(props.queryParams),
|
|
67
|
-
responseHeaders: this.mapHeaders(props.responseHeaders),
|
|
68
|
-
responseBody: props.responseBody,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export { ArkynLogRequestMapper };
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Decodes an error message from a given request data object or response object.
|
|
3
|
-
*
|
|
4
|
-
* This function attempts to extract a meaningful error message from the provided
|
|
5
|
-
* `data` or `response` objects by checking various properties in a specific order.
|
|
6
|
-
* If no valid error message is found, it returns a default message: "Missing error message".
|
|
7
|
-
*
|
|
8
|
-
* @param data - The data object that may contain error information. It can have properties
|
|
9
|
-
* such as `message`, `error`, or `error.message` that are checked for a string value.
|
|
10
|
-
* @param response - The response object that may contain a `statusText` property
|
|
11
|
-
* representing the HTTP status text.
|
|
12
|
-
* @returns A string representing the decoded error message, or a default message
|
|
13
|
-
* if no error message is found.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
function decodeErrorMessageFromRequest(data: any, response: Response): string {
|
|
17
|
-
if (data?.message && typeof data?.message === "string") {
|
|
18
|
-
return data?.message;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (data?.error && typeof data?.error === "string") {
|
|
22
|
-
return data?.error;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (data?.error?.message && typeof data?.error?.message === "string") {
|
|
26
|
-
return data?.error?.message;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (response?.statusText && typeof response?.statusText === "string") {
|
|
30
|
-
return response?.statusText;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return "Missing error message";
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export { decodeErrorMessageFromRequest };
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { BadRequest } from "../http/badResponses/badRequest";
|
|
2
|
-
|
|
3
|
-
type DecodeRequestBodyFunction = (request: Request) => Promise<any>;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Decodes the body of an incoming request into a JavaScript object.
|
|
7
|
-
*
|
|
8
|
-
* This function attempts to parse the request body in the following order:
|
|
9
|
-
* 1. Tries to parse the body as JSON.
|
|
10
|
-
* 2. If JSON parsing fails, attempts to parse the body as URL-encoded form data.
|
|
11
|
-
* 3. If both parsing attempts fail, logs the errors and returns an empty object.
|
|
12
|
-
*
|
|
13
|
-
* @param req - The incoming request object containing the body to decode.
|
|
14
|
-
* @returns A promise that resolves to the decoded data as a JavaScript object.
|
|
15
|
-
*
|
|
16
|
-
* @throws Logs errors to the console if the request body cannot be read or parsed.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const decodeRequestBody: DecodeRequestBodyFunction = async (req) => {
|
|
20
|
-
let data: any;
|
|
21
|
-
|
|
22
|
-
const arrayBuffer = await req.arrayBuffer();
|
|
23
|
-
const text = new TextDecoder().decode(arrayBuffer);
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
data = JSON.parse(text);
|
|
27
|
-
} catch (jsonError) {
|
|
28
|
-
try {
|
|
29
|
-
if (text.includes("=")) {
|
|
30
|
-
const formData = new URLSearchParams(text);
|
|
31
|
-
data = Object.fromEntries(formData.entries());
|
|
32
|
-
} else {
|
|
33
|
-
throw new BadRequest("Invalid URLSearchParams format");
|
|
34
|
-
}
|
|
35
|
-
} catch (formDataError) {
|
|
36
|
-
throw new BadRequest("Failed to extract data from request");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return data;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export { decodeRequestBody };
|