react-form-action 2.4.1 → 2.5.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
function d(a){return{type:"initial",data:a,error:null,validationError:null}}function u(a){function t(r){return{type:"success",data:r,error:null,validationError:null}}function
|
|
1
|
+
function d(a){return{type:"initial",data:a,error:null,validationError:null}}function u(a){function t(r){return{type:"success",data:r,error:null,validationError:null}}function e(r){return{type:"failure",data:null,error:r,validationError:null}}function o(r){return{type:"invalid",data:null,error:null,validationError:r}}return(...r)=>{let[n,i,...l]=r.reverse();return a({success:t,failure:e,invalid:o},...l)(i,n)}}export{d as a,u as b};
|
package/dist/client.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import React__default, { PropsWithChildren, JSX } from 'react';
|
|
3
|
-
import { F as FormAction, A as ActionState, I as InitialState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-
|
|
3
|
+
import { F as FormAction, A as ActionState, I as InitialState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DkNsdAwU.mjs';
|
|
4
4
|
import { ZodFormattedError } from 'zod';
|
|
5
5
|
import { RenderProp, RP } from 'react-render-prop-type';
|
|
6
6
|
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{a as m}from"./chunk-
|
|
1
|
+
"use client";import{a as m}from"./chunk-MQGUMLBX.mjs";import y from"react";import{createContext as P,use as S}from"react";import{useActionState as F}from"react";var p={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},u=P(null);function a(o){let t=S(u);if(!t)throw new Error("ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?");return t}function I({children:o,action:t,initialData:i,permalink:e}){let[r,n,s]=F(t,m(i),e),l=r.type==="initial"?{...r,...p,action:n,isPending:s,isInitial:!0}:r.type==="invalid"?{...r,...p,action:n,isPending:s,isInvalid:!0}:r.type==="failure"?{...r,...p,action:n,isPending:s,isFailure:!0}:{...r,...p,action:n,isPending:s,isSuccess:!0};return y.createElement(u,{value:l},o)}import A,{startTransition as C}from"react";function V({autoReset:o=!1,...t}){let{action:i}=a();return A.createElement("form",{...o?{action:i}:{onSubmit:r=>{r.preventDefault();let n=r.currentTarget;C(()=>{i(new FormData(n))})}},...t})}import c from"react";import f from"react";var h=".",d=Object.freeze({_errors:Object.freeze([])});function E({errors:o,name:t,children:i=({error:e})=>f.createElement(f.Fragment,null,e)}){if(t.length===0)return i({name:t,error:o._errors[0],errors:o._errors});let e=t.split(h),r=o;for(;e.length;){let n=e.shift();r=r[n]??d}return i({name:t,error:r._errors[0],errors:r._errors})}function $(o){function t({name:e,children:r}){let{isInvalid:n,validationError:s}=a(o);return c.createElement(E,{errors:s??d,name:e},r??(({error:x})=>n&&c.createElement(c.Fragment,null,x)))}function i({children:e}){let{isSuccess:r,data:n}=a(o);return typeof e=="function"?e(r?{isSuccess:r,data:n}:{isSuccess:r,data:n}):r&&e}return{FieldError:t,Success:i}}function X({children:o}){let{isPending:t}=a();return typeof o=="function"?o({isPending:t}):t&&o}export{I as Action,V as Form,X as Pending,E as ZodFieldError,$ as createComponents,d as noError,a as useActionContext};
|
|
@@ -22,7 +22,11 @@ type SuccessState<T> = {
|
|
|
22
22
|
error: null;
|
|
23
23
|
validationError: null;
|
|
24
24
|
};
|
|
25
|
-
type
|
|
25
|
+
type Flatten<T> = Identity<{
|
|
26
|
+
[K in keyof T]: T[K];
|
|
27
|
+
}>;
|
|
28
|
+
type Identity<T> = T;
|
|
29
|
+
type ActionState<Data, Error, ValidationError = Record<string, never>> = Flatten<InitialState<Data> | InvalidState<ValidationError> | FailureState<Error> | SuccessState<Data>>;
|
|
26
30
|
type FormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData, Arguments extends unknown[] = []> = (...args: [
|
|
27
31
|
...Arguments,
|
|
28
32
|
state: ActionState<Data, Error, ValidationError>,
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as ActionState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-
|
|
2
|
-
export { F as FormAction, I as InitialState, c as createFormAction, i as initial } from './createFormAction-
|
|
3
|
-
import { ZodTypeAny, ZodType, ZodObject, ZodRawShape, objectUtil,
|
|
1
|
+
import { A as ActionState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DkNsdAwU.mjs';
|
|
2
|
+
export { F as FormAction, I as InitialState, c as createFormAction, i as initial } from './createFormAction-DkNsdAwU.mjs';
|
|
3
|
+
import { z, ZodTypeAny, AnyZodTuple, ZodTuple, ZodType, ZodObject, ZodRawShape, objectUtil, ZodSchema, ZodEffects } from 'zod';
|
|
4
4
|
|
|
5
5
|
declare const emptyInput: unique symbol;
|
|
6
6
|
type EmptyInput = typeof emptyInput;
|
|
@@ -16,25 +16,36 @@ type InitialContext = Record<"formData", FormData>;
|
|
|
16
16
|
/**
|
|
17
17
|
* Action without schema has no input.
|
|
18
18
|
*/
|
|
19
|
-
type Action<Data, Context> = (params: {
|
|
19
|
+
type Action<Data, Context, Args extends AnyZodTuple> = (params: {
|
|
20
|
+
args: z.infer<Args>;
|
|
20
21
|
ctx: Context;
|
|
21
22
|
}) => Promise<Data>;
|
|
22
23
|
/**
|
|
23
24
|
* Action with schema will get parsed formData as the input.
|
|
24
25
|
*/
|
|
25
|
-
type SchemaAction<Data, Context, Schema extends ZodSchema> = (params: {
|
|
26
|
+
type SchemaAction<Data, Context, Args extends AnyZodTuple, Schema extends ZodSchema> = (params: {
|
|
27
|
+
args: z.infer<Args>;
|
|
26
28
|
ctx: Context;
|
|
27
29
|
input: z.infer<Schema>;
|
|
28
30
|
}) => Promise<Data>;
|
|
29
31
|
type AnyZodEffects = ZodEffects<any, any, any>;
|
|
30
|
-
type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Context = InitialContext
|
|
31
|
-
|
|
32
|
+
type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Context = InitialContext, Args extends AnyZodTuple = ZodTuple<[], null>> = {
|
|
33
|
+
args: <T extends [ZodTypeAny, ...ZodTypeAny[]]>(args: T) => FormActionBuilder<Schema, Err, Context, ZodTuple<T, null>>;
|
|
34
|
+
input: <T extends ZodTypeAny>(newInput: T extends ZodType<infer Out extends Record<string, unknown>> ? Schema extends AnyZodEffects ? "Extending schema with effect is not possible." : Schema extends EmptyInput ? T : Schema extends ZodTypeAny ? T extends AnyZodEffects ? "Your input contains effect which prevents merging it with the previous inputs." : T : T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends ZodRawShape> ? T extends ZodObject<infer O2 extends ZodRawShape> ? ZodObject<objectUtil.extendShape<O1, O2>> : never : T, Err, Context, Args>;
|
|
32
35
|
/**
|
|
33
36
|
* A finishing call on the builder completing your action.
|
|
34
37
|
* @param action async function to execute. It will receive parsed formData as input, when the builder was initialized with a schema.
|
|
35
38
|
* @returns runnable server action exportable from a module.
|
|
36
39
|
*/
|
|
37
|
-
run: Schema extends ZodTypeAny ? <Data>(action: SchemaAction<Data, Context
|
|
40
|
+
run: Schema extends ZodTypeAny ? <Data>(action: SchemaAction<Data, Flatten<Context>, Args, Schema>) => (...args: [
|
|
41
|
+
...z.infer<Args>,
|
|
42
|
+
state: ActionState<Data, Error, z.inferFormattedError<Schema> | z.inferFormattedError<Args>>,
|
|
43
|
+
payload: FormData
|
|
44
|
+
]) => Promise<Flatten<InvalidState<z.inferFormattedError<Schema> | z.inferFormattedError<Args>> | FailureState<Err> | SuccessState<Data>>> : <Data>(action: Action<Data, Flatten<Context>, Args>) => (...args: [
|
|
45
|
+
...z.infer<Args>,
|
|
46
|
+
state: ActionState<Data, Error, z.inferFormattedError<Args>>,
|
|
47
|
+
payload: FormData
|
|
48
|
+
]) => Promise<Flatten<InvalidState<z.inferFormattedError<Args>> | FailureState<Err> | SuccessState<Data>>>;
|
|
38
49
|
/**
|
|
39
50
|
* A chainable context enhancing helper.
|
|
40
51
|
* @param middleware A function which receives the current context, and returns an object which will be added to the context.
|
|
@@ -42,15 +53,15 @@ type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Cont
|
|
|
42
53
|
*/
|
|
43
54
|
use: <NewContext extends Record<string, unknown>>(middleware: ({ ctx }: {
|
|
44
55
|
ctx: Context;
|
|
45
|
-
}) => Promise<NewContext>) => FormActionBuilder<Schema, Err, Context & NewContext>;
|
|
56
|
+
}) => Promise<NewContext>) => FormActionBuilder<Schema, Err, Context & NewContext, Args>;
|
|
46
57
|
/**
|
|
47
58
|
* A chainable error handler to handle errors thrown while running the action passed to .run().
|
|
48
59
|
* It does not receive errors thrown from the context/middleware.
|
|
49
60
|
* @param processError A function which receives action's errors.
|
|
50
61
|
* @returns FormActionBuilder
|
|
51
62
|
*/
|
|
52
|
-
error: <Err>(processError: ErrorHandler<Context, Err>) => FormActionBuilder<Schema, Err, Context>;
|
|
63
|
+
error: <Err>(processError: ErrorHandler<Context, Err>) => FormActionBuilder<Schema, Err, Context, Args>;
|
|
53
64
|
};
|
|
54
|
-
declare const formAction: FormActionBuilder<typeof emptyInput, Error, InitialContext
|
|
65
|
+
declare const formAction: FormActionBuilder<typeof emptyInput, Error, InitialContext, z.ZodTuple<[], null>>;
|
|
55
66
|
|
|
56
67
|
export { ActionState, FailureState, InvalidState, SuccessState, formAction };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as
|
|
1
|
+
import{a as w,b as y}from"./chunk-MQGUMLBX.mjs";import{z as S,ZodFirstPartyTypeKind as h}from"zod";import{zfd as C}from"zod-form-data";var p=Symbol();function c(e,s=[],o,a){async function A(t){let r={formData:t};for(let d of s){let i=await d({ctx:r});r={...r,...i}}return r}return{args(t){return c(e,s,o,S.tuple(t))},input(t){if(e===p)return c(t,s,o,a);if(e._def.effect)throw new Error("Previous input is not augmentable because it contains an effect.");if(t._def.effect)throw new Error("Your input contains effect which prevents merging it with the previous inputs.");if(e._def.typeName===h.ZodObject){let r=e.merge(t);return c(r,s)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return c(e,[...s,t],o,a)},error(t){return c(e,s,t,a)},run:e===p?t=>y(({success:r,failure:d,invalid:i},...u)=>async(l,E)=>{let f=await A(E);if(a){let n=a.safeParse(u);if(!n.success){let x=n.error.flatten();return i(x.fieldErrors)}}try{return r(await t({args:u,ctx:f}))}catch(n){if(o)return d(await o({error:n,ctx:f}));throw n}}):e===p?void 0:t=>y(({success:r,failure:d,invalid:i},...u)=>{let l=C.formData(e);return async(E,f)=>{let n=await A(f);if(a){let m=a.safeParse(u);if(!m.success)return i(m.error.format())}let x=l.safeParse(f);if(!x.success)return i(x.error.format());let g=x.data;try{return r(await t({args:u,input:g,ctx:n}))}catch(m){if(o)return d(await o({error:m,ctx:n}));throw m}}})}}var N=c(p);export{y as createFormAction,N as formAction,w as initial};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-form-action",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "State management helpers for the react form actions.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"typecheck": "vitest --typecheck",
|
|
18
18
|
"format": "prettier . --write",
|
|
19
19
|
"format:check": "prettier . --check",
|
|
20
|
-
"ncu": "ncu --interactive --format group"
|
|
20
|
+
"ncu": "ncu --interactive --format group",
|
|
21
|
+
"pub:next": "npm publish --tag next"
|
|
21
22
|
},
|
|
22
23
|
"exports": {
|
|
23
24
|
".": {
|