@arkyn/server 3.0.1-beta.143 → 3.0.1-beta.145

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/dist/bundle.js +90 -323
  2. package/dist/http/badResponses/badGateway.d.ts +12 -19
  3. package/dist/http/badResponses/badGateway.d.ts.map +1 -1
  4. package/dist/http/badResponses/badGateway.js +12 -19
  5. package/dist/http/badResponses/badRequest.d.ts +10 -19
  6. package/dist/http/badResponses/badRequest.d.ts.map +1 -1
  7. package/dist/http/badResponses/badRequest.js +10 -19
  8. package/dist/http/badResponses/conflict.d.ts +10 -19
  9. package/dist/http/badResponses/conflict.d.ts.map +1 -1
  10. package/dist/http/badResponses/conflict.js +10 -19
  11. package/dist/http/badResponses/forbidden.d.ts +10 -19
  12. package/dist/http/badResponses/forbidden.d.ts.map +1 -1
  13. package/dist/http/badResponses/forbidden.js +10 -19
  14. package/dist/http/badResponses/notFound.d.ts +10 -19
  15. package/dist/http/badResponses/notFound.d.ts.map +1 -1
  16. package/dist/http/badResponses/notFound.js +10 -19
  17. package/dist/http/badResponses/notImplemented.d.ts +10 -19
  18. package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
  19. package/dist/http/badResponses/notImplemented.js +10 -19
  20. package/dist/http/badResponses/serverError.d.ts +10 -19
  21. package/dist/http/badResponses/serverError.d.ts.map +1 -1
  22. package/dist/http/badResponses/serverError.js +10 -19
  23. package/dist/http/badResponses/unauthorized.d.ts +10 -19
  24. package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
  25. package/dist/http/badResponses/unauthorized.js +10 -19
  26. package/dist/http/badResponses/unprocessableEntity.d.ts +17 -22
  27. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
  28. package/dist/http/badResponses/unprocessableEntity.js +17 -22
  29. package/dist/http/successResponses/created.d.ts +10 -14
  30. package/dist/http/successResponses/created.d.ts.map +1 -1
  31. package/dist/http/successResponses/created.js +10 -14
  32. package/dist/http/successResponses/found.d.ts +10 -14
  33. package/dist/http/successResponses/found.d.ts.map +1 -1
  34. package/dist/http/successResponses/found.js +10 -14
  35. package/dist/http/successResponses/noContent.d.ts +9 -7
  36. package/dist/http/successResponses/noContent.d.ts.map +1 -1
  37. package/dist/http/successResponses/noContent.js +9 -7
  38. package/dist/http/successResponses/success.d.ts +10 -14
  39. package/dist/http/successResponses/success.d.ts.map +1 -1
  40. package/dist/http/successResponses/success.js +10 -14
  41. package/dist/http/successResponses/updated.d.ts +11 -14
  42. package/dist/http/successResponses/updated.d.ts.map +1 -1
  43. package/dist/http/successResponses/updated.js +11 -14
  44. package/dist/services/apiService.d.ts +20 -0
  45. package/dist/services/apiService.d.ts.map +1 -1
  46. package/dist/services/apiService.js +16 -0
  47. package/dist/services/debugService.d.ts +10 -42
  48. package/dist/services/debugService.d.ts.map +1 -1
  49. package/dist/services/debugService.js +10 -42
  50. package/dist/services/logService.d.ts +5 -10
  51. package/dist/services/logService.d.ts.map +1 -1
  52. package/dist/services/logService.js +5 -10
  53. package/dist/utilities/decodeRequestBody.d.ts +11 -9
  54. package/dist/utilities/decodeRequestBody.d.ts.map +1 -1
  55. package/dist/utilities/decodeRequestBody.js +11 -9
  56. package/dist/utilities/decodeRequestErrorMessage.d.ts +12 -10
  57. package/dist/utilities/decodeRequestErrorMessage.d.ts.map +1 -1
  58. package/dist/utilities/decodeRequestErrorMessage.js +12 -10
  59. package/dist/utilities/errorHandler.d.ts +13 -38
  60. package/dist/utilities/errorHandler.d.ts.map +1 -1
  61. package/dist/utilities/errorHandler.js +13 -38
  62. package/dist/utilities/flushDebugLogs.d.ts +6 -28
  63. package/dist/utilities/flushDebugLogs.d.ts.map +1 -1
  64. package/dist/utilities/flushDebugLogs.js +6 -28
  65. package/dist/utilities/formAsyncParse.d.ts +7 -28
  66. package/dist/utilities/formAsyncParse.d.ts.map +1 -1
  67. package/dist/utilities/formAsyncParse.js +7 -28
  68. package/dist/utilities/formParse.d.ts +7 -28
  69. package/dist/utilities/formParse.d.ts.map +1 -1
  70. package/dist/utilities/formParse.js +7 -28
  71. package/dist/utilities/getScopedParams.d.ts +11 -19
  72. package/dist/utilities/getScopedParams.d.ts.map +1 -1
  73. package/dist/utilities/getScopedParams.js +11 -19
  74. package/dist/utilities/schemaValidator.d.ts +31 -102
  75. package/dist/utilities/schemaValidator.d.ts.map +1 -1
  76. package/dist/utilities/schemaValidator.js +31 -102
  77. package/dist/validations/validateCep.d.ts +2 -2
  78. package/dist/validations/validateCep.js +2 -2
  79. package/dist/validations/validateCnpj.d.ts +2 -2
  80. package/dist/validations/validateCnpj.js +2 -2
  81. package/dist/validations/validateCpf.d.ts +4 -11
  82. package/dist/validations/validateCpf.d.ts.map +1 -1
  83. package/dist/validations/validateCpf.js +4 -11
  84. package/dist/validations/validateDate.d.ts +7 -11
  85. package/dist/validations/validateDate.d.ts.map +1 -1
  86. package/dist/validations/validateDate.js +7 -11
  87. package/dist/validations/validateEmail.d.ts +2 -2
  88. package/dist/validations/validateEmail.js +2 -2
  89. package/dist/validations/validatePassword.d.ts +2 -2
  90. package/dist/validations/validatePassword.js +2 -2
  91. package/dist/validations/validatePhone.d.ts +6 -18
  92. package/dist/validations/validatePhone.d.ts.map +1 -1
  93. package/dist/validations/validatePhone.js +6 -18
  94. package/dist/validations/validateRg.d.ts +2 -2
  95. package/dist/validations/validateRg.js +2 -2
  96. package/package.json +1 -1
