react-form-action 0.4.1 → 0.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.
package/README.md CHANGED
@@ -25,6 +25,10 @@ End-to-end typesafe success, error & validation state control for Next.js 14 for
25
25
  npm i react-form-action zod-form-data
26
26
  ```
27
27
 
28
+ <a aria-label="NPM version" href="https://www.npmjs.com/package/react-form-action">
29
+ <img alt="NPM Version" src="https://img.shields.io/npm/v/react-form-action?style=for-the-badge&labelColor=24292e">
30
+ </a>
31
+
28
32
  ## Usage
29
33
 
30
34
  ### `formAction` builder
@@ -46,7 +50,7 @@ const i18nMiddleware = async () => {
46
50
  const authAction = formAction
47
51
  .use(i18nMiddleware)
48
52
  .use(async ({ ctx: { t } }) =>
49
- console.log("🎉 context enhanced by previous middlewares 🎉", t)
53
+ console.log("🎉 context enhanced by previous middlewares 🎉", t),
50
54
  )
51
55
  .error(({ error }) => {
52
56
  if (error instanceof DbError) {
@@ -70,9 +74,7 @@ export const signIn = authAction
70
74
  return t("verificationEmail.success");
71
75
  });
72
76
 
73
- import { formAction } from "react-fomr-action";
74
-
75
- export const signUp = formAction
77
+ export const signUp = authAction
76
78
  .input(
77
79
  z
78
80
  .object({
@@ -83,14 +85,18 @@ export const signUp = formAction
83
85
  .refine((data) => data.password === data.confirm, {
84
86
  message: "Passwords don't match",
85
87
  path: ["confirm"],
86
- })
88
+ }),
87
89
  ) // if using refinement, only one input call is permited, as schema with ZodEffects is not extendable.
88
90
  .run(async ({ ctx: { t }, input: { email, password } }) => {
89
91
  // 🎉 passwords match!
90
92
 
91
- await db.signUp({ email, password });
93
+ const tokenData = await db.signUp({ email, password });
94
+
95
+ if (!tokenData) {
96
+ return t("signUp.emailVerificationRequired");
97
+ }
92
98
 
93
- return "We've sent you an email!";
99
+ return t("singUp.success");
94
100
  });
95
101
  ```
96
102
 
@@ -155,7 +161,7 @@ export const updateUser = createFormAction<
155
161
 
156
162
  return failure({ message: "Failed to update user." });
157
163
  }
158
- }
164
+ },
159
165
  );
