@arkyn/server 2.0.1-beta.12 → 2.0.1-beta.13
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/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/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- 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/httpDebug.js +2 -2
- package/package.json +1 -1
- 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/index.ts +7 -1
- package/src/services/decodeErrorMessageFromRequest.ts +36 -0
- package/src/services/httpDebug.ts +2 -2
- package/dist/config/arkynInstance.d.ts +0 -28
- package/dist/config/arkynInstance.d.ts.map +0 -1
- package/dist/config/arkynInstance.js +0 -28
- package/src/config/arkynInstance.ts +0 -37
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type InboxConfigProps = {
|
|
2
|
+
inboxChannelId: string;
|
|
3
|
+
inboxUserToken: string;
|
|
4
|
+
inboxApiUrl: string;
|
|
5
|
+
};
|
|
6
|
+
type SetInboxConfigProps = {
|
|
7
|
+
inboxChannelId: string;
|
|
8
|
+
inboxUserToken: string;
|
|
9
|
+
inboxApiUrl?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
13
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
14
|
+
* user token, and API URL.
|
|
15
|
+
*/
|
|
16
|
+
declare class InboxFlowInstance {
|
|
17
|
+
private static inboxConfig?;
|
|
18
|
+
/**
|
|
19
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
20
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
21
|
+
* the method will return early without making any changes.
|
|
22
|
+
*
|
|
23
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
24
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
25
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
26
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
27
|
+
* a default URL will be used.
|
|
28
|
+
*/
|
|
29
|
+
static setInboxConfig(inboxConfig: SetInboxConfigProps): void;
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
32
|
+
*
|
|
33
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
34
|
+
* or `undefined` if no configuration has been initialized.
|
|
35
|
+
*/
|
|
36
|
+
static getInboxConfig(): InboxConfigProps | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Resets the inbox configuration to `undefined`.
|
|
39
|
+
* This method can be used to clear the current configuration.
|
|
40
|
+
*/
|
|
41
|
+
static resetInboxConfig(): void;
|
|
42
|
+
}
|
|
43
|
+
export { InboxFlowInstance };
|
|
44
|
+
//# sourceMappingURL=inboxFlowInstance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inboxFlowInstance.d.ts","sourceRoot":"","sources":["../../src/config/inboxFlowInstance.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AAEH,cAAM,iBAAiB;IACrB,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAmB;IAE9C;;;;;;;;;;OAUG;IAEH,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,mBAAmB;IAWtD;;;;;OAKG;IACH,MAAM,CAAC,cAAc,IAAI,gBAAgB,GAAG,SAAS;IAIrD;;;OAGG;IAEH,MAAM,CAAC,gBAAgB;CAGxB;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `InboxFlowInstance` class manages the configuration for the inbox flow.
|
|
3
|
+
* It allows you to set and retrieve the inbox configuration, including the channel ID,
|
|
4
|
+
* user token, and API URL.
|
|
5
|
+
*/
|
|
6
|
+
class InboxFlowInstance {
|
|
7
|
+
static inboxConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Sets the configuration for the inbox. This method initializes the inbox configuration
|
|
10
|
+
* with the provided `inboxConfig` values. If the configuration has already been set,
|
|
11
|
+
* the method will return early without making any changes.
|
|
12
|
+
*
|
|
13
|
+
* @param inboxConfig - An object containing the inbox configuration properties.
|
|
14
|
+
* @param inboxConfig.inboxChannelId - The key used to identify the inbox.
|
|
15
|
+
* @param inboxConfig.inboxUserToken - The user token for authenticating with the inbox.
|
|
16
|
+
* @param inboxConfig.inboxApiUrl - (Optional) The API URL for the inbox. If not provided,
|
|
17
|
+
* a default URL will be used.
|
|
18
|
+
*/
|
|
19
|
+
static setInboxConfig(inboxConfig) {
|
|
20
|
+
const defaultInboxURL = `https://logs-inbox-flow-logs.vw6wo7.easypanel.host/api/call`;
|
|
21
|
+
if (!!this.inboxConfig)
|
|
22
|
+
return;
|
|
23
|
+
this.inboxConfig = {
|
|
24
|
+
inboxChannelId: inboxConfig.inboxChannelId,
|
|
25
|
+
inboxUserToken: inboxConfig.inboxUserToken,
|
|
26
|
+
inboxApiUrl: inboxConfig.inboxApiUrl || defaultInboxURL,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves the current inbox configuration for the InboxFlowInstance.
|
|
31
|
+
*
|
|
32
|
+
* @returns {InboxConfigProps | undefined} The current inbox configuration if set,
|
|
33
|
+
* or `undefined` if no configuration has been initialized.
|
|
34
|
+
*/
|
|
35
|
+
static getInboxConfig() {
|
|
36
|
+
return this.inboxConfig;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resets the inbox configuration to `undefined`.
|
|
40
|
+
* This method can be used to clear the current configuration.
|
|
41
|
+
*/
|
|
42
|
+
static resetInboxConfig() {
|
|
43
|
+
this.inboxConfig = undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export { InboxFlowInstance };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ApiInstance } from "./config/apiInstance";
|
|
2
|
+
export { InboxFlowInstance } from "./config/inboxFlowInstance";
|
|
2
3
|
export { BadGateway } from "./http/badResponses/badGateway";
|
|
3
4
|
export { BadRequest } from "./http/badResponses/badRequest";
|
|
4
5
|
export { Conflict } from "./http/badResponses/conflict";
|
|
@@ -13,6 +14,7 @@ export { Found } from "./http/successResponses/found";
|
|
|
13
14
|
export { NoContent } from "./http/successResponses/noContent";
|
|
14
15
|
export { Success } from "./http/successResponses/success";
|
|
15
16
|
export { Updated } from "./http/successResponses/updated";
|
|
17
|
+
export { decodeErrorMessageFromRequest } from "./services/decodeErrorMessageFromRequest";
|
|
16
18
|
export { decodeRequestBody } from "./services/decodeRequestBody";
|
|
17
19
|
export { errorHandler } from "./services/errorHandler";
|
|
18
20
|
export { formParse } from "./services/formParse";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAG/D,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAG9E,OAAO,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAC1D,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAG1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AACzF,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
// config
|
|
2
|
+
export { ApiInstance } from "./config/apiInstance";
|
|
3
|
+
export { InboxFlowInstance } from "./config/inboxFlowInstance";
|
|
4
|
+
// http bad responses
|
|
2
5
|
export { BadGateway } from "./http/badResponses/badGateway";
|
|
3
6
|
export { BadRequest } from "./http/badResponses/badRequest";
|
|
4
7
|
export { Conflict } from "./http/badResponses/conflict";
|
|
@@ -8,11 +11,14 @@ export { NotImplemented } from "./http/badResponses/notImplemented";
|
|
|
8
11
|
export { ServerError } from "./http/badResponses/serverError";
|
|
9
12
|
export { Unauthorized } from "./http/badResponses/unauthorized";
|
|
10
13
|
export { UnprocessableEntity } from "./http/badResponses/unprocessableEntity";
|
|
14
|
+
// http success responses
|
|
11
15
|
export { Created } from "./http/successResponses/created";
|
|
12
16
|
export { Found } from "./http/successResponses/found";
|
|
13
17
|
export { NoContent } from "./http/successResponses/noContent";
|
|
14
18
|
export { Success } from "./http/successResponses/success";
|
|
15
19
|
export { Updated } from "./http/successResponses/updated";
|
|
20
|
+
// services
|
|
21
|
+
export { decodeErrorMessageFromRequest } from "./services/decodeErrorMessageFromRequest";
|
|
16
22
|
export { decodeRequestBody } from "./services/decodeRequestBody";
|
|
17
23
|
export { errorHandler } from "./services/errorHandler";
|
|
18
24
|
export { formParse } from "./services/formParse";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes an error message from a given request data object or response object.
|
|
3
|
+
*
|
|
4
|
+
* This function attempts to extract a meaningful error message from the provided
|
|
5
|
+
* `data` or `response` objects by checking various properties in a specific order.
|
|
6
|
+
* If no valid error message is found, it returns a default message: "Missing error message".
|
|
7
|
+
*
|
|
8
|
+
* @param data - The data object that may contain error information. It can have properties
|
|
9
|
+
* such as `message`, `error`, or `error.message` that are checked for a string value.
|
|
10
|
+
* @param response - The response object that may contain a `statusText` property
|
|
11
|
+
* representing the HTTP status text.
|
|
12
|
+
* @returns A string representing the decoded error message, or a default message
|
|
13
|
+
* if no error message is found.
|
|
14
|
+
*/
|
|
15
|
+
declare function decodeErrorMessageFromRequest(data: any, response: Response): string;
|
|
16
|
+
export { decodeErrorMessageFromRequest };
|
|
17
|
+
//# sourceMappingURL=decodeErrorMessageFromRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeErrorMessageFromRequest.d.ts","sourceRoot":"","sources":["../../src/services/decodeErrorMessageFromRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,iBAAS,6BAA6B,CAAC,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAkB5E;AAED,OAAO,EAAE,6BAA6B,EAAE,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes an error message from a given request data object or response object.
|
|
3
|
+
*
|
|
4
|
+
* This function attempts to extract a meaningful error message from the provided
|
|
5
|
+
* `data` or `response` objects by checking various properties in a specific order.
|
|
6
|
+
* If no valid error message is found, it returns a default message: "Missing error message".
|
|
7
|
+
*
|
|
8
|
+
* @param data - The data object that may contain error information. It can have properties
|
|
9
|
+
* such as `message`, `error`, or `error.message` that are checked for a string value.
|
|
10
|
+
* @param response - The response object that may contain a `statusText` property
|
|
11
|
+
* representing the HTTP status text.
|
|
12
|
+
* @returns A string representing the decoded error message, or a default message
|
|
13
|
+
* if no error message is found.
|
|
14
|
+
*/
|
|
15
|
+
function decodeErrorMessageFromRequest(data, response) {
|
|
16
|
+
if (data?.message && typeof data?.message === "string") {
|
|
17
|
+
return data?.message;
|
|
18
|
+
}
|
|
19
|
+
if (data?.error && typeof data?.error === "string") {
|
|
20
|
+
return data?.error;
|
|
21
|
+
}
|
|
22
|
+
if (data?.error?.message && typeof data?.error?.message === "string") {
|
|
23
|
+
return data?.error?.message;
|
|
24
|
+
}
|
|
25
|
+
if (response?.statusText && typeof response?.statusText === "string") {
|
|
26
|
+
return response?.statusText;
|
|
27
|
+
}
|
|
28
|
+
return "Missing error message";
|
|
29
|
+
}
|
|
30
|
+
export { decodeErrorMessageFromRequest };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
2
2
|
import { getCaller } from "../services/getCaller";
|
|
3
3
|
/**
|
|
4
4
|
* Logs debug information to the console when in development mode or when the
|
|
@@ -48,7 +48,7 @@ function httpDebug(name, body, cause) {
|
|
|
48
48
|
consoleData += `${debugName} Cause: ${JSON.stringify(cause, null, 2)}\n`;
|
|
49
49
|
}
|
|
50
50
|
console.log(consoleData);
|
|
51
|
-
const arkynKeys =
|
|
51
|
+
const arkynKeys = InboxFlowInstance.getInboxConfig();
|
|
52
52
|
if (arkynKeys)
|
|
53
53
|
console.log(arkynKeys);
|
|
54
54
|
}
|
package/package.json
CHANGED
|
@@ -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 };
|