@arkyn/server 2.0.1-beta.9 → 2.0.2

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 (128) hide show
  1. package/LICENSE.txt +24 -0
  2. package/README.md +116 -0
  3. package/dist/api/deleteRequest.d.ts +13 -0
  4. package/dist/api/deleteRequest.d.ts.map +1 -0
  5. package/dist/api/deleteRequest.js +14 -0
  6. package/dist/api/getRequest.d.ts +12 -0
  7. package/dist/api/getRequest.d.ts.map +1 -0
  8. package/dist/api/getRequest.js +13 -0
  9. package/dist/api/inboxFlowRequest.d.ts +40 -0
  10. package/dist/api/inboxFlowRequest.d.ts.map +1 -0
  11. package/dist/api/inboxFlowRequest.js +63 -0
  12. package/dist/api/makeRequest.d.ts +38 -0
  13. package/dist/api/makeRequest.d.ts.map +1 -0
  14. package/dist/api/makeRequest.js +103 -0
  15. package/dist/api/patchRequest.d.ts +13 -0
  16. package/dist/api/patchRequest.d.ts.map +1 -0
  17. package/dist/api/patchRequest.js +14 -0
  18. package/dist/api/postRequest.d.ts +13 -0
  19. package/dist/api/postRequest.d.ts.map +1 -0
  20. package/dist/api/postRequest.js +14 -0
  21. package/dist/api/putRequest.d.ts +13 -0
  22. package/dist/api/putRequest.d.ts.map +1 -0
  23. package/dist/api/putRequest.js +14 -0
  24. package/dist/config/apiInstance.d.ts +80 -0
  25. package/dist/config/apiInstance.d.ts.map +1 -0
  26. package/dist/config/apiInstance.js +111 -0
  27. package/dist/config/inboxFlowInstance.d.ts +44 -0
  28. package/dist/config/inboxFlowInstance.d.ts.map +1 -0
  29. package/dist/config/inboxFlowInstance.js +46 -0
  30. package/dist/http/badResponses/badGateway.d.ts +23 -0
  31. package/dist/http/badResponses/badGateway.d.ts.map +1 -1
  32. package/dist/http/badResponses/badGateway.js +24 -1
  33. package/dist/http/badResponses/badRequest.d.ts +23 -0
  34. package/dist/http/badResponses/badRequest.d.ts.map +1 -1
  35. package/dist/http/badResponses/badRequest.js +24 -1
  36. package/dist/http/badResponses/conflict.d.ts +23 -0
  37. package/dist/http/badResponses/conflict.d.ts.map +1 -1
  38. package/dist/http/badResponses/conflict.js +24 -1
  39. package/dist/http/badResponses/forbidden.d.ts +23 -0
  40. package/dist/http/badResponses/forbidden.d.ts.map +1 -1
  41. package/dist/http/badResponses/forbidden.js +25 -2
  42. package/dist/http/badResponses/notFound.d.ts +23 -0
  43. package/dist/http/badResponses/notFound.d.ts.map +1 -1
  44. package/dist/http/badResponses/notFound.js +24 -1
  45. package/dist/http/badResponses/notImplemented.d.ts +23 -0
  46. package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
  47. package/dist/http/badResponses/notImplemented.js +24 -1
  48. package/dist/http/badResponses/serverError.d.ts +23 -0
  49. package/dist/http/badResponses/serverError.d.ts.map +1 -1
  50. package/dist/http/badResponses/serverError.js +24 -1
  51. package/dist/http/badResponses/unauthorized.d.ts +23 -0
  52. package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
  53. package/dist/http/badResponses/unauthorized.js +24 -1
  54. package/dist/http/badResponses/unprocessableEntity.d.ts +28 -1
  55. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
  56. package/dist/http/badResponses/unprocessableEntity.js +30 -3
  57. package/dist/http/successResponses/created.d.ts +25 -0
  58. package/dist/http/successResponses/created.d.ts.map +1 -1
  59. package/dist/http/successResponses/created.js +25 -0
  60. package/dist/http/successResponses/found.d.ts +28 -0
  61. package/dist/http/successResponses/found.d.ts.map +1 -1
  62. package/dist/http/successResponses/found.js +28 -0
  63. package/dist/http/successResponses/noContent.d.ts +16 -1
  64. package/dist/http/successResponses/noContent.d.ts.map +1 -1
  65. package/dist/http/successResponses/noContent.js +16 -8
  66. package/dist/http/successResponses/success.d.ts +25 -0
  67. package/dist/http/successResponses/success.d.ts.map +1 -1
  68. package/dist/http/successResponses/success.js +25 -0
  69. package/dist/http/successResponses/updated.d.ts +25 -0
  70. package/dist/http/successResponses/updated.d.ts.map +1 -1
  71. package/dist/http/successResponses/updated.js +25 -0
  72. package/dist/index.d.ts +6 -0
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/index.js +10 -0
  75. package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
  76. package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
  77. package/dist/services/decodeErrorMessageFromRequest.js +30 -0
  78. package/dist/services/decodeRequestBody.d.ts.map +1 -1
  79. package/dist/services/decodeRequestBody.js +12 -16
  80. package/dist/services/errorHandler.d.ts +42 -1
  81. package/dist/services/errorHandler.d.ts.map +1 -1
  82. package/dist/services/errorHandler.js +42 -0
  83. package/dist/services/formParse.d.ts.map +1 -1
  84. package/dist/services/formParse.js +4 -2
  85. package/dist/services/getCaller.d.ts +17 -0
  86. package/dist/services/getCaller.d.ts.map +1 -0
  87. package/dist/services/getCaller.js +45 -0
  88. package/dist/services/httpDebug.d.ts +35 -0
  89. package/dist/services/httpDebug.d.ts.map +1 -0
  90. package/dist/services/httpDebug.js +56 -0
  91. package/dist/services/schemaValidator.d.ts +13 -0
  92. package/dist/services/schemaValidator.d.ts.map +1 -0
  93. package/dist/services/schemaValidator.js +51 -0
  94. package/package.json +11 -5
  95. package/src/api/deleteRequest.ts +22 -0
  96. package/src/api/getRequest.ts +20 -0
  97. package/src/api/inboxFlowRequest.ts +76 -0
  98. package/src/api/makeRequest.ts +117 -0
  99. package/src/api/patchRequest.ts +22 -0
  100. package/src/api/postRequest.ts +22 -0
  101. package/src/api/putRequest.ts +22 -0
  102. package/src/config/apiInstance.ts +148 -0
  103. package/src/config/inboxFlowInstance.ts +65 -0
  104. package/src/http/badResponses/badGateway.ts +29 -2
  105. package/src/http/badResponses/badRequest.ts +29 -2
  106. package/src/http/badResponses/conflict.ts +29 -2
  107. package/src/http/badResponses/forbidden.ts +30 -3
  108. package/src/http/badResponses/notFound.ts +29 -2
  109. package/src/http/badResponses/notImplemented.ts +29 -2
  110. package/src/http/badResponses/serverError.ts +29 -2
  111. package/src/http/badResponses/unauthorized.ts +29 -2
  112. package/src/http/badResponses/unprocessableEntity.ts +33 -2
  113. package/src/http/successResponses/created.ts +31 -2
  114. package/src/http/successResponses/found.ts +32 -0
  115. package/src/http/successResponses/noContent.ts +20 -11
  116. package/src/http/successResponses/success.ts +31 -2
  117. package/src/http/successResponses/updated.ts +31 -2
  118. package/src/index.ts +9 -1
  119. package/src/services/decodeErrorMessageFromRequest.ts +36 -0
  120. package/src/services/decodeRequestBody.ts +11 -15
  121. package/src/services/errorHandler.ts +45 -1
  122. package/src/services/formParse.ts +4 -5
  123. package/src/services/getCaller.ts +39 -18
  124. package/src/services/httpDebug.ts +46 -5
  125. package/vitest.config.ts +5 -0
  126. package/dist/http/httpDebug.d.ts +0 -3
  127. package/dist/http/httpDebug.d.ts.map +0 -1
  128. package/dist/http/httpDebug.js +0 -15