160
166
  ```
161
167
 
package/dist/client.d.mts CHANGED
@@ -1,24 +1,27 @@
1
1
  import * as React from 'react';
2
2
  import React__default, { FormHTMLAttributes } from 'react';
3
3
  import { RenderProp } from 'react-render-prop-type';
4
- import { b as InitialState, I as InvalidState, a as FailureState, S as SuccessState, F as FormState } from './createFormAction-3a51310f.js';
4
+ import { F as FormState, b as InitialState, I as InvalidState, a as FailureState, S as SuccessState } from './createFormAction-3a51310f.js';
5
5
  import { useFormStatus } from 'react-dom';
6
6
 
7
- type FormStateProps<Data, ValidationError, Error, Payload> = {
8
- action: (state: FormState<Data, ValidationError, Error>, payload: Payload) => Promise<FormState<Data, ValidationError, Error>>;
7
+ type FormStateProps<Data, Error, ValidationError, Payload> = {
8
+ action: (state: FormState<Data, Error, ValidationError>, payload: Payload) => Promise<FormState<Data, Error, ValidationError>>;
9
9
  initialData: Data;
10
10
  permalink?: string;
11
11
  };
12
12
  declare function initial<Data>(data: Data): InitialState<Data>;
13
- type FormMetaState<T extends FormState<unknown, unknown, unknown>> = T & {
13
+ type FormStatusFlags<T extends FormState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
14
+ isInitial: T extends "initial" ? true : false;
15
+ isInvalid: T extends "invalid" ? true : false;
16
+ isFailure: T extends "failure" ? true : false;
17
+ isSuccess: T extends "success" ? true : false;
18
+ };
19
+ type FormMetaState<T extends FormState<unknown, unknown, unknown>> = T & FormStatusFlags<T["type"]> & {
14
20
  isPending: boolean;
15
- isInitial: T["type"] extends "initial" ? true : false;
16
- isFailure: T["type"] extends "failure" ? true : false;
17
- isSuccess: T["type"] extends "success" ? true : false;
18
- isInvalid: T["type"] extends "invalid" ? true : false;
19
21
  };
20
- declare function Form<Data, ValidationError, Error>({ children, action, initialData, permalink, ...props }: Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & FormStateProps<Data, ValidationError, Error, FormData> & RenderProp<FormMetaState<InitialState<Data>> | FormMetaState<InvalidState<Error>> | FormMetaState<FailureState<Error>> | FormMetaState<SuccessState<Data>>>): React__default.JSX.Element;
22
+ type FormProps<Data, Error, ValidationError> = Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & FormStateProps<Data, Error, ValidationError, FormData> & RenderProp<FormMetaState<InitialState<Data>> | FormMetaState<InvalidState<ValidationError>> | FormMetaState<FailureState<Error>> | FormMetaState<SuccessState<Data>>>;
23
+ declare function Form<Data, Error, ValidationError>({ children, action, initialData, permalink, ...props }: FormProps<Data, Error, ValidationError>): React__default.JSX.Element;
21
24
 
22
25
  declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
23
26
 
24
- export { Form, FormStatus, initial };
27
+ export { Form, FormProps, FormStateProps, FormStatus, initial };
package/dist/client.mjs CHANGED
@@ -1 +1 @@
1
- "use client";import a from"react";import{useFormState as u}from"react-dom";import{useFormStatus as p}from"react-dom";function e({children:t}){return t(p())}function S(t){return{type:"initial",data:t,error:null,validationError:null}}function E({children:t,action:o,initialData:i,permalink:n,...s}){let[r,m]=u(o,S(i),n);return a.createElement("form",{action:m,...s},a.createElement(e,null,({pending:l})=>t({...r,isPending:l,isInitial:r.type==="initial",isInvalid:r.type==="invalid",isFailure:r.type==="failure",isSuccess:r.type==="success"})))}export{E as Form,e as FormStatus,S as initial};
1
+ "use client";import o from"react";import{useFormState as S}from"react-dom";import{useFormStatus as F}from"react-dom";function e({children:r}){return r(F())}function c(r){return{type:"initial",data:r,error:null,validationError:null}}var a={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function x({children:r,action:i,initialData:n,permalink:s,...l}){let[t,m]=S(i,c(n),s),u=t.type==="initial"?{...t,...a,isInitial:!0}:t.type==="invalid"?{...t,...a,isInvalid:!0}:t.type==="failure"?{...t,...a,isFailure:!0}:{...t,...a,isSuccess:!0};return o.createElement("form",{action:m,...l},o.createElement(e,null,({pending:p})=>r({...u,isPending:p})))}export{x as Form,e as FormStatus,c as initial};
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- function y(e){function o(a){return{type:"success",data:a,error:null,validationError:null}}function n(a){return{type:"failure",data:null,error:a,validationError:null}}function s(a){return{type:"invalid",data:null,error:null,validationError:a}}return(a,S)=>e({success:o,failure:n,invalid:s})(a,S)}import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as w}from"zod-form-data";var l=Symbol();function d(e,o=[],n){async function s(t){let r={formData:t};for(let i of o){let u=await i({ctx:r});r={...r,...u}}return r}return{input(t){if(e===l)return d(t,o,n);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===C.ZodObject){let r=e.merge(t);return d(r,o)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return d(e,[...o,t],n)},error(t){return d(e,o,t)},run:e===l?t=>y(({success:r,failure:i})=>async(u,x)=>{let p=await s(x);try{return r(await t({ctx:p}))}catch(c){if(n)return i(n({error:c,ctx:p}));throw c}}):e===l?void 0:t=>y(({success:r,failure:i,invalid:u})=>{let x=w.formData(e);return async(p,c)=>{let f=await s(c),m=x.safeParse(c);if(!m.success)return u(m.error.flatten());let h=m.data;try{return r(await t({input:h,ctx:f}))}catch(E){if(n)return i(n({error:E,ctx:f}));throw E}}})}}var b=d(l);export{y as createFormAction,b as formAction};
1
+ function S(e){function o(n){return{type:"success",data:n,error:null,validationError:null}}function a(n){return{type:"failure",data:null,error:n,validationError:null}}function d(n){return{type:"invalid",data:null,error:null,validationError:n}}return(n,f)=>e({success:o,failure:a,invalid:d})(n,f)}import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as w}from"zod-form-data";var l=Symbol();function s(e,o=[],a){async function d(t){let r={formData:t};for(let i of o){let u=await i({ctx:r});r={...r,...u}}return r}return{input(t){if(e===l)return s(t,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===C.ZodObject){let r=e.merge(t);return s(r,o)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return s(e,[...o,t],a)},error(t){return s(e,o,t)},run:e===l?t=>S(({success:r,failure:i})=>async(u,m)=>{let x=await d(m);try{return r(await t({ctx:x}))}catch(c){if(a)return i(a({error:c,ctx:x}));throw c}}):e===l?void 0:t=>S(({success:r,failure:i,invalid:u})=>{let m=w.formData(e);return async(x,c)=>{let y=await d(c),p=m.safeParse(c);if(!p.success)return u(p.error.format());let h=p.data;try{return r(await t({input:h,ctx:y}))}catch(E){if(a)return i(a({error:E,ctx:y}));throw E}}})}}var b=s(l);export{S as createFormAction,b as formAction};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-action",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "State management helpers for the react form actions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,9 @@
14
14
  "scripts": {
15
15
  "build": "tsup",
16
16
  "test": "vitest",
17
- "typecheck": "vitest --typecheck"
17
+ "typecheck": "vitest --typecheck",
18
+ "format": "prettier . --write",
19
+ "format:check": "prettier . --check"
18
20
  },
19
21
  "exports": {
20
22
  ".": {
@@ -51,6 +53,7 @@
51
53
  "@types/react": "^18.2.37",
52
54
  "@types/react-dom": "^18.2.15",
53
55
  "next": "^14.0.2",
56
+ "prettier": "^3.3.3",
54
57
  "react-render-prop-type": "^0.1.0",
55
58
  "tsup": "^7.2.0",
56
59
  "typescript": "^5.2.2",