@arkyn/server 3.0.1-beta.154 → 3.0.1-beta.156
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/index.js +1301 -37
- package/dist/modules/http/api/_logRequest.js +69 -0
- package/dist/modules/http/api/_makeRequest.js +69 -0
- package/dist/modules/http/api/deleteRequest.js +13 -0
- package/dist/modules/http/api/getRequest.js +12 -0
- package/dist/modules/http/api/patchRequest.js +13 -0
- package/dist/modules/http/api/postRequest.js +13 -0
- package/dist/modules/http/api/putRequest.js +13 -0
- package/dist/modules/http/badResponses/_badResponse.js +59 -0
- package/dist/modules/http/badResponses/badGateway.js +30 -0
- package/dist/modules/http/badResponses/badRequest.js +30 -0
- package/dist/modules/http/badResponses/conflict.js +30 -0
- package/dist/modules/http/badResponses/forbidden.js +30 -0
- package/dist/modules/http/badResponses/notFound.js +30 -0
- package/dist/modules/http/badResponses/notImplemented.js +30 -0
- package/dist/modules/http/badResponses/serverError.js +30 -0
- package/dist/modules/http/badResponses/unauthorized.js +30 -0
- package/dist/modules/http/badResponses/unprocessableEntity.js +36 -0
- package/dist/modules/http/successResponses/_successResponse.js +69 -0
- package/dist/modules/http/successResponses/created.js +30 -0
- package/dist/modules/http/successResponses/found.js +30 -0
- package/dist/modules/http/successResponses/noContent.js +21 -0
- package/dist/modules/http/successResponses/success.js +30 -0
- package/dist/modules/http/successResponses/updated.js +30 -0
- package/dist/modules/index.js +68 -0
- package/dist/modules/services/apiService.js +129 -0
- package/dist/modules/services/debugService.js +66 -0
- package/dist/modules/services/logMapperService.js +52 -0
- package/dist/modules/services/logService.js +31 -0
- package/dist/modules/utilities/decodeRequestBody.js +22 -0
- package/dist/modules/utilities/decodeRequestErrorMessage.js +7 -0
- package/dist/modules/utilities/errorHandler.js +54 -0
- package/dist/modules/utilities/flushDebugLogs.js +20 -0
- package/dist/modules/utilities/formAsyncParse.js +19 -0
- package/dist/modules/utilities/formParse.js +19 -0
- package/dist/modules/utilities/getScopedParams.js +11 -0
- package/dist/modules/utilities/schemaValidator.js +106 -0
- package/dist/modules/validations/validateCep.js +9 -0
- package/dist/modules/validations/validateCnpj.js +31 -0
- package/dist/modules/validations/validateCpf.js +28 -0
- package/dist/modules/validations/validateDate.js +27 -0
- package/dist/modules/validations/validateEmail.js +53 -0
- package/dist/modules/validations/validatePassword.js +16 -0
- package/dist/modules/validations/validatePhone.js +10 -0
- package/dist/modules/validations/validateRg.js +8 -0
- package/package.json +121 -87
- package/dist/bundle.js +0 -3014
- package/dist/bundle.umd.cjs +0 -9
- package/dist/http/api/_logRequest.js +0 -109
- package/dist/http/api/_makeRequest.js +0 -116
- package/dist/http/api/deleteRequest.js +0 -18
- package/dist/http/api/getRequest.js +0 -17
- package/dist/http/api/patchRequest.js +0 -18
- package/dist/http/api/postRequest.js +0 -18
- package/dist/http/api/putRequest.js +0 -18
- package/dist/http/badResponses/_badResponse.js +0 -62
- package/dist/http/badResponses/badGateway.js +0 -43
- package/dist/http/badResponses/badRequest.js +0 -41
- package/dist/http/badResponses/conflict.js +0 -42
- package/dist/http/badResponses/forbidden.js +0 -41
- package/dist/http/badResponses/notFound.js +0 -41
- package/dist/http/badResponses/notImplemented.js +0 -41
- package/dist/http/badResponses/serverError.js +0 -41
- package/dist/http/badResponses/unauthorized.js +0 -42
- package/dist/http/badResponses/unprocessableEntity.js +0 -53
- package/dist/http/successResponses/_successResponse.js +0 -75
- package/dist/http/successResponses/created.js +0 -41
- package/dist/http/successResponses/found.js +0 -41
- package/dist/http/successResponses/noContent.js +0 -32
- package/dist/http/successResponses/success.js +0 -41
- package/dist/http/successResponses/updated.js +0 -42
- package/dist/services/apiService.js +0 -174
- package/dist/services/debugService.js +0 -82
- package/dist/services/logMapperService.js +0 -68
- package/dist/services/logService.js +0 -35
- package/dist/utilities/decodeRequestBody.js +0 -40
- package/dist/utilities/decodeRequestErrorMessage.js +0 -36
- package/dist/utilities/errorHandler.js +0 -74
- package/dist/utilities/flushDebugLogs.js +0 -39
- package/dist/utilities/formAsyncParse.js +0 -37
- package/dist/utilities/formParse.js +0 -37
- package/dist/utilities/getScopedParams.js +0 -26
- package/dist/utilities/schemaValidator.js +0 -119
- package/dist/validations/validateCep.js +0 -27
- package/dist/validations/validateCnpj.js +0 -59
- package/dist/validations/validateCpf.js +0 -54
- package/dist/validations/validateDate.js +0 -51
- package/dist/validations/validateEmail.js +0 -111
- package/dist/validations/validatePassword.js +0 -34
- package/dist/validations/validatePhone.js +0 -28
- package/dist/validations/validateRg.js +0 -31
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { DebugService } from "../../services/debugService";
|
|
2
|
-
import { flushDebugLogs } from "../../utilities/flushDebugLogs";
|
|
3
|
-
/**
|
|
4
|
-
* Base class for handling successful HTTP responses with debugging capabilities.
|
|
5
|
-
* Provides logging functionality to track response and their context.
|
|
6
|
-
*/
|
|
7
|
-
class SuccessResponse {
|
|
8
|
-
_body = null;
|
|
9
|
-
_name = "SuccessResponse";
|
|
10
|
-
_status = 200;
|
|
11
|
-
_statusText = "OK";
|
|
12
|
-
_debugColor = "green";
|
|
13
|
-
get body() {
|
|
14
|
-
return this._body;
|
|
15
|
-
}
|
|
16
|
-
set body(value) {
|
|
17
|
-
this._body = value ?? null;
|
|
18
|
-
}
|
|
19
|
-
get name() {
|
|
20
|
-
return this._name;
|
|
21
|
-
}
|
|
22
|
-
set name(value) {
|
|
23
|
-
this._name = value;
|
|
24
|
-
}
|
|
25
|
-
get status() {
|
|
26
|
-
return this._status;
|
|
27
|
-
}
|
|
28
|
-
set status(value) {
|
|
29
|
-
this._status = value;
|
|
30
|
-
}
|
|
31
|
-
get statusText() {
|
|
32
|
-
return this._statusText;
|
|
33
|
-
}
|
|
34
|
-
set statusText(value) {
|
|
35
|
-
this._statusText = value;
|
|
36
|
-
}
|
|
37
|
-
get debugColor() {
|
|
38
|
-
return this._debugColor;
|
|
39
|
-
}
|
|
40
|
-
set debugColor(value) {
|
|
41
|
-
if (!["green", "yellow", "cyan", "red"].includes(value))
|
|
42
|
-
return;
|
|
43
|
-
this._debugColor = value;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Logs debug information for success responses including caller context and response details.
|
|
47
|
-
*
|
|
48
|
-
* @param {any} body - The response body or success data to be logged
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* const SuccessResponse = new SuccessResponse();
|
|
53
|
-
* SuccessResponse.onDebug({ data: "Operation completed successfully" });
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
onDebug(body) {
|
|
57
|
-
const debugs = [];
|
|
58
|
-
const { callerInfo, functionName } = DebugService.getCaller();
|
|
59
|
-
debugs.push(`Caller Function: ${functionName}`);
|
|
60
|
-
debugs.push(`Caller Location: ${callerInfo}`);
|
|
61
|
-
if (this.statusText)
|
|
62
|
-
debugs.push(`Message: ${this.statusText}`);
|
|
63
|
-
if (body)
|
|
64
|
-
debugs.push(`Body: ${JSON.stringify(body)}`);
|
|
65
|
-
flushDebugLogs({ scheme: this._debugColor, name: this.name, debugs });
|
|
66
|
-
}
|
|
67
|
-
makeBody() {
|
|
68
|
-
return {
|
|
69
|
-
name: this.name,
|
|
70
|
-
message: this.statusText,
|
|
71
|
-
body: this.body,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
export { SuccessResponse };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { SuccessResponse } from "./_successResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 201 Created — the request succeeded and a new resource was created.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* return new Created("User created successfully", { id: user.id }).toJson();
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Created extends SuccessResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Description included in the response status text.
|
|
13
|
-
* @param body - Data to include in the response body.
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, body) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Created";
|
|
18
|
-
this.status = 201;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.body = body || undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.body, responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { Created };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { SuccessResponse } from "./_successResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 302 Found — the resource was located and the response includes it in the body.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* return new Found("Products retrieved", { products }).toJson();
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Found extends SuccessResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Description included in the response status text.
|
|
13
|
-
* @param body - Data to include in the response body.
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, body) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Found";
|
|
18
|
-
this.status = 302;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.body = body || undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.body, responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { Found };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { SuccessResponse } from "./_successResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 204 No Content — the request succeeded but there is no content to return.
|
|
4
|
-
* Typically used for delete or update operations where a body is not needed.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* return new NoContent("Record deleted").toResponse();
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
class NoContent extends SuccessResponse {
|
|
12
|
-
/**
|
|
13
|
-
* @param message - Description included in the response status text.
|
|
14
|
-
*/
|
|
15
|
-
constructor(message) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "NoContent";
|
|
18
|
-
this.status = 204;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.onDebug();
|
|
21
|
-
}
|
|
22
|
-
/** Converts to a `Response` with `Content-Type: application/json` header and no body. */
|
|
23
|
-
toResponse() {
|
|
24
|
-
const responseInit = {
|
|
25
|
-
headers: { "Content-Type": "application/json" },
|
|
26
|
-
status: this.status,
|
|
27
|
-
statusText: this.statusText,
|
|
28
|
-
};
|
|
29
|
-
return new Response(null, responseInit);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export { NoContent };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { SuccessResponse } from "./_successResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 200 OK — the request succeeded and the response body contains the result.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* return new Success("Order fetched", { order }).toJson();
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Success extends SuccessResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Description included in the response status text.
|
|
13
|
-
* @param body - Data to include in the response body.
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, body) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Success";
|
|
18
|
-
this.status = 200;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.body = body || undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.body, responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { Success };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { SuccessResponse } from "./_successResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 200 OK — the request succeeded and the resource was updated.
|
|
4
|
-
* Semantically equivalent to `Success` but signals an update operation to consumers.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* return new Updated("Profile updated", { user }).toJson();
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
class Updated extends SuccessResponse {
|
|
12
|
-
/**
|
|
13
|
-
* @param message - Description included in the response status text.
|
|
14
|
-
* @param body - Data to include in the response body.
|
|
15
|
-
*/
|
|
16
|
-
constructor(message, body) {
|
|
17
|
-
super();
|
|
18
|
-
this.name = "Updated";
|
|
19
|
-
this.status = 200;
|
|
20
|
-
this.statusText = message;
|
|
21
|
-
this.body = body || undefined;
|
|
22
|
-
this.onDebug();
|
|
23
|
-
}
|
|
24
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
25
|
-
toResponse() {
|
|
26
|
-
const responseInit = {
|
|
27
|
-
headers: { "Content-Type": "application/json" },
|
|
28
|
-
status: this.status,
|
|
29
|
-
statusText: this.statusText,
|
|
30
|
-
};
|
|
31
|
-
return new Response(JSON.stringify(this.body), responseInit);
|
|
32
|
-
}
|
|
33
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
34
|
-
toJson() {
|
|
35
|
-
const responseInit = {
|
|
36
|
-
status: this.status,
|
|
37
|
-
statusText: this.statusText,
|
|
38
|
-
};
|
|
39
|
-
return Response.json(this.body, responseInit);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
export { Updated };
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { deleteRequest } from "../http/api/deleteRequest";
|
|
2
|
-
import { getRequest } from "../http/api/getRequest";
|
|
3
|
-
import { patchRequest } from "../http/api/patchRequest";
|
|
4
|
-
import { postRequest } from "../http/api/postRequest";
|
|
5
|
-
import { putRequest } from "../http/api/putRequest";
|
|
6
|
-
import { flushDebugLogs } from "../utilities/flushDebugLogs";
|
|
7
|
-
/**
|
|
8
|
-
* HTTP client for external API calls. Wraps fetch with base URL, default headers, optional auth token,
|
|
9
|
-
* and per-request overrides for all standard HTTP methods.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const api = new ApiService({
|
|
14
|
-
* baseUrl: "https://api.example.com",
|
|
15
|
-
* baseToken: session.token,
|
|
16
|
-
* enableDebug: true,
|
|
17
|
-
* });
|
|
18
|
-
*
|
|
19
|
-
* const { data } = await api.get("/users/me");
|
|
20
|
-
* const { data: created } = await api.post("/orders", { body: { productId: 1 } });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
class ApiService {
|
|
24
|
-
baseUrl;
|
|
25
|
-
baseHeaders;
|
|
26
|
-
baseToken;
|
|
27
|
-
enableDebug;
|
|
28
|
-
constructor(props) {
|
|
29
|
-
this.baseUrl = props.baseUrl;
|
|
30
|
-
this.baseHeaders = props.baseHeaders || undefined;
|
|
31
|
-
this.baseToken = props.baseToken || undefined;
|
|
32
|
-
this.enableDebug = props.enableDebug || false;
|
|
33
|
-
}
|
|
34
|
-
onDebug(endpoint, method, data) {
|
|
35
|
-
if (this.enableDebug) {
|
|
36
|
-
const debugs = [];
|
|
37
|
-
const json = (data) => JSON.stringify(data, null, 2);
|
|
38
|
-
debugs.push(`Base URL: ${this.baseUrl}`);
|
|
39
|
-
debugs.push(`Endpoint: ${endpoint}`);
|
|
40
|
-
debugs.push(`Status/Method: ${method} => ${data.status}`);
|
|
41
|
-
debugs.push(`Message: ${data.message}`);
|
|
42
|
-
if (data.headers)
|
|
43
|
-
debugs.push(`Headers: ${json(data.headers)}`);
|
|
44
|
-
if (data.body)
|
|
45
|
-
debugs.push(`Body: ${json(data.body)}`);
|
|
46
|
-
flushDebugLogs({ debugs, name: "ApiDebug", scheme: "yellow" });
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
generateHeaders(initHeaders, token) {
|
|
50
|
-
let headers = {};
|
|
51
|
-
if (this.baseToken)
|
|
52
|
-
headers = { Authorization: `Bearer ${this.baseToken}` };
|
|
53
|
-
if (this.baseHeaders)
|
|
54
|
-
headers = { ...headers, ...this.baseHeaders };
|
|
55
|
-
if (initHeaders)
|
|
56
|
-
headers = { ...headers, ...initHeaders };
|
|
57
|
-
if (token)
|
|
58
|
-
headers = { ...headers, Authorization: `Bearer ${token}` };
|
|
59
|
-
return headers;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Sends a get request to the specified endpoint.
|
|
63
|
-
* @param endpoint - The API endpoint to send the get request to.
|
|
64
|
-
* @param data - The request data, including optional headers and token.
|
|
65
|
-
* @returns The API response data.
|
|
66
|
-
*/
|
|
67
|
-
async get(endpoint, data) {
|
|
68
|
-
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
69
|
-
const response = await getRequest({
|
|
70
|
-
url: this.baseUrl + endpoint,
|
|
71
|
-
urlParams: data?.urlParams || {},
|
|
72
|
-
headers,
|
|
73
|
-
});
|
|
74
|
-
this.onDebug(endpoint, "get", {
|
|
75
|
-
headers,
|
|
76
|
-
message: response.message,
|
|
77
|
-
status: response.status,
|
|
78
|
-
});
|
|
79
|
-
return response;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Sends a post request to the specified endpoint.
|
|
83
|
-
* @param endpoint - The API endpoint to send the post request to.
|
|
84
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
85
|
-
* @returns The API response data.
|
|
86
|
-
*/
|
|
87
|
-
async post(endpoint, data) {
|
|
88
|
-
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
89
|
-
const body = data?.body;
|
|
90
|
-
const response = await postRequest({
|
|
91
|
-
url: this.baseUrl + endpoint,
|
|
92
|
-
urlParams: data?.urlParams || {},
|
|
93
|
-
headers,
|
|
94
|
-
body,
|
|
95
|
-
});
|
|
96
|
-
this.onDebug(endpoint, "post", {
|
|
97
|
-
headers,
|
|
98
|
-
body,
|
|
99
|
-
message: response.message,
|
|
100
|
-
status: response.status,
|
|
101
|
-
});
|
|
102
|
-
return response;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Sends a put request to the specified endpoint.
|
|
106
|
-
* @param endpoint - The API endpoint to send the put request to.
|
|
107
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
108
|
-
* @returns The API response data.
|
|
109
|
-
*/
|
|
110
|
-
async put(endpoint, data) {
|
|
111
|
-
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
112
|
-
const body = data?.body;
|
|
113
|
-
const response = await putRequest({
|
|
114
|
-
url: this.baseUrl + endpoint,
|
|
115
|
-
urlParams: data?.urlParams || {},
|
|
116
|
-
headers,
|
|
117
|
-
body,
|
|
118
|
-
});
|
|
119
|
-
this.onDebug(endpoint, "put", {
|
|
120
|
-
headers,
|
|
121
|
-
body,
|
|
122
|
-
message: response.message,
|
|
123
|
-
status: response.status,
|
|
124
|
-
});
|
|
125
|
-
return response;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Sends a patch request to the specified endpoint.
|
|
129
|
-
* @param endpoint - The API endpoint to send the patch request to.
|
|
130
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
131
|
-
* @returns The API response data.
|
|
132
|
-
*/
|
|
133
|
-
async patch(endpoint, data) {
|
|
134
|
-
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
135
|
-
const body = data?.body;
|
|
136
|
-
const response = await patchRequest({
|
|
137
|
-
url: this.baseUrl + endpoint,
|
|
138
|
-
urlParams: data?.urlParams || {},
|
|
139
|
-
headers,
|
|
140
|
-
body,
|
|
141
|
-
});
|
|
142
|
-
this.onDebug(endpoint, "patch", {
|
|
143
|
-
headers,
|
|
144
|
-
body,
|
|
145
|
-
message: response.message,
|
|
146
|
-
status: response.status,
|
|
147
|
-
});
|
|
148
|
-
return response;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Sends a delete request to the specified endpoint.
|
|
152
|
-
* @param endpoint - The API endpoint to send the delete request to.
|
|
153
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
154
|
-
* @returns The API response data.
|
|
155
|
-
*/
|
|
156
|
-
async delete(endpoint, data) {
|
|
157
|
-
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
158
|
-
const body = data?.body;
|
|
159
|
-
const response = await deleteRequest({
|
|
160
|
-
url: this.baseUrl + endpoint,
|
|
161
|
-
urlParams: data?.urlParams || {},
|
|
162
|
-
headers,
|
|
163
|
-
body,
|
|
164
|
-
});
|
|
165
|
-
this.onDebug(endpoint, "delete", {
|
|
166
|
-
headers,
|
|
167
|
-
body,
|
|
168
|
-
message: response.message,
|
|
169
|
-
status: response.status,
|
|
170
|
-
});
|
|
171
|
-
return response;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
export { ApiService };
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
/**
|
|
3
|
-
* Manages stack-trace configuration for debug output, allowing specific adapter/wrapper files
|
|
4
|
-
* to be skipped so logs show the actual business-logic caller.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* // Skip httpAdapter.ts so debug output shows the calling route instead
|
|
9
|
-
* DebugService.setIgnoreFile("httpAdapter.ts");
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
class DebugService {
|
|
13
|
-
/**
|
|
14
|
-
* The name of the file to ignore when analyzing the stack trace.
|
|
15
|
-
* When set, the `getCaller` function will skip stack frames containing this file name.
|
|
16
|
-
*/
|
|
17
|
-
static ignoreFiles = [];
|
|
18
|
-
/**
|
|
19
|
-
* Adds a file name to the ignore list so it is skipped when resolving the caller in stack traces.
|
|
20
|
-
*
|
|
21
|
-
* @param file - File name to ignore (e.g. `"httpAdapter.ts"`).
|
|
22
|
-
*/
|
|
23
|
-
static setIgnoreFile(file) {
|
|
24
|
-
this.ignoreFiles.push(file);
|
|
25
|
-
}
|
|
26
|
-
/** Resets the ignore list, restoring full stack trace analysis. */
|
|
27
|
-
static clearIgnoreFiles() {
|
|
28
|
-
this.ignoreFiles = [];
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Resolves the file path and function name of the code that triggered the current debug call,
|
|
32
|
-
* skipping internal node modules and any files registered with `setIgnoreFile`.
|
|
33
|
-
*
|
|
34
|
-
* @returns `{ functionName, callerInfo }` — caller function name and file path relative to `process.cwd()`.
|
|
35
|
-
*/
|
|
36
|
-
static getCaller() {
|
|
37
|
-
const projectRoot = process.cwd();
|
|
38
|
-
const err = new Error();
|
|
39
|
-
const stack = err.stack || "";
|
|
40
|
-
const stackLines = stack.split("\n").map((line) => line.trim());
|
|
41
|
-
let callerIndex = 2;
|
|
42
|
-
while (callerIndex < stackLines.length &&
|
|
43
|
-
(stackLines[callerIndex].includes("node:internal") ||
|
|
44
|
-
stackLines[callerIndex].includes("/node_modules/"))) {
|
|
45
|
-
callerIndex++;
|
|
46
|
-
}
|
|
47
|
-
if (this.ignoreFiles.length > 0) {
|
|
48
|
-
while (callerIndex < stackLines.length &&
|
|
49
|
-
this.ignoreFiles.some((ignoreFile) => stackLines[callerIndex].includes(ignoreFile))) {
|
|
50
|
-
callerIndex++;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const callerLine = stackLines[callerIndex] || "";
|
|
54
|
-
let functionName = "Unknown function";
|
|
55
|
-
let callerInfo = "Unknown caller";
|
|
56
|
-
const namedFunctionMatch = callerLine.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);
|
|
57
|
-
if (namedFunctionMatch) {
|
|
58
|
-
functionName = namedFunctionMatch[1];
|
|
59
|
-
callerInfo = namedFunctionMatch[2];
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
const anonymousFunctionMatch = callerLine.match(/at\s+(.+)/);
|
|
63
|
-
if (anonymousFunctionMatch) {
|
|
64
|
-
callerInfo = anonymousFunctionMatch[1];
|
|
65
|
-
const objectMethodMatch = callerInfo.match(/at\s+([^(\s]+)\s+/);
|
|
66
|
-
if (objectMethodMatch && objectMethodMatch[1] !== "new") {
|
|
67
|
-
functionName = objectMethodMatch[1];
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if (callerInfo.includes("(")) {
|
|
72
|
-
callerInfo = callerInfo.substring(callerInfo.indexOf("(") + 1, callerInfo.lastIndexOf(")"));
|
|
73
|
-
}
|
|
74
|
-
callerInfo = callerInfo.split(":").slice(0, -2).join(":");
|
|
75
|
-
try {
|
|
76
|
-
callerInfo = path.relative(projectRoot, callerInfo);
|
|
77
|
-
}
|
|
78
|
-
catch (e) { }
|
|
79
|
-
return { functionName, callerInfo };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
export { DebugService };
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Static service for mapping HTTP request/response data into a standardized log format.
|
|
3
|
-
*
|
|
4
|
-
* Provides utilities to transform headers and query parameters into plain objects,
|
|
5
|
-
* and produces a unified output structure for logging purposes.
|
|
6
|
-
*/
|
|
7
|
-
class LogMapperService {
|
|
8
|
-
/**
|
|
9
|
-
* Converts various header formats into a plain key-value object.
|
|
10
|
-
*
|
|
11
|
-
* @param {HeadersInit} headers - The headers to map.
|
|
12
|
-
* @returns {Record<string, string>} A plain object with header key-value pairs.
|
|
13
|
-
* @private
|
|
14
|
-
*/
|
|
15
|
-
static mapHeaders(headers) {
|
|
16
|
-
if (headers instanceof Headers) {
|
|
17
|
-
return Object.fromEntries(headers.entries());
|
|
18
|
-
}
|
|
19
|
-
else if (typeof headers === "object") {
|
|
20
|
-
return Object.entries(headers).reduce((acc, [key, value]) => {
|
|
21
|
-
if (typeof value === "string")
|
|
22
|
-
acc[key] = value;
|
|
23
|
-
else if (Array.isArray(value))
|
|
24
|
-
acc[key] = value.join(", ");
|
|
25
|
-
else
|
|
26
|
-
acc[key] = JSON.stringify(value);
|
|
27
|
-
return acc;
|
|
28
|
-
}, {});
|
|
29
|
-
}
|
|
30
|
-
return {};
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Converts URLSearchParams into a plain key-value object.
|
|
34
|
-
*
|
|
35
|
-
* @param {URLSearchParams} queryParams - The query parameters to map.
|
|
36
|
-
* @returns {Record<string, string>} A plain object with query parameter key-value pairs.
|
|
37
|
-
* @private
|
|
38
|
-
*/
|
|
39
|
-
static mapQueryParams(queryParams) {
|
|
40
|
-
const params = {};
|
|
41
|
-
queryParams.forEach((value, key) => (params[key] = value));
|
|
42
|
-
return params;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Transforms raw HTTP request/response data into a standardized log output format.
|
|
46
|
-
*
|
|
47
|
-
* @param {InputProps} props - The input properties containing request/response data.
|
|
48
|
-
* @returns {OutputProps} The mapped output object ready for logging.
|
|
49
|
-
*/
|
|
50
|
-
static handle(props) {
|
|
51
|
-
return {
|
|
52
|
-
rawUrl: props.rawUrl,
|
|
53
|
-
status: props.status,
|
|
54
|
-
method: props.method,
|
|
55
|
-
token: null,
|
|
56
|
-
elapsedTime: props.elapsedTime,
|
|
57
|
-
requestHeaders: this.mapHeaders(props.requestHeaders),
|
|
58
|
-
requestBody: props.requestBody || null,
|
|
59
|
-
queryParams: {
|
|
60
|
-
...this.mapQueryParams(props.queryParams),
|
|
61
|
-
...props.urlParams,
|
|
62
|
-
},
|
|
63
|
-
responseHeaders: this.mapHeaders(props.responseHeaders),
|
|
64
|
-
responseBody: props.responseBody || null,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
export { LogMapperService };
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Static service for log endpoint configuration and access.
|
|
3
|
-
*
|
|
4
|
-
* Stores a singleton configuration containing the traffic source identifier,
|
|
5
|
-
* user token, and log ingestion URL.
|
|
6
|
-
*/
|
|
7
|
-
class LogService {
|
|
8
|
-
static config;
|
|
9
|
-
/**
|
|
10
|
-
* Sets the log service configuration once. Subsequent calls are ignored.
|
|
11
|
-
*
|
|
12
|
-
* @param config.trafficSourceId - Traffic source identifier.
|
|
13
|
-
* @param config.userToken - User token for authentication.
|
|
14
|
-
* @param config.logBaseApiUrl - Override the default log ingestion base URL.
|
|
15
|
-
*/
|
|
16
|
-
static setConfig(config) {
|
|
17
|
-
if (!!this.config)
|
|
18
|
-
return;
|
|
19
|
-
const { trafficSourceId, userToken, logBaseApiUrl } = config;
|
|
20
|
-
const baseUrl = logBaseApiUrl || `http://62.238.8.44:8081`;
|
|
21
|
-
const apiUrl = `${baseUrl}/ingest-log`;
|
|
22
|
-
this.config = { trafficSourceId, userToken, apiUrl };
|
|
23
|
-
}
|
|
24
|
-
/** Returns the stored configuration, or `undefined` if `setConfig` has not been called. */
|
|
25
|
-
static getConfig() {
|
|
26
|
-
return this.config;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Resets the stored configuration, allowing a new initialization.
|
|
30
|
-
*/
|
|
31
|
-
static resetConfig() {
|
|
32
|
-
this.config = undefined;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export { LogService };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { BadRequest } from "../http/badResponses/badRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Decodes a request body into a plain object, trying JSON first then URL-encoded form data.
|
|
4
|
-
* Throws `BadRequest` if neither format can be parsed.
|
|
5
|
-
*
|
|
6
|
-
* @param request - The incoming request whose body will be decoded.
|
|
7
|
-
* @returns The decoded body as a plain object.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```typescript
|
|
11
|
-
* export async function action({ request }: ActionFunctionArgs) {
|
|
12
|
-
* const body = await decodeRequestBody(request);
|
|
13
|
-
* // body is now a plain JS object
|
|
14
|
-
* }
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
async function decodeRequestBody(request) {
|
|
18
|
-
let data;
|
|
19
|
-
const arrayBuffer = await request.arrayBuffer();
|
|
20
|
-
const text = new TextDecoder().decode(arrayBuffer);
|
|
21
|
-
try {
|
|
22
|
-
data = JSON.parse(text);
|
|
23
|
-
}
|
|
24
|
-
catch (jsonError) {
|
|
25
|
-
try {
|
|
26
|
-
if (text.includes("=")) {
|
|
27
|
-
const formData = new URLSearchParams(text);
|
|
28
|
-
data = Object.fromEntries(formData.entries());
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
throw new BadRequest("Invalid URLSearchParams format");
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
catch (formDataError) {
|
|
35
|
-
throw new BadRequest("Failed to extract data from request");
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return data;
|
|
39
|
-
}
|
|
40
|
-
export { decodeRequestBody };
|