@arkyn/server 3.0.1-beta.2 → 3.0.1-beta.200
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 +423 -69
- package/dist/http/api/_logRequest.d.ts +58 -0
- package/dist/http/api/_logRequest.d.ts.map +1 -0
- package/dist/http/api/_makeRequest.d.ts +61 -0
- package/dist/http/api/_makeRequest.d.ts.map +1 -0
- package/dist/http/api/deleteRequest.d.ts +17 -0
- package/dist/http/api/deleteRequest.d.ts.map +1 -0
- package/dist/http/api/getRequest.d.ts +16 -0
- package/dist/http/api/getRequest.d.ts.map +1 -0
- package/dist/http/api/patchRequest.d.ts +17 -0
- package/dist/http/api/patchRequest.d.ts.map +1 -0
- package/dist/http/api/postRequest.d.ts +17 -0
- package/dist/http/api/postRequest.d.ts.map +1 -0
- package/dist/http/api/putRequest.d.ts +17 -0
- package/dist/http/api/putRequest.d.ts.map +1 -0
- package/dist/http/badResponses/_badResponse.d.ts +25 -0
- package/dist/http/badResponses/_badResponse.d.ts.map +1 -0
- package/dist/http/badResponses/badGateway.d.ts +14 -24
- package/dist/http/badResponses/badGateway.d.ts.map +1 -1
- package/dist/http/badResponses/badRequest.d.ts +12 -24
- package/dist/http/badResponses/badRequest.d.ts.map +1 -1
- package/dist/http/badResponses/conflict.d.ts +12 -24
- package/dist/http/badResponses/conflict.d.ts.map +1 -1
- package/dist/http/badResponses/forbidden.d.ts +12 -24
- package/dist/http/badResponses/forbidden.d.ts.map +1 -1
- package/dist/http/badResponses/notFound.d.ts +12 -24
- package/dist/http/badResponses/notFound.d.ts.map +1 -1
- package/dist/http/badResponses/notImplemented.d.ts +12 -24
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
- package/dist/http/badResponses/serverError.d.ts +12 -24
- package/dist/http/badResponses/serverError.d.ts.map +1 -1
- package/dist/http/badResponses/unauthorized.d.ts +12 -24
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
- package/dist/http/badResponses/unprocessableEntity.d.ts +25 -34
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
- package/dist/http/successResponses/_successResponse.d.ts +36 -0
- package/dist/http/successResponses/_successResponse.d.ts.map +1 -0
- package/dist/http/successResponses/created.d.ts +12 -26
- package/dist/http/successResponses/created.d.ts.map +1 -1
- package/dist/http/successResponses/found.d.ts +12 -29
- package/dist/http/successResponses/found.d.ts.map +1 -1
- package/dist/http/successResponses/noContent.d.ts +12 -17
- package/dist/http/successResponses/noContent.d.ts.map +1 -1
- package/dist/http/successResponses/success.d.ts +12 -26
- package/dist/http/successResponses/success.d.ts.map +1 -1
- package/dist/http/successResponses/updated.d.ts +13 -26
- package/dist/http/successResponses/updated.d.ts.map +1 -1
- package/dist/index.d.ts +19 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1067 -28
- package/dist/modules/http/api/_logRequest.js +56 -0
- package/dist/modules/http/api/_makeRequest.js +70 -0
- package/dist/modules/http/api/deleteRequest.js +13 -0
- package/dist/modules/http/api/getRequest.js +12 -0
- package/dist/modules/http/api/patchRequest.js +13 -0
- package/dist/modules/http/api/postRequest.js +13 -0
- package/dist/modules/http/api/putRequest.js +13 -0
- package/dist/modules/http/badResponses/_badResponse.js +63 -0
- package/dist/modules/http/badResponses/badGateway.js +24 -0
- package/dist/modules/http/badResponses/badRequest.js +24 -0
- package/dist/modules/http/badResponses/conflict.js +24 -0
- package/dist/modules/http/badResponses/forbidden.js +24 -0
- package/dist/modules/http/badResponses/notFound.js +24 -0
- package/dist/modules/http/badResponses/notImplemented.js +24 -0
- package/dist/modules/http/badResponses/serverError.js +24 -0
- package/dist/modules/http/badResponses/unauthorized.js +24 -0
- package/dist/modules/http/badResponses/unprocessableEntity.js +28 -0
- package/dist/modules/http/successResponses/_successResponse.js +62 -0
- package/dist/modules/http/successResponses/created.js +24 -0
- package/dist/modules/http/successResponses/found.js +24 -0
- package/dist/modules/http/successResponses/noContent.js +17 -0
- package/dist/modules/http/successResponses/success.js +24 -0
- package/dist/modules/http/successResponses/updated.js +24 -0
- package/dist/modules/index.js +34 -0
- package/dist/modules/services/apiService.js +109 -0
- package/dist/modules/services/debugService.js +36 -0
- package/dist/modules/services/logMapperService.js +31 -0
- package/dist/modules/services/logService.js +21 -0
- package/dist/modules/utilities/decodeRequestBody.js +20 -0
- package/dist/modules/utilities/decodeRequestErrorMessage.js +6 -0
- package/dist/modules/utilities/errorHandler.js +39 -0
- package/dist/modules/utilities/flushDebugLogs.js +16 -0
- package/dist/modules/utilities/formAsyncParse.js +14 -0
- package/dist/modules/utilities/formParse.js +14 -0
- package/dist/modules/utilities/getScopedParams.js +9 -0
- package/dist/modules/utilities/schemaValidator.js +55 -0
- package/dist/modules/validations/validateCep.js +9 -0
- package/dist/modules/validations/validateCnpj.js +54 -0
- package/dist/modules/validations/validateCpf.js +27 -0
- package/dist/modules/validations/validateDate.js +26 -0
- package/dist/modules/validations/validateEmail.js +54 -0
- package/dist/modules/validations/validatePassword.js +12 -0
- package/dist/modules/validations/validatePhone.js +10 -0
- package/dist/modules/validations/validateRg.js +8 -0
- package/dist/services/apiService.d.ts +83 -0
- package/dist/services/apiService.d.ts.map +1 -0
- package/dist/services/debugService.d.ts +37 -0
- package/dist/services/debugService.d.ts.map +1 -0
- package/dist/services/logMapperService.d.ts +86 -0
- package/dist/services/logMapperService.d.ts.map +1 -0
- package/dist/services/logService.d.ts +33 -0
- package/dist/services/logService.d.ts.map +1 -0
- package/dist/utilities/decodeRequestBody.d.ts +18 -0
- package/dist/utilities/decodeRequestBody.d.ts.map +1 -0
- package/dist/utilities/decodeRequestErrorMessage.d.ts +19 -0
- package/dist/utilities/decodeRequestErrorMessage.d.ts.map +1 -0
- package/dist/utilities/errorHandler.d.ts +25 -0
- package/dist/utilities/errorHandler.d.ts.map +1 -0
- package/dist/utilities/flushDebugLogs.d.ts +24 -0
- package/dist/utilities/flushDebugLogs.d.ts.map +1 -0
- package/dist/utilities/formAsyncParse.d.ts +38 -0
- package/dist/utilities/formAsyncParse.d.ts.map +1 -0
- package/dist/utilities/formParse.d.ts +38 -0
- package/dist/utilities/formParse.d.ts.map +1 -0
- package/dist/utilities/getScopedParams.d.ts +19 -0
- package/dist/utilities/getScopedParams.d.ts.map +1 -0
- package/dist/utilities/schemaValidator.d.ts +75 -0
- package/dist/utilities/schemaValidator.d.ts.map +1 -0
- package/dist/validations/validateCep.d.ts +19 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCnpj.d.ts +21 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCpf.d.ts +16 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateDate.d.ts +23 -0
- package/dist/validations/validateDate.d.ts.map +1 -0
- package/dist/validations/validateEmail.d.ts +21 -0
- package/dist/validations/validateEmail.d.ts.map +1 -0
- package/dist/validations/validatePassword.d.ts +20 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePhone.d.ts +16 -0
- package/dist/validations/validatePhone.d.ts.map +1 -0
- package/dist/validations/validateRg.d.ts +21 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/package.json +227 -23
- package/dist/api/deleteRequest.d.ts +0 -13
- package/dist/api/deleteRequest.d.ts.map +0 -1
- package/dist/api/deleteRequest.js +0 -14
- package/dist/api/getRequest.d.ts +0 -12
- package/dist/api/getRequest.d.ts.map +0 -1
- package/dist/api/getRequest.js +0 -13
- package/dist/api/inboxFlowRequest.d.ts +0 -40
- package/dist/api/inboxFlowRequest.d.ts.map +0 -1
- package/dist/api/inboxFlowRequest.js +0 -63
- package/dist/api/makeRequest.d.ts +0 -38
- package/dist/api/makeRequest.d.ts.map +0 -1
- package/dist/api/makeRequest.js +0 -102
- package/dist/api/patchRequest.d.ts +0 -13
- package/dist/api/patchRequest.d.ts.map +0 -1
- package/dist/api/patchRequest.js +0 -14
- package/dist/api/postRequest.d.ts +0 -13
- package/dist/api/postRequest.d.ts.map +0 -1
- package/dist/api/postRequest.js +0 -14
- package/dist/api/putRequest.d.ts +0 -13
- package/dist/api/putRequest.d.ts.map +0 -1
- package/dist/api/putRequest.js +0 -14
- package/dist/config/apiInstance.d.ts +0 -80
- package/dist/config/apiInstance.d.ts.map +0 -1
- package/dist/config/apiInstance.js +0 -111
- package/dist/config/inboxFlowInstance.d.ts +0 -44
- package/dist/config/inboxFlowInstance.d.ts.map +0 -1
- package/dist/config/inboxFlowInstance.js +0 -46
- package/dist/http/badResponses/badGateway.js +0 -52
- package/dist/http/badResponses/badRequest.js +0 -52
- package/dist/http/badResponses/conflict.js +0 -52
- package/dist/http/badResponses/forbidden.js +0 -52
- package/dist/http/badResponses/notFound.js +0 -52
- package/dist/http/badResponses/notImplemented.js +0 -52
- package/dist/http/badResponses/serverError.js +0 -52
- package/dist/http/badResponses/unauthorized.js +0 -52
- package/dist/http/badResponses/unprocessableEntity.js +0 -60
- package/dist/http/successResponses/created.js +0 -54
- package/dist/http/successResponses/found.js +0 -57
- package/dist/http/successResponses/noContent.js +0 -35
- package/dist/http/successResponses/success.js +0 -54
- package/dist/http/successResponses/updated.js +0 -54
- package/dist/services/decodeErrorMessageFromRequest.d.ts +0 -17
- package/dist/services/decodeErrorMessageFromRequest.d.ts.map +0 -1
- package/dist/services/decodeErrorMessageFromRequest.js +0 -30
- package/dist/services/decodeRequestBody.d.ts +0 -17
- package/dist/services/decodeRequestBody.d.ts.map +0 -1
- package/dist/services/decodeRequestBody.js +0 -38
- package/dist/services/errorHandler.d.ts +0 -44
- package/dist/services/errorHandler.d.ts.map +0 -1
- package/dist/services/errorHandler.js +0 -93
- package/dist/services/formParse.d.ts +0 -59
- package/dist/services/formParse.d.ts.map +0 -1
- package/dist/services/formParse.js +0 -56
- package/dist/services/getCaller.d.ts +0 -17
- package/dist/services/getCaller.d.ts.map +0 -1
- package/dist/services/getCaller.js +0 -39
- package/dist/services/getScopedParams.d.ts +0 -28
- package/dist/services/getScopedParams.d.ts.map +0 -1
- package/dist/services/getScopedParams.js +0 -34
- package/dist/services/httpDebug.d.ts +0 -35
- package/dist/services/httpDebug.d.ts.map +0 -1
- package/dist/services/httpDebug.js +0 -53
- package/dist/services/schemaValidator.d.ts +0 -13
- package/dist/services/schemaValidator.d.ts.map +0 -1
- package/dist/services/schemaValidator.js +0 -51
- package/dist/types/ApiResponseDTO.d.ts +0 -17
- package/dist/types/ApiResponseDTO.d.ts.map +0 -1
- package/dist/types/ApiResponseDTO.js +0 -1
- package/src/api/deleteRequest.ts +0 -22
- package/src/api/getRequest.ts +0 -20
- package/src/api/inboxFlowRequest.ts +0 -76
- package/src/api/makeRequest.ts +0 -116
- 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/inboxFlowInstance.ts +0 -65
- 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/services/decodeErrorMessageFromRequest.ts +0 -36
- package/src/services/decodeRequestBody.ts +0 -43
- package/src/services/errorHandler.ts +0 -99
- package/src/services/formParse.ts +0 -83
- package/src/services/getCaller.ts +0 -48
- package/src/services/getScopedParams.ts +0 -43
- package/src/services/httpDebug.ts +0 -62
- 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
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { BadRequest } from "../http/badResponses/badRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Decodes the body of an incoming request into a JavaScript object.
|
|
4
|
-
*
|
|
5
|
-
* This function attempts to parse the request body in the following order:
|
|
6
|
-
* 1. Tries to parse the body as JSON.
|
|
7
|
-
* 2. If JSON parsing fails, attempts to parse the body as URL-encoded form data.
|
|
8
|
-
* 3. If both parsing attempts fail, logs the errors and returns an empty object.
|
|
9
|
-
*
|
|
10
|
-
* @param req - The incoming request object containing the body to decode.
|
|
11
|
-
* @returns A promise that resolves to the decoded data as a JavaScript object.
|
|
12
|
-
*
|
|
13
|
-
* @throws Logs errors to the console if the request body cannot be read or parsed.
|
|
14
|
-
*/
|
|
15
|
-
const decodeRequestBody = async (req) => {
|
|
16
|
-
let data;
|
|
17
|
-
const arrayBuffer = await req.arrayBuffer();
|
|
18
|
-
const text = new TextDecoder().decode(arrayBuffer);
|
|
19
|
-
try {
|
|
20
|
-
data = JSON.parse(text);
|
|
21
|
-
}
|
|
22
|
-
catch (jsonError) {
|
|
23
|
-
try {
|
|
24
|
-
if (text.includes("=")) {
|
|
25
|
-
const formData = new URLSearchParams(text);
|
|
26
|
-
data = Object.fromEntries(formData.entries());
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
throw new BadRequest("Invalid URLSearchParams format");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch (formDataError) {
|
|
33
|
-
throw new BadRequest("Failed to extract data from request");
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return data;
|
|
37
|
-
};
|
|
38
|
-
export { decodeRequestBody };
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Handles errors and converts them into appropriate HTTP responses.
|
|
3
|
-
*
|
|
4
|
-
* This function takes an error object and determines its type to return
|
|
5
|
-
* the corresponding HTTP response. It supports both success and error
|
|
6
|
-
* response types, converting them into a standardized format using the
|
|
7
|
-
* `toResponse` method when applicable.
|
|
8
|
-
*
|
|
9
|
-
* @param error - The error object to handle. It can be an instance of various
|
|
10
|
-
* HTTP response classes or a generic error.
|
|
11
|
-
*
|
|
12
|
-
* @returns The corresponding HTTP response object if the error matches a known
|
|
13
|
-
* type, or `undefined` if no match is found.
|
|
14
|
-
*
|
|
15
|
-
* ### Supported Success Responses:
|
|
16
|
-
* - `Found`
|
|
17
|
-
* - `Created`
|
|
18
|
-
* - `Updated`
|
|
19
|
-
* - `Success`
|
|
20
|
-
* - `NoContent`
|
|
21
|
-
*
|
|
22
|
-
* ### Supported Error Responses:
|
|
23
|
-
* - `BadGateway`
|
|
24
|
-
* - `BadRequest`
|
|
25
|
-
* - `Conflict`
|
|
26
|
-
* - `Forbidden`
|
|
27
|
-
* - `NotFound`
|
|
28
|
-
* - `NotImplemented`
|
|
29
|
-
* - `ServerError`
|
|
30
|
-
* - `Unauthorized`
|
|
31
|
-
* - `UnprocessableEntity`
|
|
32
|
-
*
|
|
33
|
-
* ### Example Usage:
|
|
34
|
-
* ```typescript
|
|
35
|
-
* try {
|
|
36
|
-
* // Some operation that might throw an error
|
|
37
|
-
* } catch (error) {
|
|
38
|
-
* return errorHandler(error);
|
|
39
|
-
* }
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare function errorHandler(error: any): Response;
|
|
43
|
-
export { errorHandler };
|
|
44
|
-
//# sourceMappingURL=errorHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../src/services/errorHandler.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,iBAAS,YAAY,CAAC,KAAK,EAAE,GAAG,GAAG,QAAQ,CAsC1C;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { BadGateway } from "../http/badResponses/badGateway";
|
|
2
|
-
import { BadRequest } from "../http/badResponses/badRequest";
|
|
3
|
-
import { Conflict } from "../http/badResponses/conflict";
|
|
4
|
-
import { Forbidden } from "../http/badResponses/forbidden";
|
|
5
|
-
import { NotFound } from "../http/badResponses/notFound";
|
|
6
|
-
import { NotImplemented } from "../http/badResponses/notImplemented";
|
|
7
|
-
import { ServerError } from "../http/badResponses/serverError";
|
|
8
|
-
import { Unauthorized } from "../http/badResponses/unauthorized";
|
|
9
|
-
import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
|
|
10
|
-
import { Created } from "../http/successResponses/created";
|
|
11
|
-
import { Found } from "../http/successResponses/found";
|
|
12
|
-
import { NoContent } from "../http/successResponses/noContent";
|
|
13
|
-
import { Success } from "../http/successResponses/success";
|
|
14
|
-
import { Updated } from "../http/successResponses/updated";
|
|
15
|
-
/**
|
|
16
|
-
* Handles errors and converts them into appropriate HTTP responses.
|
|
17
|
-
*
|
|
18
|
-
* This function takes an error object and determines its type to return
|
|
19
|
-
* the corresponding HTTP response. It supports both success and error
|
|
20
|
-
* response types, converting them into a standardized format using the
|
|
21
|
-
* `toResponse` method when applicable.
|
|
22
|
-
*
|
|
23
|
-
* @param error - The error object to handle. It can be an instance of various
|
|
24
|
-
* HTTP response classes or a generic error.
|
|
25
|
-
*
|
|
26
|
-
* @returns The corresponding HTTP response object if the error matches a known
|
|
27
|
-
* type, or `undefined` if no match is found.
|
|
28
|
-
*
|
|
29
|
-
* ### Supported Success Responses:
|
|
30
|
-
* - `Found`
|
|
31
|
-
* - `Created`
|
|
32
|
-
* - `Updated`
|
|
33
|
-
* - `Success`
|
|
34
|
-
* - `NoContent`
|
|
35
|
-
*
|
|
36
|
-
* ### Supported Error Responses:
|
|
37
|
-
* - `BadGateway`
|
|
38
|
-
* - `BadRequest`
|
|
39
|
-
* - `Conflict`
|
|
40
|
-
* - `Forbidden`
|
|
41
|
-
* - `NotFound`
|
|
42
|
-
* - `NotImplemented`
|
|
43
|
-
* - `ServerError`
|
|
44
|
-
* - `Unauthorized`
|
|
45
|
-
* - `UnprocessableEntity`
|
|
46
|
-
*
|
|
47
|
-
* ### Example Usage:
|
|
48
|
-
* ```typescript
|
|
49
|
-
* try {
|
|
50
|
-
* // Some operation that might throw an error
|
|
51
|
-
* } catch (error) {
|
|
52
|
-
* return errorHandler(error);
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
function errorHandler(error) {
|
|
57
|
-
switch (true) {
|
|
58
|
-
case error instanceof Response:
|
|
59
|
-
return error;
|
|
60
|
-
case error instanceof Found:
|
|
61
|
-
return error.toResponse();
|
|
62
|
-
case error instanceof Created:
|
|
63
|
-
return error.toResponse();
|
|
64
|
-
case error instanceof Updated:
|
|
65
|
-
return error.toResponse();
|
|
66
|
-
case error instanceof Success:
|
|
67
|
-
return error.toResponse();
|
|
68
|
-
case error instanceof NoContent:
|
|
69
|
-
return error.toResponse();
|
|
70
|
-
}
|
|
71
|
-
switch (true) {
|
|
72
|
-
case error instanceof BadGateway:
|
|
73
|
-
return error.toResponse();
|
|
74
|
-
case error instanceof BadRequest:
|
|
75
|
-
return error.toResponse();
|
|
76
|
-
case error instanceof Conflict:
|
|
77
|
-
return error.toResponse();
|
|
78
|
-
case error instanceof Forbidden:
|
|
79
|
-
return error.toResponse();
|
|
80
|
-
case error instanceof NotFound:
|
|
81
|
-
return error.toResponse();
|
|
82
|
-
case error instanceof NotImplemented:
|
|
83
|
-
return error.toResponse();
|
|
84
|
-
case error instanceof ServerError:
|
|
85
|
-
return error.toResponse();
|
|
86
|
-
case error instanceof Unauthorized:
|
|
87
|
-
return error.toResponse();
|
|
88
|
-
case error instanceof UnprocessableEntity:
|
|
89
|
-
return error.toResponse();
|
|
90
|
-
}
|
|
91
|
-
return new ServerError("Server error", error).toResponse();
|
|
92
|
-
}
|
|
93
|
-
export { errorHandler };
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { Schema } from "zod";
|
|
2
|
-
type SuccessResponse<T extends FormParseProps> = {
|
|
3
|
-
success: true;
|
|
4
|
-
data: T[1] extends Schema<infer U> ? U : never;
|
|
5
|
-
};
|
|
6
|
-
type ErrorResponse = {
|
|
7
|
-
success: false;
|
|
8
|
-
fields: {
|
|
9
|
-
[x: string]: string;
|
|
10
|
-
};
|
|
11
|
-
fieldErrors: {
|
|
12
|
-
[x: string]: string;
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
type FormParseProps = [formData: {
|
|
16
|
-
[k: string]: any;
|
|
17
|
-
}, schema: Schema];
|
|
18
|
-
type FormParseReturnType<T extends FormParseProps> = SuccessResponse<T> | ErrorResponse;
|
|
19
|
-
/**
|
|
20
|
-
* Parses form data using a Zod schema and returns the result.
|
|
21
|
-
*
|
|
22
|
-
* @template T - A type that extends `FormParseProps`.
|
|
23
|
-
*
|
|
24
|
-
* @param {T} param0 - An array containing the form data and the Zod schema.
|
|
25
|
-
* @param {T[0]} param0[0] - The form data to be validated.
|
|
26
|
-
* @param {T[1]} param0[1] - The Zod schema used for validation.
|
|
27
|
-
*
|
|
28
|
-
* @returns {FormParseReturnType<T>} An object containing the validation result.
|
|
29
|
-
* - If validation fails, it includes:
|
|
30
|
-
* - `success`: A boolean indicating the validation status (false).
|
|
31
|
-
* - `fieldErrors`: An object mapping field names to their respective error messages.
|
|
32
|
-
* - `fields`: The original form data.
|
|
33
|
-
* - If validation succeeds, it includes:
|
|
34
|
-
* - `success`: A boolean indicating the validation status (true).
|
|
35
|
-
* - `data`: The parsed and validated data.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* import { z } from "zod";
|
|
40
|
-
*
|
|
41
|
-
* const schema = z.object({
|
|
42
|
-
* name: z.string().min(1, "Name is required"),
|
|
43
|
-
* age: z.number().min(18, "Must be at least 18"),
|
|
44
|
-
* });
|
|
45
|
-
*
|
|
46
|
-
* const formData = { name: "", age: 17 };
|
|
47
|
-
*
|
|
48
|
-
* const result = formParse([formData, schema]);
|
|
49
|
-
*
|
|
50
|
-
* if (!result.success) {
|
|
51
|
-
* console.log(result.fieldErrors); // { name: "Name is required", age: "Must be at least 18" }
|
|
52
|
-
* } else {
|
|
53
|
-
* console.log(result.data); // Parsed data
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
declare function formParse<T extends FormParseProps>([formData, schema,]: T): FormParseReturnType<T>;
|
|
58
|
-
export { formParse };
|
|
59
|
-
//# sourceMappingURL=formParse.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formParse.d.ts","sourceRoot":"","sources":["../../src/services/formParse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAElC,KAAK,eAAe,CAAC,CAAC,SAAS,cAAc,IAAI;IAC/C,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;CAChD,CAAC;AAEF,KAAK,aAAa,GAAG;IACnB,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAChC,WAAW,EAAE;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACtC,CAAC;AAEF,KAAK,cAAc,GAAG,CAAC,QAAQ,EAAE;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEvE,KAAK,mBAAmB,CAAC,CAAC,SAAS,cAAc,IAC7C,eAAe,CAAC,CAAC,CAAC,GAClB,aAAa,CAAC;AAElB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iBAAS,SAAS,CAAC,CAAC,SAAS,cAAc,EAAE,CAC3C,QAAQ,EACR,MAAM,EACP,EAAE,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAmB5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parses form data using a Zod schema and returns the result.
|
|
3
|
-
*
|
|
4
|
-
* @template T - A type that extends `FormParseProps`.
|
|
5
|
-
*
|
|
6
|
-
* @param {T} param0 - An array containing the form data and the Zod schema.
|
|
7
|
-
* @param {T[0]} param0[0] - The form data to be validated.
|
|
8
|
-
* @param {T[1]} param0[1] - The Zod schema used for validation.
|
|
9
|
-
*
|
|
10
|
-
* @returns {FormParseReturnType<T>} An object containing the validation result.
|
|
11
|
-
* - If validation fails, it includes:
|
|
12
|
-
* - `success`: A boolean indicating the validation status (false).
|
|
13
|
-
* - `fieldErrors`: An object mapping field names to their respective error messages.
|
|
14
|
-
* - `fields`: The original form data.
|
|
15
|
-
* - If validation succeeds, it includes:
|
|
16
|
-
* - `success`: A boolean indicating the validation status (true).
|
|
17
|
-
* - `data`: The parsed and validated data.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* import { z } from "zod";
|
|
22
|
-
*
|
|
23
|
-
* const schema = z.object({
|
|
24
|
-
* name: z.string().min(1, "Name is required"),
|
|
25
|
-
* age: z.number().min(18, "Must be at least 18"),
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* const formData = { name: "", age: 17 };
|
|
29
|
-
*
|
|
30
|
-
* const result = formParse([formData, schema]);
|
|
31
|
-
*
|
|
32
|
-
* if (!result.success) {
|
|
33
|
-
* console.log(result.fieldErrors); // { name: "Name is required", age: "Must be at least 18" }
|
|
34
|
-
* } else {
|
|
35
|
-
* console.log(result.data); // Parsed data
|
|
36
|
-
* }
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
function formParse([formData, schema,]) {
|
|
40
|
-
const zodResponse = schema.safeParse(formData);
|
|
41
|
-
if (zodResponse.success === false) {
|
|
42
|
-
const errorsObject = Object.fromEntries(zodResponse.error.errors.map((item) => [
|
|
43
|
-
item.path.join("."),
|
|
44
|
-
item.message,
|
|
45
|
-
]));
|
|
46
|
-
return {
|
|
47
|
-
success: zodResponse.success,
|
|
48
|
-
fieldErrors: errorsObject,
|
|
49
|
-
fields: formData,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
return { success: zodResponse.success, data: zodResponse.data };
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export { formParse };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retrieves information about the caller of the current function.
|
|
3
|
-
*
|
|
4
|
-
* This function analyzes the stack trace to determine the file path and function name
|
|
5
|
-
* of the caller. It excludes stack trace entries related to the `@arkyn/server` package
|
|
6
|
-
* and attempts to resolve the file path relative to the project root directory.
|
|
7
|
-
*
|
|
8
|
-
* @returns An object containing:
|
|
9
|
-
* - `functionName`: The name of the function that called the current function, or "Unknown function" if it cannot be determined.
|
|
10
|
-
* - `callerInfo`: The file path of the caller relative to the project root, or "Unknown caller" if it cannot be determined.
|
|
11
|
-
*/
|
|
12
|
-
declare function getCaller(): {
|
|
13
|
-
functionName: string;
|
|
14
|
-
callerInfo: string;
|
|
15
|
-
};
|
|
16
|
-
export { getCaller };
|
|
17
|
-
//# sourceMappingURL=getCaller.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getCaller.d.ts","sourceRoot":"","sources":["../../src/services/getCaller.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,iBAAS,SAAS;;;EA+BjB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
/**
|
|
3
|
-
* Retrieves information about the caller of the current function.
|
|
4
|
-
*
|
|
5
|
-
* This function analyzes the stack trace to determine the file path and function name
|
|
6
|
-
* of the caller. It excludes stack trace entries related to the `@arkyn/server` package
|
|
7
|
-
* and attempts to resolve the file path relative to the project root directory.
|
|
8
|
-
*
|
|
9
|
-
* @returns An object containing:
|
|
10
|
-
* - `functionName`: The name of the function that called the current function, or "Unknown function" if it cannot be determined.
|
|
11
|
-
* - `callerInfo`: The file path of the caller relative to the project root, or "Unknown caller" if it cannot be determined.
|
|
12
|
-
*/
|
|
13
|
-
function getCaller() {
|
|
14
|
-
const projectRoot = process.cwd();
|
|
15
|
-
const err = new Error();
|
|
16
|
-
const stack = err.stack || "";
|
|
17
|
-
const stackLines = stack.split("\n").map((line) => line.trim());
|
|
18
|
-
const callerLine = stackLines[4] || "";
|
|
19
|
-
let functionName = "unknown";
|
|
20
|
-
let callerInfo = "unknown location";
|
|
21
|
-
const functionMatch = callerLine.match(/at\s+([^\s]+)\s+\((.*)\)/);
|
|
22
|
-
if (functionMatch) {
|
|
23
|
-
functionName = functionMatch[1];
|
|
24
|
-
callerInfo = functionMatch[2];
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
const locationMatch = callerLine.match(/at\s+(.*)/);
|
|
28
|
-
if (locationMatch) {
|
|
29
|
-
callerInfo = locationMatch[1];
|
|
30
|
-
const pathParts = callerInfo.split("/");
|
|
31
|
-
const lastPart = pathParts[pathParts.length - 1] || "";
|
|
32
|
-
const fileParts = lastPart.split(":");
|
|
33
|
-
functionName = fileParts[0] || "unknown";
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
callerInfo = path.relative(projectRoot, callerInfo);
|
|
37
|
-
return { functionName, callerInfo };
|
|
38
|
-
}
|
|
39
|
-
export { getCaller };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
type GetScopedParamsFunction = (request: Request, scope?: string) => URLSearchParams;
|
|
2
|
-
/**
|
|
3
|
-
* Extracts and returns scoped query parameters from a request URL.
|
|
4
|
-
*
|
|
5
|
-
* @param request - The incoming request object containing the URL.
|
|
6
|
-
* @param scope - An optional string representing the scope prefix for filtering query parameters.
|
|
7
|
-
* If no scope is provided, all query parameters are returned.
|
|
8
|
-
*
|
|
9
|
-
* @returns A `URLSearchParams` object containing the scoped query parameters.
|
|
10
|
-
* If a scope is provided, only parameters with keys starting with the scope
|
|
11
|
-
* (e.g., `scope:key`) are included, and the scope prefix is removed from the keys.
|
|
12
|
-
* If no scope is provided, all query parameters are returned as-is.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* // Example 1: No scope provided
|
|
16
|
-
* const request = { url: "https://example.com?key1=value1&key2=value2" };
|
|
17
|
-
* const params = getScopedParams(request);
|
|
18
|
-
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* // Example 2: Scope provided
|
|
22
|
-
* const request = { url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3" };
|
|
23
|
-
* const params = getScopedParams(request, "scope");
|
|
24
|
-
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
25
|
-
*/
|
|
26
|
-
declare const getScopedParams: GetScopedParamsFunction;
|
|
27
|
-
export { getScopedParams };
|
|
28
|
-
//# sourceMappingURL=getScopedParams.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getScopedParams.d.ts","sourceRoot":"","sources":["../../src/services/getScopedParams.ts"],"names":[],"mappings":"AAAA,KAAK,uBAAuB,GAAG,CAC7B,OAAO,EAAE,OAAO,EAChB,KAAK,CAAC,EAAE,MAAM,KACX,eAAe,CAAC;AACrB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,QAAA,MAAM,eAAe,EAAE,uBAWtB,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts and returns scoped query parameters from a request URL.
|
|
3
|
-
*
|
|
4
|
-
* @param request - The incoming request object containing the URL.
|
|
5
|
-
* @param scope - An optional string representing the scope prefix for filtering query parameters.
|
|
6
|
-
* If no scope is provided, all query parameters are returned.
|
|
7
|
-
*
|
|
8
|
-
* @returns A `URLSearchParams` object containing the scoped query parameters.
|
|
9
|
-
* If a scope is provided, only parameters with keys starting with the scope
|
|
10
|
-
* (e.g., `scope:key`) are included, and the scope prefix is removed from the keys.
|
|
11
|
-
* If no scope is provided, all query parameters are returned as-is.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* // Example 1: No scope provided
|
|
15
|
-
* const request = { url: "https://example.com?key1=value1&key2=value2" };
|
|
16
|
-
* const params = getScopedParams(request);
|
|
17
|
-
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Example 2: Scope provided
|
|
21
|
-
* const request = { url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3" };
|
|
22
|
-
* const params = getScopedParams(request, "scope");
|
|
23
|
-
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
24
|
-
*/
|
|
25
|
-
const getScopedParams = (request, scope = "") => {
|
|
26
|
-
const url = new URL(request.url);
|
|
27
|
-
if (scope === "")
|
|
28
|
-
return url.searchParams;
|
|
29
|
-
const scopedSearchParams = Array.from(url.searchParams.entries())
|
|
30
|
-
.filter(([key]) => key.startsWith(`${scope}:`))
|
|
31
|
-
.map(([key, value]) => [key.replace(`${scope}:`, ""), value]);
|
|
32
|
-
return new URLSearchParams(scopedSearchParams);
|
|
33
|
-
};
|
|
34
|
-
export { getScopedParams };
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Logs debug information to the console when in development mode or when the
|
|
3
|
-
* `SHOW_ERRORS_IN_CONSOLE` environment variable is set to "true".
|
|
4
|
-
*
|
|
5
|
-
* This function provides detailed information about the caller function,
|
|
6
|
-
* its location, and the provided body and cause, if any.
|
|
7
|
-
*
|
|
8
|
-
* @param name - A string representing the name or context of the debug log.
|
|
9
|
-
* @param body - The main content or data to be logged.
|
|
10
|
-
* @param cause - (Optional) Additional information or error cause to be logged.
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* The debug logs are only displayed when the application is running in
|
|
14
|
-
* development mode (`NODE_ENV === "development"`) or when the
|
|
15
|
-
* `SHOW_ERRORS_IN_CONSOLE` environment variable is explicitly set to "true".
|
|
16
|
-
*
|
|
17
|
-
* The logs include:
|
|
18
|
-
* - The name of the debug context.
|
|
19
|
-
* - The caller function name and its location.
|
|
20
|
-
* - The provided body content.
|
|
21
|
-
* - The optional cause, if provided.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* httpDebug("FetchUserData", { userId: 123 });
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* httpDebug("FetchUserDataError", { userId: 123 }, new Error("User not found"));
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
declare function httpDebug(name: string, body: any, cause?: any): void;
|
|
34
|
-
export { httpDebug };
|
|
35
|
-
//# sourceMappingURL=httpDebug.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"httpDebug.d.ts","sourceRoot":"","sources":["../../src/services/httpDebug.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,iBAAS,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,QAuBtD;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { InboxFlowInstance } from "../config/inboxFlowInstance";
|
|
2
|
-
import { getCaller } from "../services/getCaller";
|
|
3
|
-
/**
|
|
4
|
-
* Logs debug information to the console when in development mode or when the
|
|
5
|
-
* `SHOW_ERRORS_IN_CONSOLE` environment variable is set to "true".
|
|
6
|
-
*
|
|
7
|
-
* This function provides detailed information about the caller function,
|
|
8
|
-
* its location, and the provided body and cause, if any.
|
|
9
|
-
*
|
|
10
|
-
* @param name - A string representing the name or context of the debug log.
|
|
11
|
-
* @param body - The main content or data to be logged.
|
|
12
|
-
* @param cause - (Optional) Additional information or error cause to be logged.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* The debug logs are only displayed when the application is running in
|
|
16
|
-
* development mode (`NODE_ENV === "development"`) or when the
|
|
17
|
-
* `SHOW_ERRORS_IN_CONSOLE` environment variable is explicitly set to "true".
|
|
18
|
-
*
|
|
19
|
-
* The logs include:
|
|
20
|
-
* - The name of the debug context.
|
|
21
|
-
* - The caller function name and its location.
|
|
22
|
-
* - The provided body content.
|
|
23
|
-
* - The optional cause, if provided.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* httpDebug("FetchUserData", { userId: 123 });
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```typescript
|
|
32
|
-
* httpDebug("FetchUserDataError", { userId: 123 }, new Error("User not found"));
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
function httpDebug(name, body, cause) {
|
|
36
|
-
const isDebugMode = process.env.NODE_ENV === "development" ||
|
|
37
|
-
process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
|
|
38
|
-
if (isDebugMode) {
|
|
39
|
-
const reset = "\x1b[0m";
|
|
40
|
-
const cyan = "\x1b[36m";
|
|
41
|
-
const debugName = `${cyan}[ARKYN-DEBUG]${reset}`;
|
|
42
|
-
const { callerInfo, functionName } = getCaller();
|
|
43
|
-
let consoleData = `${debugName} ${name} initialized\n`;
|
|
44
|
-
consoleData += `${debugName} Caller Function: ${functionName}\n`;
|
|
45
|
-
consoleData += `${debugName} Caller Location: ${callerInfo}\n`;
|
|
46
|
-
consoleData += `${debugName} Body: ${JSON.stringify(body, null, 2)}\n`;
|
|
47
|
-
if (cause) {
|
|
48
|
-
consoleData += `${debugName} Cause: ${JSON.stringify(cause, null, 2)}\n`;
|
|
49
|
-
}
|
|
50
|
-
console.log(consoleData);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export { httpDebug };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Schema, z } from "zod";
|
|
2
|
-
declare class SchemaValidator<T extends Schema> {
|
|
3
|
-
readonly schema: T;
|
|
4
|
-
functionName: string;
|
|
5
|
-
callerInfo: string;
|
|
6
|
-
constructor(schema: T);
|
|
7
|
-
isValid(data: any): boolean;
|
|
8
|
-
safeValidate(data: any): z.SafeParseReturnType<z.infer<T>, z.infer<T>>;
|
|
9
|
-
validate(data: any): z.infer<T>;
|
|
10
|
-
formValidate(data: any, message?: string): z.infer<T>;
|
|
11
|
-
}
|
|
12
|
-
export { SchemaValidator };
|
|
13
|
-
//# sourceMappingURL=schemaValidator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/services/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAiBhC,cAAM,eAAe,CAAC,CAAC,SAAS,MAAM;IAIxB,QAAQ,CAAC,MAAM,EAAE,CAAC;IAH9B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;gBAEE,MAAM,EAAE,CAAC;IAM9B,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO;IAI3B,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAItE,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAQ/B,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;CAoBtD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Schema, z } from "zod";
|
|
2
|
-
import { ServerError } from "../http/badResponses/serverError";
|
|
3
|
-
import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
|
|
4
|
-
import { formParse } from "./formParse";
|
|
5
|
-
import { getCaller } from "./getCaller";
|
|
6
|
-
import { httpDebug } from "./httpDebug";
|
|
7
|
-
function formatErrorMessage(error) {
|
|
8
|
-
const title = "Error validating:";
|
|
9
|
-
const lines = error.issues.map(({ path, message }) => `-> ${path.join(".")}: ${message}`);
|
|
10
|
-
return [title, ...lines].join("\n");
|
|
11
|
-
}
|
|
12
|
-
class SchemaValidator {
|
|
13
|
-
schema;
|
|
14
|
-
functionName;
|
|
15
|
-
callerInfo;
|
|
16
|
-
constructor(schema) {
|
|
17
|
-
this.schema = schema;
|
|
18
|
-
const { callerInfo, functionName } = getCaller();
|
|
19
|
-
this.callerInfo = callerInfo;
|
|
20
|
-
this.functionName = functionName;
|
|
21
|
-
}
|
|
22
|
-
isValid(data) {
|
|
23
|
-
return this.schema.safeParse(data).success;
|
|
24
|
-
}
|
|
25
|
-
safeValidate(data) {
|
|
26
|
-
return this.schema.safeParse(data);
|
|
27
|
-
}
|
|
28
|
-
validate(data) {
|
|
29
|
-
try {
|
|
30
|
-
return this.schema.parse(data);
|
|
31
|
-
}
|
|
32
|
-
catch (error) {
|
|
33
|
-
throw new ServerError(formatErrorMessage(error));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
formValidate(data, message) {
|
|
37
|
-
const formParsed = formParse([data, this.schema]);
|
|
38
|
-
if (!formParsed.success) {
|
|
39
|
-
httpDebug("UnprocessableEntity", formParsed);
|
|
40
|
-
const firstErrorKey = Object.keys(formParsed.fieldErrors)[0];
|
|
41
|
-
throw new UnprocessableEntity({
|
|
42
|
-
fields: formParsed.fields,
|
|
43
|
-
fieldErrors: formParsed.fieldErrors,
|
|
44
|
-
data: { scrollTo: firstErrorKey },
|
|
45
|
-
message,
|
|
46
|
-
}, false);
|
|
47
|
-
}
|
|
48
|
-
return formParsed.data;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
export { SchemaValidator };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
type ApiSuccessResponse<T = any> = {
|
|
2
|
-
success: true;
|
|
3
|
-
status: number;
|
|
4
|
-
message: string;
|
|
5
|
-
response: T;
|
|
6
|
-
cause: null;
|
|
7
|
-
};
|
|
8
|
-
type ApiFailedResponse = {
|
|
9
|
-
success: false;
|
|
10
|
-
status: number;
|
|
11
|
-
message: string;
|
|
12
|
-
response: any;
|
|
13
|
-
cause: string | Error | null;
|
|
14
|
-
};
|
|
15
|
-
type ApiResponseDTO<T = any> = ApiSuccessResponse<T> | ApiFailedResponse;
|
|
16
|
-
export type { ApiResponseDTO };
|
|
17
|
-
//# sourceMappingURL=ApiResponseDTO.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ApiResponseDTO.d.ts","sourceRoot":"","sources":["../../src/types/ApiResponseDTO.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,CAAC,CAAC,GAAG,GAAG,IAAI;IACjC,OAAO,EAAE,IAAI,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,CAAC,CAAC;IACZ,KAAK,EAAE,IAAI,CAAC;CACb,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF,KAAK,cAAc,CAAC,CAAC,GAAG,GAAG,IAAI,kBAAkB,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC;AAEzE,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/api/deleteRequest.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ApiResponseDTO } from "../types/ApiResponseDTO";
|
|
2
|
-
import { makeRequest } from "./makeRequest";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Sends a DELETE request to the specified URL with optional headers and body.
|
|
6
|
-
*
|
|
7
|
-
* @template T - The expected type of the response data.
|
|
8
|
-
* @param {string} url - The URL to send the DELETE request to.
|
|
9
|
-
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
-
* @param {any} [body] - Optional body to include in the request.
|
|
11
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
async function deleteRequest<T = any>(
|
|
15
|
-
url: string,
|
|
16
|
-
headers: HeadersInit = {},
|
|
17
|
-
body?: any
|
|
18
|
-
): Promise<ApiResponseDTO<T>> {
|
|
19
|
-
return makeRequest("DELETE", url, headers, body);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export { deleteRequest };
|
package/src/api/getRequest.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ApiResponseDTO } from "../types/ApiResponseDTO";
|
|
2
|
-
import { makeRequest } from "./makeRequest";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Sends a GET request to the specified URL with optional headers.
|
|
6
|
-
*
|
|
7
|
-
* @template T - The expected type of the response data.
|
|
8
|
-
* @param {string} url - The URL to send the GET request to.
|
|
9
|
-
* @param {HeadersInit} [headers={}] - Optional headers to include in the request.
|
|
10
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
async function getRequest<T = any>(
|
|
14
|
-
url: string,
|
|
15
|
-
headers: HeadersInit = {}
|
|
16
|
-
): Promise<ApiResponseDTO<T>> {
|
|
17
|
-
return makeRequest("GET", url, headers);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { getRequest };
|