@@ -13,49 +13,24 @@ 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 success responses, converting them into standard HTTP Response objects.
16
+ * Converts any thrown value into a `Response`. Recognizes all `@arkyn/server` success and error
17
+ * response classes, native `Response` objects, and falls back to a 500 `ServerError` for anything else.
17
18
  *
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.
19
+ * Intended to be used as the catch handler of a route action or loader:
21
20
  *
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)
27
- *
28
- * @returns {Response} A standard HTTP Response object with appropriate status code, headers, and body
29
- *
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
- * ```
40
- *
41
- * @example
42
- * ```typescript
43
- * // Handling an error response
44
- * try {
45
- * const user = await findUser(id);
46
- * if (!user) throw new NotFound("User not found");
47
- * } catch (error) {
48
- * return errorHandler(error);
49
- * }
50
- * ```
21
+ * @param error - The thrown value to convert.
22
+ * @returns A `Response` with the appropriate HTTP status, headers, and JSON body.
51
23
  *
52
24
  * @example
53
25
  * ```typescript
54
- * // Handling unexpected errors
55
- * try {
56
- * await riskyOperation();
57
- * } catch (error) {
58
- * return errorHandler(error); // Returns ServerError response
26
+ * export async function action({ request }: ActionFunctionArgs) {
27
+ * try {
28
+ * const user = await findUser(id);
29
+ * if (!user) throw new NotFound("User not found");
30
+ * return new Success("User retrieved", { user }).toJson();
31
+ * } catch (error) {
32
+ * return errorHandler(error);
33
+ * }
59
34
  * }
60
35
  * ```
61
36
  */
@@ -1,39 +1,17 @@
1
1
  /**
2
- * Flushes debug logs to the console with colored output in development mode.
2
+ * Writes colored `[name] message` lines to the console, but only when
3
+ * `NODE_ENV === "development"` or `DEBUG_MODE === "true"`. No-op in production.
3
4
  *
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}
5
+ * @param props.name - Label shown before each line (e.g. `"API"`, `"Auth"`).
6
+ * @param props.scheme - Color of the label tag: `"cyan"` info, `"green"` success, `"yellow"` warning, `"red"` error.
7
+ * @param props.debugs - Lines of text to print, one per console entry.
18
8
  *
19
9
  * @example
20
10
  * ```typescript
21
- * // Log API request information
22
11
  * flushDebugLogs({
23
12
  * name: "API",
24
13
  * 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..."]
14
+ * debugs: ["POST /api/users", "Status: 201"],
37
15
  * });
38
16
  * ```
39
17
  */
@@ -1 +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,CAyBP;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"flushDebugLogs.d.ts","sourceRoot":"","sources":["../../src/utilities/flushDebugLogs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;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,CAyBP;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
@@ -1,39 +1,17 @@
1
1
  /**
2
- * Flushes debug logs to the console with colored output in development mode.
2
+ * Writes colored `[name] message` lines to the console, but only when
3
+ * `NODE_ENV === "development"` or `DEBUG_MODE === "true"`. No-op in production.
3
4
  *
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}
5
+ * @param props.name - Label shown before each line (e.g. `"API"`, `"Auth"`).
6
+ * @param props.scheme - Color of the label tag: `"cyan"` info, `"green"` success, `"yellow"` warning, `"red"` error.
7
+ * @param props.debugs - Lines of text to print, one per console entry.
18
8
  *
19
9
  * @example
20
10
  * ```typescript
21
- * // Log API request information
22
11
  * flushDebugLogs({
23
12
  * name: "API",
24
13
  * 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..."]
14
+ * debugs: ["POST /api/users", "Status: 201"],
37
15
  * });
38
16
  * ```
39
17
  */
@@ -17,40 +17,19 @@ type FormParseProps = [formData: {
17
17
  }, schema: ZodType];
18
18
  type FormParseReturnType<T extends FormParseProps> = SuccessResponse<T> | ErrorResponse;
19
19
  /**
20
- * Asynchronously parses form data using a Zod schema and returns the result.
20
+ * Async variant of `formParse` uses `safeParseAsync` to support Zod schemas with async refinements.
21
+ * Returns `{ success: true, data }` on success or `{ success: false, fieldErrors, fields }` on failure.
21
22
  *
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.
23
+ * @param formData - The raw form data object to validate.
24
+ * @param schema - The Zod schema to validate against.
36
25
  *
37
26
  * @example
38
27
  * ```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]);
