@arkyn/server 3.0.1-beta.9 → 3.0.1-beta.91

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 (151) hide show
  1. package/README.md +368 -65
  2. package/dist/api/arkynLogRequest.d.ts +1 -1
  3. package/dist/api/arkynLogRequest.d.ts.map +1 -1
  4. package/dist/api/arkynLogRequest.js +24 -19
  5. package/dist/api/makeRequest.d.ts +1 -1
  6. package/dist/api/makeRequest.d.ts.map +1 -1
  7. package/dist/api/makeRequest.js +9 -7
  8. package/dist/bundle.js +1507 -0
  9. package/dist/bundle.umd.cjs +10 -0
  10. package/dist/http/badResponses/_badResponse.d.ts +11 -0
  11. package/dist/http/badResponses/_badResponse.d.ts.map +1 -0
  12. package/dist/http/badResponses/_badResponse.js +20 -0
  13. package/dist/http/badResponses/badGateway.d.ts +2 -1
  14. package/dist/http/badResponses/badGateway.d.ts.map +1 -1
  15. package/dist/http/badResponses/badGateway.js +9 -3
  16. package/dist/http/badResponses/badRequest.d.ts +2 -1
  17. package/dist/http/badResponses/badRequest.d.ts.map +1 -1
  18. package/dist/http/badResponses/badRequest.js +9 -3
  19. package/dist/http/badResponses/conflict.d.ts +2 -1
  20. package/dist/http/badResponses/conflict.d.ts.map +1 -1
  21. package/dist/http/badResponses/conflict.js +9 -3
  22. package/dist/http/badResponses/forbidden.d.ts +2 -1
  23. package/dist/http/badResponses/forbidden.d.ts.map +1 -1
  24. package/dist/http/badResponses/forbidden.js +9 -3
  25. package/dist/http/badResponses/notFound.d.ts +2 -1
  26. package/dist/http/badResponses/notFound.d.ts.map +1 -1
  27. package/dist/http/badResponses/notFound.js +9 -3
  28. package/dist/http/badResponses/notImplemented.d.ts +2 -1
  29. package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
  30. package/dist/http/badResponses/notImplemented.js +9 -3
  31. package/dist/http/badResponses/serverError.d.ts +2 -1
  32. package/dist/http/badResponses/serverError.d.ts.map +1 -1
  33. package/dist/http/badResponses/serverError.js +9 -3
  34. package/dist/http/badResponses/unauthorized.d.ts +2 -1
  35. package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
  36. package/dist/http/badResponses/unauthorized.js +9 -3
  37. package/dist/http/badResponses/unprocessableEntity.d.ts +3 -2
  38. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
  39. package/dist/http/badResponses/unprocessableEntity.js +9 -4
  40. package/dist/http/successResponses/_successResponse.d.ts +22 -0
  41. package/dist/http/successResponses/_successResponse.d.ts.map +1 -0
  42. package/dist/http/successResponses/_successResponse.js +37 -0
  43. package/dist/http/successResponses/created.d.ts +2 -1
  44. package/dist/http/successResponses/created.d.ts.map +1 -1
  45. package/dist/http/successResponses/created.js +4 -1
  46. package/dist/http/successResponses/found.d.ts +2 -1
  47. package/dist/http/successResponses/found.d.ts.map +1 -1
  48. package/dist/http/successResponses/found.js +4 -1
  49. package/dist/http/successResponses/noContent.d.ts +2 -1
  50. package/dist/http/successResponses/noContent.d.ts.map +1 -1
  51. package/dist/http/successResponses/noContent.js +4 -1
  52. package/dist/http/successResponses/success.d.ts +2 -1
  53. package/dist/http/successResponses/success.d.ts.map +1 -1
  54. package/dist/http/successResponses/success.js +4 -1
  55. package/dist/http/successResponses/updated.d.ts +2 -1
  56. package/dist/http/successResponses/updated.d.ts.map +1 -1
  57. package/dist/http/successResponses/updated.js +4 -1
  58. package/dist/index.d.ts +13 -3
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +14 -4
  61. package/dist/{config/apiInstance.d.ts → services/apiService.d.ts} +25 -21
  62. package/dist/services/apiService.d.ts.map +1 -0
  63. package/dist/{config/apiInstance.js → services/apiService.js} +45 -23
  64. package/dist/{config/arkynLogInstance.d.ts → services/arkynLogService.d.ts} +11 -11
  65. package/dist/services/arkynLogService.d.ts.map +1 -0
  66. package/dist/{config/arkynLogInstance.js → services/arkynLogService.js} +8 -11
  67. package/dist/services/debugService.d.ts +54 -0
  68. package/dist/services/debugService.d.ts.map +1 -0
  69. package/dist/services/debugService.js +57 -0
  70. package/dist/services/flushDebugLogs.d.ts +8 -0
  71. package/dist/services/flushDebugLogs.d.ts.map +1 -0
  72. package/dist/services/flushDebugLogs.js +20 -0
  73. package/{src/services/formParse.ts → dist/services/formAsyncParse.d.ts} +20 -44
  74. package/dist/services/formAsyncParse.d.ts.map +1 -0
  75. package/dist/services/formAsyncParse.js +58 -0
  76. package/dist/services/formParse.d.ts +3 -3
  77. package/dist/services/formParse.d.ts.map +1 -1
  78. package/dist/services/formParse.js +7 -5
  79. package/dist/services/getCaller.d.ts.map +1 -1
  80. package/dist/services/getCaller.js +9 -14
  81. package/dist/services/measureRouteExecution.d.ts +3 -0
  82. package/dist/services/measureRouteExecution.d.ts.map +1 -0
  83. package/dist/services/measureRouteExecution.js +24 -0
  84. package/dist/services/schemaValidator.d.ts +148 -3
  85. package/dist/services/schemaValidator.d.ts.map +1 -1
  86. package/dist/services/schemaValidator.js +160 -4
  87. package/dist/validations/validateCep.d.ts +24 -0
  88. package/dist/validations/validateCep.d.ts.map +1 -0
  89. package/dist/validations/validateCep.js +33 -0
  90. package/dist/validations/validateCnpj.d.ts +22 -0
  91. package/dist/validations/validateCnpj.d.ts.map +1 -0
  92. package/dist/validations/validateCnpj.js +52 -0
  93. package/dist/validations/validateCpf.d.ts +24 -0
  94. package/dist/validations/validateCpf.d.ts.map +1 -0
  95. package/dist/validations/validateCpf.js +54 -0
  96. package/dist/validations/validateDate.d.ts +34 -0
  97. package/dist/validations/validateDate.d.ts.map +1 -0
  98. package/dist/validations/validateDate.js +73 -0
  99. package/dist/validations/validateEmail.d.ts +22 -0
  100. package/dist/validations/validateEmail.d.ts.map +1 -0
  101. package/dist/validations/validateEmail.js +123 -0
  102. package/dist/validations/validatePassword.d.ts +21 -0
  103. package/dist/validations/validatePassword.d.ts.map +1 -0
  104. package/dist/validations/validatePassword.js +34 -0
  105. package/dist/validations/validatePhone.d.ts +29 -0
  106. package/dist/validations/validatePhone.d.ts.map +1 -0
  107. package/dist/validations/validatePhone.js +44 -0
  108. package/dist/validations/validateRg.d.ts +22 -0
  109. package/dist/validations/validateRg.d.ts.map +1 -0
  110. package/dist/validations/validateRg.js +31 -0
  111. package/package.json +34 -18
  112. package/dist/config/apiInstance.d.ts.map +0 -1
  113. package/dist/config/arkynLogInstance.d.ts.map +0 -1
  114. package/dist/services/httpDebug.d.ts +0 -35
  115. package/dist/services/httpDebug.d.ts.map +0 -1
  116. package/dist/services/httpDebug.js +0 -52
  117. package/src/api/arkynLogRequest.ts +0 -125
  118. package/src/api/deleteRequest.ts +0 -22
  119. package/src/api/getRequest.ts +0 -20
  120. package/src/api/makeRequest.ts +0 -120
  121. package/src/api/patchRequest.ts +0 -22
  122. package/src/api/postRequest.ts +0 -22
  123. package/src/api/putRequest.ts +0 -22
  124. package/src/config/apiInstance.ts +0 -148
  125. package/src/config/arkynLogInstance.ts +0 -70
  126. package/src/http/badResponses/badGateway.ts +0 -63
  127. package/src/http/badResponses/badRequest.ts +0 -63
  128. package/src/http/badResponses/conflict.ts +0 -63
  129. package/src/http/badResponses/forbidden.ts +0 -63
  130. package/src/http/badResponses/notFound.ts +0 -63
  131. package/src/http/badResponses/notImplemented.ts +0 -63
  132. package/src/http/badResponses/serverError.ts +0 -63
  133. package/src/http/badResponses/unauthorized.ts +0 -63
  134. package/src/http/badResponses/unprocessableEntity.ts +0 -79
  135. package/src/http/successResponses/created.ts +0 -64
  136. package/src/http/successResponses/found.ts +0 -67
  137. package/src/http/successResponses/noContent.ts +0 -42
  138. package/src/http/successResponses/success.ts +0 -64
  139. package/src/http/successResponses/updated.ts +0 -64
  140. package/src/index.ts +0 -31
  141. package/src/mapper/arkynLogRequestMapper.ts +0 -73
  142. package/src/services/decodeErrorMessageFromRequest.ts +0 -36
  143. package/src/services/decodeRequestBody.ts +0 -43
  144. package/src/services/errorHandler.ts +0 -99
  145. package/src/services/getCaller.ts +0 -82
  146. package/src/services/getScopedParams.ts +0 -43
  147. package/src/services/httpDebug.ts +0 -61
  148. package/src/services/schemaValidator.ts +0 -66
  149. package/src/types/ApiResponseDTO.ts +0 -19
  150. package/tsconfig.json +0 -21
  151. package/vitest.config.ts +0 -5
