@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.
Files changed (51) hide show
  1. package/dist/api/deleteRequest.d.ts +13 -0
  2. package/dist/api/deleteRequest.d.ts.map +1 -0
  3. package/dist/api/deleteRequest.js +14 -0
  4. package/dist/api/getRequest.d.ts +12 -0
  5. package/dist/api/getRequest.d.ts.map +1 -0
  6. package/dist/api/getRequest.js +13 -0
  7. package/dist/api/inboxFlowRequest.d.ts +40 -0
  8. package/dist/api/inboxFlowRequest.d.ts.map +1 -0
  9. package/dist/api/inboxFlowRequest.js +63 -0
  10. package/dist/api/makeRequest.d.ts +38 -0
  11. package/dist/api/makeRequest.d.ts.map +1 -0
  12. package/dist/api/makeRequest.js +103 -0
  13. package/dist/api/patchRequest.d.ts +13 -0
  14. package/dist/api/patchRequest.d.ts.map +1 -0
  15. package/dist/api/patchRequest.js +14 -0
  16. package/dist/api/postRequest.d.ts +13 -0
  17. package/dist/api/postRequest.d.ts.map +1 -0
  18. package/dist/api/postRequest.js +14 -0
  19. package/dist/api/putRequest.d.ts +13 -0
  20. package/dist/api/putRequest.d.ts.map +1 -0
  21. package/dist/api/putRequest.js +14 -0
  22. package/dist/config/apiInstance.d.ts +80 -0
  23. package/dist/config/apiInstance.d.ts.map +1 -0
  24. package/dist/config/apiInstance.js +111 -0
  25. package/dist/config/inboxFlowInstance.d.ts +44 -0
  26. package/dist/config/inboxFlowInstance.d.ts.map +1 -0
  27. package/dist/config/inboxFlowInstance.js +46 -0
  28. package/dist/index.d.ts +3 -1
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +7 -1
  31. package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
  32. package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
  33. package/dist/services/decodeErrorMessageFromRequest.js +30 -0
  34. package/dist/services/httpDebug.js +2 -2
  35. package/package.json +1 -1
  36. package/src/api/deleteRequest.ts +22 -0
  37. package/src/api/getRequest.ts +20 -0
  38. package/src/api/inboxFlowRequest.ts +76 -0
  39. package/src/api/makeRequest.ts +117 -0
  40. package/src/api/patchRequest.ts +22 -0
  41. package/src/api/postRequest.ts +22 -0
  42. package/src/api/putRequest.ts +22 -0
  43. package/src/config/apiInstance.ts +148 -0
  44. package/src/config/inboxFlowInstance.ts +65 -0
  45. package/src/index.ts +7 -1
  46. package/src/services/decodeErrorMessageFromRequest.ts +36 -0
  47. package/src/services/httpDebug.ts +2 -2
  48. package/dist/config/arkynInstance.d.ts +0 -28
  49. package/dist/config/arkynInstance.d.ts.map +0 -1
  50. package/dist/config/arkynInstance.js +0 -28
  51. package/src/config/arkynInstance.ts +0 -37
@@ -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 };
package/src/index.ts CHANGED
@@ -1,5 +1,8 @@
1
- export { ArkynInstance } from "./config/arkynInstance";
1
+ // config
2
+ export { ApiInstance } from "./config/apiInstance";
3
+ export { InboxFlowInstance } from "./config/inboxFlowInstance";
2
4
 
5
+ // http bad responses
3
6
  export { BadGateway } from "./http/badResponses/badGateway";
4
7
  export { BadRequest } from "./http/badResponses/badRequest";
5
8
  export { Conflict } from "./http/badResponses/conflict";
@@ -10,12 +13,15 @@ export { ServerError } from "./http/badResponses/serverError";
10
13
  export { Unauthorized } from "./http/badResponses/unauthorized";
11
14
  export { UnprocessableEntity } from "./http/badResponses/unprocessableEntity";
12
15
 
16
+ // http success responses
13
17
  export { Created } from "./http/successResponses/created";
14
18
  export { Found } from "./http/successResponses/found";
15
19
  export { NoContent } from "./http/successResponses/noContent";
16
20
  export { Success } from "./http/successResponses/success";
17
21
  export { Updated } from "./http/successResponses/updated";
18
22
 
23
+ // services
24
+ export { decodeErrorMessageFromRequest } from "./services/decodeErrorMessageFromRequest";
19
25
  export { decodeRequestBody } from "./services/decodeRequestBody";
20
26
  export { errorHandler } from "./services/errorHandler";
21
27
  export { formParse } from "./services/formParse";
