UNPKG

react-form-action

Version:

State management helpers for the react form actions.

2 lines (1 loc) 1.54 kB
import{a as w,b as A}from"./chunk-LPB4R2UB.mjs";import{z as c}from"zod/v4";import{zfd as C}from"zod-form-data";var T=Symbol();function d(r,i=[],a,n){async function f(t){let e={formData:t};for(let p of i){let u=await p({ctx:e});e={...e,...u}}return e}return{args(t){let e=n?[...n,...t]:t;return d(r,i,a,e)},input(t){if(r===T)return d(t,i,a,n);if(r._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(r._zod.def.type==="object"){let e=c.object({...r.shape,...t.shape});return d(e,i)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return d(r,[...i,t],a,n)},error(t){return d(r,i,t,n)},run:r===T?t=>A(({success:e,failure:p,invalid:u},...s)=>async(y,g)=>{let x=await f(g);if(n){let o=c.tuple(n).safeParse(s);if(!o.success)return u(c.treeifyError(o.error))}try{return e(await t({args:s,ctx:x}))}catch(o){if(a)return p(await a({args:s,error:o,ctx:x}));throw o}}):r===T?void 0:t=>A(({success:e,failure:p,invalid:u},...s)=>{let y=C.formData(r);return async(g,x)=>{let o=await f(x);if(n){let l=c.tuple(n).safeParse(s);if(!l.success)return u(c.treeifyError(l.error))}let m=y.safeParse(x);if(!m.success)return u(c.treeifyError(m.error));let E=m.data;try{return e(await t({args:s,input:E,ctx:o}))}catch(l){if(a)return p(await a({args:s,error:l,ctx:o}));throw l}}})}}var k=d(T);export{A as createFormAction,k as formAction,w as initial};