@arkyn/server 3.0.1-beta.2 → 3.0.1-beta.21
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 +82 -0
- package/dist/api/makeRequest.d.ts +1 -1
- package/dist/api/makeRequest.d.ts.map +1 -1
- package/dist/api/makeRequest.js +18 -17
- 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 +44 -0
- package/dist/config/arkynLogInstance.d.ts.map +1 -0
- package/dist/config/arkynLogInstance.js +49 -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/formParse.d.ts +3 -3
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +8 -5
- 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 -1
- 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 +16 -19
- package/src/api/arkynLogRequest.ts +118 -0
- package/src/api/makeRequest.ts +20 -18
- package/src/config/apiInstance.ts +17 -17
- package/src/config/arkynLogInstance.ts +70 -0
- package/src/index.ts +1 -1
- package/src/mapper/arkynLogRequestMapper.ts +73 -0
- package/src/services/formParse.ts +11 -8
- package/src/services/getCaller.ts +54 -20
- package/src/services/httpDebug.ts +0 -1
- package/src/services/measureRouteExecution.ts +31 -0
- package/src/services/schemaValidator.ts +4 -4
- package/tsconfig.json +2 -2
- 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
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
function formParse([formData, schema,]) {
|
|
40
40
|
const zodResponse = schema.safeParse(formData);
|
|
41
41
|
if (zodResponse.success === false) {
|
|
42
|
-
const errorsObject = Object.fromEntries(zodResponse.error.
|
|
43
|
-
|
|
44
|
-
item.message
|
|
45
|
-
|
|
42
|
+
const errorsObject = Object.fromEntries(zodResponse.error.issues.map((item) => {
|
|
43
|
+
console.log(item);
|
|
44
|
+
return [item.path.join("."), item.message];
|
|
45
|
+
}));
|
|
46
46
|
return {
|
|
47
47
|
success: zodResponse.success,
|
|
48
48
|
fieldErrors: errorsObject,
|
|
@@ -50,7 +50,10 @@ function formParse([formData, schema,]) {
|
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
52
|
else {
|
|
53
|
-
return {
|
|
53
|
+
return {
|
|
54
|
+
success: zodResponse.success,
|
|
55
|
+
data: zodResponse.data,
|
|
56
|
+
};
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
export { formParse };
|
|
@@ -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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"measureRouteExecution.d.ts","sourceRoot":"","sources":["../../src/services/measureRouteExecution.ts"],"names":[],"mappings":"AAAA,iBAAS,qBAAqB,CAAC,CAAC,GAAG,OAAO,EACxC,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,IAEG,OAAO,GAAG,KAAG,OAAO,CAAC,CAAC,CAAC,CAyB9D;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function measureRouteExecution(handler) {
|
|
2
|
+
return async function measuredHandler(props) {
|
|
3
|
+
const startTime = performance.now();
|
|
4
|
+
try {
|
|
5
|
+
const result = await handler(props);
|
|
6
|
+
const url = new URL(props.request.url);
|
|
7
|
+
const endTime = performance.now();
|
|
8
|
+
const duration = (endTime - startTime).toFixed(2);
|
|
9
|
+
console.log({
|
|
10
|
+
domain: url.hostname,
|
|
11
|
+
pathname: url.pathname,
|
|
12
|
+
method: props.request.method,
|
|
13
|
+
duration,
|
|
14
|
+
});
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
const endTime = performance.now();
|
|
19
|
+
console.error("");
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export { measureRouteExecution };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
declare class SchemaValidator<T extends
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
|
+
declare class SchemaValidator<T extends ZodType> {
|
|
3
3
|
readonly schema: T;
|
|
4
4
|
functionName: string;
|
|
5
5
|
callerInfo: string;
|
|
6
6
|
constructor(schema: T);
|
|
7
7
|
isValid(data: any): boolean;
|
|
8
|
-
safeValidate(data: any): z.
|
|
8
|
+
safeValidate(data: any): z.ZodSafeParseResult<z.infer<T>>;
|
|
9
9
|
validate(data: any): z.infer<T>;
|
|
10
10
|
formValidate(data: any, message?: string): z.infer<T>;
|
|
11
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/services/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/services/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAiBjC,cAAM,eAAe,CAAC,CAAC,SAAS,OAAO;IAIzB,QAAQ,CAAC,MAAM,EAAE,CAAC;IAH9B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;gBAEE,MAAM,EAAE,CAAC;IAM9B,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO;IAI3B,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAIzD,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAQ/B,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;CAoBtD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkyn/server",
|
|
3
|
-
"version": "3.0.1-beta.
|
|
3
|
+
"version": "3.0.1-beta.21",
|
|
4
4
|
"author": "Arkyn | Lucas Gonçalves",
|
|
5
5
|
"main": "./dist/bundle.js",
|
|
6
6
|
"module": "./src/index.ts",
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"@aws-sdk/client-s3": "^3.782.0",
|
|
10
|
-
"@mjackson/multipart-parser": "^0.8.2",
|
|
11
|
-
"sharp": "^0.33.5",
|
|
12
|
-
"zod": "^3.24.2"
|
|
13
|
-
},
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"bun-types": "latest",
|
|
16
|
-
"vitest": "^3.1.1",
|
|
17
|
-
"typescript": "^5.8.3"
|
|
18
|
-
},
|
|
19
|
-
"peerDependencies": {
|
|
20
|
-
"@react-router/node": ">=7.6.0"
|
|
21
|
-
},
|
|
22
|
-
"description": "Server-side utilities for projects.",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
23
9
|
"license": "Apache-2.0",
|
|
10
|
+
"description": "Server-side utilities for projects.",
|
|
24
11
|
"scripts": {
|
|
25
12
|
"clean": "rm -rf dist",
|
|
26
13
|
"build": "bun run clean && bunx tsc --project tsconfig.json",
|
|
27
14
|
"test": "vitest --config vitest.config.ts",
|
|
28
15
|
"typecheck": "bunx tsc --project tsconfig.json --noEmit"
|
|
29
16
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@arkyn/shared": "*",
|
|
19
|
+
"zod": "^4.0.17"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"bun-types": "latest",
|
|
23
|
+
"vitest": "^3.2.4",
|
|
24
|
+
"typescript": "^5.9.2"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@react-router/node": "^7.6.2"
|
|
28
|
+
}
|
|
32
29
|
}
|
|
@@ -0,0 +1,118 @@
|
|
|
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 { arkynUserToken, arkynApiUrl } = 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(
|
|
100
|
+
arkynApiUrl.replace(
|
|
101
|
+
":trafficSourceId",
|
|
102
|
+
arkynInstance.arkynTrafficSourceId
|
|
103
|
+
),
|
|
104
|
+
{
|
|
105
|
+
method: "POST",
|
|
106
|
+
body,
|
|
107
|
+
headers: {
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
Authorization: `Bearer ${arkynUserToken}`,
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
httpDebug("arkyn log error", "Error sending request", err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { arkynLogRequest };
|
package/src/api/makeRequest.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { ArkynLogRequestMapper } from "../mapper/arkynLogRequestMapper";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
1
3
|
import type { ApiResponseDTO } from "../types/ApiResponseDTO";
|
|
2
|
-
import {
|
|
4
|
+
import { arkynLogRequest } from "./arkynLogRequest";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Makes an HTTP request using the Fetch API and returns a standardized response.
|
|
@@ -39,7 +41,7 @@ import { inboxFlowRequest } from "./inboxFlowRequest";
|
|
|
39
41
|
async function makeRequest<T = any>(
|
|
40
42
|
method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET",
|
|
41
43
|
url: string,
|
|
42
|
-
|
|
44
|
+
rawHeaders: HeadersInit = {},
|
|
43
45
|
body?: any
|
|
44
46
|
): Promise<ApiResponseDTO<T>> {
|
|
45
47
|
const successMessage = {
|
|
@@ -51,15 +53,16 @@ async function makeRequest<T = any>(
|
|
|
51
53
|
};
|
|
52
54
|
|
|
53
55
|
try {
|
|
56
|
+
const startTime = performance.now();
|
|
57
|
+
|
|
58
|
+
const headers = { ...rawHeaders, "Content-Type": "application/json" };
|
|
54
59
|
const response = await fetch(url, {
|
|
55
60
|
method,
|
|
56
|
-
headers
|
|
57
|
-
...headers,
|
|
58
|
-
"Content-Type": "application/json",
|
|
59
|
-
},
|
|
61
|
+
headers,
|
|
60
62
|
body: body ? JSON.stringify(body) : undefined,
|
|
61
63
|
});
|
|
62
64
|
|
|
65
|
+
const elapsedTime = performance.now() - startTime;
|
|
63
66
|
const status = response.status;
|
|
64
67
|
|
|
65
68
|
let data: any = null;
|
|
@@ -69,14 +72,20 @@ async function makeRequest<T = any>(
|
|
|
69
72
|
data = null;
|
|
70
73
|
}
|
|
71
74
|
|
|
72
|
-
|
|
75
|
+
const logData = ArkynLogRequestMapper.handle({
|
|
76
|
+
elapsedTime,
|
|
73
77
|
method,
|
|
78
|
+
queryParams: new URL(url).searchParams,
|
|
79
|
+
requestHeaders: headers,
|
|
80
|
+
requestBody: body,
|
|
81
|
+
responseBody: data,
|
|
82
|
+
responseHeaders: response.headers,
|
|
74
83
|
status,
|
|
75
|
-
|
|
76
|
-
response: JSON.stringify(data),
|
|
77
|
-
token: (headers as any)?.Authorization || "TOKEN_NOT_FOUND",
|
|
84
|
+
url,
|
|
78
85
|
});
|
|
79
86
|
|
|
87
|
+
arkynLogRequest(logData);
|
|
88
|
+
|
|
80
89
|
if (!response.ok) {
|
|
81
90
|
return {
|
|
82
91
|
success: false,
|
|
@@ -95,14 +104,7 @@ async function makeRequest<T = any>(
|
|
|
95
104
|
cause: null,
|
|
96
105
|
};
|
|
97
106
|
} catch (err) {
|
|
98
|
-
|
|
99
|
-
method,
|
|
100
|
-
request: JSON.stringify(headers),
|
|
101
|
-
response: String(err),
|
|
102
|
-
status: 500,
|
|
103
|
-
token: "TOKEN_NOT_FOUND",
|
|
104
|
-
});
|
|
105
|
-
|
|
107
|
+
httpDebug("Network error or request failed", null, err);
|
|
106
108
|
return {
|
|
107
109
|
success: false,
|
|
108
110
|
status: 0,
|
|
@@ -4,7 +4,7 @@ import { patchRequest } from "../api/patchRequest";
|
|
|
4
4
|
import { postRequest } from "../api/postRequest";
|
|
5
5
|
import { putRequest } from "../api/putRequest";
|
|
6
6
|
|
|
7
|
-
type
|
|
7
|
+
type ApiInstanceConstructorProps = {
|
|
8
8
|
baseUrl: string;
|
|
9
9
|
baseHeaders?: HeadersInit;
|
|
10
10
|
baseToken?: string | null;
|
|
@@ -38,7 +38,7 @@ class ApiInstance {
|
|
|
38
38
|
* @param props.baseToken - Optional base token for authorization.
|
|
39
39
|
*/
|
|
40
40
|
|
|
41
|
-
constructor(props:
|
|
41
|
+
constructor(props: ApiInstanceConstructorProps) {
|
|
42
42
|
this.baseUrl = props.baseUrl;
|
|
43
43
|
this.baseHeaders = props.baseHeaders || undefined;
|
|
44
44
|
this.baseToken = props.baseToken || undefined;
|
|
@@ -76,26 +76,26 @@ class ApiInstance {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
|
-
* Sends a
|
|
80
|
-
* @param route - The API route to send the
|
|
79
|
+
* Sends a get request to the specified route.
|
|
80
|
+
* @param route - The API route to send the get request to.
|
|
81
81
|
* @param data - The request data, including optional headers and token.
|
|
82
82
|
* @returns The API response data.
|
|
83
83
|
*/
|
|
84
84
|
|
|
85
|
-
async
|
|
85
|
+
async get(route: string, data?: ApiRequestDataWithoutBodyProps) {
|
|
86
86
|
const url = this.generateURL(route);
|
|
87
87
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
88
88
|
return await getRequest(url, headers);
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
|
-
* Sends a
|
|
93
|
-
* @param route - The API route to send the
|
|
92
|
+
* Sends a post request to the specified route.
|
|
93
|
+
* @param route - The API route to send the post request to.
|
|
94
94
|
* @param data - The request data, including body, optional headers, and token.
|
|
95
95
|
* @returns The API response data.
|
|
96
96
|
*/
|
|
97
97
|
|
|
98
|
-
async
|
|
98
|
+
async post(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
99
99
|
const url = this.generateURL(route);
|
|
100
100
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
101
101
|
const body = data?.body;
|
|
@@ -103,13 +103,13 @@ class ApiInstance {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
* Sends a
|
|
107
|
-
* @param route - The API route to send the
|
|
106
|
+
* Sends a put request to the specified route.
|
|
107
|
+
* @param route - The API route to send the put request to.
|
|
108
108
|
* @param data - The request data, including body, optional headers, and token.
|
|
109
109
|
* @returns The API response data.
|
|
110
110
|
*/
|
|
111
111
|
|
|
112
|
-
async
|
|
112
|
+
async put(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
113
113
|
const url = this.generateURL(route);
|
|
114
114
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
115
115
|
const body = data?.body;
|
|
@@ -117,13 +117,13 @@ class ApiInstance {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* Sends a
|
|
121
|
-
* @param route - The API route to send the
|
|
120
|
+
* Sends a patch request to the specified route.
|
|
121
|
+
* @param route - The API route to send the patch request to.
|
|
122
122
|
* @param data - The request data, including body, optional headers, and token.
|
|
123
123
|
* @returns The API response data.
|
|
124
124
|
*/
|
|
125
125
|
|
|
126
|
-
async
|
|
126
|
+
async patch(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
127
127
|
const url = this.generateURL(route);
|
|
128
128
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
129
129
|
const body = data?.body;
|
|
@@ -131,13 +131,13 @@ class ApiInstance {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
/**
|
|
134
|
-
* Sends a
|
|
135
|
-
* @param route - The API route to send the
|
|
134
|
+
* Sends a delete request to the specified route.
|
|
135
|
+
* @param route - The API route to send the delete request to.
|
|
136
136
|
* @param data - The request data, including body, optional headers, and token.
|
|
137
137
|
* @returns The API response data.
|
|
138
138
|
*/
|
|
139
139
|
|
|
140
|
-
async
|
|
140
|
+
async delete(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
141
141
|
const url = this.generateURL(route);
|
|
142
142
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
143
143
|
const body = data?.body;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
type ArkynConfigProps = {
|
|
2
|
+
arkynTrafficSourceId: string;
|
|
3
|
+
arkynUserToken: string;
|
|
4
|
+
arkynApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type SetArkynConfigProps = {
|
|
8
|
+
arkynTrafficSourceId: string;
|
|
9
|
+
arkynUserToken: string;
|
|
10
|
+
arkynLogBaseApiUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The `ArkynLogInstance` class manages the configuration for the arkyn flow.
|
|
15
|
+
* It allows you to set and retrieve the arkyn configuration, including the traffic source ID,
|
|
16
|
+
* user token, and API URL.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class ArkynLogInstance {
|
|
20
|
+
private static arkynConfig?: ArkynConfigProps;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets the configuration for the arkyn. This method initializes the arkyn configuration
|
|
24
|
+
* with the provided `arkynConfig` values. If the configuration has already been set,
|
|
25
|
+
* the method will return early without making any changes.
|
|
26
|
+
*
|
|
27
|
+
* @param arkynConfig - An object containing the arkyn configuration properties.
|
|
28
|
+
* @param arkynConfig.arkynTrafficSourceId - The key used to identify the arkyn.
|
|
29
|
+
* @param arkynConfig.arkynUserToken - The user token for authenticating with the arkyn.
|
|
30
|
+
* @param arkynConfig.arkynLogBaseApiUrl - (Optional) The API URL for the arkyn. If not provided,
|
|
31
|
+
* a default URL will be used.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
static setArkynConfig(arkynConfig: SetArkynConfigProps) {
|
|
35
|
+
if (!!this.arkynConfig) return;
|
|
36
|
+
|
|
37
|
+
let defaultArkynURL = `https://logs-arkyn-flow-logs.vw6wo7.easypanel.host`;
|
|
38
|
+
let arkynLogBaseApiUrl = arkynConfig.arkynLogBaseApiUrl || defaultArkynURL;
|
|
39
|
+
|
|
40
|
+
arkynLogBaseApiUrl =
|
|
41
|
+
arkynLogBaseApiUrl + "/http-traffic-records/:trafficSourceId";
|
|
42
|
+
|
|
43
|
+
this.arkynConfig = {
|
|
44
|
+
arkynTrafficSourceId: arkynConfig.arkynTrafficSourceId,
|
|
45
|
+
arkynUserToken: arkynConfig.arkynUserToken,
|
|
46
|
+
arkynApiUrl: arkynLogBaseApiUrl,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves the current arkyn configuration for the ArkynLogInstance.
|
|
52
|
+
*
|
|
53
|
+
* @returns {ArkynConfigProps | undefined} The current arkyn configuration if set,
|
|
54
|
+
* or `undefined` if no configuration has been initialized.
|
|
55
|
+
*/
|
|
56
|
+
static getArkynConfig(): ArkynConfigProps | undefined {
|
|
57
|
+
return this.arkynConfig;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resets the arkyn configuration to `undefined`.
|
|
62
|
+
* This method can be used to clear the current configuration.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
static resetArkynConfig() {
|
|
66
|
+
this.arkynConfig = undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { ArkynLogInstance };
|
package/src/index.ts
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
|
|
|
5
5
|
// http bad responses
|
|
6
6
|
export { BadGateway } from "./http/badResponses/badGateway";
|