react-form-action 2.9.1 → 2.10.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 +1 -0
- package/dist/client.d.mts +77 -74
- package/dist/client.mjs +1 -1
- package/dist/{createFormAction-DjhozgiG.d.mts → createFormAction-4p5Fvn2A.d.mts} +23 -26
- package/dist/createFormAction-BwMYAzm2.mjs +1 -0
- package/dist/index.d.mts +38 -49
- package/dist/index.mjs +1 -1
- package/package.json +17 -17
- package/dist/chunk-LPB4R2UB.mjs +0 -1
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ End-to-end typesafe success, error & validation state control for Next.js form a
|
|
|
22
22
|
- ✅ The `React.useActionState()` accessible via the `useActionContext()` hook.
|
|
23
23
|
- ✅ Computes progress flags like `isInvalid`, `isSuccess` based on the envelope type.
|
|
24
24
|
- ✅ Binds the action with the arguments from the _args_ prop.
|
|
25
|
+
- ✅ Has an optional `validate` prop to validate the form data client-side before calling the server action.
|
|
25
26
|
|
|
26
27
|
**Context-bound `<Form />` component**
|
|
27
28
|
|
package/dist/client.d.mts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { a as InvalidState, i as InitialState, n as FailureState, o as SuccessState, r as FormAction, t as ActionState } from "./createFormAction-4p5Fvn2A.mjs";
|
|
2
|
+
import React$1, { JSX, PropsWithChildren } from "react";
|
|
3
|
+
import { $ZodErrorTree } from "zod/v4/core";
|
|
4
|
+
//#region src/Action.d.ts
|
|
6
5
|
type ActionProps<Data, Error, ValidationError, Arguments extends unknown[] = []> = PropsWithChildren<Arguments extends [unknown, ...unknown[]] ? {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
action: FormAction<Data, Error, ValidationError, FormData, Arguments>;
|
|
7
|
+
args: Arguments;
|
|
8
|
+
initialData: Data;
|
|
9
|
+
permalink?: string;
|
|
10
|
+
validate?: (payload: FormData) => null | ValidationError;
|
|
11
11
|
} : {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
action: FormAction<Data, Error, ValidationError, FormData, Arguments>;
|
|
13
|
+
initialData: Data;
|
|
14
|
+
args?: undefined;
|
|
15
|
+
permalink?: string;
|
|
16
|
+
validate?: (payload: FormData) => null | ValidationError;
|
|
16
17
|
}>;
|
|
17
18
|
type ActionStatusFlags<T extends ActionState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
isInitial: T extends "initial" ? true : false;
|
|
20
|
+
isInvalid: T extends "invalid" ? true : false;
|
|
21
|
+
isFailure: T extends "failure" ? true : false;
|
|
22
|
+
isSuccess: T extends "success" ? true : false;
|
|
22
23
|
};
|
|
23
24
|
type ActionContextState<T extends ActionState<unknown, unknown, unknown>> = T & ActionStatusFlags<T["type"]> & {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
/**
|
|
26
|
+
* The dispatch function returned from React.useActionState()
|
|
27
|
+
*/
|
|
28
|
+
action: (payload: FormData) => void;
|
|
29
|
+
isPending: boolean;
|
|
29
30
|
};
|
|
30
31
|
/**
|
|
31
32
|
* NOTE: ActionContextState<ActionState<...>> would not allow discriminate the union inside of the ActionState.
|
|
@@ -35,74 +36,76 @@ type SpreadActionContext<Data = unknown, Error = unknown, ValidationError = unkn
|
|
|
35
36
|
* A hook to consume the form action state from the context.
|
|
36
37
|
*/
|
|
37
38
|
declare function useActionContext<Data, Error, ValidationError, Args extends unknown[] = []>(action?: FormAction<Data, Error, ValidationError, FormData, Args>): SpreadActionContext<Data, Error, ValidationError>;
|
|
38
|
-
declare function Action<Data, Error, ValidationError, Args extends unknown[] = []>({ children, action: formAction, initialData, args, permalink, }: ActionProps<Data, Error, ValidationError, Args>):
|
|
39
|
-
|
|
39
|
+
declare function Action<Data, Error, ValidationError, Args extends unknown[] = []>({ children, action: formAction, initialData, args, permalink, validate }: ActionProps<Data, Error, ValidationError, Args>): React$1.JSX.Element;
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/Form.d.ts
|
|
40
42
|
type FormProps = Omit<JSX.IntrinsicElements["form"], "action"> & {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Opt-in into automatic form reset by using the form "action" prop.
|
|
45
|
+
* By default, the onSubmit with a custom transition which opts-out of the implicit form reset.
|
|
46
|
+
* See for more. https://github.com/facebook/react/issues/29034
|
|
47
|
+
* @default false
|
|
48
|
+
*/
|
|
49
|
+
autoReset?: boolean;
|
|
48
50
|
};
|
|
49
51
|
declare function Form({ autoReset, ...props }: FormProps): JSX.Element;
|
|
50
|
-
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/ZodFieldError.d.ts
|
|
51
54
|
declare const SEPARATOR: ".";
|
|
52
|
-
type Paths<Shape extends $ZodErrorTree<object>, Props = Required<NonNullable<Shape["properties"]>>> = {
|
|
53
|
-
[k in keyof Props]: k extends string ? NonNullable<Props[k]> extends $ZodErrorTree<object> ? "properties" extends keyof NonNullable<Props[k]> ? `${k}${typeof SEPARATOR}${Paths<NonNullable<Props[k]>>}` : k : k : never;
|
|
54
|
-
}[keyof Props];
|
|
55
|
+
type Paths<Shape extends $ZodErrorTree<object>, Props = Required<NonNullable<Shape["properties"]>>> = { [k in keyof Props]: k extends string ? NonNullable<Props[k]> extends $ZodErrorTree<object> ? "properties" extends keyof NonNullable<Props[k]> ? `${k}${typeof SEPARATOR}${Paths<NonNullable<Props[k]>>}` : k : k : never; }[keyof Props];
|
|
55
56
|
type InferZodErrorPaths<T> = T extends $ZodErrorTree<object> ? Paths<T> : never;
|
|
56
57
|
type ZodFieldErrorProps<Errors extends $ZodErrorTree<any>, Name extends "" | InferZodErrorPaths<Errors>> = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
errors: Errors;
|
|
59
|
+
name: Name;
|
|
60
|
+
children?: (props: ZodFieldErrorChildrenProps<Name>) => React$1.ReactNode;
|
|
60
61
|
};
|
|
61
62
|
type ZodFieldErrorChildrenProps<Name> = {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
name: Name;
|
|
64
|
+
error?: string;
|
|
65
|
+
errors: readonly string[];
|
|
65
66
|
};
|
|
66
67
|
declare const noError: Readonly<{
|
|
67
|
-
|
|
68
|
+
errors: readonly string[];
|
|
68
69
|
}>;
|
|
69
|
-
declare function ZodFieldError<Errors extends $ZodErrorTree<object>, Name extends "" | InferZodErrorPaths<Errors> = "">({ errors, name, children
|
|
70
|
-
|
|
70
|
+
declare function ZodFieldError<Errors extends $ZodErrorTree<object>, Name extends "" | InferZodErrorPaths<Errors> = "">({ errors, name, children }: ZodFieldErrorProps<Errors, Name>): React$1.ReactNode;
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/createComponents.d.ts
|
|
71
73
|
/**
|
|
72
74
|
* Creates a typed components for actions created with the formAction builder.
|
|
73
75
|
*/
|
|
74
76
|
declare function createComponents<Data, Error, ValidationError extends $ZodErrorTree<any>, Args extends unknown[] = []>(action: FormAction<Data, Error, ValidationError, FormData, Args>): {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
77
|
+
FieldError: <Name extends "" | InferZodErrorPaths<ValidationError>>({ name, children }: {
|
|
78
|
+
name: Name;
|
|
79
|
+
children?: (props: ZodFieldErrorChildrenProps<Name>) => React$1.ReactNode;
|
|
80
|
+
}) => React$1.JSX.Element;
|
|
81
|
+
Success: ({ children }: PropsWithChildren | {
|
|
82
|
+
children?: (props: {
|
|
83
|
+
isSuccess: false;
|
|
84
|
+
data: Data | null;
|
|
85
|
+
} | {
|
|
86
|
+
isSuccess: true;
|
|
87
|
+
data: Data;
|
|
88
|
+
}) => React$1.ReactNode;
|
|
89
|
+
}) => React$1.ReactNode;
|
|
90
|
+
Invalid: ({ children }: PropsWithChildren | {
|
|
91
|
+
children?: (props: {
|
|
92
|
+
isInvalid: false;
|
|
93
|
+
validationError: null;
|
|
94
|
+
} | {
|
|
95
|
+
isInvalid: true;
|
|
96
|
+
validationError: ValidationError;
|
|
97
|
+
}) => React$1.ReactNode;
|
|
98
|
+
}) => React$1.ReactNode;
|
|
97
99
|
};
|
|
98
|
-
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/Pending.d.ts
|
|
99
102
|
/**
|
|
100
103
|
* Conditionally renders the children, when the form action is in the "pending" state.
|
|
101
104
|
*/
|
|
102
|
-
declare function Pending({ children
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}):
|
|
107
|
-
|
|
108
|
-
export { Action,
|
|
105
|
+
declare function Pending({ children }: PropsWithChildren | {
|
|
106
|
+
children?: (props: {
|
|
107
|
+
isPending: boolean;
|
|
108
|
+
}) => React.ReactNode;
|
|
109
|
+
}): import("react").ReactNode;
|
|
110
|
+
//#endregion
|
|
111
|
+
export { Action, ActionContextState, ActionProps, Form, FormProps, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorChildrenProps, ZodFieldErrorProps, createComponents, noError, useActionContext };
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{
|
|
1
|
+
"use client";import{n as e,t}from"./createFormAction-BwMYAzm2.mjs";import n,{createContext as r,startTransition as i,use as a,useActionState as o}from"react";const s={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},c=r(null);function l(e){let t=a(c);if(!t)throw Error(`ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?`);return t}function u({children:r,action:i,initialData:a,args:l,permalink:u,validate:d}){let f=l?i.bind(null,...l):i,[p,m,h]=o(async(e,n)=>{if(d){let r=await t(({invalid:e,success:t})=>async(n,r)=>{let i=d(r);return i?e(i):t(!0)})(e,n);if(r.type===`invalid`)return r}return f(e,n)},e(a),u),g=p.type===`initial`?{...p,...s,action:m,isPending:h,isInitial:!0}:p.type===`invalid`?{...p,...s,action:m,isPending:h,isInvalid:!0}:p.type===`failure`?{...p,...s,action:m,isPending:h,isFailure:!0}:{...p,...s,action:m,isPending:h,isSuccess:!0};return n.createElement(c,{value:g},r)}function d({autoReset:e=!1,...t}){let{action:r}=l(),a=e?{action:r}:{onSubmit:e=>{e.preventDefault();let t=e.currentTarget;i(()=>{r(new FormData(t))})}};return n.createElement(`form`,{...a,...t})}const f=Object.freeze({errors:Object.freeze([])});function p({errors:e,name:t,children:r=({error:e})=>n.createElement(n.Fragment,null,e)}){if(t.length===0)return r({name:t,error:e.errors[0],errors:e.errors});let i=t.split(`.`),a=e;for(;i.length;){let e=i.shift();if(!a.properties){a=f;break}a=a.properties[e]??f}return r({name:t,error:a.errors[0],errors:a.errors})}function m(e){function t({name:t,children:r}){let{isInvalid:i,validationError:a}=l(e);return n.createElement(p,{errors:a??f,name:t},r??(({error:e})=>i&&n.createElement(n.Fragment,null,e)))}function r({children:t}){let{isSuccess:n,data:r}=l(e);return typeof t==`function`?t({isSuccess:n,data:r}):n&&t}function i({children:t}){let{isInvalid:n,validationError:r}=l(e);return typeof t==`function`?t({isInvalid:n,validationError:r}):n&&t}return{FieldError:t,Success:r,Invalid:i}}function h({children:e}){let{isPending:t}=l();return typeof e==`function`?e({isPending:t}):t&&e}export{u as Action,d as Form,h as Pending,p as ZodFieldError,m as createComponents,f as noError,l as useActionContext};
|
|
@@ -1,38 +1,35 @@
|
|
|
1
|
+
//#region src/createFormAction.d.ts
|
|
1
2
|
type InitialState<T> = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
type: "initial";
|
|
4
|
+
data: T;
|
|
5
|
+
error: null;
|
|
6
|
+
validationError: null;
|
|
6
7
|
};
|
|
7
8
|
type InvalidState<T> = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
type: "invalid";
|
|
10
|
+
data: null;
|
|
11
|
+
error: null;
|
|
12
|
+
validationError: T;
|
|
12
13
|
};
|
|
13
14
|
type FailureState<T> = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
type: "failure";
|
|
16
|
+
data: null;
|
|
17
|
+
error: T;
|
|
18
|
+
validationError: null;
|
|
18
19
|
};
|
|
19
20
|
type SuccessState<T> = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
type: "success";
|
|
22
|
+
data: T;
|
|
23
|
+
error: null;
|
|
24
|
+
validationError: null;
|
|
24
25
|
};
|
|
25
26
|
type ActionState<Data, Error, ValidationError = Record<string, never>> = InitialState<Data> | InvalidState<ValidationError> | FailureState<Error> | SuccessState<Data>;
|
|
26
|
-
type FormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData, Arguments extends unknown[] = []> = (...args: [
|
|
27
|
-
...Arguments,
|
|
28
|
-
state: ActionState<Data, Error, ValidationError>,
|
|
29
|
-
payload: Payload
|
|
30
|
-
]) => Promise<ActionState<Data, Error, ValidationError>>;
|
|
27
|
+
type FormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData, Arguments extends unknown[] = []> = (...args: [...Arguments, state: ActionState<Data, Error, ValidationError>, payload: Payload]) => Promise<ActionState<Data, Error, ValidationError>>;
|
|
31
28
|
declare function initial<Data>(data: Data): InitialState<Data>;
|
|
32
29
|
declare function createFormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData, Arguments extends [...unknown[]] = []>(formAction: (params: {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
success: (data: Data) => SuccessState<Data>;
|
|
31
|
+
failure: (error: Error) => FailureState<Error>;
|
|
32
|
+
invalid: (validationError: ValidationError) => InvalidState<ValidationError>;
|
|
36
33
|
}, ...args: Arguments) => FormAction<Data, Error, ValidationError, Payload>): (...args: [...Arguments, state: ActionState<Data, Error, ValidationError>, payload: Payload]) => Promise<ActionState<Data, Error, ValidationError>>;
|
|
37
|
-
|
|
38
|
-
export {
|
|
34
|
+
//#endregion
|
|
35
|
+
export { InvalidState as a, initial as c, InitialState as i, FailureState as n, SuccessState as o, FormAction as r, createFormAction as s, ActionState as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return{type:`initial`,data:e,error:null,validationError:null}}function t(e){function t(e){return{type:`success`,data:e,error:null,validationError:null}}function n(e){return{type:`failure`,data:null,error:e,validationError:null}}function r(e){return{type:`invalid`,data:null,error:null,validationError:e}}return(...i)=>{let a=i.pop(),o=i.pop(),s=[...i];return e({success:t,failure:n,invalid:r},...s)(o,a)}}export{e as n,t};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,68 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { a as InvalidState, c as initial, i as InitialState, n as FailureState, o as SuccessState, r as FormAction, s as createFormAction, t as ActionState } from "./createFormAction-4p5Fvn2A.mjs";
|
|
2
|
+
import { ZodObject, ZodTuple, ZodType, z } from "zod/v4";
|
|
3
|
+
import { $ZodErrorTree, $ZodShape, $ZodType } from "zod/v4/core";
|
|
4
|
+
//#region src/formAction.d.ts
|
|
6
5
|
declare const emptyInput: unique symbol;
|
|
7
6
|
type EmptyInput = typeof emptyInput;
|
|
8
|
-
type Flatten<T> = Identity<{
|
|
9
|
-
[K in keyof T]: T[K];
|
|
10
|
-
}>;
|
|
7
|
+
type Flatten<T> = Identity<{ [K in keyof T]: T[K]; }>;
|
|
11
8
|
type Identity<T> = T;
|
|
12
9
|
type ErrorHandler<Context, Err, Args> = (params: {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
error: unknown;
|
|
11
|
+
ctx: Context;
|
|
12
|
+
args: Args;
|
|
16
13
|
}) => Promise<Err>;
|
|
17
14
|
type InitialContext = Record<"formData", FormData>;
|
|
18
15
|
/**
|
|
19
16
|
* Action without schema has no input.
|
|
20
17
|
*/
|
|
21
18
|
type Action<Data, Context, Args extends unknown[]> = (params: {
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
args: Args;
|
|
20
|
+
ctx: Context;
|
|
24
21
|
}) => Promise<Data>;
|
|
25
22
|
/**
|
|
26
23
|
* Action with schema will get parsed formData as the input.
|
|
27
24
|
*/
|
|
28
25
|
type SchemaAction<Data, Context, Args extends unknown[], Schema extends ZodType> = (params: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
args: Args;
|
|
27
|
+
ctx: Context;
|
|
28
|
+
input: z.output<Schema>;
|
|
32
29
|
}) => Promise<Data>;
|
|
33
30
|
type FormActionBuilder<Schema extends ZodType | EmptyInput, Args extends $ZodType[] = [], TErr = unknown, Context = InitialContext, PlainArgs extends unknown[] = z.output<ZodTuple<Args, null>>> = {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}) => Promise<NewContext>) => FormActionBuilder<Schema, Args, TErr, Context & NewContext>;
|
|
58
|
-
/**
|
|
59
|
-
* A chainable error handler to handle errors thrown while running the action passed to .run().
|
|
60
|
-
* It does not receive errors thrown from the context/middleware.
|
|
61
|
-
* @param processError A function which receives action's errors.
|
|
62
|
-
* @returns FormActionBuilder
|
|
63
|
-
*/
|
|
64
|
-
error: <TErr>(processError: ErrorHandler<Context, TErr, PlainArgs>) => FormActionBuilder<Schema, Args, TErr, Context>;
|
|
31
|
+
args: <T extends [$ZodType, ...$ZodType[]]>(args: T) => FormActionBuilder<Schema, [...Args, ...T], TErr, Context>;
|
|
32
|
+
input: <T extends ZodObject>(newInput: T extends ZodObject ? T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends $ZodShape> ? T extends ZodObject<infer O2 extends $ZodShape> ? ZodObject<O1 & O2> : never : T, Args, TErr, Context>;
|
|
33
|
+
/**
|
|
34
|
+
* A finishing call on the builder completing your action.
|
|
35
|
+
* @param action async function to execute. It will receive parsed formData as input, when the builder was initialized with a schema.
|
|
36
|
+
* @returns runnable server action exportable from a module.
|
|
37
|
+
*/
|
|
38
|
+
run: Schema extends ZodType ? <Data>(action: SchemaAction<Data, Flatten<Context>, PlainArgs, Schema>) => (...args: [...PlainArgs, state: ActionState<Data, TErr, $ZodErrorTree<z.output<Schema>> & $ZodErrorTree<PlainArgs>>, payload: FormData]) => Promise<Flatten<InvalidState<$ZodErrorTree<z.output<Schema>> & $ZodErrorTree<PlainArgs>> | FailureState<TErr> | SuccessState<Data>>> : <Data>(action: Action<Data, Flatten<Context>, PlainArgs>) => (...args: [...PlainArgs, state: ActionState<Data, TErr, $ZodErrorTree<PlainArgs>>, payload: FormData]) => Promise<Flatten<InvalidState<$ZodErrorTree<PlainArgs>> | FailureState<TErr> | SuccessState<Data>>>;
|
|
39
|
+
/**
|
|
40
|
+
* A chainable context enhancing helper.
|
|
41
|
+
* @param middleware A function which receives the current context, and returns an object which will be added to the context.
|
|
42
|
+
* @returns FormActionBuilder
|
|
43
|
+
*/
|
|
44
|
+
use: <NewContext extends Record<string, unknown>>(middleware: ({ ctx }: {
|
|
45
|
+
ctx: Context;
|
|
46
|
+
}) => Promise<NewContext>) => FormActionBuilder<Schema, Args, TErr, Context & NewContext>;
|
|
47
|
+
/**
|
|
48
|
+
* A chainable error handler to handle errors thrown while running the action passed to .run().
|
|
49
|
+
* It does not receive errors thrown from the context/middleware.
|
|
50
|
+
* @param processError A function which receives action's errors.
|
|
51
|
+
* @returns FormActionBuilder
|
|
52
|
+
*/
|
|
53
|
+
error: <TErr>(processError: ErrorHandler<Context, TErr, PlainArgs>) => FormActionBuilder<Schema, Args, TErr, Context>;
|
|
65
54
|
};
|
|
66
55
|
declare const formAction: FormActionBuilder<typeof emptyInput, [], unknown, InitialContext, []>;
|
|
67
|
-
|
|
68
|
-
export { ActionState, FailureState, InvalidState, SuccessState, formAction };
|
|
56
|
+
//#endregion
|
|
57
|
+
export { ActionState, FailureState, FormAction, InitialState, InvalidState, SuccessState, createFormAction, formAction, initial };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{n as e,t}from"./createFormAction-BwMYAzm2.mjs";import{z as n}from"zod/v4";import{zfd as r}from"zod-form-data";const i=Symbol();function a(e,o=[],s,c){async function l(e){let t={formData:e};for(let e of o){let n=await e({ctx:t});t={...t,...n}}return t}return{args(t){return a(e,o,s,c?[...c,...t]:t)},input(t){if(e===i)return a(t,o,s,c);if(e._zod.def.checks)throw Error(`Previous input is not augmentable because it contains an effect.`);if(t._zod.def.checks)throw Error(`Your input contains effect which prevents merging it with the previous inputs.`);if(e._zod.def.type===`object`)return a(n.object({...e.shape,...t.shape}),o,s,c);throw Error(`Merging inputs works only for object schemas without effects.`)},use(t){return a(e,[...o,t],s,c)},error(t){return a(e,o,t,c)},run:e===i?e=>t(({success:t,failure:r,invalid:i},...a)=>async(o,u)=>{let d=await l(u);if(c){let e=n.tuple(c).safeParse(a);if(!e.success)return i(n.treeifyError(e.error))}try{return t(await e({args:a,ctx:d}))}catch(e){if(s)return r(await s({args:a,error:e,ctx:d}));throw e}}):e===i?void 0:i=>t(({success:t,failure:a,invalid:o},...u)=>{let d=r.formData(e);return async(e,r)=>{let f=await l(r);if(c){let e=n.tuple(c).safeParse(u);if(!e.success)return o(n.treeifyError(e.error))}let p=d.safeParse(r);if(!p.success)return o(n.treeifyError(p.error));let m=p.data;try{return t(await i({args:u,input:m,ctx:f}))}catch(e){if(s)return a(await s({args:u,error:e,ctx:f}));throw e}}})}}const o=a(i);export{t as createFormAction,o as formAction,e as initial};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-form-action",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "State management helpers for the react form actions.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"/dist"
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
|
-
"build": "
|
|
15
|
+
"build": "tsdown src/index.ts src/client.ts --minify",
|
|
16
16
|
"test": "vitest",
|
|
17
17
|
"typecheck": "vitest --typecheck",
|
|
18
18
|
"format": "prettier . --write",
|
|
@@ -56,21 +56,21 @@
|
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@testing-library/dom": "^10.4.1",
|
|
59
|
-
"@testing-library/jest-dom": "^
|
|
60
|
-
"@testing-library/react": "^16.3.
|
|
59
|
+
"@testing-library/jest-dom": "^7.0.0",
|
|
60
|
+
"@testing-library/react": "^16.3.2",
|
|
61
61
|
"@testing-library/user-event": "^14.6.1",
|
|
62
|
-
"@types/react": "^19.2.
|
|
63
|
-
"@types/react-dom": "^19.2.
|
|
64
|
-
"@vitejs/plugin-react": "^
|
|
65
|
-
"happy-dom": "^20.0
|
|
66
|
-
"npm-check-updates": "^
|
|
67
|
-
"prettier": "^3.6
|
|
68
|
-
"react": "^19.2.
|
|
69
|
-
"react-dom": "^19.2.
|
|
70
|
-
"
|
|
71
|
-
"typescript": "^
|
|
72
|
-
"vitest": "^4.
|
|
73
|
-
"zod": "^
|
|
74
|
-
"zod-form-data": "^3.0.
|
|
62
|
+
"@types/react": "^19.2.17",
|
|
63
|
+
"@types/react-dom": "^19.2.3",
|
|
64
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
65
|
+
"happy-dom": "^20.11.0",
|
|
66
|
+
"npm-check-updates": "^22.2.9",
|
|
67
|
+
"prettier": "^3.9.6",
|
|
68
|
+
"react": "^19.2.7",
|
|
69
|
+
"react-dom": "^19.2.7",
|
|
70
|
+
"tsdown": "^0.22.12",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"vitest": "^4.1.10",
|
|
73
|
+
"zod": "^4.4.3",
|
|
74
|
+
"zod-form-data": "^3.0.3"
|
|
75
75
|
}
|
|
76
76
|
}
|
package/dist/chunk-LPB4R2UB.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function u(a){return{type:"initial",data:a,error:null,validationError:null}}function d(a){function t(r){return{type:"success",data:r,error:null,validationError:null}}function o(r){return{type:"failure",data:null,error:r,validationError:null}}function n(r){return{type:"invalid",data:null,error:null,validationError:r}}return(...r)=>{let e=r.pop(),i=r.pop(),l=[...r];return a({success:t,failure:o,invalid:n},...l)(i,e)}}export{u as a,d as b};
|