package/README.md CHANGED
@@ -1,116 +1,419 @@
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.
1
+ # @arkyn/server
2
2
 
3
- ---
3
+ A comprehensive server-side utilities package for building robust backend applications. Provides HTTP response helpers, error handlers, request utilities, and pre-configured API instances to streamline your server-side development workflow.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@arkyn/server.svg)](https://www.npmjs.com/package/@arkyn/server)
6
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
4
8
 
5
- ## Installation
9
+ ## ✨ Features
6
10
 
7
- Install the package using npm:
11
+ - 🌐 **HTTP Response Helpers** - Pre-built success and error response functions
12
+ - ⚙️ **API Configurations** - Ready-to-use API and logging instances
13
+ - 🔍 **Request Utilities** - Body parsing, parameter extraction, and debugging tools
14
+ - 🛡️ **Error Handling** - Comprehensive error handling and validation
15
+ - 🧪 **Schema Validation** - Zod integration for data validation
16
+ - 🚀 **Remix Integration** - Optimized for React Router and Remix applications
17
+
18
+ ## 📦 Installation
8
19
 
9
20
  ```bash
10
21
  npm install @arkyn/server
11
22
  ```
12
23
 
13
- ---
24
+ ## 🚀 Quick Start
14
25
 
15
- ## Features
26
+ ```typescript
27
+ import { Success, BadRequest, errorHandler } from "@arkyn/server";
16
28
 
17
- ### Settings
29
+ export async function loader({ request }: LoaderFunctionArgs) {
30
+ try {
31
+ const data = await fetchUserData();
32
+ return Success(data);
33
+ } catch (error) {
34
+ return errorHandler(error);
35
+ }
36
+ }
18
37
 
19
- - **`ApiInstance`**
20
- Pre-configured instance for API calls.
38
+ export async function action({ request }: ActionFunctionArgs) {
39
+ const body = await decodeRequestBody(request);
21
40
 
22
- - **`ArkynLogInstance`**
23
- Pre-configured instance for integration with Inbox Flow.
41
+ if (!body.email) {
42
+ return BadRequest("Email is required");
43
+ }
24
44
 
25
- ---
45
+ const user = await createUser(body);
46
+ return Created(user);
47
+ }
48
+ ```
26
49
 
27
- ### HTTP Responses - Errors
50
+ ## 📋 API Reference
28
51
 
29
- - **`BadGateway(message: string): Error`**
30
- Represents an HTTP 502 error.
52
+ ### Configuration
31
53
 
32
- - **`BadRequest(message: string): Error`**
33
- Represents an HTTP 400 error.
54
+ #### ApiInstance
34
55
 
35
- - **`Conflict(message: string): Error`**
36
- Represents an HTTP 409 error.
56
+ Pre-configured Axios instance for external API calls.
37
57
 
38
- - **`Forbidden(message: string): Error`**
39
- Represents an HTTP 403 error.
58
+ ```typescript
59
+ import { ApiInstance } from "@arkyn/server";
40
60
 
41
- - **`NotFound(message: string): Error`**
42
- Represents an HTTP 404 error.
61
+ const api = new ApiInstance({ baseUrl: "https://google.com" });
62
+ const response = await api.get("/users");
63
+ ```
43
64
 
44
- - **`NotImplemented(message: string): Error`**
45
- Represents an HTTP 501 error.
65
+ #### ArkynLogInstance
46
66
 
47
- - **`ServerError(message: string): Error`**
48
- Represents an HTTP 500 error.
67
+ Pre-configured instance for integration with logging services.
49
68
 
50
- - **`Unauthorized(message: string): Error`**
51
- Represents an HTTP 401 error.
69
+ ```typescript
70
+ import { ArkynLogInstance } from "@arkyn/server";
52
71
 
53
- - **`UnprocessableEntity(message: string): Error`**
54
- Represents an HTTP 422 error.
72
+ ArkynLogInstance.setArkynConfig();
73
+ ```
55
74
 
56
75
  ---
57
76
 
58
- ### HTTP Responses - Success
77
+ ### HTTP Error Responses
59
78
 
60
- - **`Created(data: any): Response`**
61
- Represents an HTTP 201 response.
79
+ #### BadRequest(message: string): Error
62
80
 
63
- - **`Found(data: any): Response`**
64
- Represents an HTTP 302 response.
81
+ Returns HTTP 400 for client errors.
65
82
 
66
- - **`NoContent(): Response`**
67
- Represents an HTTP 204 response.
83
+ ```typescript
84
+ import { BadRequest } from "@arkyn/server";
85
+
86
+ if (!email) {
87
+ return BadRequest("Email is required");
88
+ }
89
+ ```
90
+
91
+ #### Unauthorized(message: string): Error
92
+
93
+ Returns HTTP 401 for authentication errors.
94
+
95
+ ```typescript
96
+ import { Unauthorized } from "@arkyn/server";
97
+
98
+ if (!isAuthenticated) {
99
+ return Unauthorized("Authentication required");
100
+ }
101
+ ```
68
102
 
69
- - **`Success(data: any): Response`**
70
- Represents an HTTP 200 response.
103
+ #### Forbidden(message: string): Error
104
+
105
+ Returns HTTP 403 for authorization errors.
106
+
107
+ ```typescript
108
+ import { Forbidden } from "@arkyn/server";
109
+
110
+ if (!hasPermission) {
111
+ return Forbidden("Insufficient permissions");
112
+ }
113
+ ```
114
+
115
+ #### NotFound(message: string): Error
116
+
117
+ Returns HTTP 404 for missing resources.
118
+
119
+ ```typescript
120
+ import { NotFound } from "@arkyn/server";
121
+
122
+ const user = await findUser(id);
123
+ if (!user) {
124
+ return NotFound("User not found");
125
+ }
126
+ ```
127
+
128
+ #### Conflict(message: string): Error
129
+
130
+ Returns HTTP 409 for resource conflicts.
131
+
132
+ ```typescript
133
+ import { Conflict } from "@arkyn/server";
134
+
135
+ const existingUser = await findUserByEmail(email);
136
+ if (existingUser) {
137
+ return Conflict("Email already exists");
138
+ }
139
+ ```
71
140
 
72
- - **`Updated(data: any): Response`**
73
- Represents an HTTP 200 response for updates.
141
+ #### UnprocessableEntity(message: string): Error
142
+
143
+ Returns HTTP 422 for validation errors.
144
+
145
+ ```typescript
146
+ import { UnprocessableEntity } from "@arkyn/server";
147
+
148
+ if (!isValidEmail(email)) {
149
+ return UnprocessableEntity("Invalid email format");
150
+ }
151
+ ```
152
+
153
+ #### ServerError(message: string): Error
154
+
155
+ Returns HTTP 500 for internal errors.
156
+
157
+ #### BadGateway(message: string): Error
158
+
159
+ Returns HTTP 502 for gateway errors.
160
+
161
+ #### NotImplemented(message: string): Error
162
+
163
+ Returns HTTP 501 for unimplemented features.
74
164
 
75
165
  ---
76
166
 
77
- ### Services
167
+ ### HTTP Success Responses
168
+
169
+ #### Success(data: any): Response
170
+
171
+ Returns HTTP 200 with data.
172
+
173
+ ```typescript
174
+ import { Success } from "@arkyn/server";
175
+
176
+ export async function loader() {
177
+ const users = await getUsers();
178
+ return Success(users);
179
+ }
180
+ ```
181
+
182
+ #### Created(data: any): Response
183
+
184
+ Returns HTTP 201 for successful creation.
185
+
186
+ ```typescript
187
+ import { Created } from "@arkyn/server";
188
+
189
+ export async function action({ request }) {
190
+ const user = await createUser(await request.json());
191
+ return Created(user);
192
+ }
193
+ ```
194
+
195
+ #### Updated(data: any): Response
78
196
 
79
- - **`decodeErrorMessageFromRequest(request: Request): string`**
80
- Decodes error messages from a request.
197
+ Returns HTTP 200 for successful updates.
81
198
 
82
- - **`decodeRequestBody(request: Request): any`**
83
- Decodes the body of a request.
199
+ ```typescript
200
+ import { Updated } from "@arkyn/server";
84
201
 
85
- - **`errorHandler(error: Error): Response`**
86
- Handles errors and returns an appropriate response.
202
+ const updatedUser = await updateUser(id, data);
203
+ return Updated(updatedUser);
204
+ ```
205
+
206
+ #### NoContent(): Response
87
207
 
88
- - **`formParse(request: Request): FormData`**
89
- Parses form data.
208
+ Returns HTTP 204 for successful operations without content.
90
209
 
91
- - **`getCaller(): string`**
92
- Returns the name of the function that invoked the method.
210
+ ```typescript
211
+ import { NoContent } from "@arkyn/server";
93
212
 
94
- - **`getScopedParams(request: Request): URLSearchParams`**
95
- Retrieves scoped parameters from a request.
213
+ await deleteUser(id);
214
+ return NoContent();
215
+ ```
96
216
 
97
- - **`httpDebug(request: Request): void`**
98
- Displays debug information for an HTTP request.
217
+ #### Found(data: any): Response
99
218
 
100
- - **`SchemaValidator(schema: any, data: any): boolean`**
101
- Validates data against a schema.
219
+ Returns HTTP 302 for redirects with data.
102
220
 
103
- - **`sendFileToS3(file: File, config: AwsConfig): Promise<{ url: string }>`**
104
- Uploads a file to AWS S3.
221
+ ```typescript
222
+ import { Found } from "@arkyn/server";
223
+
224
+ return Found({ redirectUrl: "/dashboard" });
225
+ ```
105
226
 
106
227
  ---
107
228
 
108
- ## Contribution
229
+ ### Utilities & Services
230
+
231
+ #### errorHandler(error: Error): Response
232
+
233
+ Centralized error handling that returns appropriate HTTP responses.
109
234
 
110
- Contributions are welcome! Feel free to open issues or submit pull requests to improve the project.
235
+ ```typescript
236
+ import { errorHandler } from "@arkyn/server";
237
+
238
+ export async function action({ request }) {
239
+ try {
240
+ return await processRequest(request);
241
+ } catch (error) {
242
+ return errorHandler(error); // Automatically handles different error types
243
+ }
244
+ }
245
+ ```
246
+
247
+ #### decodeRequestBody(request: Request): any
248
+
249
+ Safely decodes JSON request bodies.
250
+
251
+ ```typescript
252
+ import { decodeRequestBody } from "@arkyn/server";
253
+
254
+ export async function action({ request }) {
255
+ const body = await decodeRequestBody(request);
256
+ console.log(body); // Parsed JSON object
257
+ }
258
+ ```
259
+
260
+ #### formParse(request: Request): FormData
261
+
262
+ Parses multipart form data from requests.
263
+
264
+ ```typescript
265
+ import { formParse } from "@arkyn/server";
266
+
267
+ export async function action({ request }) {
268
+ const formData = await formParse(request);
269
+ const file = formData.get("avatar") as File;
270
+ }
271
+ ```
272
+
273
+ #### getScopedParams(request: Request): URLSearchParams
274
+
275
+ Extracts URL parameters from requests.
276
+
277
+ ```typescript
278
+ import { getScopedParams } from "@arkyn/server";
279
+
280
+ export async function loader({ request }) {
281
+ const params = getScopedParams(request);
282
+ const page = params.get("page") || "1";
283
+ }
284
+ ```
285
+
286
+ #### SchemaValidator(schema: any, data: any): boolean
287
+
288
+ Validates data against Zod schemas.
289
+
290
+ ```typescript
291
+ import { SchemaValidator } from "@arkyn/server";
292
+ import { z } from "zod";
293
+
294
+ const userSchema = z.object({
295
+ email: z.string().email(),
296
+ name: z.string().min(2),
297
+ });
298
+
299
+ export async function action({ request }) {
300
+ const body = await decodeRequestBody(request);
301
+
302
+ if (!SchemaValidator(userSchema, body)) {
303
+ return BadRequest("Invalid data format");
304
+ }
305
+ }
306
+ ```
307
+
308
+ #### httpDebug(request: Request): void
309
+
310
+ Logs detailed information about HTTP requests for debugging.
311
+
312
+ ```typescript
313
+ import { httpDebug } from "@arkyn/server";
314
+
315
+ export async function loader({ request }) {
316
+ httpDebug(request); // Logs method, URL, headers, etc.
317
+ }
318
+ ```
319
+
320
+ #### getCaller(): string
321
+
322
+ Returns the name of the calling function for debugging purposes.
323
+
324
+ ```typescript
325
+ import { getCaller } from "@arkyn/server";
326
+
327
+ function myFunction() {
328
+ console.log(`Called from: ${getCaller()}`);
329
+ }
330
+ ```
331
+
332
+ #### decodeErrorMessageFromRequest(request: Request): string
333
+
334
+ Extracts error messages from request objects.
335
+
336
+ ```typescript
337
+ import { decodeErrorMessageFromRequest } from "@arkyn/server";
338
+
339
+ const errorMessage = decodeErrorMessageFromRequest(request);
340
+ ```
341
+
342
+ ## 🔧 Advanced Usage
343
+
344
+ ### Custom Error Handling
345
+
346
+ ```typescript
347
+ import { errorHandler, BadRequest, ServerError } from "@arkyn/server";
348
+
349
+ export async function action({ request }) {
350
+ try {
351
+ const body = await decodeRequestBody(request);
352
+
353
+ // Custom validation
354
+ if (!body.email) {
355
+ throw new BadRequest("Email is required");
356
+ }
357
+
358
+ const result = await processData(body);
359
+ return Success(result);
360
+ } catch (error) {
361
+ if (error instanceof BadRequest) {
362
+ return error; // Return as-is
363
+ }
364
+
365
+ // Log unexpected errors
366
+ console.error("Unexpected error:", error);
367
+ return ServerError("Internal server error");
368
+ }
369
+ }
370
+ ```
371
+
372
+ ### Request Pipeline
373
+
374
+ ```typescript
375
+ import {
376
+ decodeRequestBody,
377
+ SchemaValidator,
378
+ getScopedParams,
379
+ Success,
380
+ BadRequest,
381
+ } from "@arkyn/server";
382
+
383
+ export async function action({ request }) {
384
+ // 1. Parse request
385
+ const body = await decodeRequestBody(request);
386
+ const params = getScopedParams(request);
387
+
388
+ // 2. Validate
389
+ if (!SchemaValidator(mySchema, body)) {
390
+ return BadRequest("Invalid request data");
391
+ }
392
+
393
+ // 3. Process
394
+ const result = await processRequest(body, params);
395
+
396
+ // 4. Respond
397
+ return Success(result);
398
+ }
399
+ ```
111
400
 
112
401
  ---
113
402
 
114
- ## License
403
+ ## 🤝 Contributing
404
+
405
+ Contributions are welcome! Please read our contributing guidelines and submit pull requests to help improve the package.
406
+
407
+ ## 📄 License
408
+
409
+ This project is licensed under the Apache 2.0 License - see the [LICENSE](./LICENSE.txt) file for details.
410
+
411
+ ## 🔗 Links
412
+
413
+ - [GitHub Repository](https://github.com/Lucas-Eduardo-Goncalves/arkyn)
414
+ - [NPM Package](https://www.npmjs.com/package/@arkyn/server)
415
+ - [Full Documentation](https://github.com/Lucas-Eduardo-Goncalves/arkyn#readme)
416
+
417
+ ---
115
418
 
116
- This project is licensed under the Apache 2.0 License. See the `LICENSE` file for more details.
419
+ Made with ❤️ by the Arkyn team
@@ -26,7 +26,7 @@ type ConfigProps = {
26
26
  * @param config.responseBody - The body of the response received.
27
27
  *
28
28
  * @remarks
29
- * - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
29
+ * - This function retrieves the inbox flow configuration using `InboxFlowService.getInboxConfig()`.
30
30
  * - If the configuration is not available, the function will return early without performing any action.
31
31
  * - In a development environment (`NODE_ENV === "development"`), the function will also return early.
32
32
  * - The request is sent as a POST request to the inbox API URL with the provided configuration details.
@@ -1 +1 @@
1
- {"version":3,"file":"arkynLogRequest.d.ts","sourceRoot":"","sources":["../../src/api/arkynLogRequest.ts"],"names":[],"mappings":"AAGA,KAAK,WAAW,GAAG;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpD,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,iBAAe,eAAe,CAAC,MAAM,EAAE,WAAW,iBAiEjD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"arkynLogRequest.d.ts","sourceRoot":"","sources":["../../src/api/arkynLogRequest.ts"],"names":[],"mappings":"AAGA,KAAK,WAAW,GAAG;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpD,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,iBAAe,eAAe,CAAC,MAAM,EAAE,WAAW,iBAyDjD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -1,5 +1,5 @@
1
- import { ArkynLogInstance } from "../config/arkynLogInstance";
2
- import { httpDebug } from "../services/httpDebug";
1
+ import { ArkynLogService } from "../services/arkynLogService";
2
+ import { flushDebugLogs } from "../services/flushDebugLogs";
3
3
  /**
4
4
  * Sends a request to the inbox flow API with the provided configuration.
5
5
  *
@@ -16,7 +16,7 @@ import { httpDebug } from "../services/httpDebug";
16
16
  * @param config.responseBody - The body of the response received.
17
17
  *
18
18
  * @remarks
19
- * - This function retrieves the inbox flow configuration using `InboxFlowInstance.getInboxConfig()`.
19
+ * - This function retrieves the inbox flow configuration using `InboxFlowService.getInboxConfig()`.
20
20
  * - If the configuration is not available, the function will return early without performing any action.
21
21
  * - In a development environment (`NODE_ENV === "development"`), the function will also return early.
22
22
  * - The request is sent as a POST request to the inbox API URL with the provided configuration details.
@@ -41,43 +41,48 @@ import { httpDebug } from "../services/httpDebug";
41
41
  * ```
42
42
  */
43
43
  async function arkynLogRequest(config) {
44
- const arkynInstance = ArkynLogInstance.getArkynConfig();
45
- if (!arkynInstance)
44
+ const arkynService = ArkynLogService.getArkynConfig();
45
+ if (!arkynService)
46
46
  return;
47
- const { arkynUserToken, arkynApiUrl } = arkynInstance;
47
+ const { userToken, apiUrl, trafficSourceId } = arkynService;
48
48
  const { elapsedTime, method, queryParams, requestBody, requestHeaders, responseBody, responseHeaders, status, token, rawUrl, } = config;
49
- // if (process.env.NODE_ENV === "development") return;
49
+ if (process.env.NODE_ENV === "development")
50
+ return;
50
51
  try {
51
52
  const url = new URL(rawUrl);
52
- let protocol = "HTTPS";
53
+ let protocol = "https";
53
54
  if (url.protocol === "http:")
54
- protocol = "HTTP";
55
+ protocol = "http";
55
56
  const body = JSON.stringify({
56
57
  domainUrl: url.protocol + "//" + url.host,
57
58
  pathnameUrl: url.pathname,
59
+ trafficSourceId: trafficSourceId,
58
60
  status,
59
61
  protocol,
60
- method,
62
+ method: method.toLowerCase(),
61
63
  trafficUserId: null,
62
64
  elapsedTime,
63
- requestHeaders,
64
- requestBody,
65
- queryParams,
66
- responseHeaders,
67
- responseBody,
65
+ requestHeaders: JSON.stringify(requestHeaders),
66
+ requestBody: JSON.stringify(requestBody),
67
+ queryParams: JSON.stringify(queryParams),
68
+ responseHeaders: JSON.stringify(responseHeaders),
69
+ responseBody: JSON.stringify(responseBody),
68
70
  });
69
- console.log(arkynApiUrl.replace(":trafficSourceId", arkynInstance.arkynTrafficSourceId));
70
- await fetch(arkynApiUrl.replace(":trafficSourceId", arkynInstance.arkynTrafficSourceId), {
71
+ await fetch(apiUrl, {
71
72
  method: "POST",
72
73
  body,
73
74
  headers: {
74
75
  "Content-Type": "application/json",
75
- Authorization: `Bearer ${arkynUserToken}`,
76
+ Authorization: `Bearer ${userToken}`,
76
77
  },
77
78
  });
78
79
  }
79
80
  catch (err) {
80
- httpDebug("arkyn log error", "Error sending request", err);
81
+ flushDebugLogs({
82
+ debugs: [`Error sending request: ${err}`],
83
+ name: "ARKYN_LOG_ERROR",
84
+ scheme: "red",
85
+ });
81
86
  }
82
87
  }
83
88
  export { arkynLogRequest };
@@ -33,6 +33,6 @@ import type { ApiResponseDTO } from "../types/ApiResponseDTO";
33
33
  * }
34
34
  * ```
35
35
  */
36
- declare function makeRequest<T = any>(method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET", url: string, headers?: HeadersInit, body?: any): Promise<ApiResponseDTO<T>>;
36
+ declare function makeRequest<T = any>(method: "POST" | "PUT" | "DELETE" | "PATCH" | "GET", url: string, rawHeaders?: HeadersInit, body?: any): Promise<ApiResponseDTO<T>>;
37
37
  export { makeRequest };
38
38
  //# sourceMappingURL=makeRequest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/api/makeRequest.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,CAwE5B;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"makeRequest.d.ts","sourceRoot":"","sources":["../../src/api/makeRequest.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,UAAU,GAAE,WAAgB,EAC5B,IAAI,CAAC,EAAE,GAAG,GACT,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CA2E5B;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { ArkynLogRequestMapper } from "../mapper/arkynLogRequestMapper";
2
- import { httpDebug } from "../services/httpDebug";
2
+ import { flushDebugLogs } from "../services/flushDebugLogs";
3
3
  import { arkynLogRequest } from "./arkynLogRequest";
4
4
  /**
5
5
  * Makes an HTTP request using the Fetch API and returns a standardized response.
@@ -35,7 +35,7 @@ import { arkynLogRequest } from "./arkynLogRequest";
35
35
  * }
36
36
  * ```
37
37
  */
38
- async function makeRequest(method, url, headers = {}, body) {
38
+ async function makeRequest(method, url, rawHeaders = {}, body) {
39
39
  const successMessage = {
40
40
  POST: "Resource created successfully",
41
41
  PUT: "Resource updated successfully",
@@ -45,12 +45,10 @@ async function makeRequest(method, url, headers = {}, body) {
45
45
  };
46
46
  try {
47
47
  const startTime = performance.now();
48
+ const headers = { ...rawHeaders, "Content-Type": "application/json" };
48
49
  const response = await fetch(url, {
49
50
  method,
50
- headers: {
51
- ...headers,
52
- "Content-Type": "application/json",
53
- },
51
+ headers,
54
52
  body: body ? JSON.stringify(body) : undefined,
55
53
  });
56
54
  const elapsedTime = performance.now() - startTime;
@@ -92,7 +90,11 @@ async function makeRequest(method, url, headers = {}, body) {
92
90
  };
93
91
  }
94
92
  catch (err) {
95
- httpDebug("Network error or request failed", null, err);
93
+ flushDebugLogs({
94
+ debugs: [`Network error or request failed: ${err}`],
95
+ name: "ARKYN_MAKE_REQUEST_ERROR",
96
+ scheme: "red",
97
+ });
96
98
  return {
97
99
  success: false,
98
100
  status: 0,