package/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ Copyright 2025 Lucas Gonçalves
8
+
9
+ Licensed under the Apache License, Version 2.0 (the "License");
10
+ you may not use this file except in compliance with the License.
11
+ You may obtain a copy of the License at
12
+
13
+ http://www.apache.org/licenses/LICENSE-2.0
14
+
15
+ Unless required by applicable law or agreed to in writing, software
16
+ distributed under the License is distributed on an "AS IS" BASIS,
17
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ See the License for the specific language governing permissions and
19
+ limitations under the License.
20
+
21
+ ---
22
+
23
+ For more information or to contribute, visit:
24
+ https://github.com/Lucas-Eduardo-Goncalves
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ The `@arkyn/server` package provides utilities and services to streamline backend application development. It includes API configurations, HTTP error handlers, and various services for file manipulation.
2
+
3
+ ---
4
+
5
+ ## Installation
6
+
7
+ Install the package using npm:
8
+
9
+ ```bash
10
+ npm install @arkyn/server
11
+ ```
12
+
13
+ ---
14
+
15
+ ## Features
16
+
17
+ ### Settings
18
+
19
+ - **`ApiInstance`**
20
+ Pre-configured instance for API calls.
21
+
22
+ - **`InboxFlowInstance`**
23
+ Pre-configured instance for integration with Inbox Flow.
24
+
25
+ ---
26
+
27
+ ### HTTP Responses - Errors
28
+
29
+ - **`BadGateway(message: string): Error`**
30
+ Represents an HTTP 502 error.
31
+
32
+ - **`BadRequest(message: string): Error`**
33
+ Represents an HTTP 400 error.
34
+
35
+ - **`Conflict(message: string): Error`**
36
+ Represents an HTTP 409 error.
37
+
38
+ - **`Forbidden(message: string): Error`**
39
+ Represents an HTTP 403 error.
40
+
41
+ - **`NotFound(message: string): Error`**
42
+ Represents an HTTP 404 error.
43
+
44
+ - **`NotImplemented(message: string): Error`**
45
+ Represents an HTTP 501 error.
46
+
47
+ - **`ServerError(message: string): Error`**
48
+ Represents an HTTP 500 error.
49
+
50
+ - **`Unauthorized(message: string): Error`**
51
+ Represents an HTTP 401 error.
52
+
53
+ - **`UnprocessableEntity(message: string): Error`**
54
+ Represents an HTTP 422 error.
55
+
56
+ ---
57
+
58
+ ### HTTP Responses - Success
59
+
60
+ - **`Created(data: any): Response`**
61
+ Represents an HTTP 201 response.
62
+
63
+ - **`Found(data: any): Response`**
64
+ Represents an HTTP 302 response.
65
+
66
+ - **`NoContent(): Response`**
67
+ Represents an HTTP 204 response.
68
+
69
+ - **`Success(data: any): Response`**
70
+ Represents an HTTP 200 response.
71
+
72
+ - **`Updated(data: any): Response`**
73
+ Represents an HTTP 200 response for updates.
74
+
75
+ ---
76
+
77
+ ### Services
78
+
79
+ - **`decodeErrorMessageFromRequest(request: Request): string`**
80
+ Decodes error messages from a request.
81
+
82
+ - **`decodeRequestBody(request: Request): any`**
83
+ Decodes the body of a request.
84
+
85
+ - **`errorHandler(error: Error): Response`**
86
+ Handles errors and returns an appropriate response.
87
+
88
+ - **`formParse(request: Request): FormData`**
89
+ Parses form data.
90
+
91
+ - **`getCaller(): string`**
92
+ Returns the name of the function that invoked the method.
93
+
94
+ - **`getScopedParams(request: Request): URLSearchParams`**
95
+ Retrieves scoped parameters from a request.
96
+
97
+ - **`httpDebug(request: Request): void`**
98
+ Displays debug information for an HTTP request.
99
+
100
+ - **`SchemaValidator(schema: any, data: any): boolean`**
101
+ Validates data against a schema.
102
+
103
+ - **`sendFileToS3(file: File, config: AwsConfig): Promise<{ url: string }>`**
104
+ Uploads a file to AWS S3.
105
+
106
+ ---
107
+
108
+ ## Contribution
109
+
110
+ Contributions are welcome! Feel free to open issues or submit pull requests to improve the project.
111
+
112
+ ---
113
+
114
+ ## License
115
+
116
+ This project is licensed under the Apache 2.0 License. See the `LICENSE` file for more details.
@@ -0,0 +1,13 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Sends a DELETE request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the DELETE request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} [body] - Optional body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ declare function deleteRequest<T = any>(url: string, headers?: HeadersInit, body?: any): Promise<ApiResponseDTO<T>>;
12
+ export { deleteRequest };
13
+ //# sourceMappingURL=deleteRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteRequest.d.ts","sourceRoot":"","sources":["../../src/api/deleteRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;;GAQG;AAEH,iBAAe,aAAa,CAAC,CAAC,GAAG,GAAG,EAClC,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,EACzB,IAAI,CAAC,EAAE,GAAG,GACT,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAE5B;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { makeRequest } from "./makeRequest";
2
+ /**
3
+ * Sends a DELETE request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the DELETE request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} [body] - Optional body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ async function deleteRequest(url, headers = {}, body) {
12
+ return makeRequest("DELETE", url, headers, body);
13
+ }
14
+ export { deleteRequest };
@@ -0,0 +1,12 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Sends a GET request to the specified URL with optional headers.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the GET request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
9
+ */
10
+ declare function getRequest<T = any>(url: string, headers?: HeadersInit): Promise<ApiResponseDTO<T>>;
11
+ export { getRequest };
12
+ //# sourceMappingURL=getRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRequest.d.ts","sourceRoot":"","sources":["../../src/api/getRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;GAOG;AAEH,iBAAe,UAAU,CAAC,CAAC,GAAG,GAAG,EAC/B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,GACxB,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAE5B;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { makeRequest } from "./makeRequest";
2
+ /**
3
+ * Sends a GET request to the specified URL with optional headers.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the GET request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
9
+ */
10
+ async function getRequest(url, headers = {}) {
11
+ return makeRequest("GET", url, headers);
12
+ }
13
+ export { getRequest };
@@ -0,0 +1,40 @@
1
+ type ConfigProps = {
2
+ status: number;
3
+ method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "ERROR";
4
+ request: string;
5
+ response: string;
6
+ token: string;
7
+ };
8
+ /**
9
+ * Sends a request to the inbox flow API with the provided configuration.
10
+ *
11
+ * @param config - The configuration object for the request.
12
+ * @param config.status - The HTTP status code associated with the request.
13
+ * @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", "PATCH", or "ERROR".
14
+ * @param config.request - The request payload or details as a string.
15
+ * @param config.response - The response payload or details as a string.
16
+ * @param config.token - The token associated with the request for authentication or identification.
17
+ *
18
+ * @remarks
19
+ * - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
20
+ * - If the configuration is not available, the function will return early without performing any action.
21
+ * - In a development environment (`NODE_ENV === "development"`), the function will also return early.
22
+ * - The request is sent as a POST request to the inbox API URL with the provided configuration details.
23
+ * - If an error occurs during the request, it will be logged using the `httpDebug` service.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const config = {
28
+ * status: 200,
29
+ * method: "POST",
30
+ * request: JSON.stringify({ key: "value" }),
31
+ * response: JSON.stringify({ success: true }),
32
+ * token: "example-token",
33
+ * };
34
+ *
35
+ * await inboxFlowRequest(config);
36
+ * ```
37
+ */
38
+ declare function inboxFlowRequest(config: ConfigProps): Promise<void>;
39
+ export { inboxFlowRequest };
40
+ //# sourceMappingURL=inboxFlowRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inboxFlowRequest.d.ts","sourceRoot":"","sources":["../../src/api/inboxFlowRequest.ts"],"names":[],"mappings":"AAGA,KAAK,WAAW,GAAG;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAC;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,iBAAe,gBAAgB,CAAC,MAAM,EAAE,WAAW,iBA+BlD;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { InboxFlowInstance } from "../config/inboxFlowInstance";
2
+ import { httpDebug } from "../services/httpDebug";
3
+ /**
4
+ * Sends a request to the inbox flow API with the provided configuration.
5
+ *
6
+ * @param config - The configuration object for the request.
7
+ * @param config.status - The HTTP status code associated with the request.
8
+ * @param config.method - The HTTP method used for the request. Can be "POST", "GET", "PUT", "DELETE", "PATCH", or "ERROR".
9
+ * @param config.request - The request payload or details as a string.
10
+ * @param config.response - The response payload or details as a string.
11
+ * @param config.token - The token associated with the request for authentication or identification.
12
+ *
13
+ * @remarks
14
+ * - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
15
+ * - If the configuration is not available, the function will return early without performing any action.
16
+ * - In a development environment (`NODE_ENV === "development"`), the function will also return early.
17
+ * - The request is sent as a POST request to the inbox API URL with the provided configuration details.
18
+ * - If an error occurs during the request, it will be logged using the `httpDebug` service.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const config = {
23
+ * status: 200,
24
+ * method: "POST",
25
+ * request: JSON.stringify({ key: "value" }),
26
+ * response: JSON.stringify({ success: true }),
27
+ * token: "example-token",
28
+ * };
29
+ *
30
+ * await inboxFlowRequest(config);
31
+ * ```
32
+ */
33
+ async function inboxFlowRequest(config) {
34
+ const inboxFlowInstance = InboxFlowInstance.getInboxConfig();
35
+ if (!inboxFlowInstance)
36
+ return;
37
+ const { inboxChannelId, inboxUserToken, inboxApiUrl } = inboxFlowInstance;
38
+ const { status, method, request, response, token } = config;
39
+ if (process.env.NODE_ENV === "development")
40
+ return;
41
+ try {
42
+ const body = JSON.stringify({
43
+ status,
44
+ channelId: inboxChannelId,
45
+ method,
46
+ token,
47
+ request,
48
+ response,
49
+ });
50
+ await fetch(inboxApiUrl, {
51
+ method: "POST",
52
+ body,
53
+ headers: {
54
+ "Content-Type": "application/json",
55
+ Authorization: `Bearer ${inboxUserToken}`,
56
+ },
57
+ });
58
+ }
59
+ catch (err) {
60
+ httpDebug("inboxFlowRequest", "Error sending inbox flow request", err);
61
+ }
62
+ }
63
+ export { inboxFlowRequest };
@@ -0,0 +1,38 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Makes an HTTP request using the Fetch API and returns a standardized response.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param method - The HTTP method to use for the request. Supported methods are:
7
+ * - "POST": Create a new resource.
8
+ * - "PUT": Update an existing resource.
9
+ * - "DELETE": Remove a resource.
10
+ * - "PATCH": Partially update a resource.
11
+ * - "GET": Retrieve a resource.
12
+ * @param url - The URL to which the request is sent.
13
+ * @param headers - Optional headers to include in the request. Defaults to an empty object.
14
+ * @param body - Optional body to include in the request. Should be serializable to JSON.
15
+ * @returns A promise that resolves to an `ApiResponseDTO<T>` object containing:
16
+ * - `success`: A boolean indicating whether the request was successful.
17
+ * - `status`: The HTTP status code of the response.
18
+ * - `message`: A message describing the result of the request.
19
+ * - `response`: The parsed JSON response data, or `null` if parsing fails.
20
+ * - `cause`: Additional error information, if applicable.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { makeRequest } from "./makeRequest";
25
+ *
26
+ * async function fetchData() {
27
+ * const response = await makeRequest("GET", "https://api.example.com/data");
28
+ * if (response.success) {
29
+ * console.log("Data:", response.response);
30
+ * } else {
31
+ * console.error("Error:", response.message);
32
+ * }
33
+ * }
34
+ * ```
35
+ */
36
+ declare function makeRequest<T = any>(method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET", url: string, headers?: HeadersInit, body?: any): Promise<ApiResponseDTO<T>>;
37
+ export { makeRequest };
38
+ //# sourceMappingURL=makeRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/api/makeRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAInD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,iBAAe,WAAW,CAAC,CAAC,GAAG,GAAG,EAChC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,KAAK,EACnD,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,EACzB,IAAI,CAAC,EAAE,GAAG,GACT,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAsE5B;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,103 @@
1
+ import { InboxFlowInstance } from "../config/inboxFlowInstance";
2
+ import { inboxFlowRequest } from "./inboxFlowRequest";
3
+ /**
4
+ * Makes an HTTP request using the Fetch API and returns a standardized response.
5
+ *
6
+ * @template T - The expected type of the response data.
7
+ * @param method - The HTTP method to use for the request. Supported methods are:
8
+ * - "POST": Create a new resource.
9
+ * - "PUT": Update an existing resource.
10
+ * - "DELETE": Remove a resource.
11
+ * - "PATCH": Partially update a resource.
12
+ * - "GET": Retrieve a resource.
13
+ * @param url - The URL to which the request is sent.
14
+ * @param headers - Optional headers to include in the request. Defaults to an empty object.
15
+ * @param body - Optional body to include in the request. Should be serializable to JSON.
16
+ * @returns A promise that resolves to an `ApiResponseDTO<T>` object containing:
17
+ * - `success`: A boolean indicating whether the request was successful.
18
+ * - `status`: The HTTP status code of the response.
19
+ * - `message`: A message describing the result of the request.
20
+ * - `response`: The parsed JSON response data, or `null` if parsing fails.
21
+ * - `cause`: Additional error information, if applicable.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { makeRequest } from "./makeRequest";
26
+ *
27
+ * async function fetchData() {
28
+ * const response = await makeRequest("GET", "https://api.example.com/data");
29
+ * if (response.success) {
30
+ * console.log("Data:", response.response);
31
+ * } else {
32
+ * console.error("Error:", response.message);
33
+ * }
34
+ * }
35
+ * ```
36
+ */
37
+ async function makeRequest(method, url, headers = {}, body) {
38
+ const successMessage = {
39
+ POST: "Resource created successfully",
40
+ PUT: "Resource updated successfully",
41
+ DELETE: "Resource deleted successfully",
42
+ PATCH: "Resource patched successfully",
43
+ GET: "Request successful",
44
+ };
45
+ try {
46
+ const response = await fetch(url, {
47
+ method,
48
+ headers: {
49
+ ...headers,
50
+ "Content-Type": "application/json",
51
+ },
52
+ body: body ? JSON.stringify(body) : undefined,
53
+ });
54
+ const status = response.status;
55
+ let data = null;
56
+ try {
57
+ data = await response.json();
58
+ }
59
+ catch {
60
+ data = null;
61
+ }
62
+ inboxFlowRequest({
63
+ method,
64
+ status,
65
+ request: JSON.stringify(response.headers),
66
+ response: JSON.stringify(data),
67
+ token: headers?.Authorization || "TOKEN_NOT_FOUND",
68
+ });
69
+ if (!response.ok) {
70
+ return {
71
+ success: false,
72
+ status,
73
+ message: data?.message || response.statusText || "Request failed",
74
+ response: data,
75
+ cause: null,
76
+ };
77
+ }
78
+ return {
79
+ success: true,
80
+ status,
81
+ message: data?.message || successMessage[method],
82
+ response: data,
83
+ cause: null,
84
+ };
85
+ }
86
+ catch (err) {
87
+ inboxFlowRequest({
88
+ method,
89
+ request: JSON.stringify(headers),
90
+ response: String(err),
91
+ status: 500,
92
+ token: "TOKEN_NOT_FOUND",
93
+ });
94
+ return {
95
+ success: false,
96
+ status: 0,
97
+ message: "Network error or request failed",
98
+ response: null,
99
+ cause: err instanceof Error ? err.message : String(err),
100
+ };
101
+ }
102
+ }
103
+ export { makeRequest };
@@ -0,0 +1,13 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ declare function patchRequest<T = any>(url: string, headers: HeadersInit | undefined, body: any): Promise<ApiResponseDTO<T>>;
12
+ export { patchRequest };
13
+ //# sourceMappingURL=patchRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patchRequest.d.ts","sourceRoot":"","sources":["../../src/api/patchRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;;GAQG;AAEH,iBAAe,YAAY,CAAC,CAAC,GAAG,GAAG,EACjC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,YAAK,EACzB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAE5B;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { makeRequest } from "./makeRequest";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ async function patchRequest(url, headers = {}, body) {
12
+ return makeRequest("PATCH", url, headers, body);
13
+ }
14
+ export { patchRequest };
@@ -0,0 +1,13 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ declare function postRequest<T = any>(url: string, headers: HeadersInit | undefined, body: any): Promise<ApiResponseDTO<T>>;
12
+ export { postRequest };
13
+ //# sourceMappingURL=postRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postRequest.d.ts","sourceRoot":"","sources":["../../src/api/postRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;;GAQG;AAEH,iBAAe,WAAW,CAAC,CAAC,GAAG,GAAG,EAChC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,YAAK,EACzB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAE5B;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { makeRequest } from "./makeRequest";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ async function postRequest(url, headers = {}, body) {
12
+ return makeRequest("POST", url, headers, body);
13
+ }
14
+ export { postRequest };
@@ -0,0 +1,13 @@
1
+ import type { ApiResponseDTO } from "@arkyn/types";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ declare function putRequest<T = any>(url: string, headers: HeadersInit | undefined, body: any): Promise<ApiResponseDTO<T>>;
12
+ export { putRequest };
13
+ //# sourceMappingURL=putRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"putRequest.d.ts","sourceRoot":"","sources":["../../src/api/putRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;;GAQG;AAEH,iBAAe,UAAU,CAAC,CAAC,GAAG,GAAG,EAC/B,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,YAAK,EACzB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAE5B;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { makeRequest } from "./makeRequest";
2
+ /**
3
+ * Sends a PATCH request to the specified URL with optional headers and body.
4
+ *
5
+ * @template T - The expected type of the response data.
6
+ * @param {string} url - The URL to send the PATCH request to.
7
+ * @param {HeadersInit} [headers={}] - Optional headers to include in the request.
8
+ * @param {any} body - The body to include in the request.
9
+ * @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
10
+ */
11
+ async function putRequest(url, headers = {}, body) {
12
+ return makeRequest("PUT", url, headers, body);
13
+ }
14
+ export { putRequest };
@@ -0,0 +1,80 @@
1
+ type ApiAinstanceContructorProps = {
2
+ baseUrl: string;
3
+ baseHeaders?: HeadersInit;
4
+ baseToken?: string | null;
5
+ };
6
+ type ApiRequestDataWithoutBodyProps = {
7
+ headers?: HeadersInit;
8
+ token?: string;
9
+ };
10
+ type ApiRequestDataWithBodyProps = {
11
+ body?: any;
12
+ headers?: HeadersInit;
13
+ token?: string;
14
+ };
15
+ /**
16
+ * Class representing an API instance to handle HTTP requests with base configurations.
17
+ */
18
+ declare class ApiInstance {
19
+ private baseUrl;
20
+ private baseHeaders?;
21
+ private baseToken?;
22
+ /**
23
+ * Creates an instance of ApiInstance.
24
+ * @param props - The configuration properties for the API instance.
25
+ * @param props.baseUrl - The base URL for the API.
26
+ * @param props.baseHeaders - Optional base headers to include in all requests.
27
+ * @param props.baseToken - Optional base token for authorization.
28
+ */
29
+ constructor(props: ApiAinstanceContructorProps);
30
+ /**
31
+ * Generates the full URL by appending the route to the base URL.
32
+ * @param route - The route to append to the base URL.
33
+ * @returns The full URL as a string.
34
+ */
35
+ private generateURL;
36
+ /**
37
+ * Generates the headers for a request by merging base headers, provided headers, and tokens.
38
+ * @param initHeaders - Initial headers to include in the request.
39
+ * @param token - Optional token to override the base token.
40
+ * @returns The merged headers as a HeadersInit object.
41
+ */
42
+ private generateHeaders;
43
+ /**
44
+ * Sends a GET request to the specified route.
45
+ * @param route - The API route to send the GET request to.
46
+ * @param data - The request data, including optional headers and token.
47
+ * @returns The API response data.
48
+ */
49
+ GET(route: string, data?: ApiRequestDataWithoutBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
50
+ /**
51
+ * Sends a POST request to the specified route.
52
+ * @param route - The API route to send the POST request to.
53
+ * @param data - The request data, including body, optional headers, and token.
54
+ * @returns The API response data.
55
+ */
56
+ POST(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
57
+ /**
58
+ * Sends a PUT request to the specified route.
59
+ * @param route - The API route to send the PUT request to.
60
+ * @param data - The request data, including body, optional headers, and token.
61
+ * @returns The API response data.
62
+ */
63
+ PUT(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
64
+ /**
65
+ * Sends a PATCH request to the specified route.
66
+ * @param route - The API route to send the PATCH request to.
67
+ * @param data - The request data, including body, optional headers, and token.
68
+ * @returns The API response data.
69
+ */
70
+ PATCH(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
71
+ /**
72
+ * Sends a DELETE request to the specified route.
73
+ * @param route - The API route to send the DELETE request to.
74
+ * @param data - The request data, including body, optional headers, and token.
75
+ * @returns The API response data.
76
+ */
77
+ DELETE(route: string, data?: ApiRequestDataWithBodyProps): Promise<import("@arkyn/types").ApiResponseDTO<any>>;
78
+ }
79
+ export { ApiInstance };
80
+ //# sourceMappingURL=apiInstance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apiInstance.d.ts","sourceRoot":"","sources":["../../src/config/apiInstance.ts"],"names":[],"mappings":"AAMA,KAAK,2BAA2B,GAAG;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AAEH,cAAM,WAAW;IACf,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAC,CAAc;IAClC,OAAO,CAAC,SAAS,CAAC,CAAS;IAE3B;;;;;;OAMG;gBAES,KAAK,EAAE,2BAA2B;IAM9C;;;;OAIG;IAEH,OAAO,CAAC,WAAW;IAInB;;;;;OAKG;IAEH,OAAO,CAAC,eAAe;IAcvB;;;;;OAKG;IAEG,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,8BAA8B;IAM9D;;;;;OAKG;IAEG,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO5D;;;;;OAKG;IAEG,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO3D;;;;;OAKG;IAEG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAO7D;;;;;OAKG;IAEG,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;CAM/D;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}