react-form-action 2.6.0 β†’ 2.7.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/README.md CHANGED
@@ -49,7 +49,7 @@ import { formAction } from "react-form-action";
49
49
  import { z } from "zod";
50
50
 
51
51
  export const subscribeAction = formAction
52
- .input(z.object({ email: z.string().email() }))
52
+ .input(z.object({ email: z.email() }))
53
53
  .run(async ({ input }) => {
54
54
  return input.email;
55
55
  });
@@ -138,7 +138,7 @@ const i18nMiddleware = async () => {
138
138
  const authAction = formAction
139
139
  .use(i18nMiddleware)
140
140
  .use(async ({ ctx: { t } }) =>
141
- console.log("πŸŽ‰ context enhanced by previous middlewares πŸŽ‰", t)
141
+ console.log("πŸŽ‰ context enhanced by previous middlewares πŸŽ‰", t),
142
142
  )
143
143
  .error(async ({ error }) => {
144
144
  if (error instanceof DbError) {
@@ -151,7 +151,7 @@ const authAction = formAction
151
151
  });
152
152
 
153
153
  export const signIn = authAction
154
- .input(z.object({ email: z.string().email() }))
154
+ .input(z.object({ email: z.email() }))
155
155
  // πŸŽ‰ extend the previous input (only without refinements and transforms)
156
156
  .input(z.object({ password: z.string() }))
157
157
  .run(async ({ ctx: { t }, input: { email, password } }) => {
@@ -166,14 +166,14 @@ export const signUp = authAction
166
166
  .input(
167
167
  z
168
168
  .object({
169
- email: z.string().email(),
169
+ email: z.email(),
170
170
  password: z.string(),
171
171
  confirm: z.string(),
172
172
  })
173
173
  .refine((data) => data.password === data.confirm, {
174
174
  message: "Passwords don't match",
175
175
  path: ["confirm"],
176
- })
176
+ }),
177
177
  ) // if using refinement, only one input call is permited, as schema with ZodEffects is not extendable.
178
178
  .run(async ({ ctx: { t }, input: { email, password } }) => {
179
179
  // πŸŽ‰ passwords match!
@@ -193,32 +193,37 @@ export const signUp = authAction
193
193
  The `formAction` builder supports action arguments binding:
194
194
 
195
195
  ```ts
196
- // app/update-user/[userId]/action.tsx
196
+ // app/[locale]/update-user/[userId]/action.tsx
197
197
  import { formAction } from "react-form-action";
198
198
 
199
199
  export const updateUser = formAction
200
- .args([z.string().uuid()])
200
+ .args([z.string().uuid(), z.enum("fr", "en")])
201
201
  .run(async ({ args: [userId] }) => {
202
202
  return userId;
203
203
  // ^? string
204
+ })
205
+ .error(({ args: [_, locale] }) => {
206
+ return locale === "fr"
207
+ ? "Γ‰chec de la mise Γ  jour de l'utilisateur"
208
+ : "Failed to update user";
204
209
  });
205
210
  ```
206
211
 
207
212
  ```tsx
208
- // app/update-user/[userId]/page.tsx
213
+ // app/[locale]/update-user/[userId]/page.tsx
209
214
  import { Action } from "react-form-action/client";
210
215
 
216
+ import { locale as localeParam } from "next/root-params";
211
217
  import { updateUser } from "./action";
212
218
  import { UpdateUserForm } from "./form";
213
219
 
214
220
  export default function Page({
215
221
  params,
216
- }: {
217
- params: Promise<{ userId: string }>;
218
- }) {
222
+ }: PageProps<"/[locale]/update-user/[userId]">) {
223
+ const locale = await localeParam();
219
224
  const { userId } = await params;
220
225
 
221
- const action = updateUser.bind(null, userId);
226
+ const action = updateUser.bind(null, userId, locale);
222
227
 
223
228
  return (
224
229
  <Action action={action} initialData="">
@@ -291,7 +296,7 @@ export const updateUser = createFormAction<
291
296
 
292
297
  return failure({ message: "Failed to update user." });
293
298
  }
294
- }
299
+ },
295
300
  );
296
301
  ```
297
302
 
@@ -301,7 +306,7 @@ The action creator supports arguments binding:
301
306
  export const updateUser = createFormAction(
302
307
  (
303
308
  { success, failure, invalid },
304
- userId: string /* Here you can specify multiple arguments */
309
+ userId: string /* Here you can specify multiple arguments */,
305
310
  ) =>
306
311
  async (prevState, formData) => {
307
312
  try {
@@ -327,7 +332,7 @@ export const updateUser = createFormAction(
327
332
  } catch (error) {
328
333
  // handle error
329
334
  }
330
- }
335
+ },
331
336
  );
332
337
 
333
338
  // call bind as usuall, the "123" becomes the "userId"
@@ -421,7 +426,7 @@ export const signUpAction = authAction
421
426
  z
422
427
  .object({
423
428
  user: z.object({
424
- email: z.string().email(),
429
+ email: z.email(),
425
430
  name: z.string(),
426
431
  }),
427
432
  password: z.string().min(8),
@@ -429,7 +434,7 @@ export const signUpAction = authAction
429
434
  })
430
435
  .refine((data) => data.password === data.confirm, {
431
436
  message: "Passwords don't match",
432
- })
437
+ }),
433
438
  )
434
439
  .run(async ({ ctx, input }) => {
435
440
  return null;
package/dist/index.d.mts CHANGED
@@ -9,9 +9,10 @@ type Flatten<T> = Identity<{
9
9
  [K in keyof T]: T[K];
10
10
  }>;
11
11
  type Identity<T> = T;
12
- type ErrorHandler<Context, Err> = (params: {
12
+ type ErrorHandler<Context, Err, Args extends ZodTuple> = (params: {
13
13
  error: unknown;
14
14
  ctx: Context;
15
+ args: z.infer<Args>;
15
16
  }) => Promise<Err>;
16
17
  type InitialContext = Record<"formData", FormData>;
17
18
  /**
@@ -60,7 +61,7 @@ type FormActionBuilder<Schema extends ZodType | EmptyInput, TErr = unknown, Cont
60
61
  * @param processError A function which receives action's errors.
61
62
  * @returns FormActionBuilder
62
63
  */
63
- error: <TErr>(processError: ErrorHandler<Context, TErr>) => FormActionBuilder<Schema, TErr, Context, Args>;
64
+ error: <TErr>(processError: ErrorHandler<Context, TErr, Args>) => FormActionBuilder<Schema, TErr, Context, Args>;
64
65
  };
65
66
  declare const formAction: FormActionBuilder<typeof emptyInput, unknown, InitialContext, z.ZodTuple<[], null>>;
66
67
 
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{a as h,b as f}from"./chunk-LPB4R2UB.mjs";import{z as T}from"zod/v4";import{zfd as g}from"zod-form-data";var m=Symbol();function c(e,s=[],n,a){async function A(t){let r={formData:t};for(let i of s){let u=await i({ctx:r});r={...r,...u}}return r}return{args(t){return c(e,s,n,T.tuple(t))},input(t){if(e===m)return c(t,s,n,a);if(e._zod.def.checks)throw new Error("Previous input is not augmentable because it contains an effect.");if(t._zod.def.checks)throw new Error("Your input contains effect which prevents merging it with the previous inputs.");if(e._zod.def.type==="object"){let r=T.object({...e.shape,...t.shape});return c(r,s)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return c(e,[...s,t],n,a)},error(t){return c(e,s,t,a)},run:e===m?t=>f(({success:r,failure:i,invalid:u},...d)=>async(E,y)=>{let p=await A(y);if(a){let o=a.safeParse(d);if(!o.success)return u(T.treeifyError(o.error))}try{return r(await t({args:d,ctx:p}))}catch(o){if(n)return i(await n({error:o,ctx:p}));throw o}}):e===m?void 0:t=>f(({success:r,failure:i,invalid:u},...d)=>{let E=g.formData(e);return async(y,p)=>{let o=await A(p);if(a){let x=a.safeParse(d);if(!x.success)return u(T.treeifyError(x.error))}let l=E.safeParse(p);if(!l.success)return u(T.treeifyError(l.error));let C=l.data;try{return r(await t({args:d,input:C,ctx:o}))}catch(x){if(n)return i(await n({error:x,ctx:o}));throw x}}})}}var j=c(m);export{f as createFormAction,j as formAction,h as initial};
1
+ import{a as h,b as f}from"./chunk-LPB4R2UB.mjs";import{z as T}from"zod/v4";import{zfd as C}from"zod-form-data";var m=Symbol();function c(e,u=[],n,a){async function A(t){let r={formData:t};for(let d of u){let i=await d({ctx:r});r={...r,...i}}return r}return{args(t){return c(e,u,n,T.tuple(t))},input(t){if(e===m)return c(t,u,n,a);if(e._zod.def.checks)throw new Error("Previous input is not augmentable because it contains an effect.");if(t._zod.def.checks)throw new Error("Your input contains effect which prevents merging it with the previous inputs.");if(e._zod.def.type==="object"){let r=T.object({...e.shape,...t.shape});return c(r,u)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return c(e,[...u,t],n,a)},error(t){return c(e,u,t,a)},run:e===m?t=>f(({success:r,failure:d,invalid:i},...s)=>async(E,g)=>{let p=await A(g);if(a){let o=a.safeParse(s);if(!o.success)return i(T.treeifyError(o.error))}try{return r(await t({args:s,ctx:p}))}catch(o){if(n)return d(await n({args:s,error:o,ctx:p}));throw o}}):e===m?void 0:t=>f(({success:r,failure:d,invalid:i},...s)=>{let E=C.formData(e);return async(g,p)=>{let o=await A(p);if(a){let x=a.safeParse(s);if(!x.success)return i(T.treeifyError(x.error))}let l=E.safeParse(p);if(!l.success)return i(T.treeifyError(l.error));let y=l.data;try{return r(await t({args:s,input:y,ctx:o}))}catch(x){if(n)return d(await n({args:s,error:x,ctx:o}));throw x}}})}}var j=c(m);export{f as createFormAction,j as formAction,h as initial};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-action",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "description": "State management helpers for the react form actions.",
5
5
  "repository": {
6
6
  "type": "git",