28
+ * const schema = z.object({ email: z.string().email() });
29
+ * const result = await formAsyncParse([{ email: "bad" }, schema]);
49
30
  *
50
31
  * 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
32
+ * console.log(result.fieldErrors); // { email: "Invalid email" }
54
33
  * }
55
34
  * ```
56
35
  */
@@ -1 +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"}
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;;;;;;;;;;;;;;;;GAgBG;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"}
@@ -1,38 +1,17 @@
1
1
  /**
2
- * Asynchronously parses form data using a Zod schema and returns the result.
2
+ * Async variant of `formParse` uses `safeParseAsync` to support Zod schemas with async refinements.
3
+ * Returns `{ success: true, data }` on success or `{ success: false, fieldErrors, fields }` on failure.
3
4
  *
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.
5
+ * @param formData - The raw form data object to validate.
6
+ * @param schema - The Zod schema to validate against.
18
7
  *
19
8
  * @example
20
9
  * ```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]);
10
+ * const schema = z.object({ email: z.string().email() });
11
+ * const result = await formAsyncParse([{ email: "bad" }, schema]);
31
12
  *
32
13
  * 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
14
+ * console.log(result.fieldErrors); // { email: "Invalid email" }
36
15
  * }
37
16
  * ```
38
17
  */
@@ -17,40 +17,19 @@ type FormParseProps = [formData: {
17
17
  }, schema: ZodType];
18
18
  type FormParseReturnType<T extends FormParseProps> = SuccessResponse<T> | ErrorResponse;
19
19
  /**
20
- * Parses form data using a Zod schema and returns the result.
20
+ * Validates form data against a Zod schema synchronously.
21
+ * Returns `{ success: true, data }` on success or `{ success: false, fieldErrors, fields }` on failure.
21
22
  *
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 {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.
23
+ * @param formData - The raw form data object to validate.
24
+ * @param schema - The Zod schema to validate against.
36
25
  *
37
26
  * @example
38
27
  * ```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]);
