@arkyn/server 3.0.1-beta.8 → 3.0.1-beta.81
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/README.md +368 -65
- package/dist/api/arkynLogRequest.d.ts +1 -1
- package/dist/api/arkynLogRequest.d.ts.map +1 -1
- package/dist/api/arkynLogRequest.js +16 -12
- package/dist/api/makeRequest.d.ts +1 -1
- package/dist/api/makeRequest.d.ts.map +1 -1
- package/dist/api/makeRequest.js +9 -7
- package/dist/bundle.js +1506 -0
- package/dist/bundle.umd.cjs +10 -0
- package/dist/http/badResponses/_badResponse.d.ts +11 -0
- package/dist/http/badResponses/_badResponse.d.ts.map +1 -0
- package/dist/http/badResponses/_badResponse.js +20 -0
- package/dist/http/badResponses/badGateway.d.ts +2 -1
- package/dist/http/badResponses/badGateway.d.ts.map +1 -1
- package/dist/http/badResponses/badGateway.js +9 -3
- package/dist/http/badResponses/badRequest.d.ts +2 -1
- package/dist/http/badResponses/badRequest.d.ts.map +1 -1
- package/dist/http/badResponses/badRequest.js +9 -3
- package/dist/http/badResponses/conflict.d.ts +2 -1
- package/dist/http/badResponses/conflict.d.ts.map +1 -1
- package/dist/http/badResponses/conflict.js +9 -3
- package/dist/http/badResponses/forbidden.d.ts +2 -1
- package/dist/http/badResponses/forbidden.d.ts.map +1 -1
- package/dist/http/badResponses/forbidden.js +9 -3
- package/dist/http/badResponses/notFound.d.ts +2 -1
- package/dist/http/badResponses/notFound.d.ts.map +1 -1
- package/dist/http/badResponses/notFound.js +9 -3
- package/dist/http/badResponses/notImplemented.d.ts +2 -1
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
- package/dist/http/badResponses/notImplemented.js +9 -3
- package/dist/http/badResponses/serverError.d.ts +2 -1
- package/dist/http/badResponses/serverError.d.ts.map +1 -1
- package/dist/http/badResponses/serverError.js +9 -3
- package/dist/http/badResponses/unauthorized.d.ts +2 -1
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
- package/dist/http/badResponses/unauthorized.js +9 -3
- package/dist/http/badResponses/unprocessableEntity.d.ts +3 -2
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
- package/dist/http/badResponses/unprocessableEntity.js +9 -4
- package/dist/http/successResponses/_successResponse.d.ts +22 -0
- package/dist/http/successResponses/_successResponse.d.ts.map +1 -0
- package/dist/http/successResponses/_successResponse.js +37 -0
- package/dist/http/successResponses/created.d.ts +2 -1
- package/dist/http/successResponses/created.d.ts.map +1 -1
- package/dist/http/successResponses/created.js +4 -1
- package/dist/http/successResponses/found.d.ts +2 -1
- package/dist/http/successResponses/found.d.ts.map +1 -1
- package/dist/http/successResponses/found.js +4 -1
- package/dist/http/successResponses/noContent.d.ts +2 -1
- package/dist/http/successResponses/noContent.d.ts.map +1 -1
- package/dist/http/successResponses/noContent.js +4 -1
- package/dist/http/successResponses/success.d.ts +2 -1
- package/dist/http/successResponses/success.d.ts.map +1 -1
- package/dist/http/successResponses/success.js +4 -1
- package/dist/http/successResponses/updated.d.ts +2 -1
- package/dist/http/successResponses/updated.d.ts.map +1 -1
- package/dist/http/successResponses/updated.js +4 -1
- package/dist/index.d.ts +13 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -4
- package/dist/{config/apiInstance.d.ts → services/apiService.d.ts} +25 -21
- package/dist/services/apiService.d.ts.map +1 -0
- package/dist/{config/apiInstance.js → services/apiService.js} +45 -23
- package/dist/{config/arkynLogInstance.d.ts → services/arkynLogService.d.ts} +11 -11
- package/dist/services/arkynLogService.d.ts.map +1 -0
- package/dist/{config/arkynLogInstance.js → services/arkynLogService.js} +8 -12
- package/dist/services/debugService.d.ts +54 -0
- package/dist/services/debugService.d.ts.map +1 -0
- package/dist/services/debugService.js +57 -0
- package/dist/services/flushDebugLogs.d.ts +8 -0
- package/dist/services/flushDebugLogs.d.ts.map +1 -0
- package/dist/services/flushDebugLogs.js +20 -0
- package/{src/services/formParse.ts → dist/services/formAsyncParse.d.ts} +20 -44
- package/dist/services/formAsyncParse.d.ts.map +1 -0
- package/dist/services/formAsyncParse.js +58 -0
- package/dist/services/formParse.d.ts +3 -3
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +7 -5
- package/dist/services/getCaller.d.ts.map +1 -1
- package/dist/services/getCaller.js +9 -14
- package/dist/services/measureRouteExecution.d.ts +3 -0
- package/dist/services/measureRouteExecution.d.ts.map +1 -0
- package/dist/services/measureRouteExecution.js +24 -0
- package/dist/services/schemaValidator.d.ts +148 -3
- package/dist/services/schemaValidator.d.ts.map +1 -1
- package/dist/services/schemaValidator.js +160 -4
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +54 -0
- package/dist/validations/validateDate.d.ts +34 -0
- package/dist/validations/validateDate.d.ts.map +1 -0
- package/dist/validations/validateDate.js +73 -0
- package/dist/validations/validateEmail.d.ts +22 -0
- package/dist/validations/validateEmail.d.ts.map +1 -0
- package/dist/validations/validateEmail.js +123 -0
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +29 -0
- package/dist/validations/validatePhone.d.ts.map +1 -0
- package/dist/validations/validatePhone.js +44 -0
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +34 -18
- package/dist/config/apiInstance.d.ts.map +0 -1
- package/dist/config/arkynLogInstance.d.ts.map +0 -1
- package/dist/services/httpDebug.d.ts +0 -35
- package/dist/services/httpDebug.d.ts.map +0 -1
- package/dist/services/httpDebug.js +0 -52
- package/src/api/arkynLogRequest.ts +0 -116
- package/src/api/deleteRequest.ts +0 -22
- package/src/api/getRequest.ts +0 -20
- package/src/api/makeRequest.ts +0 -120
- package/src/api/patchRequest.ts +0 -22
- package/src/api/postRequest.ts +0 -22
- package/src/api/putRequest.ts +0 -22
- package/src/config/apiInstance.ts +0 -148
- package/src/config/arkynLogInstance.ts +0 -75
- package/src/http/badResponses/badGateway.ts +0 -63
- package/src/http/badResponses/badRequest.ts +0 -63
- package/src/http/badResponses/conflict.ts +0 -63
- package/src/http/badResponses/forbidden.ts +0 -63
- package/src/http/badResponses/notFound.ts +0 -63
- package/src/http/badResponses/notImplemented.ts +0 -63
- package/src/http/badResponses/serverError.ts +0 -63
- package/src/http/badResponses/unauthorized.ts +0 -63
- package/src/http/badResponses/unprocessableEntity.ts +0 -79
- package/src/http/successResponses/created.ts +0 -64
- package/src/http/successResponses/found.ts +0 -67
- package/src/http/successResponses/noContent.ts +0 -42
- package/src/http/successResponses/success.ts +0 -64
- package/src/http/successResponses/updated.ts +0 -64
- package/src/index.ts +0 -31
- package/src/mapper/arkynLogRequestMapper.ts +0 -73
- package/src/services/decodeErrorMessageFromRequest.ts +0 -36
- package/src/services/decodeRequestBody.ts +0 -43
- package/src/services/errorHandler.ts +0 -99
- package/src/services/getCaller.ts +0 -82
- package/src/services/getScopedParams.ts +0 -43
- package/src/services/httpDebug.ts +0 -61
- package/src/services/schemaValidator.ts +0 -66
- package/src/types/ApiResponseDTO.ts +0 -19
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -5
package/README.md
CHANGED
|
@@ -1,116 +1,419 @@
|
|
|
1
|
-
|
|
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
|
+
[](https://www.npmjs.com/package/@arkyn/server)
|
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
4
8
|
|
|
5
|
-
##
|
|
9
|
+
## ✨ Features
|
|
6
10
|
|
|
7
|
-
|
|
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
|
-
|
|
26
|
+
```typescript
|
|
27
|
+
import { Success, BadRequest, errorHandler } from "@arkyn/server";
|
|
16
28
|
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
38
|
+
export async function action({ request }: ActionFunctionArgs) {
|
|
39
|
+
const body = await decodeRequestBody(request);
|
|
21
40
|
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
50
|
+
## 📋 API Reference
|
|
28
51
|
|
|
29
|
-
|
|
30
|
-
Represents an HTTP 502 error.
|
|
52
|
+
### Configuration
|
|
31
53
|
|
|
32
|
-
|
|
33
|
-
Represents an HTTP 400 error.
|
|
54
|
+
#### ApiInstance
|
|
34
55
|
|
|
35
|
-
-
|
|
36
|
-
Represents an HTTP 409 error.
|
|
56
|
+
Pre-configured Axios instance for external API calls.
|
|
37
57
|
|
|
38
|
-
|
|
39
|
-
|
|
58
|
+
```typescript
|
|
59
|
+
import { ApiInstance } from "@arkyn/server";
|
|
40
60
|
|
|
41
|
-
|
|
42
|
-
|
|
61
|
+
const api = new ApiInstance({ baseUrl: "https://google.com" });
|
|
62
|
+
const response = await api.get("/users");
|
|
63
|
+
```
|
|
43
64
|
|
|
44
|
-
|
|
45
|
-
Represents an HTTP 501 error.
|
|
65
|
+
#### ArkynLogInstance
|
|
46
66
|
|
|
47
|
-
-
|
|
48
|
-
Represents an HTTP 500 error.
|
|
67
|
+
Pre-configured instance for integration with logging services.
|
|
49
68
|
|
|
50
|
-
|
|
51
|
-
|
|
69
|
+
```typescript
|
|
70
|
+
import { ArkynLogInstance } from "@arkyn/server";
|
|
52
71
|
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
ArkynLogInstance.setArkynConfig();
|
|
73
|
+
```
|
|
55
74
|
|
|
56
75
|
---
|
|
57
76
|
|
|
58
|
-
### HTTP Responses
|
|
77
|
+
### HTTP Error Responses
|
|
59
78
|
|
|
60
|
-
|
|
61
|
-
Represents an HTTP 201 response.
|
|
79
|
+
#### BadRequest(message: string): Error
|
|
62
80
|
|
|
63
|
-
|
|
64
|
-
Represents an HTTP 302 response.
|
|
81
|
+
Returns HTTP 400 for client errors.
|
|
65
82
|
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
70
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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
|
-
###
|
|
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
|
-
|
|
80
|
-
Decodes error messages from a request.
|
|
197
|
+
Returns HTTP 200 for successful updates.
|
|
81
198
|
|
|
82
|
-
|
|
83
|
-
|
|
199
|
+
```typescript
|
|
200
|
+
import { Updated } from "@arkyn/server";
|
|
84
201
|
|
|
85
|
-
|
|
86
|
-
|
|
202
|
+
const updatedUser = await updateUser(id, data);
|
|
203
|
+
return Updated(updatedUser);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### NoContent(): Response
|
|
87
207
|
|
|
88
|
-
|
|
89
|
-
Parses form data.
|
|
208
|
+
Returns HTTP 204 for successful operations without content.
|
|
90
209
|
|
|
91
|
-
|
|
92
|
-
|
|
210
|
+
```typescript
|
|
211
|
+
import { NoContent } from "@arkyn/server";
|
|
93
212
|
|
|
94
|
-
|
|
95
|
-
|
|
213
|
+
await deleteUser(id);
|
|
214
|
+
return NoContent();
|
|
215
|
+
```
|
|
96
216
|
|
|
97
|
-
|
|
98
|
-
Displays debug information for an HTTP request.
|
|
217
|
+
#### Found(data: any): Response
|
|
99
218
|
|
|
100
|
-
|
|
101
|
-
Validates data against a schema.
|
|
219
|
+
Returns HTTP 302 for redirects with data.
|
|
102
220
|
|
|
103
|
-
|
|
104
|
-
|
|
221
|
+
```typescript
|
|
222
|
+
import { Found } from "@arkyn/server";
|
|
223
|
+
|
|
224
|
+
return Found({ redirectUrl: "/dashboard" });
|
|
225
|
+
```
|
|
105
226
|
|
|
106
227
|
---
|
|
107
228
|
|
|
108
|
-
|
|
229
|
+
### Utilities & Services
|
|
230
|
+
|
|
231
|
+
#### errorHandler(error: Error): Response
|
|
232
|
+
|
|
233
|
+
Centralized error handling that returns appropriate HTTP responses.
|
|
109
234
|
|
|
110
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
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 `
|
|
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,
|
|
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,iBA4DjD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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 `
|
|
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,13 +41,10 @@ import { httpDebug } from "../services/httpDebug";
|
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
43
|
async function arkynLogRequest(config) {
|
|
44
|
-
const
|
|
45
|
-
if (!
|
|
44
|
+
const arkynService = ArkynLogService.getArkynConfig();
|
|
45
|
+
if (!arkynService)
|
|
46
46
|
return;
|
|
47
|
-
|
|
48
|
-
const { arkynUserToken } = arkynInstance;
|
|
49
|
-
let arkynApiUrl = arkynInstance.arkynApiUrl;
|
|
50
|
-
arkynApiUrl.replace(":trafficSourceId", arkynInstance.arkynTrafficSourceId);
|
|
47
|
+
const { userToken, apiUrl, trafficSourceId } = arkynService;
|
|
51
48
|
const { elapsedTime, method, queryParams, requestBody, requestHeaders, responseBody, responseHeaders, status, token, rawUrl, } = config;
|
|
52
49
|
// if (process.env.NODE_ENV === "development") return;
|
|
53
50
|
try {
|
|
@@ -58,6 +55,7 @@ async function arkynLogRequest(config) {
|
|
|
58
55
|
const body = JSON.stringify({
|
|
59
56
|
domainUrl: url.protocol + "//" + url.host,
|
|
60
57
|
pathnameUrl: url.pathname,
|
|
58
|
+
trafficSourceId: trafficSourceId,
|
|
61
59
|
status,
|
|
62
60
|
protocol,
|
|
63
61
|
method,
|
|
@@ -69,17 +67,23 @@ async function arkynLogRequest(config) {
|
|
|
69
67
|
responseHeaders,
|
|
70
68
|
responseBody,
|
|
71
69
|
});
|
|
72
|
-
await fetch(
|
|
70
|
+
const data = await fetch(apiUrl, {
|
|
73
71
|
method: "POST",
|
|
74
72
|
body,
|
|
75
73
|
headers: {
|
|
76
74
|
"Content-Type": "application/json",
|
|
77
|
-
Authorization: `Bearer ${
|
|
75
|
+
Authorization: `Bearer ${userToken}`,
|
|
78
76
|
},
|
|
79
77
|
});
|
|
78
|
+
console.log(data);
|
|
80
79
|
}
|
|
81
80
|
catch (err) {
|
|
82
|
-
|
|
81
|
+
console.log(err);
|
|
82
|
+
flushDebugLogs({
|
|
83
|
+
debugs: [`Error sending request: ${err}`],
|
|
84
|
+
name: "ARKYN_LOG_ERROR",
|
|
85
|
+
scheme: "red",
|
|
86
|
+
});
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
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,
|
|
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,
|
|
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"}
|
package/dist/api/makeRequest.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ArkynLogRequestMapper } from "../mapper/arkynLogRequestMapper";
|
|
2
|
-
import {
|
|
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,
|
|
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
|
-
|
|
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,
|