@arkyn/server 3.0.1-beta.0 → 3.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.
- package/README.md +1 -1
- package/dist/api/arkynLogRequest.d.ts +55 -0
- package/dist/api/arkynLogRequest.d.ts.map +1 -0
- package/dist/api/arkynLogRequest.js +83 -0
- package/dist/api/makeRequest.d.ts.map +1 -1
- package/dist/api/makeRequest.js +15 -12
- package/dist/config/arkynLogInstance.d.ts +44 -0
- package/dist/config/arkynLogInstance.d.ts.map +1 -0
- package/dist/config/arkynLogInstance.js +48 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/mapper/arkynLogRequestMapper.d.ts +30 -0
- package/dist/mapper/arkynLogRequestMapper.d.ts.map +1 -0
- package/dist/mapper/arkynLogRequestMapper.js +44 -0
- package/dist/services/errorHandler.js +1 -1
- package/dist/services/getCaller.d.ts.map +1 -1
- package/dist/services/getCaller.js +41 -15
- package/dist/services/httpDebug.d.ts.map +1 -1
- package/dist/services/httpDebug.js +0 -4
- package/package.json +1 -1
- package/src/api/arkynLogRequest.ts +112 -0
- package/src/api/makeRequest.ts +17 -13
- package/src/config/arkynLogInstance.ts +68 -0
- package/src/index.ts +1 -1
- package/src/mapper/arkynLogRequestMapper.ts +73 -0
- package/src/services/getCaller.ts +54 -20
- package/src/services/httpDebug.ts +0 -1
- package/dist/api/inboxFlowRequest.d.ts +0 -40
- package/dist/api/inboxFlowRequest.d.ts.map +0 -1
- package/dist/api/inboxFlowRequest.js +0 -63
- package/dist/config/inboxFlowInstance.d.ts +0 -44
- package/dist/config/inboxFlowInstance.d.ts.map +0 -1
- package/dist/config/inboxFlowInstance.js +0 -46
- package/src/api/inboxFlowRequest.ts +0 -76
- package/src/config/inboxFlowInstance.ts +0 -65
package/README.md
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
type ConfigProps = {
|
|
2
|
+
rawUrl: string;
|
|
3
|
+
status: number;
|
|
4
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
5
|
+
token: string | null;
|
|
6
|
+
elapsedTime: number;
|
|
7
|
+
requestHeaders: Record<string, string>;
|
|
8
|
+
requestBody: Record<string, string>;
|
|
9
|
+
queryParams: Record<string, string>;
|
|
10
|
+
responseHeaders: Record<string, string>;
|
|
11
|
+
responseBody: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Sends a request to the inbox flow API with the provided configuration.
|
|
15
|
+
*
|
|
16
|
+
* @param config - The configuration object for the request.
|
|
17
|
+
* @param config.rawUrl - The raw URL of the request.
|
|
18
|
+
* @param config.status - The HTTP status code associated with the request.
|
|
19
|
+
* @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", or "PATCH".
|
|
20
|
+
* @param config.token - The authentication token for the request.
|
|
21
|
+
* @param config.elapsedTime - The elapsed time for the request in milliseconds.
|
|
22
|
+
* @param config.requestHeaders - The headers sent with the request.
|
|
23
|
+
* @param config.requestBody - The body of the request, if applicable.
|
|
24
|
+
* @param config.queryParams - The query parameters for the request.
|
|
25
|
+
* @param config.responseHeaders - The headers received in the response.
|
|
26
|
+
* @param config.responseBody - The body of the response received.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
|
|
30
|
+
* - If the configuration is not available, the function will return early without performing any action.
|
|
31
|
+
* - In a development environment (`NODE_ENV === "development"`), the function will also return early.
|
|
32
|
+
* - The request is sent as a POST request to the inbox API URL with the provided configuration details.
|
|
33
|
+
* - If an error occurs during the request, it will be logged using the `httpDebug` service.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const config = {
|
|
38
|
+
* rawUrl: "https://example.com/api/data",
|
|
39
|
+
* status: 200,
|
|
40
|
+
* method: "GET",
|
|
41
|
+
* token: "auth-token-123",
|
|
42
|
+
* elapsedTime: "150ms",
|
|
43
|
+
* requestHeaders: { "Accept": "application/json", "Authorization": "Bearer token123" },
|
|
44
|
+
* requestBody: {},
|
|
45
|
+
* queryParams: { "page": "1", "limit": "10" },
|
|
46
|
+
* responseHeaders: { "Content-Type": "application/json" },
|
|
47
|
+
* responseBody: { "data": "example response" }
|
|
48
|
+
* };
|
|
49
|
+
*
|
|
50
|
+
* await arkynLogRequest(config);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
declare function arkynLogRequest(config: ConfigProps): Promise<void>;
|
|
54
|
+
export { arkynLogRequest };
|
|
55
|
+
//# sourceMappingURL=arkynLogRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arkynLogRequest.d.ts","sourceRoot":"","sources":["../../src/api/arkynLogRequest.ts"],"names":[],"mappings":"AAGA,KAAK,WAAW,GAAG;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpD,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,iBAAe,eAAe,CAAC,MAAM,EAAE,WAAW,iBAoDjD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ArkynLogInstance } from "../config/arkynLogInstance";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
3
|
+
/**
|
|
4
|
+
* Sends a request to the inbox flow API with the provided configuration.
|
|
5
|
+
*
|
|
6
|
+
* @param config - The configuration object for the request.
|
|
7
|
+
* @param config.rawUrl - The raw URL of the request.
|
|
8
|
+
* @param config.status - The HTTP status code associated with the request.
|
|
9
|
+
* @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", or "PATCH".
|
|
10
|
+
* @param config.token - The authentication token for the request.
|
|
11
|
+
* @param config.elapsedTime - The elapsed time for the request in milliseconds.
|
|
12
|
+
* @param config.requestHeaders - The headers sent with the request.
|
|
13
|
+
* @param config.requestBody - The body of the request, if applicable.
|
|
14
|
+
* @param config.queryParams - The query parameters for the request.
|
|
15
|
+
* @param config.responseHeaders - The headers received in the response.
|
|
16
|
+
* @param config.responseBody - The body of the response received.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
|
|
20
|
+
* - If the configuration is not available, the function will return early without performing any action.
|
|
21
|
+
* - In a development environment (`NODE_ENV === "development"`), the function will also return early.
|
|
22
|
+
* - The request is sent as a POST request to the inbox API URL with the provided configuration details.
|
|
23
|
+
* - If an error occurs during the request, it will be logged using the `httpDebug` service.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const config = {
|
|
28
|
+
* rawUrl: "https://example.com/api/data",
|
|
29
|
+
* status: 200,
|
|
30
|
+
* method: "GET",
|
|
31
|
+
* token: "auth-token-123",
|
|
32
|
+
* elapsedTime: "150ms",
|
|
33
|
+
* requestHeaders: { "Accept": "application/json", "Authorization": "Bearer token123" },
|
|
34
|
+
* requestBody: {},
|
|
35
|
+
* queryParams: { "page": "1", "limit": "10" },
|
|
36
|
+
* responseHeaders: { "Content-Type": "application/json" },
|
|
37
|
+
* responseBody: { "data": "example response" }
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* await arkynLogRequest(config);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
async function arkynLogRequest(config) {
|
|
44
|
+
const arkynInstance = ArkynLogInstance.getArkynConfig();
|
|
45
|
+
if (!arkynInstance)
|
|
46
|
+
return;
|
|
47
|
+
const { arkynApiUrl, arkynUserToken } = arkynInstance;
|
|
48
|
+
const { elapsedTime, method, queryParams, requestBody, requestHeaders, responseBody, responseHeaders, status, token, rawUrl, } = config;
|
|
49
|
+
if (process.env.NODE_ENV === "development")
|
|
50
|
+
return;
|
|
51
|
+
try {
|
|
52
|
+
const url = new URL(rawUrl);
|
|
53
|
+
let protocol = "HTTPS";
|
|
54
|
+
if (url.protocol === "http:")
|
|
55
|
+
protocol = "HTTP";
|
|
56
|
+
const body = JSON.stringify({
|
|
57
|
+
domainUrl: url.protocol + "//" + url.host,
|
|
58
|
+
pathnameUrl: url.pathname,
|
|
59
|
+
status,
|
|
60
|
+
protocol,
|
|
61
|
+
method,
|
|
62
|
+
trafficUserId: null,
|
|
63
|
+
elapsedTime,
|
|
64
|
+
requestHeaders,
|
|
65
|
+
requestBody,
|
|
66
|
+
queryParams,
|
|
67
|
+
responseHeaders,
|
|
68
|
+
responseBody,
|
|
69
|
+
});
|
|
70
|
+
await fetch(arkynApiUrl, {
|
|
71
|
+
method: "POST",
|
|
72
|
+
body,
|
|
73
|
+
headers: {
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
Authorization: `Bearer ${arkynUserToken}`,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
httpDebug("arkyn log error", "Error sending request", err);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export { arkynLogRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/api/makeRequest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/api/makeRequest.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,iBAAe,WAAW,CAAC,CAAC,GAAG,GAAG,EAChC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,KAAK,EACnD,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,EACzB,IAAI,CAAC,EAAE,GAAG,GACT,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAwE5B;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/api/makeRequest.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ArkynLogRequestMapper } from "../mapper/arkynLogRequestMapper";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
3
|
+
import { arkynLogRequest } from "./arkynLogRequest";
|
|
2
4
|
/**
|
|
3
5
|
* Makes an HTTP request using the Fetch API and returns a standardized response.
|
|
4
6
|
*
|
|
@@ -42,6 +44,7 @@ async function makeRequest(method, url, headers = {}, body) {
|
|
|
42
44
|
GET: "Request successful",
|
|
43
45
|
};
|
|
44
46
|
try {
|
|
47
|
+
const startTime = performance.now();
|
|
45
48
|
const response = await fetch(url, {
|
|
46
49
|
method,
|
|
47
50
|
headers: {
|
|
@@ -50,6 +53,7 @@ async function makeRequest(method, url, headers = {}, body) {
|
|
|
50
53
|
},
|
|
51
54
|
body: body ? JSON.stringify(body) : undefined,
|
|
52
55
|
});
|
|
56
|
+
const elapsedTime = performance.now() - startTime;
|
|
53
57
|
const status = response.status;
|
|
54
58
|
let data = null;
|
|
55
59
|
try {
|
|
@@ -58,13 +62,18 @@ async function makeRequest(method, url, headers = {}, body) {
|
|
|
58
62
|
catch {
|
|
59
63
|
data = null;
|
|
60
64
|
}
|
|
61
|
-
|
|
65
|
+
const logData = ArkynLogRequestMapper.handle({
|
|
66
|
+
elapsedTime,
|
|
62
67
|
method,
|
|
68
|
+
queryParams: new URL(url).searchParams,
|
|
69
|
+
requestHeaders: headers,
|
|
70
|
+
requestBody: body,
|
|
71
|
+
responseBody: data,
|
|
72
|
+
responseHeaders: response.headers,
|
|
63
73
|
status,
|
|
64
|
-
|
|
65
|
-
response: JSON.stringify(data),
|
|
66
|
-
token: headers?.Authorization || "TOKEN_NOT_FOUND",
|
|
74
|
+
url,
|
|
67
75
|
});
|
|
76
|
+
arkynLogRequest(logData);
|
|
68
77
|
if (!response.ok) {
|
|
69
78
|
return {
|
|
70
79
|
success: false,
|
|
@@ -83,13 +92,7 @@ async function makeRequest(method, url, headers = {}, body) {
|
|
|
83
92
|
};
|
|
84
93
|
}
|
|
85
94
|
catch (err) {
|
|
86
|
-
|
|
87
|
-
method,
|
|
88
|
-
request: JSON.stringify(headers),
|
|
89
|
-
response: String(err),
|
|
90
|
-
status: 500,
|
|
91
|
-
token: "TOKEN_NOT_FOUND",
|
|
92
|
-
});
|
|
95
|
+
httpDebug("Network error or request failed", null, err);
|
|
93
96
|
return {
|
|
94
97
|
success: false,
|
|
95
98
|
status: 0,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type ArkynConfigProps = {
|
|
2
|
+
arkynTrafficSourceId: string;
|
|
3
|
+
arkynUserToken: string;
|
|
4
|
+
arkynApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
type SetArkynConfigProps = {
|
|
7
|
+
arkynTrafficSourceId: string;
|
|
8
|
+
arkynUserToken: string;
|
|
9
|
+
arkynApiUrl?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The `ArkynLogInstance` class manages the configuration for the arkyn flow.
|
|
13
|
+
* It allows you to set and retrieve the arkyn configuration, including the traffic source ID,
|
|
14
|
+
* user token, and API URL.
|
|
15
|
+
*/
|
|
16
|
+
declare class ArkynLogInstance {
|
|
17
|
+
private static arkynConfig?;
|
|
18
|
+
/**
|
|
19
|
+
* Sets the configuration for the arkyn. This method initializes the arkyn configuration
|
|
20
|
+
* with the provided `arkynConfig` values. If the configuration has already been set,
|
|
21
|
+
* the method will return early without making any changes.
|
|
22
|
+
*
|
|
23
|
+
* @param arkynConfig - An object containing the arkyn configuration properties.
|
|
24
|
+
* @param arkynConfig.arkynTrafficSourceId - The key used to identify the arkyn.
|
|
25
|
+
* @param arkynConfig.arkynUserToken - The user token for authenticating with the arkyn.
|
|
26
|
+
* @param arkynConfig.arkynApiUrl - (Optional) The API URL for the arkyn. If not provided,
|
|
27
|
+
* a default URL will be used.
|
|
28
|
+
*/
|
|
29
|
+
static setArkynConfig(arkynConfig: SetArkynConfigProps): void;
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves the current arkyn configuration for the ArkynLogInstance.
|
|
32
|
+
*
|
|
33
|
+
* @returns {ArkynConfigProps | undefined} The current arkyn configuration if set,
|
|
34
|
+
* or `undefined` if no configuration has been initialized.
|
|
35
|
+
*/
|
|
36
|
+
static getArkynConfig(): ArkynConfigProps | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Resets the arkyn configuration to `undefined`.
|
|
39
|
+
* This method can be used to clear the current configuration.
|
|
40
|
+
*/
|
|
41
|
+
static resetArkynConfig(): void;
|
|
42
|
+
}
|
|
43
|
+
export { ArkynLogInstance };
|
|
44
|
+
//# sourceMappingURL=arkynLogInstance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arkynLogInstance.d.ts","sourceRoot":"","sources":["../../src/config/arkynLogInstance.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACtB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AAEH,cAAM,gBAAgB;IACpB,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAmB;IAE9C;;;;;;;;;;OAUG;IAEH,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,mBAAmB;IActD;;;;;OAKG;IACH,MAAM,CAAC,cAAc,IAAI,gBAAgB,GAAG,SAAS;IAIrD;;;OAGG;IAEH,MAAM,CAAC,gBAAgB;CAGxB;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ArkynLogInstance` class manages the configuration for the arkyn flow.
|
|
3
|
+
* It allows you to set and retrieve the arkyn configuration, including the traffic source ID,
|
|
4
|
+
* user token, and API URL.
|
|
5
|
+
*/
|
|
6
|
+
class ArkynLogInstance {
|
|
7
|
+
static arkynConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Sets the configuration for the arkyn. This method initializes the arkyn configuration
|
|
10
|
+
* with the provided `arkynConfig` values. If the configuration has already been set,
|
|
11
|
+
* the method will return early without making any changes.
|
|
12
|
+
*
|
|
13
|
+
* @param arkynConfig - An object containing the arkyn configuration properties.
|
|
14
|
+
* @param arkynConfig.arkynTrafficSourceId - The key used to identify the arkyn.
|
|
15
|
+
* @param arkynConfig.arkynUserToken - The user token for authenticating with the arkyn.
|
|
16
|
+
* @param arkynConfig.arkynApiUrl - (Optional) The API URL for the arkyn. If not provided,
|
|
17
|
+
* a default URL will be used.
|
|
18
|
+
*/
|
|
19
|
+
static setArkynConfig(arkynConfig) {
|
|
20
|
+
if (!!this.arkynConfig)
|
|
21
|
+
return;
|
|
22
|
+
let defaultArkynURL = `https://logs-arkyn-flow-logs.vw6wo7.easypanel.host/http-traffic-records/:trafficSourceId`;
|
|
23
|
+
let arkynApiUrl = arkynConfig.arkynApiUrl || defaultArkynURL;
|
|
24
|
+
arkynApiUrl.replace(":trafficSourceId", arkynConfig.arkynTrafficSourceId);
|
|
25
|
+
this.arkynConfig = {
|
|
26
|
+
arkynTrafficSourceId: arkynConfig.arkynTrafficSourceId,
|
|
27
|
+
arkynUserToken: arkynConfig.arkynUserToken,
|
|
28
|
+
arkynApiUrl: arkynConfig.arkynApiUrl || defaultArkynURL,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves the current arkyn configuration for the ArkynLogInstance.
|
|
33
|
+
*
|
|
34
|
+
* @returns {ArkynConfigProps | undefined} The current arkyn configuration if set,
|
|
35
|
+
* or `undefined` if no configuration has been initialized.
|
|
36
|
+
*/
|
|
37
|
+
static getArkynConfig() {
|
|
38
|
+
return this.arkynConfig;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resets the arkyn configuration to `undefined`.
|
|
42
|
+
* This method can be used to clear the current configuration.
|
|
43
|
+
*/
|
|
44
|
+
static resetArkynConfig() {
|
|
45
|
+
this.arkynConfig = undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export { ArkynLogInstance };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { ApiInstance } from "./config/apiInstance";
|
|
2
|
-
export {
|
|
2
|
+
export { ArkynLogInstance } from "./config/arkynLogInstance";
|
|
3
3
|
export { BadGateway } from "./http/badResponses/badGateway";
|
|
4
4
|
export { BadRequest } from "./http/badResponses/badRequest";
|
|
5
5
|
export { Conflict } from "./http/badResponses/conflict";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,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,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// config
|
|
2
2
|
export { ApiInstance } from "./config/apiInstance";
|
|
3
|
-
export {
|
|
3
|
+
export { ArkynLogInstance } from "./config/arkynLogInstance";
|
|
4
4
|
// http bad responses
|
|
5
5
|
export { BadGateway } from "./http/badResponses/badGateway";
|
|
6
6
|
export { BadRequest } from "./http/badResponses/badRequest";
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
type OutputProps = {
|
|
13
|
+
rawUrl: string;
|
|
14
|
+
status: number;
|
|
15
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
16
|
+
token: string | null;
|
|
17
|
+
elapsedTime: number;
|
|
18
|
+
requestHeaders: Record<string, string>;
|
|
19
|
+
requestBody: Record<string, string>;
|
|
20
|
+
queryParams: Record<string, string>;
|
|
21
|
+
responseHeaders: Record<string, string>;
|
|
22
|
+
responseBody: any;
|
|
23
|
+
};
|
|
24
|
+
declare class ArkynLogRequestMapper {
|
|
25
|
+
private static mapHeaders;
|
|
26
|
+
private static mapQueryParams;
|
|
27
|
+
static handle(props: InputProps): OutputProps;
|
|
28
|
+
}
|
|
29
|
+
export { ArkynLogRequestMapper };
|
|
30
|
+
//# sourceMappingURL=arkynLogRequestMapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ar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(headers) {
|
|
3
|
+
if (headers instanceof Headers) {
|
|
4
|
+
return Object.fromEntries(headers.entries());
|
|
5
|
+
}
|
|
6
|
+
else if (typeof headers === "object") {
|
|
7
|
+
return Object.entries(headers).reduce((acc, [key, value]) => {
|
|
8
|
+
if (typeof value === "string") {
|
|
9
|
+
acc[key] = value;
|
|
10
|
+
}
|
|
11
|
+
else if (Array.isArray(value)) {
|
|
12
|
+
acc[key] = value.join(", ");
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
acc[key] = JSON.stringify(value);
|
|
16
|
+
}
|
|
17
|
+
return acc;
|
|
18
|
+
}, {});
|
|
19
|
+
}
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
static mapQueryParams(queryParams) {
|
|
23
|
+
const params = {};
|
|
24
|
+
queryParams.forEach((value, key) => {
|
|
25
|
+
params[key] = value;
|
|
26
|
+
});
|
|
27
|
+
return params;
|
|
28
|
+
}
|
|
29
|
+
static handle(props) {
|
|
30
|
+
return {
|
|
31
|
+
rawUrl: props.url,
|
|
32
|
+
status: props.status,
|
|
33
|
+
method: props.method,
|
|
34
|
+
token: null,
|
|
35
|
+
elapsedTime: props.elapsedTime,
|
|
36
|
+
requestHeaders: this.mapHeaders(props.requestHeaders),
|
|
37
|
+
requestBody: props.requestBody,
|
|
38
|
+
queryParams: this.mapQueryParams(props.queryParams),
|
|
39
|
+
responseHeaders: this.mapHeaders(props.responseHeaders),
|
|
40
|
+
responseBody: props.responseBody,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export { ArkynLogRequestMapper };
|
|
@@ -88,6 +88,6 @@ function errorHandler(error) {
|
|
|
88
88
|
case error instanceof UnprocessableEntity:
|
|
89
89
|
return error.toResponse();
|
|
90
90
|
}
|
|
91
|
-
return new ServerError("Server error").toResponse();
|
|
91
|
+
return new ServerError("Server error", error).toResponse();
|
|
92
92
|
}
|
|
93
93
|
export { errorHandler };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getCaller.d.ts","sourceRoot":"","sources":["../../src/services/getCaller.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;
|
|
1
|
+
{"version":3,"file":"getCaller.d.ts","sourceRoot":"","sources":["../../src/services/getCaller.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,iBAAS,SAAS;;;EAkEjB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -15,25 +15,51 @@ function getCaller() {
|
|
|
15
15
|
const err = new Error();
|
|
16
16
|
const stack = err.stack || "";
|
|
17
17
|
const stackLines = stack.split("\n").map((line) => line.trim());
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
// The first line is the error message
|
|
19
|
+
// The second line is this function (getCaller)
|
|
20
|
+
// The third line should be the direct caller
|
|
21
|
+
// We start from 2 because indexes are zero-based
|
|
22
|
+
let callerIndex = 2;
|
|
23
|
+
// Ignore internal or infrastructure lines if necessary
|
|
24
|
+
while (callerIndex < stackLines.length &&
|
|
25
|
+
(stackLines[callerIndex].includes("node:internal") ||
|
|
26
|
+
stackLines[callerIndex].includes("/node_modules/"))) {
|
|
27
|
+
callerIndex++;
|
|
25
28
|
}
|
|
29
|
+
const callerLine = stackLines[callerIndex] || "";
|
|
30
|
+
let functionName = "Unknown function";
|
|
31
|
+
let callerInfo = "Unknown caller";
|
|
32
|
+
// Default for named functions: "at functionName (file:line:column)"
|
|
33
|
+
const namedFunctionMatch = callerLine.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);
|
|
34
|
+
if (namedFunctionMatch) {
|
|
35
|
+
functionName = namedFunctionMatch[1];
|
|
36
|
+
callerInfo = namedFunctionMatch[2];
|
|
37
|
+
}
|
|
38
|
+
// Default for anonymous functions or methods: "at file:line:column"
|
|
26
39
|
else {
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
29
|
-
callerInfo =
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
const anonymousFunctionMatch = callerLine.match(/at\s+(.+)/);
|
|
41
|
+
if (anonymousFunctionMatch) {
|
|
42
|
+
callerInfo = anonymousFunctionMatch[1];
|
|
43
|
+
// Tenta extrair nome da função de padrões como Object.method ou Class.method
|
|
44
|
+
const objectMethodMatch = callerInfo.match(/at\s+([^(\s]+)\s+/);
|
|
45
|
+
if (objectMethodMatch && objectMethodMatch[1] !== "new") {
|
|
46
|
+
functionName = objectMethodMatch[1];
|
|
47
|
+
}
|
|
34
48
|
}
|
|
35
49
|
}
|
|
36
|
-
|
|
50
|
+
// Handles file paths
|
|
51
|
+
if (callerInfo.includes("(")) {
|
|
52
|
+
callerInfo = callerInfo.substring(callerInfo.indexOf("(") + 1, callerInfo.lastIndexOf(")"));
|
|
53
|
+
}
|
|
54
|
+
// Remove the line:column part of the file path
|
|
55
|
+
callerInfo = callerInfo.split(":").slice(0, -2).join(":");
|
|
56
|
+
// Make the path relative to the project
|
|
57
|
+
try {
|
|
58
|
+
callerInfo = path.relative(projectRoot, callerInfo);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
// If it fails to relativize, use the original path
|
|
62
|
+
}
|
|
37
63
|
return { functionName, callerInfo };
|
|
38
64
|
}
|
|
39
65
|
export { getCaller };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpDebug.d.ts","sourceRoot":"","sources":["../../src/services/httpDebug.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"httpDebug.d.ts","sourceRoot":"","sources":["../../src/services/httpDebug.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,iBAAS,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,QAuBtD;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
2
1
|
import { getCaller } from "../services/getCaller";
|
|
3
2
|
/**
|
|
4
3
|
* Logs debug information to the console when in development mode or when the
|
|
@@ -47,9 +46,6 @@ function httpDebug(name, body, cause) {
|
|
|
47
46
|
if (cause) {
|
|
48
47
|
consoleData += `${debugName} Cause: ${JSON.stringify(cause, null, 2)}\n`;
|
|
49
48
|
}
|
|
50
|
-
const arkynKeys = InboxFlowInstance.getInboxConfig();
|
|
51
|
-
if (arkynKeys)
|
|
52
|
-
console.log(arkynKeys);
|
|
53
49
|
console.log(consoleData);
|
|
54
50
|
}
|
|
55
51
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ArkynLogInstance } from "../config/arkynLogInstance";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
3
|
+
|
|
4
|
+
type ConfigProps = {
|
|
5
|
+
rawUrl: string;
|
|
6
|
+
status: number;
|
|
7
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
8
|
+
token: string | null;
|
|
9
|
+
elapsedTime: number;
|
|
10
|
+
requestHeaders: Record<string, string>;
|
|
11
|
+
requestBody: Record<string, string>;
|
|
12
|
+
queryParams: Record<string, string>;
|
|
13
|
+
responseHeaders: Record<string, string>;
|
|
14
|
+
responseBody: Record<string, string>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Sends a request to the inbox flow API with the provided configuration.
|
|
19
|
+
*
|
|
20
|
+
* @param config - The configuration object for the request.
|
|
21
|
+
* @param config.rawUrl - The raw URL of the request.
|
|
22
|
+
* @param config.status - The HTTP status code associated with the request.
|
|
23
|
+
* @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", or "PATCH".
|
|
24
|
+
* @param config.token - The authentication token for the request.
|
|
25
|
+
* @param config.elapsedTime - The elapsed time for the request in milliseconds.
|
|
26
|
+
* @param config.requestHeaders - The headers sent with the request.
|
|
27
|
+
* @param config.requestBody - The body of the request, if applicable.
|
|
28
|
+
* @param config.queryParams - The query parameters for the request.
|
|
29
|
+
* @param config.responseHeaders - The headers received in the response.
|
|
30
|
+
* @param config.responseBody - The body of the response received.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
|
|
34
|
+
* - If the configuration is not available, the function will return early without performing any action.
|
|
35
|
+
* - In a development environment (`NODE_ENV === "development"`), the function will also return early.
|
|
36
|
+
* - The request is sent as a POST request to the inbox API URL with the provided configuration details.
|
|
37
|
+
* - If an error occurs during the request, it will be logged using the `httpDebug` service.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const config = {
|
|
42
|
+
* rawUrl: "https://example.com/api/data",
|
|
43
|
+
* status: 200,
|
|
44
|
+
* method: "GET",
|
|
45
|
+
* token: "auth-token-123",
|
|
46
|
+
* elapsedTime: "150ms",
|
|
47
|
+
* requestHeaders: { "Accept": "application/json", "Authorization": "Bearer token123" },
|
|
48
|
+
* requestBody: {},
|
|
49
|
+
* queryParams: { "page": "1", "limit": "10" },
|
|
50
|
+
* responseHeaders: { "Content-Type": "application/json" },
|
|
51
|
+
* responseBody: { "data": "example response" }
|
|
52
|
+
* };
|
|
53
|
+
*
|
|
54
|
+
* await arkynLogRequest(config);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
async function arkynLogRequest(config: ConfigProps) {
|
|
59
|
+
const arkynInstance = ArkynLogInstance.getArkynConfig();
|
|
60
|
+
if (!arkynInstance) return;
|
|
61
|
+
|
|
62
|
+
const { arkynApiUrl, arkynUserToken } = arkynInstance;
|
|
63
|
+
|
|
64
|
+
const {
|
|
65
|
+
elapsedTime,
|
|
66
|
+
method,
|
|
67
|
+
queryParams,
|
|
68
|
+
requestBody,
|
|
69
|
+
requestHeaders,
|
|
70
|
+
responseBody,
|
|
71
|
+
responseHeaders,
|
|
72
|
+
status,
|
|
73
|
+
token,
|
|
74
|
+
rawUrl,
|
|
75
|
+
} = config;
|
|
76
|
+
|
|
77
|
+
if (process.env.NODE_ENV === "development") return;
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const url = new URL(rawUrl);
|
|
81
|
+
let protocol: "HTTPS" | "HTTP" = "HTTPS";
|
|
82
|
+
if (url.protocol === "http:") protocol = "HTTP";
|
|
83
|
+
|
|
84
|
+
const body = JSON.stringify({
|
|
85
|
+
domainUrl: url.protocol + "//" + url.host,
|
|
86
|
+
pathnameUrl: url.pathname,
|
|
87
|
+
status,
|
|
88
|
+
protocol,
|
|
89
|
+
method,
|
|
90
|
+
trafficUserId: null,
|
|
91
|
+
elapsedTime,
|
|
92
|
+
requestHeaders,
|
|
93
|
+
requestBody,
|
|
94
|
+
queryParams,
|
|
95
|
+
responseHeaders,
|
|
96
|
+
responseBody,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
await fetch(arkynApiUrl, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
body,
|
|
102
|
+
headers: {
|
|
103
|
+
"Content-Type": "application/json",
|
|
104
|
+
Authorization: `Bearer ${arkynUserToken}`,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
} catch (err) {
|
|
108
|
+
httpDebug("arkyn log error", "Error sending request", err);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { arkynLogRequest };
|