@arkyn/server 2.0.1-beta.3 → 2.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/LICENSE.txt +24 -0
- package/README.md +116 -0
- package/dist/api/deleteRequest.d.ts +13 -0
- package/dist/api/deleteRequest.d.ts.map +1 -0
- package/dist/api/deleteRequest.js +14 -0
- package/dist/api/getRequest.d.ts +12 -0
- package/dist/api/getRequest.d.ts.map +1 -0
- package/dist/api/getRequest.js +13 -0
- package/dist/api/inboxFlowRequest.d.ts +40 -0
- package/dist/api/inboxFlowRequest.d.ts.map +1 -0
- package/dist/api/inboxFlowRequest.js +63 -0
- package/dist/api/makeRequest.d.ts +38 -0
- package/dist/api/makeRequest.d.ts.map +1 -0
- package/dist/api/makeRequest.js +103 -0
- package/dist/api/patchRequest.d.ts +13 -0
- package/dist/api/patchRequest.d.ts.map +1 -0
- package/dist/api/patchRequest.js +14 -0
- package/dist/api/postRequest.d.ts +13 -0
- package/dist/api/postRequest.d.ts.map +1 -0
- package/dist/api/postRequest.js +14 -0
- package/dist/api/putRequest.d.ts +13 -0
- package/dist/api/putRequest.d.ts.map +1 -0
- package/dist/api/putRequest.js +14 -0
- package/dist/config/apiInstance.d.ts +80 -0
- package/dist/config/apiInstance.d.ts.map +1 -0
- package/dist/config/apiInstance.js +111 -0
- package/dist/config/inboxFlowInstance.d.ts +44 -0
- package/dist/config/inboxFlowInstance.d.ts.map +1 -0
- package/dist/config/inboxFlowInstance.js +46 -0
- package/dist/http/badResponses/badGateway.d.ts +23 -0
- package/dist/http/badResponses/badGateway.d.ts.map +1 -1
- package/dist/http/badResponses/badGateway.js +24 -1
- package/dist/http/badResponses/badRequest.d.ts +23 -0
- package/dist/http/badResponses/badRequest.d.ts.map +1 -1
- package/dist/http/badResponses/badRequest.js +24 -1
- package/dist/http/badResponses/conflict.d.ts +23 -0
- package/dist/http/badResponses/conflict.d.ts.map +1 -1
- package/dist/http/badResponses/conflict.js +24 -1
- package/dist/http/badResponses/forbidden.d.ts +23 -0
- package/dist/http/badResponses/forbidden.d.ts.map +1 -1
- package/dist/http/badResponses/forbidden.js +25 -2
- package/dist/http/badResponses/notFound.d.ts +23 -0
- package/dist/http/badResponses/notFound.d.ts.map +1 -1
- package/dist/http/badResponses/notFound.js +24 -1
- package/dist/http/badResponses/notImplemented.d.ts +23 -0
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
- package/dist/http/badResponses/notImplemented.js +24 -1
- package/dist/http/badResponses/serverError.d.ts +23 -0
- package/dist/http/badResponses/serverError.d.ts.map +1 -1
- package/dist/http/badResponses/serverError.js +24 -1
- package/dist/http/badResponses/unauthorized.d.ts +23 -0
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
- package/dist/http/badResponses/unauthorized.js +24 -1
- package/dist/http/badResponses/unprocessableEntity.d.ts +28 -1
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
- package/dist/http/badResponses/unprocessableEntity.js +30 -3
- package/dist/http/successResponses/created.d.ts +25 -0
- package/dist/http/successResponses/created.d.ts.map +1 -1
- package/dist/http/successResponses/created.js +25 -0
- package/dist/http/successResponses/found.d.ts +28 -0
- package/dist/http/successResponses/found.d.ts.map +1 -1
- package/dist/http/successResponses/found.js +28 -0
- package/dist/http/successResponses/noContent.d.ts +16 -1
- package/dist/http/successResponses/noContent.d.ts.map +1 -1
- package/dist/http/successResponses/noContent.js +16 -8
- package/dist/http/successResponses/success.d.ts +25 -0
- package/dist/http/successResponses/success.d.ts.map +1 -1
- package/dist/http/successResponses/success.js +25 -0
- package/dist/http/successResponses/updated.d.ts +25 -0
- package/dist/http/successResponses/updated.d.ts.map +1 -1
- package/dist/http/successResponses/updated.js +25 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
- package/dist/services/decodeErrorMessageFromRequest.js +30 -0
- package/dist/services/decodeRequestBody.d.ts.map +1 -1
- package/dist/services/decodeRequestBody.js +12 -16
- package/dist/services/errorHandler.d.ts +42 -1
- package/dist/services/errorHandler.d.ts.map +1 -1
- package/dist/services/errorHandler.js +42 -0
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +4 -2
- package/dist/services/getCaller.d.ts +17 -0
- package/dist/services/getCaller.d.ts.map +1 -0
- package/dist/services/getCaller.js +45 -0
- package/dist/services/httpDebug.d.ts +35 -0
- package/dist/services/httpDebug.d.ts.map +1 -0
- package/dist/services/httpDebug.js +56 -0
- package/dist/services/schemaValidator.d.ts +13 -0
- package/dist/services/schemaValidator.d.ts.map +1 -0
- package/dist/services/schemaValidator.js +51 -0
- package/package.json +13 -6
- package/src/api/deleteRequest.ts +22 -0
- package/src/api/getRequest.ts +20 -0
- package/src/api/inboxFlowRequest.ts +76 -0
- package/src/api/makeRequest.ts +117 -0
- package/src/api/patchRequest.ts +22 -0
- package/src/api/postRequest.ts +22 -0
- package/src/api/putRequest.ts +22 -0
- package/src/config/apiInstance.ts +148 -0
- package/src/config/inboxFlowInstance.ts +65 -0
- package/src/http/badResponses/badGateway.ts +30 -3
- package/src/http/badResponses/badRequest.ts +30 -3
- package/src/http/badResponses/conflict.ts +30 -3
- package/src/http/badResponses/forbidden.ts +31 -4
- package/src/http/badResponses/notFound.ts +30 -3
- package/src/http/badResponses/notImplemented.ts +30 -3
- package/src/http/badResponses/serverError.ts +30 -3
- package/src/http/badResponses/unauthorized.ts +30 -3
- package/src/http/badResponses/unprocessableEntity.ts +36 -5
- package/src/http/successResponses/created.ts +31 -2
- package/src/http/successResponses/found.ts +32 -0
- package/src/http/successResponses/noContent.ts +20 -11
- package/src/http/successResponses/success.ts +31 -2
- package/src/http/successResponses/updated.ts +31 -2
- package/src/index.ts +11 -0
- package/src/services/decodeErrorMessageFromRequest.ts +36 -0
- package/src/services/decodeRequestBody.ts +11 -15
- package/src/services/errorHandler.ts +45 -1
- package/src/services/formParse.ts +4 -5
- package/src/services/getCaller.ts +58 -0
- package/src/services/httpDebug.ts +64 -0
- package/src/services/schemaValidator.ts +66 -0
- package/vitest.config.ts +5 -0
- package/dist/http/httpDebug.d.ts +0 -3
- package/dist/http/httpDebug.d.ts.map +0 -1
- package/dist/http/httpDebug.js +0 -15
- package/src/http/httpDebug.ts +0 -31
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Schema, z } from "zod";
|
|
2
|
+
import { ServerError } from "../http/badResponses/serverError";
|
|
3
|
+
import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
|
|
4
|
+
import { formParse } from "./formParse";
|
|
5
|
+
import { getCaller } from "./getCaller";
|
|
6
|
+
import { httpDebug } from "./httpDebug";
|
|
7
|
+
function formatErrorMessage(error) {
|
|
8
|
+
const title = "Error validating:";
|
|
9
|
+
const lines = error.issues.map(({ path, message }) => `-> ${path.join(".")}: ${message}`);
|
|
10
|
+
return [title, ...lines].join("\n");
|
|
11
|
+
}
|
|
12
|
+
class SchemaValidator {
|
|
13
|
+
schema;
|
|
14
|
+
functionName;
|
|
15
|
+
callerInfo;
|
|
16
|
+
constructor(schema) {
|
|
17
|
+
this.schema = schema;
|
|
18
|
+
const { callerInfo, functionName } = getCaller();
|
|
19
|
+
this.callerInfo = callerInfo;
|
|
20
|
+
this.functionName = functionName;
|
|
21
|
+
}
|
|
22
|
+
isValid(data) {
|
|
23
|
+
return this.schema.safeParse(data).success;
|
|
24
|
+
}
|
|
25
|
+
safeValidate(data) {
|
|
26
|
+
return this.schema.safeParse(data);
|
|
27
|
+
}
|
|
28
|
+
validate(data) {
|
|
29
|
+
try {
|
|
30
|
+
return this.schema.parse(data);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
throw new ServerError(formatErrorMessage(error));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
formValidate(data, message) {
|
|
37
|
+
const formParsed = formParse([data, this.schema]);
|
|
38
|
+
if (!formParsed.success) {
|
|
39
|
+
httpDebug("UnprocessableEntity", formParsed);
|
|
40
|
+
const firstErrorKey = Object.keys(formParsed.fieldErrors)[0];
|
|
41
|
+
throw new UnprocessableEntity({
|
|
42
|
+
fields: formParsed.fields,
|
|
43
|
+
fieldErrors: formParsed.fieldErrors,
|
|
44
|
+
data: { scrollTo: firstErrorKey },
|
|
45
|
+
message,
|
|
46
|
+
}, false);
|
|
47
|
+
}
|
|
48
|
+
return formParsed.data;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export { SchemaValidator };
|
package/package.json
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkyn/server",
|
|
3
|
-
"version": "2.0.1-beta.
|
|
3
|
+
"version": "2.0.1-beta.30",
|
|
4
4
|
"main": "./dist/bundle.js",
|
|
5
5
|
"module": "./src/index.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"author": "Arkyn | Lucas Gonçalves",
|
|
10
|
+
"description": "Server-side utilities for projects.",
|
|
8
11
|
"scripts": {
|
|
9
12
|
"clean": "rm -rf dist",
|
|
10
13
|
"build": "bun run clean && bunx tsc --project tsconfig.json",
|
|
14
|
+
"test": "vitest --config vitest.config.ts",
|
|
11
15
|
"typecheck": "bunx tsc --project tsconfig.json --noEmit"
|
|
12
16
|
},
|
|
13
17
|
"dependencies": {
|
|
14
|
-
"@arkyn/server": "*",
|
|
15
18
|
"@arkyn/shared": "*",
|
|
16
|
-
"@aws-sdk/client-s3": "^3.
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
+
"@aws-sdk/client-s3": "^3.782.0",
|
|
20
|
+
"sharp": "^0.33.5",
|
|
21
|
+
"zod": "^3.24.2"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@remix-run/node": "^2.16.4"
|
|
19
25
|
},
|
|
20
26
|
"devDependencies": {
|
|
21
27
|
"bun-types": "latest",
|
|
22
|
-
"
|
|
28
|
+
"vitest": "^3.1.1",
|
|
29
|
+
"typescript": "^5.8.3"
|
|
23
30
|
}
|
|
24
31
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a DELETE request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the DELETE request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} [body] - Optional body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function deleteRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body?: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("DELETE", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { deleteRequest };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a GET request to the specified URL with optional headers.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the GET request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
async function getRequest<T = any>(
|
|
14
|
+
url: string,
|
|
15
|
+
headers: HeadersInit = {}
|
|
16
|
+
): Promise<ApiResponseDTO<T>> {
|
|
17
|
+
return makeRequest("GET", url, headers);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { getRequest };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
2
|
+
import { httpDebug } from "../services/httpDebug";
|
|
3
|
+
|
|
4
|
+
type ConfigProps = {
|
|
5
|
+
status: number;
|
|
6
|
+
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "ERROR";
|
|
7
|
+
request: string;
|
|
8
|
+
response: string;
|
|
9
|
+
token: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sends a request to the inbox flow API with the provided configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param config - The configuration object for the request.
|
|
16
|
+
* @param config.status - The HTTP status code associated with the request.
|
|
17
|
+
* @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", "PATCH", or "ERROR".
|
|
18
|
+
* @param config.request - The request payload or details as a string.
|
|
19
|
+
* @param config.response - The response payload or details as a string.
|
|
20
|
+
* @param config.token - The token associated with the request for authentication or identification.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
|
|
24
|
+
* - If the configuration is not available, the function will return early without performing any action.
|
|
25
|
+
* - In a development environment (`NODE_ENV === "development"`), the function will also return early.
|
|
26
|
+
* - The request is sent as a POST request to the inbox API URL with the provided configuration details.
|
|
27
|
+
* - If an error occurs during the request, it will be logged using the `httpDebug` service.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const config = {
|
|
32
|
+
* status: 200,
|
|
33
|
+
* method: "POST",
|
|
34
|
+
* request: JSON.stringify({ key: "value" }),
|
|
35
|
+
* response: JSON.stringify({ success: true }),
|
|
36
|
+
* token: "example-token",
|
|
37
|
+
* };
|
|
38
|
+
*
|
|
39
|
+
* await inboxFlowRequest(config);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
async function inboxFlowRequest(config: ConfigProps) {
|
|
44
|
+
const inboxFlowInstance = InboxFlowInstance.getInboxConfig();
|
|
45
|
+
if (!inboxFlowInstance) return;
|
|
46
|
+
|
|
47
|
+
const { inboxChannelId, inboxUserToken, inboxApiUrl } = inboxFlowInstance;
|
|
48
|
+
|
|
49
|
+
const { status, method, request, response, token } = config;
|
|
50
|
+
|
|
51
|
+
if (process.env.NODE_ENV === "development") return;
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const body = JSON.stringify({
|
|
55
|
+
status,
|
|
56
|
+
channelId: inboxChannelId,
|
|
57
|
+
method,
|
|
58
|
+
token,
|
|
59
|
+
request,
|
|
60
|
+
response,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await fetch(inboxApiUrl, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
body,
|
|
66
|
+
headers: {
|
|
67
|
+
"Content-Type": "application/json",
|
|
68
|
+
Authorization: `Bearer ${inboxUserToken}`,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
} catch (err) {
|
|
72
|
+
httpDebug("inboxFlowRequest", "Error sending inbox flow request", err);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { inboxFlowRequest };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
3
|
+
import { inboxFlowRequest } from "./inboxFlowRequest";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Makes an HTTP request using the Fetch API and returns a standardized response.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The expected type of the response data.
|
|
9
|
+
* @param method - The HTTP method to use for the request. Supported methods are:
|
|
10
|
+
* - "POST": Create a new resource.
|
|
11
|
+
* - "PUT": Update an existing resource.
|
|
12
|
+
* - "DELETE": Remove a resource.
|
|
13
|
+
* - "PATCH": Partially update a resource.
|
|
14
|
+
* - "GET": Retrieve a resource.
|
|
15
|
+
* @param url - The URL to which the request is sent.
|
|
16
|
+
* @param headers - Optional headers to include in the request. Defaults to an empty object.
|
|
17
|
+
* @param body - Optional body to include in the request. Should be serializable to JSON.
|
|
18
|
+
* @returns A promise that resolves to an `ApiResponseDTO<T>` object containing:
|
|
19
|
+
* - `success`: A boolean indicating whether the request was successful.
|
|
20
|
+
* - `status`: The HTTP status code of the response.
|
|
21
|
+
* - `message`: A message describing the result of the request.
|
|
22
|
+
* - `response`: The parsed JSON response data, or `null` if parsing fails.
|
|
23
|
+
* - `cause`: Additional error information, if applicable.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { makeRequest } from "./makeRequest";
|
|
28
|
+
*
|
|
29
|
+
* async function fetchData() {
|
|
30
|
+
* const response = await makeRequest("GET", "https://api.example.com/data");
|
|
31
|
+
* if (response.success) {
|
|
32
|
+
* console.log("Data:", response.response);
|
|
33
|
+
* } else {
|
|
34
|
+
* console.error("Error:", response.message);
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
async function makeRequest<T = any>(
|
|
41
|
+
method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET",
|
|
42
|
+
url: string,
|
|
43
|
+
headers: HeadersInit = {},
|
|
44
|
+
body?: any
|
|
45
|
+
): Promise<ApiResponseDTO<T>> {
|
|
46
|
+
const successMessage = {
|
|
47
|
+
POST: "Resource created successfully",
|
|
48
|
+
PUT: "Resource updated successfully",
|
|
49
|
+
DELETE: "Resource deleted successfully",
|
|
50
|
+
PATCH: "Resource patched successfully",
|
|
51
|
+
GET: "Request successful",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(url, {
|
|
56
|
+
method,
|
|
57
|
+
headers: {
|
|
58
|
+
...headers,
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
},
|
|
61
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const status = response.status;
|
|
65
|
+
|
|
66
|
+
let data: any = null;
|
|
67
|
+
try {
|
|
68
|
+
data = await response.json();
|
|
69
|
+
} catch {
|
|
70
|
+
data = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
inboxFlowRequest({
|
|
74
|
+
method,
|
|
75
|
+
status,
|
|
76
|
+
request: JSON.stringify(response.headers),
|
|
77
|
+
response: JSON.stringify(data),
|
|
78
|
+
token: (headers as any)?.Authorization || "TOKEN_NOT_FOUND",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
return {
|
|
83
|
+
success: false,
|
|
84
|
+
status,
|
|
85
|
+
message: data?.message || response.statusText || "Request failed",
|
|
86
|
+
response: data,
|
|
87
|
+
cause: null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
success: true,
|
|
93
|
+
status,
|
|
94
|
+
message: data?.message || successMessage[method],
|
|
95
|
+
response: data,
|
|
96
|
+
cause: null,
|
|
97
|
+
};
|
|
98
|
+
} catch (err) {
|
|
99
|
+
inboxFlowRequest({
|
|
100
|
+
method,
|
|
101
|
+
request: JSON.stringify(headers),
|
|
102
|
+
response: String(err),
|
|
103
|
+
status: 500,
|
|
104
|
+
token: "TOKEN_NOT_FOUND",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
status: 0,
|
|
110
|
+
message: "Network error or request failed",
|
|
111
|
+
response: null,
|
|
112
|
+
cause: err instanceof Error ? err.message : String(err),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { makeRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function patchRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("PATCH", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { patchRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function postRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("POST", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { postRequest };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ApiResponseDTO } from "@arkyn/types";
|
|
2
|
+
import { makeRequest } from "./makeRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The expected type of the response data.
|
|
8
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
9
|
+
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
+
* @param {any} body - The body to include in the request.
|
|
11
|
+
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function putRequest<T = any>(
|
|
15
|
+
url: string,
|
|
16
|
+
headers: HeadersInit = {},
|
|
17
|
+
body: any
|
|
18
|
+
): Promise<ApiResponseDTO<T>> {
|
|
19
|
+
return makeRequest("PUT", url, headers, body);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { putRequest };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { deleteRequest } from "../api/deleteRequest";
|
|
2
|
+
import { getRequest } from "../api/getRequest";
|
|
3
|
+
import { patchRequest } from "../api/patchRequest";
|
|
4
|
+
import { postRequest } from "../api/postRequest";
|
|
5
|
+
import { putRequest } from "../api/putRequest";
|
|
6
|
+
|
|
7
|
+
type ApiAinstanceContructorProps = {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
baseHeaders?: HeadersInit;
|
|
10
|
+
baseToken?: string | null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type ApiRequestDataWithoutBodyProps = {
|
|
14
|
+
headers?: HeadersInit;
|
|
15
|
+
token?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type ApiRequestDataWithBodyProps = {
|
|
19
|
+
body?: any;
|
|
20
|
+
headers?: HeadersInit;
|
|
21
|
+
token?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Class representing an API instance to handle HTTP requests with base configurations.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
class ApiInstance {
|
|
29
|
+
private baseUrl: string;
|
|
30
|
+
private baseHeaders?: HeadersInit;
|
|
31
|
+
private baseToken?: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates an instance of ApiInstance.
|
|
35
|
+
* @param props - The configuration properties for the API instance.
|
|
36
|
+
* @param props.baseUrl - The base URL for the API.
|
|
37
|
+
* @param props.baseHeaders - Optional base headers to include in all requests.
|
|
38
|
+
* @param props.baseToken - Optional base token for authorization.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
constructor(props: ApiAinstanceContructorProps) {
|
|
42
|
+
this.baseUrl = props.baseUrl;
|
|
43
|
+
this.baseHeaders = props.baseHeaders || undefined;
|
|
44
|
+
this.baseToken = props.baseToken || undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Generates the full URL by appending the route to the base URL.
|
|
49
|
+
* @param route - The route to append to the base URL.
|
|
50
|
+
* @returns The full URL as a string.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
private generateURL(route: string) {
|
|
54
|
+
return this.baseUrl + route;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Generates the headers for a request by merging base headers, provided headers, and tokens.
|
|
59
|
+
* @param initHeaders - Initial headers to include in the request.
|
|
60
|
+
* @param token - Optional token to override the base token.
|
|
61
|
+
* @returns The merged headers as a HeadersInit object.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
private generateHeaders(
|
|
65
|
+
initHeaders: HeadersInit,
|
|
66
|
+
token?: string
|
|
67
|
+
): HeadersInit {
|
|
68
|
+
let headers: HeadersInit = {};
|
|
69
|
+
if (this.baseToken) headers = { Authorization: `Bearer ${this.baseToken}` };
|
|
70
|
+
if (this.baseHeaders) headers = { ...headers, ...this.baseHeaders };
|
|
71
|
+
|
|
72
|
+
if (initHeaders) headers = { ...headers, ...initHeaders };
|
|
73
|
+
if (token) headers = { ...headers, Authorization: `Bearer ${token}` };
|
|
74
|
+
|
|
75
|
+
return headers;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sends a GET request to the specified route.
|
|
80
|
+
* @param route - The API route to send the GET request to.
|
|
81
|
+
* @param data - The request data, including optional headers and token.
|
|
82
|
+
* @returns The API response data.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
async GET(route: string, data?: ApiRequestDataWithoutBodyProps) {
|
|
86
|
+
const url = this.generateURL(route);
|
|
87
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
88
|
+
return await getRequest(url, headers);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sends a POST request to the specified route.
|
|
93
|
+
* @param route - The API route to send the POST request to.
|
|
94
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
95
|
+
* @returns The API response data.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
async POST(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
99
|
+
const url = this.generateURL(route);
|
|
100
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
101
|
+
const body = data?.body;
|
|
102
|
+
return await postRequest(url, headers, body);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Sends a PUT request to the specified route.
|
|
107
|
+
* @param route - The API route to send the PUT request to.
|
|
108
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
109
|
+
* @returns The API response data.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
async PUT(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
113
|
+
const url = this.generateURL(route);
|
|
114
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
115
|
+
const body = data?.body;
|
|
116
|
+
return await putRequest(url, headers, body);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sends a PATCH request to the specified route.
|
|
121
|
+
* @param route - The API route to send the PATCH request to.
|
|
122
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
123
|
+
* @returns The API response data.
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
async PATCH(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
127
|
+
const url = this.generateURL(route);
|
|
128
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
129
|
+
const body = data?.body;
|
|
130
|
+
return await patchRequest(url, headers, body);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sends a DELETE request to the specified route.
|
|
135
|
+
* @param route - The API route to send the DELETE request to.
|
|
136
|
+
* @param data - The request data, including body, optional headers, and token.
|
|
137
|
+
* @returns The API response data.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
async DELETE(route: string, data?: ApiRequestDataWithBodyProps) {
|
|
141
|
+
const url = this.generateURL(route);
|
|
142
|
+
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
143
|
+
const body = data?.body;
|
|
144
|
+
return await deleteRequest(url, headers, body);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { ApiInstance };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
type InboxConfigProps = {
|
|
2
|
+
inboxChannelId: string;
|
|
3
|
+
inboxUserToken: string;
|
|
4
|
+
inboxApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type SetInboxConfigProps = {
|
|
8
|
+
inboxChannelId: string;
|
|
9
|
+
inboxUserToken: string;
|
|
10
|
+
inboxApiUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
15
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
16
|
+
* user token, and API URL.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class InboxFlowInstance {
|
|
20
|
+
private static inboxConfig?: InboxConfigProps;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
24
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
25
|
+
* the method will return early without making any changes.
|
|
26
|
+
*
|
|
27
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
28
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
29
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
30
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
31
|
+
* a default URL will be used.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
static setInboxConfig(inboxConfig: SetInboxConfigProps) {
|
|
35
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
36
|
+
if (!!this.inboxConfig) return;
|
|
37
|
+
|
|
38
|
+
this.inboxConfig = {
|
|
39
|
+
inboxChannelId: inboxConfig.inboxChannelId,
|
|
40
|
+
inboxUserToken: inboxConfig.inboxUserToken,
|
|
41
|
+
inboxApiUrl: inboxConfig.inboxApiUrl || defaultInboxURL,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
47
|
+
*
|
|
48
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
49
|
+
* or `undefined` if no configuration has been initialized.
|
|
50
|
+
*/
|
|
51
|
+
static getInboxConfig(): InboxConfigProps | undefined {
|
|
52
|
+
return this.inboxConfig;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Resets the inbox configuration to `undefined`.
|
|
57
|
+
* This method can be used to clear the current configuration.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
static resetInboxConfig() {
|
|
61
|
+
this.inboxConfig = undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { InboxFlowInstance };
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import { httpDebug } from "
|
|
1
|
+
import { httpDebug } from "../../services/httpDebug";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents an HTTP error response with a status code of 502 (Bad Gateway).
|
|
5
|
+
* This class is used to standardize the structure of a "Bad Gateway" error response,
|
|
6
|
+
* including the response body, headers, status, and status text.
|
|
7
|
+
*/
|
|
2
8
|
|
|
3
9
|
class BadGateway {
|
|
4
10
|
body: any;
|
|
@@ -6,6 +12,13 @@ class BadGateway {
|
|
|
6
12
|
status: number = 502;
|
|
7
13
|
statusText: string;
|
|
8
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of the `BadGateway` class.
|
|
17
|
+
*
|
|
18
|
+
* @param message - A descriptive message explaining the cause of the error.
|
|
19
|
+
* @param cause - Optional additional information about the cause of the error.
|
|
20
|
+
*/
|
|
21
|
+
|
|
9
22
|
constructor(message: string, cause?: any) {
|
|
10
23
|
this.body = { name: "BadGateway", message: message };
|
|
11
24
|
this.statusText = message;
|
|
@@ -13,7 +26,14 @@ class BadGateway {
|
|
|
13
26
|
httpDebug("BadGateway", this.body, this.cause);
|
|
14
27
|
}
|
|
15
28
|
|
|
16
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Converts the `BadGateway` 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 {
|
|
17
37
|
const responseInit: ResponseInit = {
|
|
18
38
|
headers: { "Content-Type": "application/json" },
|
|
19
39
|
status: this.status,
|
|
@@ -23,7 +43,14 @@ class BadGateway {
|
|
|
23
43
|
return new Response(JSON.stringify(this.body), responseInit);
|
|
24
44
|
}
|
|
25
45
|
|
|
26
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `BadGateway` 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 {
|
|
27
54
|
const responseInit: ResponseInit = {
|
|
28
55
|
status: this.status,
|
|
29
56
|
statusText: this.statusText,
|