@@ -0,0 +1,36 @@
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
+
16
+ function decodeErrorMessageFromRequest(data: any, response: Response): string {
17
+ if (data?.message && typeof data?.message === "string") {
18
+ return data?.message;
19
+ }
20
+
21
+ if (data?.error && typeof data?.error === "string") {
22
+ return data?.error;
23
+ }
24
+
25
+ if (data?.error?.message && typeof data?.error?.message === "string") {
26
+ return data?.error?.message;
27
+ }
28
+
29
+ if (response?.statusText && typeof response?.statusText === "string") {
30
+ return response?.statusText;
31
+ }
32
+
33
+ return "Missing error message";
34
+ }
35
+
36
+ export { decodeErrorMessageFromRequest };
@@ -1,4 +1,4 @@
1
- import { ArkynInstance } from "../config/arkynInstance";
1
+ import { InboxFlowInstance } from "../config/inboxFlowInstance";
2
2
  import { getCaller } from "../services/getCaller";
3
3
 
4
4
  /**
@@ -56,7 +56,7 @@ function httpDebug(name: string, body: any, cause?: any) {
56
56
  }
57
57
 
58
58
  console.log(consoleData);
59
- const arkynKeys = ArkynInstance.getInboxKeys();
59
+ const arkynKeys = InboxFlowInstance.getInboxConfig();
60
60
  if (arkynKeys) console.log(arkynKeys);
61
61
  }
62
62
  }
@@ -1,28 +0,0 @@
1
- type InboxKeysProps = {
2
- inboxKey: string;
3
- inboxUserToken: string;
4
- };
5
- /**
6
- * The `ArkynInstance` class is a singleton-like utility designed to manage and provide access
7
- * to inbox configuration keys (`InboxKeysProps`) within the application. It ensures that the
8
- * inbox keys are set before they can be retrieved, throwing an error if they are accessed
9
- * without being initialized.
10
- *
11
- * @example
12
- * ```typescript
13
- * // Setting the inbox keys
14
- * ArkynInstance.setInboxKeys({ key1: "value1", key2: "value2" });
15
- *
16
- * // Retrieving the inbox keys
17
- * const inboxKeys = ArkynInstance.getInboxKeys();
18
- * console.log(inboxKeys);
19
- * ```
20
- */
21
- declare class ArkynInstance {
22
- static inboxKeys?: InboxKeysProps;
23
- constructor();
24
- static setInboxKeys(arkynConfig: InboxKeysProps): void;
25
- static getInboxKeys(): InboxKeysProps | undefined;
26
- }
27
- export { ArkynInstance };
28
- //# sourceMappingURL=arkynInstance.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"arkynInstance.d.ts","sourceRoot":"","sources":["../../src/config/arkynInstance.ts"],"names":[],"mappings":"AAAA,KAAK,cAAc,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AAEH,cAAM,aAAa;IACjB,MAAM,CAAC,SAAS,CAAC,EAAE,cAAc,CAAC;;IAIlC,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,cAAc;IAI/C,MAAM,CAAC,YAAY;CAGpB;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -1,28 +0,0 @@
1
- /**
2
- * The `ArkynInstance` class is a singleton-like utility designed to manage and provide access
3
- * to inbox configuration keys (`InboxKeysProps`) within the application. It ensures that the
4
- * inbox keys are set before they can be retrieved, throwing an error if they are accessed
5
- * without being initialized.
6
- *
7
- * @example
8
- * ```typescript
9
- * // Setting the inbox keys
10
- * ArkynInstance.setInboxKeys({ key1: "value1", key2: "value2" });
11
- *
12
- * // Retrieving the inbox keys
13
- * const inboxKeys = ArkynInstance.getInboxKeys();
14
- * console.log(inboxKeys);
15
- * ```
16
- */
17
- class ArkynInstance {
18
- static inboxKeys;
19
- constructor() { }
20
- static setInboxKeys(arkynConfig) {
21
- if (!this.inboxKeys)
22
- this.inboxKeys = arkynConfig;
23
- }
24
- static getInboxKeys() {
25
- return this.inboxKeys;
26
- }
27
- }
28
- export { ArkynInstance };
@@ -1,37 +0,0 @@
1
- type InboxKeysProps = {
2
- inboxKey: string;
3
- inboxUserToken: string;
4
- };
5
-
6
- /**
7
- * The `ArkynInstance` class is a singleton-like utility designed to manage and provide access
8
- * to inbox configuration keys (`InboxKeysProps`) within the application. It ensures that the
9
- * inbox keys are set before they can be retrieved, throwing an error if they are accessed
10
- * without being initialized.
11
- *
12
- * @example
13
- * ```typescript
14
- * // Setting the inbox keys
15
- * ArkynInstance.setInboxKeys({ key1: "value1", key2: "value2" });
16
- *
17
- * // Retrieving the inbox keys
18
- * const inboxKeys = ArkynInstance.getInboxKeys();
19
- * console.log(inboxKeys);
20
- * ```
21
- */
22
-
23
- class ArkynInstance {
24
- static inboxKeys?: InboxKeysProps;
25
-
26
- constructor() {}
27
-
28
- static setInboxKeys(arkynConfig: InboxKeysProps) {
29
- if (!this.inboxKeys) this.inboxKeys = arkynConfig;
30
- }
31
-
32
- static getInboxKeys() {
33
- return this.inboxKeys;
34
- }
35
- }
36
-
37
- export { ArkynInstance };