@firtoz/router-toolkit 5.2.0 → 5.3.0
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/package.json +1 -1
- package/src/formAction.ts +27 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firtoz/router-toolkit",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Type-safe React Router 7 framework mode helpers with enhanced fetching, form submission, and state management",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"module": "./src/index.ts",
|
package/src/formAction.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Type-safe form action utility for React Router 7
|
|
3
3
|
*
|
|
4
|
-
* This module provides a wrapper for React Router actions that handles form data
|
|
5
|
-
* using Zod schemas and provides structured error handling with MaybeError.
|
|
4
|
+
* This module provides a wrapper for React Router actions that handles form data and JSON
|
|
5
|
+
* validation using Zod schemas and provides structured error handling with MaybeError.
|
|
6
|
+
*
|
|
7
|
+
* Supports both:
|
|
8
|
+
* - **JSON requests** (`Content-Type: application/json`) - parsed with `request.json()` and validated directly
|
|
9
|
+
* - **FormData requests** (`multipart/form-data` or `application/x-www-form-urlencoded`) - parsed with `request.formData()` and validated with zod-form-data
|
|
6
10
|
*
|
|
7
11
|
* ## Overview
|
|
8
12
|
*
|
|
@@ -230,13 +234,18 @@ export interface FormActionConfig<
|
|
|
230
234
|
}
|
|
231
235
|
|
|
232
236
|
/**
|
|
233
|
-
* Creates a type-safe form action handler that validates form data and provides structured error handling.
|
|
237
|
+
* Creates a type-safe form action handler that validates form data or JSON and provides structured error handling.
|
|
234
238
|
*
|
|
235
239
|
* This function wraps a React Router action to:
|
|
236
|
-
* 1.
|
|
237
|
-
* 2.
|
|
238
|
-
* 3.
|
|
239
|
-
* 4.
|
|
240
|
+
* 1. Detect content type (JSON vs FormData) from the request headers
|
|
241
|
+
* 2. Parse and validate the request body using a Zod schema
|
|
242
|
+
* 3. Call the provided handler with validated data
|
|
243
|
+
* 4. Return structured errors for validation failures, handler errors, or unknown errors
|
|
244
|
+
* 5. Preserve React Router Response objects (redirects, etc.) by re-throwing them
|
|
245
|
+
*
|
|
246
|
+
* **Content-Type handling:**
|
|
247
|
+
* - `application/json`: Uses `request.json()` and validates directly with the schema
|
|
248
|
+
* - `multipart/form-data` or `application/x-www-form-urlencoded`: Uses `request.formData()` and validates with zod-form-data
|
|
240
249
|
*
|
|
241
250
|
* @template TSchema - The Zod schema type for form validation
|
|
242
251
|
* @template TResult - The success result type from the handler (defaults to undefined)
|
|
@@ -303,19 +312,25 @@ export const formAction = <
|
|
|
303
312
|
args: ActionArgs,
|
|
304
313
|
): Promise<MaybeError<TResult, FormActionError<TError, TSchema>>> => {
|
|
305
314
|
try {
|
|
306
|
-
const
|
|
307
|
-
const
|
|
315
|
+
const contentType = args.request.headers.get("Content-Type");
|
|
316
|
+
const isJson = contentType?.includes("application/json") ?? false;
|
|
317
|
+
|
|
318
|
+
const parseResult = isJson
|
|
319
|
+
? await schema.safeParseAsync(await args.request.json())
|
|
320
|
+
: await zfd
|
|
321
|
+
.formData(schema)
|
|
322
|
+
.safeParseAsync(await args.request.formData());
|
|
308
323
|
|
|
309
|
-
if (!
|
|
324
|
+
if (!parseResult.success) {
|
|
310
325
|
return fail({
|
|
311
326
|
type: "validation" as const,
|
|
312
327
|
error: z.treeifyError<z.infer<TSchema>>(
|
|
313
|
-
|
|
328
|
+
parseResult.error as z.core.$ZodError<z.infer<TSchema>>,
|
|
314
329
|
),
|
|
315
330
|
});
|
|
316
331
|
}
|
|
317
332
|
|
|
318
|
-
const handlerResult = await handler(args,
|
|
333
|
+
const handlerResult = await handler(args, parseResult.data);
|
|
319
334
|
if (!handlerResult.success) {
|
|
320
335
|
return fail({
|
|
321
336
|
type: "handler" as const,
|