28
+ * const schema = z.object({ name: z.string().min(1, "Required"), age: z.number().min(18) });
29
+ * const result = formParse([{ name: "", age: 15 }, schema]);
49
30
  *
50
31
  * 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
32
+ * console.log(result.fieldErrors); // { name: "Required", age: "..." }
54
33
  * }
55
34
  * ```
56
35
  */
@@ -1 +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"}
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;;;;;;;;;;;;;;;;GAgBG;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"}
@@ -1,38 +1,17 @@
1
1
  /**
2
- * Parses form data using a Zod schema and returns the result.
2
+ * Validates form data against a Zod schema synchronously.
3
+ * Returns `{ success: true, data }` on success or `{ success: false, fieldErrors, fields }` on failure.
3
4
  *
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 {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.
5
+ * @param formData - The raw form data object to validate.
6
+ * @param schema - The Zod schema to validate against.
18
7
  *
19
8
  * @example
20
9
  * ```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]);
10
+ * const schema = z.object({ name: z.string().min(1, "Required"), age: z.number().min(18) });
11
+ * const result = formParse([{ name: "", age: 15 }, schema]);
31
12
  *
32
13
  * 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
14
+ * console.log(result.fieldErrors); // { name: "Required", age: "..." }
36
15
  * }
37
16
  * ```
38
17
  */
@@ -1,26 +1,18 @@
1
1
  /**
2
- * Extracts and returns scoped query parameters from a request URL.
2
+ * Extracts URL search parameters from a request, optionally filtered by a namespace prefix
3
+ * (e.g. `scope:key` → `key`). Without a scope, returns all search params as-is.
3
4
  *
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
- * 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"
5
+ * @param request - The incoming request whose URL will be parsed.
6
+ * @param scope - Namespace prefix to filter by (e.g. `"table"` matches `table:page`, `table:sort`).
7
+ * @returns A `URLSearchParams` object with the matching params, stripped of the scope prefix.
18
8
  *
19
9
  * @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"
10
+ * ```typescript
11
+ * // URL: /products?table:page=2&table:sort=asc&other=1
12
+ * const params = getScopedParams(request, "table");
13
+ * params.get("page"); // "2"
14
+ * params.get("sort"); // "asc"
15
+ * ```
24
16
  */
25
17
  declare function getScopedParams(request: Request, scope?: string): URLSearchParams;
26
18
  export { getScopedParams };
@@ -1 +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
+ {"version":3,"file":"getScopedParams.d.ts","sourceRoot":"","sources":["../../src/utilities/getScopedParams.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,iBAAS,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,GAAE,MAAW,mBAW5D;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -1,26 +1,18 @@
1
1
  /**
2
- * Extracts and returns scoped query parameters from a request URL.
2
+ * Extracts URL search parameters from a request, optionally filtered by a namespace prefix
3
+ * (e.g. `scope:key` → `key`). Without a scope, returns all search params as-is.
3
4
  *
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
- * 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"
5
+ * @param request - The incoming request whose URL will be parsed.
6
+ * @param scope - Namespace prefix to filter by (e.g. `"table"` matches `table:page`, `table:sort`).
7
+ * @returns A `URLSearchParams` object with the matching params, stripped of the scope prefix.
18
8
  *
19
9
  * @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"
10
+ * ```typescript
11
+ * // URL: /products?table:page=2&table:sort=asc&other=1
12
+ * const params = getScopedParams(request, "table");
13
+ * params.get("page"); // "2"
14
+ * params.get("sort"); // "asc"
15
+ * ```
24
16
  */
25
17
  function getScopedParams(request, scope = "") {
26
18
  const url = new URL(request.url);
@@ -1,143 +1,72 @@
1
1
  import type { ZodType, z } from "zod";
2
2
  /**
3
- * A schema validator class that provides multiple validation methods for Zod schemas.
3
+ * Wraps a Zod schema with convenience validation methods suited for server-side use:
4
+ * - `isValid` — boolean check, no throws
5
+ * - `safeValidate` — raw Zod result, no throws
6
+ * - `validate` — throws `ServerError` on failure (for trusted/internal data)
7
+ * - `formValidate` / `formAsyncValidate` — throws `UnprocessableEntity` on failure (for user-submitted forms)
4
8
  *
5
9
  * @example
6
10
  * ```typescript
