@arkyn/server 3.0.1-beta.12 → 3.0.1-beta.121
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/bundle.js +1518 -0
- package/dist/bundle.umd.cjs +5 -0
- package/dist/{api/arkynLogRequest.d.ts → http/api/_logRequest.d.ts} +22 -19
- package/dist/{api/arkynLogRequest.d.ts.map → http/api/_logRequest.d.ts.map} +1 -1
- package/dist/http/api/_logRequest.js +88 -0
- package/dist/http/api/_makeRequest.d.ts +61 -0
- package/dist/http/api/_makeRequest.d.ts.map +1 -0
- package/dist/{api/makeRequest.js → http/api/_makeRequest.js} +33 -20
- package/dist/http/api/deleteRequest.d.ts +17 -0
- package/dist/http/api/deleteRequest.d.ts.map +1 -0
- package/dist/http/api/deleteRequest.js +18 -0
- package/dist/http/api/getRequest.d.ts +16 -0
- package/dist/http/api/getRequest.d.ts.map +1 -0
- package/dist/http/api/getRequest.js +17 -0
- package/dist/http/api/patchRequest.d.ts +17 -0
- package/dist/http/api/patchRequest.d.ts.map +1 -0
- package/dist/http/api/patchRequest.js +18 -0
- package/dist/http/api/postRequest.d.ts +17 -0
- package/dist/http/api/postRequest.d.ts.map +1 -0
- package/dist/http/api/postRequest.js +18 -0
- package/dist/http/api/putRequest.d.ts +17 -0
- package/dist/http/api/putRequest.d.ts.map +1 -0
- package/dist/http/api/putRequest.js +18 -0
- package/dist/http/badResponses/_badResponse.d.ts +22 -0
- package/dist/http/badResponses/_badResponse.d.ts.map +1 -0
- package/dist/http/badResponses/_badResponse.js +51 -0
- package/dist/http/badResponses/badGateway.d.ts +6 -9
- package/dist/http/badResponses/badGateway.d.ts.map +1 -1
- package/dist/http/badResponses/badGateway.js +12 -14
- package/dist/http/badResponses/badRequest.d.ts +6 -9
- package/dist/http/badResponses/badRequest.d.ts.map +1 -1
- package/dist/http/badResponses/badRequest.js +12 -14
- package/dist/http/badResponses/conflict.d.ts +6 -9
- package/dist/http/badResponses/conflict.d.ts.map +1 -1
- package/dist/http/badResponses/conflict.js +12 -14
- package/dist/http/badResponses/forbidden.d.ts +6 -9
- package/dist/http/badResponses/forbidden.d.ts.map +1 -1
- package/dist/http/badResponses/forbidden.js +12 -14
- package/dist/http/badResponses/notFound.d.ts +6 -9
- package/dist/http/badResponses/notFound.d.ts.map +1 -1
- package/dist/http/badResponses/notFound.js +12 -14
- package/dist/http/badResponses/notImplemented.d.ts +6 -9
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
- package/dist/http/badResponses/notImplemented.js +12 -14
- package/dist/http/badResponses/serverError.d.ts +6 -9
- package/dist/http/badResponses/serverError.d.ts.map +1 -1
- package/dist/http/badResponses/serverError.js +12 -14
- package/dist/http/badResponses/unauthorized.d.ts +4 -7
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
- package/dist/http/badResponses/unauthorized.js +10 -12
- package/dist/http/badResponses/unprocessableEntity.d.ts +15 -19
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
- package/dist/http/badResponses/unprocessableEntity.js +18 -21
- package/dist/http/successResponses/_successResponse.d.ts +33 -0
- package/dist/http/successResponses/_successResponse.d.ts.map +1 -0
- package/dist/http/successResponses/_successResponse.js +66 -0
- package/dist/http/successResponses/created.d.ts +9 -19
- package/dist/http/successResponses/created.d.ts.map +1 -1
- package/dist/http/successResponses/created.js +16 -25
- package/dist/http/successResponses/found.d.ts +10 -23
- package/dist/http/successResponses/found.d.ts.map +1 -1
- package/dist/http/successResponses/found.js +17 -29
- package/dist/http/successResponses/noContent.d.ts +6 -13
- package/dist/http/successResponses/noContent.d.ts.map +1 -1
- package/dist/http/successResponses/noContent.js +12 -17
- package/dist/http/successResponses/success.d.ts +9 -19
- package/dist/http/successResponses/success.d.ts.map +1 -1
- package/dist/http/successResponses/success.js +16 -25
- package/dist/http/successResponses/updated.d.ts +9 -19
- package/dist/http/successResponses/updated.d.ts.map +1 -1
- package/dist/http/successResponses/updated.js +16 -25
- package/dist/index.d.ts +19 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -13
- package/dist/services/apiService.d.ts +63 -0
- package/dist/services/apiService.d.ts.map +1 -0
- package/dist/services/apiService.js +158 -0
- package/dist/services/debugService.d.ts +69 -0
- package/dist/services/debugService.d.ts.map +1 -0
- package/dist/services/debugService.js +114 -0
- package/dist/services/logMapperService.d.ts +86 -0
- package/dist/services/logMapperService.d.ts.map +1 -0
- package/dist/services/logMapperService.js +68 -0
- package/dist/services/logService.d.ts +38 -0
- package/dist/services/logService.d.ts.map +1 -0
- package/dist/services/logService.js +40 -0
- package/dist/{services → utilities}/decodeRequestBody.d.ts +3 -4
- package/dist/utilities/decodeRequestBody.d.ts.map +1 -0
- package/dist/{services → utilities}/decodeRequestBody.js +5 -5
- package/dist/{services/decodeErrorMessageFromRequest.d.ts → utilities/decodeRequestErrorMessage.d.ts} +6 -6
- package/dist/utilities/decodeRequestErrorMessage.d.ts.map +1 -0
- package/dist/{services/decodeErrorMessageFromRequest.js → utilities/decodeRequestErrorMessage.js} +5 -5
- package/dist/utilities/errorHandler.d.ts +50 -0
- package/dist/utilities/errorHandler.d.ts.map +1 -0
- package/dist/{services → utilities}/errorHandler.js +34 -28
- package/dist/utilities/flushDebugLogs.d.ts +46 -0
- package/dist/utilities/flushDebugLogs.d.ts.map +1 -0
- package/dist/utilities/flushDebugLogs.js +59 -0
- package/dist/utilities/formAsyncParse.d.ts +59 -0
- package/dist/utilities/formAsyncParse.d.ts.map +1 -0
- package/dist/utilities/formAsyncParse.js +58 -0
- package/dist/{services → utilities}/formParse.d.ts +6 -6
- package/dist/utilities/formParse.d.ts.map +1 -0
- package/dist/{services → utilities}/formParse.js +10 -8
- package/dist/{services → utilities}/getScopedParams.d.ts +3 -4
- package/dist/utilities/getScopedParams.d.ts.map +1 -0
- package/dist/{services → utilities}/getScopedParams.js +4 -4
- package/dist/utilities/schemaValidator.d.ts +146 -0
- package/dist/utilities/schemaValidator.d.ts.map +1 -0
- package/dist/utilities/schemaValidator.js +191 -0
- package/dist/validations/validateCep.d.ts +19 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +27 -0
- package/dist/validations/validateCnpj.d.ts +21 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +59 -0
- package/dist/validations/validateCpf.d.ts +23 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +61 -0
- package/dist/validations/validateDate.d.ts +27 -0
- package/dist/validations/validateDate.d.ts.map +1 -0
- package/dist/validations/validateDate.js +55 -0
- package/dist/validations/validateEmail.d.ts +21 -0
- package/dist/validations/validateEmail.d.ts.map +1 -0
- package/dist/validations/validateEmail.js +111 -0
- package/dist/validations/validatePassword.d.ts +20 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +28 -0
- package/dist/validations/validatePhone.d.ts.map +1 -0
- package/dist/validations/validatePhone.js +44 -0
- package/dist/validations/validateRg.d.ts +21 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +34 -18
- package/dist/api/arkynLogRequest.js +0 -82
- 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/makeRequest.d.ts +0 -38
- package/dist/api/makeRequest.d.ts.map +0 -1
- 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/arkynLogInstance.d.ts +0 -44
- package/dist/config/arkynLogInstance.d.ts.map +0 -1
- package/dist/config/arkynLogInstance.js +0 -49
- package/dist/mapper/arkynLogRequestMapper.d.ts +0 -30
- package/dist/mapper/arkynLogRequestMapper.d.ts.map +0 -1
- package/dist/mapper/arkynLogRequestMapper.js +0 -44
- package/dist/services/decodeErrorMessageFromRequest.d.ts.map +0 -1
- package/dist/services/decodeRequestBody.d.ts.map +0 -1
- package/dist/services/errorHandler.d.ts +0 -44
- package/dist/services/errorHandler.d.ts.map +0 -1
- package/dist/services/formParse.d.ts.map +0 -1
- package/dist/services/getCaller.d.ts +0 -17
- package/dist/services/getCaller.d.ts.map +0 -1
- package/dist/services/getCaller.js +0 -65
- package/dist/services/getScopedParams.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/dist/services/measureRouteExecution.d.ts +0 -3
- package/dist/services/measureRouteExecution.d.ts.map +0 -1
- package/dist/services/measureRouteExecution.js +0 -24
- 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/arkynLogRequest.ts +0 -118
- package/src/api/deleteRequest.ts +0 -22
- package/src/api/getRequest.ts +0 -20
- package/src/api/makeRequest.ts +0 -118
- 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 -70
- 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/formParse.ts +0 -83
- package/src/services/getCaller.ts +0 -82
- package/src/services/getScopedParams.ts +0 -43
- package/src/services/httpDebug.ts +0 -61
- package/src/services/measureRouteExecution.ts +0 -31
- 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/dist/{services/decodeErrorMessageFromRequest.js → utilities/decodeRequestErrorMessage.js}
RENAMED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* `data` or `response` objects by checking various properties in a specific order.
|
|
6
6
|
* If no valid error message is found, it returns a default message: "Missing error message".
|
|
7
7
|
*
|
|
8
|
-
* @param data - The data object that may contain error information. It can have properties
|
|
8
|
+
* @param {any} data - The data object that may contain error information. It can have properties
|
|
9
9
|
* such as `message`, `error`, or `error.message` that are checked for a string value.
|
|
10
|
-
* @param response - The response object that may contain a `statusText` property
|
|
10
|
+
* @param {Response} response - The response object that may contain a `statusText` property
|
|
11
11
|
* representing the HTTP status text.
|
|
12
|
-
* @returns A string representing the decoded error message, or a default message
|
|
12
|
+
* @returns {string} A string representing the decoded error message, or a default message
|
|
13
13
|
* if no error message is found.
|
|
14
14
|
*/
|
|
15
|
-
function
|
|
15
|
+
function decodeRequestErrorMessage(data, response) {
|
|
16
16
|
if (data?.message && typeof data?.message === "string") {
|
|
17
17
|
return data?.message;
|
|
18
18
|
}
|
|
@@ -27,4 +27,4 @@ function decodeErrorMessageFromRequest(data, response) {
|
|
|
27
27
|
}
|
|
28
28
|
return "Missing error message";
|
|
29
29
|
}
|
|
30
|
-
export {
|
|
30
|
+
export { decodeRequestErrorMessage };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles errors and success responses, converting them into standard HTTP Response objects.
|
|
3
|
+
*
|
|
4
|
+
* This function acts as a centralized error and response handler that processes various
|
|
5
|
+
* response types (both success and error responses) and converts them into HTTP Response
|
|
6
|
+
* objects. If the error is not recognized, it defaults to returning a ServerError response.
|
|
7
|
+
*
|
|
8
|
+
* @param {any} error - The error or response object to be handled. Can be:
|
|
9
|
+
* - A native Response object
|
|
10
|
+
* - A success response instance (Found, Created, Updated, Success, NoContent)
|
|
11
|
+
* - An error response instance (BadGateway, BadRequest, Conflict, Forbidden, NotFound, NotImplemented, ServerError, Unauthorized, UnprocessableEntity)
|
|
12
|
+
* - Any other error object (will be wrapped in ServerError)
|
|
13
|
+
*
|
|
14
|
+
* @returns {Response} A standard HTTP Response object with appropriate status code, headers, and body
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Handling a success response
|
|
19
|
+
* try {
|
|
20
|
+
* const result = await someOperation();
|
|
21
|
+
* throw new Success("Operation successful", result);
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* return errorHandler(error);
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Handling an error response
|
|
30
|
+
* try {
|
|
31
|
+
* const user = await findUser(id);
|
|
32
|
+
* if (!user) throw new NotFound("User not found");
|
|
33
|
+
* } catch (error) {
|
|
34
|
+
* return errorHandler(error);
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Handling unexpected errors
|
|
41
|
+
* try {
|
|
42
|
+
* await riskyOperation();
|
|
43
|
+
* } catch (error) {
|
|
44
|
+
* return errorHandler(error); // Returns ServerError response
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function errorHandler(error: any): Response;
|
|
49
|
+
export { errorHandler };
|
|
50
|
+
//# sourceMappingURL=errorHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../src/utilities/errorHandler.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,iBAAS,YAAY,CAAC,KAAK,EAAE,GAAG,GAAG,QAAQ,CAsC1C;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -13,45 +13,51 @@ import { NoContent } from "../http/successResponses/noContent";
|
|
|
13
13
|
import { Success } from "../http/successResponses/success";
|
|
14
14
|
import { Updated } from "../http/successResponses/updated";
|
|
15
15
|
/**
|
|
16
|
-
* Handles errors and
|
|
16
|
+
* Handles errors and success responses, converting them into standard HTTP Response objects.
|
|
17
17
|
*
|
|
18
|
-
* This function
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* `toResponse` method when applicable.
|
|
18
|
+
* This function acts as a centralized error and response handler that processes various
|
|
19
|
+
* response types (both success and error responses) and converts them into HTTP Response
|
|
20
|
+
* objects. If the error is not recognized, it defaults to returning a ServerError response.
|
|
22
21
|
*
|
|
23
|
-
* @param error - The error object to
|
|
24
|
-
*
|
|
22
|
+
* @param {any} error - The error or response object to be handled. Can be:
|
|
23
|
+
* - A native Response object
|
|
24
|
+
* - A success response instance (Found, Created, Updated, Success, NoContent)
|
|
25
|
+
* - An error response instance (BadGateway, BadRequest, Conflict, Forbidden, NotFound, NotImplemented, ServerError, Unauthorized, UnprocessableEntity)
|
|
26
|
+
* - Any other error object (will be wrapped in ServerError)
|
|
25
27
|
*
|
|
26
|
-
* @returns
|
|
27
|
-
* type, or `undefined` if no match is found.
|
|
28
|
+
* @returns {Response} A standard HTTP Response object with appropriate status code, headers, and body
|
|
28
29
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* - `Conflict`
|
|
40
|
-
* - `Forbidden`
|
|
41
|
-
* - `NotFound`
|
|
42
|
-
* - `NotImplemented`
|
|
43
|
-
* - `ServerError`
|
|
44
|
-
* - `Unauthorized`
|
|
45
|
-
* - `UnprocessableEntity`
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Handling a success response
|
|
33
|
+
* try {
|
|
34
|
+
* const result = await someOperation();
|
|
35
|
+
* throw new Success("Operation successful", result);
|
|
36
|
+
* } catch (error) {
|
|
37
|
+
* return errorHandler(error);
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
46
40
|
*
|
|
47
|
-
*
|
|
41
|
+
* @example
|
|
48
42
|
* ```typescript
|
|
43
|
+
* // Handling an error response
|
|
49
44
|
* try {
|
|
50
|
-
*
|
|
45
|
+
* const user = await findUser(id);
|
|
46
|
+
* if (!user) throw new NotFound("User not found");
|
|
51
47
|
* } catch (error) {
|
|
52
48
|
* return errorHandler(error);
|
|
53
49
|
* }
|
|
54
50
|
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // Handling unexpected errors
|
|
55
|
+
* try {
|
|
56
|
+
* await riskyOperation();
|
|
57
|
+
* } catch (error) {
|
|
58
|
+
* return errorHandler(error); // Returns ServerError response
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
55
61
|
*/
|
|
56
62
|
function errorHandler(error) {
|
|
57
63
|
switch (true) {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flushes debug logs to the console with colored output in development mode.
|
|
3
|
+
*
|
|
4
|
+
* This function only outputs logs when running in development mode or when
|
|
5
|
+
* the DEBUG_MODE environment variable is set to "true".
|
|
6
|
+
* Each log entry is prefixed with a colored name tag based on the specified color scheme.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} props - Configuration object for debug logging.
|
|
9
|
+
* @param {string} props.name - The name/label to display before each log entry (e.g., "API", "Database").
|
|
10
|
+
* @param {"yellow" | "cyan" | "red" | "green"} props.scheme - The color scheme for the name tag:
|
|
11
|
+
* - "yellow": Warning-level logs
|
|
12
|
+
* - "cyan": Info-level logs
|
|
13
|
+
* - "red": Error-level logs
|
|
14
|
+
* - "green": Success-level logs
|
|
15
|
+
* @param {string[]} props.debugs - Array of debug messages to be logged.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Log API request information
|
|
22
|
+
* flushDebugLogs({
|
|
23
|
+
* name: "API",
|
|
24
|
+
* scheme: "cyan",
|
|
25
|
+
* debugs: ["POST /api/users", "Status: 201", "Response time: 45ms"]
|
|
26
|
+
* });
|
|
27
|
+
* // Output:
|
|
28
|
+
* // [API] POST /api/users
|
|
29
|
+
* // [API] Status: 201
|
|
30
|
+
* // [API] Response time: 45ms
|
|
31
|
+
*
|
|
32
|
+
* // Log error messages
|
|
33
|
+
* flushDebugLogs({
|
|
34
|
+
* name: "ERROR",
|
|
35
|
+
* scheme: "red",
|
|
36
|
+
* debugs: ["Database connection failed", "Retrying in 5s..."]
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function flushDebugLogs(props: {
|
|
41
|
+
scheme: "yellow" | "cyan" | "red" | "green";
|
|
42
|
+
name: string;
|
|
43
|
+
debugs: string[];
|
|
44
|
+
}): void;
|
|
45
|
+
export { flushDebugLogs };
|
|
46
|
+
//# sourceMappingURL=flushDebugLogs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flushDebugLogs.d.ts","sourceRoot":"","sources":["../../src/utilities/flushDebugLogs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,iBAAS,cAAc,CAAC,KAAK,EAAE;IAC7B,MAAM,EAAE,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,CAAC;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,GAAG,IAAI,CAwBP;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flushes debug logs to the console with colored output in development mode.
|
|
3
|
+
*
|
|
4
|
+
* This function only outputs logs when running in development mode or when
|
|
5
|
+
* the DEBUG_MODE environment variable is set to "true".
|
|
6
|
+
* Each log entry is prefixed with a colored name tag based on the specified color scheme.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} props - Configuration object for debug logging.
|
|
9
|
+
* @param {string} props.name - The name/label to display before each log entry (e.g., "API", "Database").
|
|
10
|
+
* @param {"yellow" | "cyan" | "red" | "green"} props.scheme - The color scheme for the name tag:
|
|
11
|
+
* - "yellow": Warning-level logs
|
|
12
|
+
* - "cyan": Info-level logs
|
|
13
|
+
* - "red": Error-level logs
|
|
14
|
+
* - "green": Success-level logs
|
|
15
|
+
* @param {string[]} props.debugs - Array of debug messages to be logged.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Log API request information
|
|
22
|
+
* flushDebugLogs({
|
|
23
|
+
* name: "API",
|
|
24
|
+
* scheme: "cyan",
|
|
25
|
+
* debugs: ["POST /api/users", "Status: 201", "Response time: 45ms"]
|
|
26
|
+
* });
|
|
27
|
+
* // Output:
|
|
28
|
+
* // [API] POST /api/users
|
|
29
|
+
* // [API] Status: 201
|
|
30
|
+
* // [API] Response time: 45ms
|
|
31
|
+
*
|
|
32
|
+
* // Log error messages
|
|
33
|
+
* flushDebugLogs({
|
|
34
|
+
* name: "ERROR",
|
|
35
|
+
* scheme: "red",
|
|
36
|
+
* debugs: ["Database connection failed", "Retrying in 5s..."]
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function flushDebugLogs(props) {
|
|
41
|
+
const isDebugMode = process.env.NODE_ENV === "development" ||
|
|
42
|
+
process.env?.DEBUG_MODE === "true";
|
|
43
|
+
if (isDebugMode) {
|
|
44
|
+
const reset = "\x1b[0m";
|
|
45
|
+
const colors = {
|
|
46
|
+
yellow: "\x1b[33m",
|
|
47
|
+
cyan: "\x1b[36m",
|
|
48
|
+
red: "\x1b[31m",
|
|
49
|
+
green: "\x1b[32m",
|
|
50
|
+
};
|
|
51
|
+
const debugName = `${colors[props.scheme]}[${props.name}]${reset}`;
|
|
52
|
+
let consoleData = `\n`;
|
|
53
|
+
props.debugs.forEach((debug) => {
|
|
54
|
+
consoleData += `${debugName} ${debug.trim()}\n`;
|
|
55
|
+
});
|
|
56
|
+
console.log(consoleData);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export { flushDebugLogs };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ZodType } from "zod";
|
|
2
|
+
type SuccessResponse<T extends FormParseProps> = {
|
|
3
|
+
success: true;
|
|
4
|
+
data: T[1] extends ZodType<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: ZodType];
|
|
18
|
+
type FormParseReturnType<T extends FormParseProps> = SuccessResponse<T> | ErrorResponse;
|
|
19
|
+
/**
|
|
20
|
+
* Asynchronously parses form data using a Zod schema and returns the result.
|
|
21
|
+
*
|
|
22
|
+
* @template T - A type that extends `FormParseProps`.
|
|
23
|
+
*
|
|
24
|
+
* @param {T} props - An array containing the form data and the Zod schema.
|
|
25
|
+
* @param {T[0]} formData[0] - The form data to be validated.
|
|
26
|
+
* @param {T[1]} schema[1] - The Zod schema used for validation.
|
|
27
|
+
*
|
|
28
|
+
* @returns {Promise<FormParseReturnType<T>>} A promise that resolves to 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 = await formAsyncParse([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 formAsyncParse<T extends FormParseProps>([formData, schema,]: T): Promise<FormParseReturnType<T>>;
|
|
58
|
+
export { formAsyncParse };
|
|
59
|
+
//# sourceMappingURL=formAsyncParse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formAsyncParse.d.ts","sourceRoot":"","sources":["../../src/utilities/formAsyncParse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,KAAK,eAAe,CAAC,CAAC,SAAS,cAAc,IAAI;IAC/C,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;CACjD,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,OAAO,CAAC,CAAC;AAExE,KAAK,mBAAmB,CAAC,CAAC,SAAS,cAAc,IAC7C,eAAe,CAAC,CAAC,CAAC,GAClB,aAAa,CAAC;AAElB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iBAAe,cAAc,CAAC,CAAC,SAAS,cAAc,EAAE,CACtD,QAAQ,EACR,MAAM,EACP,EAAE,CAAC,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAqBrC;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asynchronously parses form data using a Zod schema and returns the result.
|
|
3
|
+
*
|
|
4
|
+
* @template T - A type that extends `FormParseProps`.
|
|
5
|
+
*
|
|
6
|
+
* @param {T} props - An array containing the form data and the Zod schema.
|
|
7
|
+
* @param {T[0]} formData[0] - The form data to be validated.
|
|
8
|
+
* @param {T[1]} schema[1] - The Zod schema used for validation.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Promise<FormParseReturnType<T>>} A promise that resolves to 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 = await formAsyncParse([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
|
+
async function formAsyncParse([formData, schema,]) {
|
|
40
|
+
const zodResponse = await schema.safeParseAsync(formData);
|
|
41
|
+
if (zodResponse.success === false) {
|
|
42
|
+
const errorsObject = Object.fromEntries(zodResponse.error.issues.map((item) => {
|
|
43
|
+
return [item.path.join("."), item.message];
|
|
44
|
+
}));
|
|
45
|
+
return {
|
|
46
|
+
success: zodResponse.success,
|
|
47
|
+
fieldErrors: errorsObject,
|
|
48
|
+
fields: formData,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return {
|
|
53
|
+
success: zodResponse.success,
|
|
54
|
+
data: zodResponse.data,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export { formAsyncParse };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ZodType } from "zod";
|
|
2
2
|
type SuccessResponse<T extends FormParseProps> = {
|
|
3
3
|
success: true;
|
|
4
|
-
data: T[1] extends
|
|
4
|
+
data: T[1] extends ZodType<infer U> ? U : never;
|
|
5
5
|
};
|
|
6
6
|
type ErrorResponse = {
|
|
7
7
|
success: false;
|
|
@@ -14,16 +14,16 @@ type ErrorResponse = {
|
|
|
14
14
|
};
|
|
15
15
|
type FormParseProps = [formData: {
|
|
16
16
|
[k: string]: any;
|
|
17
|
-
}, schema:
|
|
17
|
+
}, schema: ZodType];
|
|
18
18
|
type FormParseReturnType<T extends FormParseProps> = SuccessResponse<T> | ErrorResponse;
|
|
19
19
|
/**
|
|
20
20
|
* Parses form data using a Zod schema and returns the result.
|
|
21
21
|
*
|
|
22
22
|
* @template T - A type that extends `FormParseProps`.
|
|
23
23
|
*
|
|
24
|
-
* @param {T}
|
|
25
|
-
* @param {T[0]}
|
|
26
|
-
* @param {T[1]}
|
|
24
|
+
* @param {T} props - An array containing the form data and the Zod schema.
|
|
25
|
+
* @param {T[0]} formData[0] - The form data to be validated.
|
|
26
|
+
* @param {T[1]} schema[1] - The Zod schema used for validation.
|
|
27
27
|
*
|
|
28
28
|
* @returns {FormParseReturnType<T>} An object containing the validation result.
|
|
29
29
|
* - If validation fails, it includes:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formParse.d.ts","sourceRoot":"","sources":["../../src/utilities/formParse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,KAAK,eAAe,CAAC,CAAC,SAAS,cAAc,IAAI;IAC/C,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;CACjD,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,OAAO,CAAC,CAAC;AAExE,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,CAqB5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @template T - A type that extends `FormParseProps`.
|
|
5
5
|
*
|
|
6
|
-
* @param {T}
|
|
7
|
-
* @param {T[0]}
|
|
8
|
-
* @param {T[1]}
|
|
6
|
+
* @param {T} props - An array containing the form data and the Zod schema.
|
|
7
|
+
* @param {T[0]} formData[0] - The form data to be validated.
|
|
8
|
+
* @param {T[1]} schema[1] - The Zod schema used for validation.
|
|
9
9
|
*
|
|
10
10
|
* @returns {FormParseReturnType<T>} An object containing the validation result.
|
|
11
11
|
* - If validation fails, it includes:
|
|
@@ -39,10 +39,9 @@
|
|
|
39
39
|
function formParse([formData, schema,]) {
|
|
40
40
|
const zodResponse = schema.safeParse(formData);
|
|
41
41
|
if (zodResponse.success === false) {
|
|
42
|
-
const errorsObject = Object.fromEntries(zodResponse.error.
|
|
43
|
-
item.path.join("."),
|
|
44
|
-
|
|
45
|
-
]));
|
|
42
|
+
const errorsObject = Object.fromEntries(zodResponse.error.issues.map((item) => {
|
|
43
|
+
return [item.path.join("."), item.message];
|
|
44
|
+
}));
|
|
46
45
|
return {
|
|
47
46
|
success: zodResponse.success,
|
|
48
47
|
fieldErrors: errorsObject,
|
|
@@ -50,7 +49,10 @@ function formParse([formData, schema,]) {
|
|
|
50
49
|
};
|
|
51
50
|
}
|
|
52
51
|
else {
|
|
53
|
-
return {
|
|
52
|
+
return {
|
|
53
|
+
success: zodResponse.success,
|
|
54
|
+
data: zodResponse.data,
|
|
55
|
+
};
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
export { formParse };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
type GetScopedParamsFunction = (request: Request, scope?: string) => URLSearchParams;
|
|
2
1
|
/**
|
|
3
2
|
* Extracts and returns scoped query parameters from a request URL.
|
|
4
3
|
*
|
|
5
|
-
* @param request - The incoming request object containing the URL.
|
|
6
|
-
* @param scope - An optional string representing the scope prefix for filtering query parameters.
|
|
4
|
+
* @param {Request} request - The incoming request object containing the URL.
|
|
5
|
+
* @param {string} scope - An optional string representing the scope prefix for filtering query parameters.
|
|
7
6
|
* If no scope is provided, all query parameters are returned.
|
|
8
7
|
*
|
|
9
8
|
* @returns A `URLSearchParams` object containing the scoped query parameters.
|
|
@@ -23,6 +22,6 @@ type GetScopedParamsFunction = (request: Request, scope?: string) => URLSearchPa
|
|
|
23
22
|
* const params = getScopedParams(request, "scope");
|
|
24
23
|
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
25
24
|
*/
|
|
26
|
-
declare
|
|
25
|
+
declare function getScopedParams(request: Request, scope?: string): URLSearchParams;
|
|
27
26
|
export { getScopedParams };
|
|
28
27
|
//# sourceMappingURL=getScopedParams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getScopedParams.d.ts","sourceRoot":"","sources":["../../src/utilities/getScopedParams.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,iBAAS,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,GAAE,MAAW,mBAW5D;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Extracts and returns scoped query parameters from a request URL.
|
|
3
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.
|
|
4
|
+
* @param {Request} request - The incoming request object containing the URL.
|
|
5
|
+
* @param {string} scope - An optional string representing the scope prefix for filtering query parameters.
|
|
6
6
|
* If no scope is provided, all query parameters are returned.
|
|
7
7
|
*
|
|
8
8
|
* @returns A `URLSearchParams` object containing the scoped query parameters.
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* const params = getScopedParams(request, "scope");
|
|
23
23
|
* console.log(params.toString()); // Output: "key1=value1&key2=value2"
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
function getScopedParams(request, scope = "") {
|
|
26
26
|
const url = new URL(request.url);
|
|
27
27
|
if (scope === "")
|
|
28
28
|
return url.searchParams;
|
|
@@ -30,5 +30,5 @@ const getScopedParams = (request, scope = "") => {
|
|
|
30
30
|
.filter(([key]) => key.startsWith(`${scope}:`))
|
|
31
31
|
.map(([key, value]) => [key.replace(`${scope}:`, ""), value]);
|
|
32
32
|
return new URLSearchParams(scopedSearchParams);
|
|
33
|
-
}
|
|
33
|
+
}
|
|
34
34
|
export { getScopedParams };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* A schema validator class that provides multiple validation methods for Zod schemas.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { z } from "zod";
|
|
8
|
+
*
|
|
9
|
+
* const userSchema = z.object({
|
|
10
|
+
* name: z.string().min(1, "Name is required"),
|
|
11
|
+
* email: z.string().email("Invalid email"),
|
|
12
|
+
* age: z.number().min(18, "Must be at least 18")
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* const validator = new SchemaValidator(userSchema);
|
|
16
|
+
*
|
|
17
|
+
* // Check if data is valid without throwing
|
|
18
|
+
* const isValid = validator.isValid({ name: "John", email: "john@example.com", age: 25 });
|
|
19
|
+
*
|
|
20
|
+
* // Validate and throw ServerError on failure
|
|
21
|
+
* try {
|
|
22
|
+
* const validData = validator.validate({ name: "John", email: "john@example.com", age: 25 });
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error(error.message);
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* // Form validation with UnprocessableEntity error
|
|
28
|
+
* try {
|
|
29
|
+
* const formData = validator.formValidate(requestBody);
|
|
30
|
+
* } catch (error) {
|
|
31
|
+
* // Returns structured error with fieldErrors for forms
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare class SchemaValidator<T extends ZodType> {
|
|
36
|
+
readonly schema: T;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new SchemaValidator instance.
|
|
39
|
+
* @param {T} schema - The Zod schema to use for validation.
|
|
40
|
+
*/
|
|
41
|
+
constructor(schema: T);
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the provided data is valid according to the schema without throwing errors.
|
|
44
|
+
*
|
|
45
|
+
* @param {any} data - The data to validate.
|
|
46
|
+
*
|
|
47
|
+
* @returns {boolean} True if the data is valid, false otherwise.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const validator = new SchemaValidator(userSchema);
|
|
52
|
+
* const isValid = validator.isValid({ name: "John", email: "invalid-email" });
|
|
53
|
+
* console.log(isValid); // false
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
isValid(data: any): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Safely validates data and returns the complete parse result without throwing errors.
|
|
59
|
+
*
|
|
60
|
+
* @param {any} data - The data to validate.
|
|
61
|
+
* @returns {z.ZodSafeParseResult<z.infer<T>>} The Zod safe parse result containing success status and data or error.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const validator = new SchemaValidator(userSchema);
|
|
66
|
+
* const result = validator.safeValidate({ name: "", email: "john@example.com" });
|
|
67
|
+
*
|
|
68
|
+
* if (result.success) {
|
|
69
|
+
* console.log(result.data); // Validated data
|
|
70
|
+
* } else {
|
|
71
|
+
* console.log(result.error.issues); // Validation errors
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
safeValidate(data: any): z.ZodSafeParseResult<z.infer<T>>;
|
|
76
|
+
/**
|
|
77
|
+
* Validates data and returns the parsed result, throwing a ServerError on validation failure.
|
|
78
|
+
*
|
|
79
|
+
* @param {any} data - The data to validate.
|
|
80
|
+
* @throws {ServerError} When validation fails, with a formatted error message.
|
|
81
|
+
* @returns {z.infer<T>} The validated and parsed data.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const validator = new SchemaValidator(userSchema);
|
|
86
|
+
*
|
|
87
|
+
* try {
|
|
88
|
+
* const validUser = validator.validate({ name: "John", email: "john@example.com", age: 25 });
|
|
89
|
+
* console.log(validUser); // { name: "John", email: "john@example.com", age: 25 }
|
|
90
|
+
* } catch (error) {
|
|
91
|
+
* console.error(error.message); // "Error validating:\n-> name: String must contain at least 1 character(s)"
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
validate(data: any): z.infer<T>;
|
|
96
|
+
/**
|
|
97
|
+
* Validates form data and returns the parsed result, throwing an UnprocessableEntity error on validation failure.
|
|
98
|
+
* This method is specifically designed for form validation in web applications.
|
|
99
|
+
*
|
|
100
|
+
* @param {any} data - The form data to validate.
|
|
101
|
+
* @param {string} [message] - Optional custom error message.
|
|
102
|
+
* @throws {UnprocessableEntity} When validation fails, with structured field errors for form handling.
|
|
103
|
+
* @returns {z.infer<T>} The validated and parsed form data.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const validator = new SchemaValidator(userSchema);
|
|
108
|
+
*
|
|
109
|
+
* try {
|
|
110
|
+
* const validFormData = validator.formValidate(requestBody, "User data is invalid");
|
|
111
|
+
* console.log(validFormData);
|
|
112
|
+
* } catch (error) {
|
|
113
|
+
* // UnprocessableEntity with fieldErrors, fields, and scrollTo data
|
|
114
|
+
* console.log(error.fieldErrors); // { name: "Name is required", email: "Invalid email" }
|
|
115
|
+
* console.log(error.data.scrollTo); // "name" (first error field)
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
formValidate(data: any, message?: string): z.infer<T>;
|
|
120
|
+
/**
|
|
121
|
+
* Asynchronously validates form data and returns the parsed result, throwing an UnprocessableEntity error on validation failure.
|
|
122
|
+
* This method is the async version of formValidate, designed for form validation with async schemas.
|
|
123
|
+
*
|
|
124
|
+
* @param {any} data - The form data to validate.
|
|
125
|
+
* @param {string} [message] - Optional custom error message.
|
|
126
|
+
* @throws {UnprocessableEntity} When validation fails, with structured field errors for form handling.
|
|
127
|
+
* @returns {Promise<z.infer<T>>} A promise that resolves to the validated and parsed form data.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const validator = new SchemaValidator(userSchemaWithAsyncValidation);
|
|
132
|
+
*
|
|
133
|
+
* try {
|
|
134
|
+
* const validFormData = await validator.formAsyncValidate(requestBody, "User data is invalid");
|
|
135
|
+
* console.log(validFormData);
|
|
136
|
+
* } catch (error) {
|
|
137
|
+
* // UnprocessableEntity with fieldErrors, fields, and scrollTo data
|
|
138
|
+
* console.log(error.fieldErrors); // { name: "Name is required", email: "Invalid email" }
|
|
139
|
+
* console.log(error.data.scrollTo); // "name" (first error field)
|
|
140
|
+
* }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
formAsyncValidate(data: any, message?: string): Promise<z.infer<T>>;
|
|
144
|
+
}
|
|
145
|
+
export { SchemaValidator };
|
|
146
|
+
//# sourceMappingURL=schemaValidator.d.ts.map
|