7
- * import { z } from "zod";
11
+ * const validator = new SchemaValidator(z.object({ email: z.string().email() }));
8
12
  *
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
- * }
13
+ * // Inside a Remix action:
14
+ * const body = validator.formValidate(await decodeRequestBody(request));
33
15
  * ```
34
16
  */
35
17
  declare class SchemaValidator<T extends ZodType> {
36
18
  readonly schema: T;
37
19
  /**
38
- * Creates a new SchemaValidator instance.
39
- * @param {T} schema - The Zod schema to use for validation.
20
+ * @param schema - The Zod schema used for all validation methods on this instance.
40
21
  */
41
22
  constructor(schema: T);
42
23
  /**
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.
24
+ * Returns `true` if the data satisfies the schema, `false` otherwise. Never throws.
48
25
  *
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
- * ```
26
+ * @param data - The value to check.
55
27
  */
56
28
  isValid(data: any): boolean;
57
29
  /**
58
- * Safely validates data and returns the complete parse result without throwing errors.
30
+ * Validates data and returns the raw Zod `safeParseResult` without throwing.
31
+ * Useful when you need access to the full error details.
59
32
  *
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
- * ```
33
+ * @param data - The value to validate.
74
34
  */
75
35
  safeValidate(data: any): z.ZodSafeParseResult<z.infer<T>>;
76
36
  /**
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);
37
+ * Validates data and returns the typed result, throwing `ServerError` on failure.
38
+ * Use for validating internal/trusted data (e.g. env vars, config objects).
86
39
  *
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
- * ```
40
+ * @param data - The value to validate.
41
+ * @throws `ServerError` with a formatted field-by-field error message.
94
42
  */
95
43
  validate(data: any): z.infer<T>;
96
44
  /**
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.
45
+ * Validates form data and returns the typed result, throwing `UnprocessableEntity` on failure.
46
+ * The error includes `fieldErrors`, `fields`, and `data.scrollTo` (first failing field name).
99
47
  *
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.
48
+ * @param data - The raw form data to validate.
49
+ * @param message - Optional human-readable error message for the 422 response.
50
+ * @throws `UnprocessableEntity` with structured field errors for client-side form handling.
104
51
  *
105
52
  * @example
106
53
  * ```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
- * }
54
+ * const validator = new SchemaValidator(registerSchema);
55
+ * const body = validator.formValidate(await decodeRequestBody(request));
117
56
  * ```
118
57
  */
119
58
  formValidate(data: any, message?: string): z.infer<T>;
120
59
  /**
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.
60
+ * Async version of `formValidate` for schemas with async refinements (e.g. uniqueness checks).
123
61
  *
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.
62
+ * @param data - The raw form data to validate.
63
+ * @param message - Optional human-readable error message for the 422 response.
64
+ * @throws `UnprocessableEntity` with structured field errors for client-side form handling.
128
65
  *
129
66
  * @example
130
67
  * ```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
- * }
68
+ * const validator = new SchemaValidator(registerSchema);
69
+ * const body = await validator.formAsyncValidate(await decodeRequestBody(request));
141
70
  * ```
142
71
  */
143
72
  formAsyncValidate(data: any, message?: string): Promise<z.infer<T>>;
@@ -1 +1 @@
1
- {"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/utilities/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgBtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,cAAM,eAAe,CAAC,CAAC,SAAS,OAAO;IAKzB,QAAQ,CAAC,MAAM,EAAE,CAAC;IAJ9B;;;OAGG;gBACkB,MAAM,EAAE,CAAC;IAE9B;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO;IAI3B;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAIzD;;;;;;;;;;;;;;;;;;OAkBG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAQ/B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAiBrD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,iBAAiB,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAgB1E;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/utilities/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgBtC;;;;;;;;;;;;;;GAcG;AACH,cAAM,eAAe,CAAC,CAAC,SAAS,OAAO;IAIzB,QAAQ,CAAC,MAAM,EAAE,CAAC;IAH9B;;OAEG;gBACkB,MAAM,EAAE,CAAC;IAE9B;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO;IAI3B;;;;;OAKG;IACH,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAIzD;;;;;;OAMG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAQ/B;;;;;;;;;;;;;OAaG;IACH,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAiBrD;;;;;;;;;;;;OAYG;IACG,iBAAiB,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAgB1E;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}