@devup-api/hookform
Advanced tools
@@ -1,2 +0,1 @@ | ||
| "use client"; | ||
| "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),T=require("@devup-api/zod"),$=require("@hookform/resolvers/zod"),R=require("@tanstack/react-query"),n=require("react"),k=require("react-hook-form"),E=n.createContext(null);function K(i,c,s){for(const d of s){const u=d.split(".");let a=i,e=c;for(const y of u)a=a?.[y],e=e?.[y];if(!Object.is(a,e))return!0}return!1}function P(i,c,s=""){return new Proxy(i,{get(d,u){if(typeof u=="symbol")return Reflect.get(d,u);const a=s?`${s}.${u}`:u,e=Reflect.get(d,u);return e===null||typeof e!="object"||typeof e=="function"?(c.add(a),e):P(e,c,a)}})}function H(){const i=n.useContext(E);if(!i)throw new Error("useApiFormContext must be used within an ApiForm");const c=n.useRef(new Set),s=n.useRef(null),d=n.useRef(null),u=n.useCallback(()=>{const e=i.getState();return!s.current||!d.current||c.current.size>0&&K(s.current,e,c.current)?(s.current=e,d.current=e,e):d.current},[i]),a=n.useSyncExternalStore(i.subscribe,u,u);return n.useMemo(()=>P(a,c.current),[a])}function O({api:i,method:c,path:s,openapi:d,requestOptions:u,onSuccess:a,onError:e,onValidationError:y,children:h,defaultValues:p,mode:j="onSubmit",formOptions:C,formProps:M,resetOnSuccess:q=!1,queryClient:S,fetchDefaultValues:o}){const b=n.useRef(new Set),F=n.useRef(null),x=n.useRef(null),v=n.useRef(!1),w=n.useCallback(()=>{for(const r of b.current)r()},[]),m=R.useQuery({queryKey:o?["apiFormDefaultValues",o.method??"get",o.path,o.options]:["apiFormDefaultValues","disabled"],queryFn:o?async()=>{const r=o.method??"get",t=await i[r](o.path,o.options);if(t.error)throw t.error;return o.transform?o.transform(t.data):t.data}:R.skipToken,enabled:!!o,...S&&{queryClient:S}}),z=n.useMemo(()=>o&&m.data?m.data:p,[o,m.data,p]),A=T.pathSchemas?.[c]?.[s],l=k.useForm({defaultValues:z,mode:j,resolver:A?$.zodResolver(A):void 0,...C}),f=R.useMutation({mutationKey:[c,s,u],mutationFn:async r=>{const t=await i[c](s,{...u,body:r});if(t.error)throw t.error;return t.data},onSuccess:r=>{a?.(r),q&&l.reset()},onError:r=>{e?.(r)},...S&&{queryClient:S}});F.current=l,x.current=f,v.current=m.isLoading,n.useEffect(()=>{o&&m.data&&l.reset(m.data)},[o,m.data,l]),n.useEffect(()=>{w()},[f.isPending,f.isSuccess,f.isError,f.error,f.data,m.isLoading,w]);const L=l.handleSubmit(r=>{f.mutate(r)},r=>{y?.(r)}),Q=n.useMemo(()=>({getState:()=>{const r=F.current,t=x.current;return{form:r,mutation:{isPending:t.isPending,isSuccess:t.isSuccess,isError:t.isError,error:t.error,data:t.data,mutate:t.mutate,mutateAsync:t.mutateAsync,reset:t.reset},isLoadingDefaultValues:v.current}},subscribe:r=>(b.current.add(r),()=>{b.current.delete(r)})}),[]);return g.jsx(E.Provider,{value:Q,children:g.jsx(k.FormProvider,{...l,children:g.jsx("form",{onSubmit:L,...M,children:h})})})}exports.ApiForm=O;exports.useApiFormContext=H; | ||
| "use client";"use client";let e=require(`react-hook-form`),t=require(`@devup-api/zod`),n=require(`@hookform/resolvers/zod`),r=require(`@tanstack/react-query`),i=require(`react`),a=require(`react/jsx-runtime`);var o=(0,i.createContext)(null);function s(e,t,n){for(let r of n){let n=r.split(`.`),i=e,a=t;for(let e of n)i=i?.[e],a=a?.[e];if(!Object.is(i,a))return!0}return!1}function c(e,t,n=``){return new Proxy(e,{get(e,r){if(typeof r==`symbol`)return Reflect.get(e,r);let i=n?`${n}.${r}`:r,a=Reflect.get(e,r);return typeof a!=`object`||!a||typeof a==`function`?(t.add(i),a):c(a,t,i)}})}function l(){let e=(0,i.useContext)(o);if(!e)throw Error(`useApiFormContext must be used within an ApiForm`);let t=(0,i.useRef)(new Set),n=(0,i.useRef)(null),r=(0,i.useRef)(null),a=(0,i.useCallback)(()=>{let i=e.getState();return!n.current||!r.current||t.current.size>0&&s(n.current,i,t.current)?(n.current=i,r.current=i,i):r.current},[e]),l=(0,i.useSyncExternalStore)(e.subscribe,a,a);return(0,i.useMemo)(()=>c(l,t.current),[l])}function u({api:s,method:c,path:l,openapi:u,requestOptions:d,onSuccess:f,onError:p,onValidationError:m,children:h,defaultValues:g,mode:_=`onSubmit`,formOptions:v,formProps:y,resetOnSuccess:b=!1,queryClient:x,fetchDefaultValues:S}){let C=(0,i.useRef)(new Set),w=(0,i.useRef)(null),T=(0,i.useRef)(null),E=(0,i.useRef)(!1),D=(0,i.useCallback)(()=>{for(let e of C.current)e()},[]),O=(0,r.useQuery)({queryKey:S?[`apiFormDefaultValues`,S.method??`get`,S.path,S.options]:[`apiFormDefaultValues`,`disabled`],queryFn:S?async()=>{let e=await s[S.method??`get`](S.path,S.options);if(e.error)throw e.error;return S.transform?S.transform(e.data):e.data}:r.skipToken,enabled:!!S,...x&&{queryClient:x}}),k=(0,i.useMemo)(()=>S&&O.data?O.data:g,[S,O.data,g]),A=t.pathSchemas?.[c]?.[l],j=(0,e.useForm)({defaultValues:k,mode:_,resolver:A?(0,n.zodResolver)(A):void 0,...v}),M=(0,r.useMutation)({mutationKey:[c,l,d],mutationFn:async e=>{let t=await s[c](l,{...d,body:e});if(t.error)throw t.error;return t.data},onSuccess:e=>{f?.(e),b&&j.reset()},onError:e=>{p?.(e)},...x&&{queryClient:x}});w.current=j,T.current=M,E.current=O.isLoading,(0,i.useEffect)(()=>{S&&O.data&&j.reset(O.data)},[S,O.data,j]),(0,i.useEffect)(()=>{D()},[M.isPending,M.isSuccess,M.isError,M.error,M.data,O.isLoading,D]);let N=j.handleSubmit(e=>{M.mutate(e)},e=>{m?.(e)}),P=(0,i.useMemo)(()=>({getState:()=>{let e=w.current,t=T.current;return{form:e,mutation:{isPending:t.isPending,isSuccess:t.isSuccess,isError:t.isError,error:t.error,data:t.data,mutate:t.mutate,mutateAsync:t.mutateAsync,reset:t.reset},isLoadingDefaultValues:E.current}},subscribe:e=>(C.current.add(e),()=>{C.current.delete(e)})}),[]);return(0,a.jsx)(o.Provider,{value:P,children:(0,a.jsx)(e.FormProvider,{...j,children:(0,a.jsx)(`form`,{onSubmit:N,...y,children:h})})})}exports.ApiForm=u,exports.useApiFormContext=l; |
+134
-158
| "use client"; | ||
| import { jsx as g } from "react/jsx-runtime"; | ||
| import { pathSchemas as $ } from "@devup-api/zod"; | ||
| import { zodResolver as _ } from "@hookform/resolvers/zod"; | ||
| import { useQuery as B, skipToken as G, useMutation as H } from "@tanstack/react-query"; | ||
| import { createContext as I, useContext as J, useRef as f, useCallback as P, useSyncExternalStore as N, useMemo as b, useEffect as E } from "react"; | ||
| import { useForm as O, FormProvider as U } from "react-hook-form"; | ||
| const h = I(null); | ||
| function W(u, i, o) { | ||
| for (const a of o) { | ||
| const s = a.split("."); | ||
| let c = u, r = i; | ||
| for (const p of s) | ||
| c = c?.[p], r = r?.[p]; | ||
| if (!Object.is(c, r)) | ||
| return !0; | ||
| } | ||
| return !1; | ||
| "use client"; | ||
| import { FormProvider as e, useForm as t } from "react-hook-form"; | ||
| import { pathSchemas as n } from "@devup-api/zod"; | ||
| import { zodResolver as r } from "@hookform/resolvers/zod"; | ||
| import { skipToken as i, useMutation as a, useQuery as o } from "@tanstack/react-query"; | ||
| import { createContext as s, useCallback as c, useContext as l, useEffect as u, useMemo as d, useRef as f, useSyncExternalStore as p } from "react"; | ||
| import { jsx as m } from "react/jsx-runtime"; | ||
| //#region src/api-form.tsx | ||
| var h = s(null); | ||
| function g(e, t, n) { | ||
| for (let r of n) { | ||
| let n = r.split("."), i = e, a = t; | ||
| for (let e of n) i = i?.[e], a = a?.[e]; | ||
| if (!Object.is(i, a)) return !0; | ||
| } | ||
| return !1; | ||
| } | ||
| function k(u, i, o = "") { | ||
| return new Proxy(u, { | ||
| get(a, s) { | ||
| if (typeof s == "symbol") | ||
| return Reflect.get(a, s); | ||
| const c = o ? `${o}.${s}` : s, r = Reflect.get(a, s); | ||
| return r === null || typeof r != "object" || typeof r == "function" ? (i.add(c), r) : k(r, i, c); | ||
| } | ||
| }); | ||
| function _(e, t, n = "") { | ||
| return new Proxy(e, { get(e, r) { | ||
| if (typeof r == "symbol") return Reflect.get(e, r); | ||
| let i = n ? `${n}.${r}` : r, a = Reflect.get(e, r); | ||
| return typeof a != "object" || !a || typeof a == "function" ? (t.add(i), a) : _(a, t, i); | ||
| } }); | ||
| } | ||
| function tr() { | ||
| const u = J(h); | ||
| if (!u) | ||
| throw new Error("useApiFormContext must be used within an ApiForm"); | ||
| const i = f(/* @__PURE__ */ new Set()), o = f( | ||
| null | ||
| ), a = f( | ||
| null | ||
| ), s = P(() => { | ||
| const r = u.getState(); | ||
| return !o.current || !a.current || i.current.size > 0 && W( | ||
| o.current, | ||
| r, | ||
| i.current | ||
| ) ? (o.current = r, a.current = r, r) : a.current; | ||
| }, [u]), c = N(u.subscribe, s, s); | ||
| return b( | ||
| () => k(c, i.current), | ||
| [c] | ||
| ); | ||
| function v() { | ||
| let e = l(h); | ||
| if (!e) throw Error("useApiFormContext must be used within an ApiForm"); | ||
| let t = f(/* @__PURE__ */ new Set()), n = f(null), r = f(null), i = c(() => { | ||
| let i = e.getState(); | ||
| return !n.current || !r.current || t.current.size > 0 && g(n.current, i, t.current) ? (n.current = i, r.current = i, i) : r.current; | ||
| }, [e]), a = p(e.subscribe, i, i); | ||
| return d(() => _(a, t.current), [a]); | ||
| } | ||
| function er({ | ||
| api: u, | ||
| method: i, | ||
| path: o, | ||
| openapi: a, | ||
| requestOptions: s, | ||
| onSuccess: c, | ||
| onError: r, | ||
| onValidationError: p, | ||
| children: C, | ||
| defaultValues: F, | ||
| mode: L = "onSubmit", | ||
| formOptions: M, | ||
| formProps: j, | ||
| resetOnSuccess: z = !1, | ||
| queryClient: y, | ||
| fetchDefaultValues: n | ||
| }) { | ||
| const S = f(/* @__PURE__ */ new Set()), x = f(null), R = f(null), v = f(!1), w = P(() => { | ||
| for (const t of S.current) | ||
| t(); | ||
| }, []), m = B({ | ||
| queryKey: n ? [ | ||
| "apiFormDefaultValues", | ||
| n.method ?? "get", | ||
| n.path, | ||
| n.options | ||
| ] : ["apiFormDefaultValues", "disabled"], | ||
| queryFn: n ? async () => { | ||
| const t = n.method ?? "get", e = await u[t]( | ||
| n.path, | ||
| n.options | ||
| ); | ||
| if (e.error) | ||
| throw e.error; | ||
| return n.transform ? n.transform(e.data) : e.data; | ||
| } : G, | ||
| enabled: !!n, | ||
| ...y && { queryClient: y } | ||
| }), K = b(() => n && m.data ? m.data : F, [n, m.data, F]), A = $?.[i]?.[o], l = O({ | ||
| // biome-ignore lint/suspicious/noExplicitAny: Complex generic type inference | ||
| defaultValues: K, | ||
| mode: L, | ||
| // biome-ignore lint/suspicious/noExplicitAny: Zod v3/v4 compatibility | ||
| resolver: A ? _(A) : void 0, | ||
| ...M | ||
| }), d = H({ | ||
| mutationKey: [i, o, s], | ||
| mutationFn: async (t) => { | ||
| const e = await u[i](o, { | ||
| ...s, | ||
| body: t | ||
| }); | ||
| if (e.error) | ||
| throw e.error; | ||
| return e.data; | ||
| }, | ||
| onSuccess: (t) => { | ||
| c?.(t), z && l.reset(); | ||
| }, | ||
| onError: (t) => { | ||
| r?.(t); | ||
| }, | ||
| ...y && { queryClient: y } | ||
| }); | ||
| x.current = l, R.current = d, v.current = m.isLoading, E(() => { | ||
| n && m.data && l.reset(m.data); | ||
| }, [n, m.data, l]), E(() => { | ||
| w(); | ||
| }, [ | ||
| d.isPending, | ||
| d.isSuccess, | ||
| d.isError, | ||
| d.error, | ||
| d.data, | ||
| m.isLoading, | ||
| w | ||
| ]); | ||
| const Q = l.handleSubmit( | ||
| (t) => { | ||
| d.mutate(t); | ||
| }, | ||
| (t) => { | ||
| p?.(t); | ||
| } | ||
| ), T = b(() => ({ | ||
| getState: () => { | ||
| const t = x.current, e = R.current; | ||
| return { | ||
| form: t, | ||
| mutation: { | ||
| isPending: e.isPending, | ||
| isSuccess: e.isSuccess, | ||
| isError: e.isError, | ||
| error: e.error, | ||
| data: e.data, | ||
| mutate: e.mutate, | ||
| mutateAsync: e.mutateAsync, | ||
| reset: e.reset | ||
| }, | ||
| isLoadingDefaultValues: v.current | ||
| }; | ||
| }, | ||
| subscribe: (t) => (S.current.add(t), () => { | ||
| S.current.delete(t); | ||
| }) | ||
| }), []); | ||
| return /* @__PURE__ */ g(h.Provider, { value: T, children: /* @__PURE__ */ g(U, { ...l, children: /* @__PURE__ */ g("form", { onSubmit: Q, ...j, children: C }) }) }); | ||
| function y({ api: s, method: l, path: p, openapi: g, requestOptions: _, onSuccess: v, onError: y, onValidationError: b, children: x, defaultValues: S, mode: C = "onSubmit", formOptions: w, formProps: T, resetOnSuccess: E = !1, queryClient: D, fetchDefaultValues: O }) { | ||
| let k = f(/* @__PURE__ */ new Set()), A = f(null), j = f(null), M = f(!1), N = c(() => { | ||
| for (let e of k.current) e(); | ||
| }, []), P = o({ | ||
| queryKey: O ? [ | ||
| "apiFormDefaultValues", | ||
| O.method ?? "get", | ||
| O.path, | ||
| O.options | ||
| ] : ["apiFormDefaultValues", "disabled"], | ||
| queryFn: O ? async () => { | ||
| let e = await s[O.method ?? "get"](O.path, O.options); | ||
| if (e.error) throw e.error; | ||
| return O.transform ? O.transform(e.data) : e.data; | ||
| } : i, | ||
| enabled: !!O, | ||
| ...D && { queryClient: D } | ||
| }), F = d(() => O && P.data ? P.data : S, [ | ||
| O, | ||
| P.data, | ||
| S | ||
| ]), I = n?.[l]?.[p], L = t({ | ||
| defaultValues: F, | ||
| mode: C, | ||
| resolver: I ? r(I) : void 0, | ||
| ...w | ||
| }), R = a({ | ||
| mutationKey: [ | ||
| l, | ||
| p, | ||
| _ | ||
| ], | ||
| mutationFn: async (e) => { | ||
| let t = await s[l](p, { | ||
| ..._, | ||
| body: e | ||
| }); | ||
| if (t.error) throw t.error; | ||
| return t.data; | ||
| }, | ||
| onSuccess: (e) => { | ||
| v?.(e), E && L.reset(); | ||
| }, | ||
| onError: (e) => { | ||
| y?.(e); | ||
| }, | ||
| ...D && { queryClient: D } | ||
| }); | ||
| A.current = L, j.current = R, M.current = P.isLoading, u(() => { | ||
| O && P.data && L.reset(P.data); | ||
| }, [ | ||
| O, | ||
| P.data, | ||
| L | ||
| ]), u(() => { | ||
| N(); | ||
| }, [ | ||
| R.isPending, | ||
| R.isSuccess, | ||
| R.isError, | ||
| R.error, | ||
| R.data, | ||
| P.isLoading, | ||
| N | ||
| ]); | ||
| let z = L.handleSubmit((e) => { | ||
| R.mutate(e); | ||
| }, (e) => { | ||
| b?.(e); | ||
| }), B = d(() => ({ | ||
| getState: () => { | ||
| let e = A.current, t = j.current; | ||
| return { | ||
| form: e, | ||
| mutation: { | ||
| isPending: t.isPending, | ||
| isSuccess: t.isSuccess, | ||
| isError: t.isError, | ||
| error: t.error, | ||
| data: t.data, | ||
| mutate: t.mutate, | ||
| mutateAsync: t.mutateAsync, | ||
| reset: t.reset | ||
| }, | ||
| isLoadingDefaultValues: M.current | ||
| }; | ||
| }, | ||
| subscribe: (e) => (k.current.add(e), () => { | ||
| k.current.delete(e); | ||
| }) | ||
| }), []); | ||
| return /* @__PURE__ */ m(h.Provider, { | ||
| value: B, | ||
| children: /* @__PURE__ */ m(e, { | ||
| ...L, | ||
| children: /* @__PURE__ */ m("form", { | ||
| onSubmit: z, | ||
| ...T, | ||
| children: x | ||
| }) | ||
| }) | ||
| }); | ||
| } | ||
| export { | ||
| er as ApiForm, | ||
| tr as useApiFormContext | ||
| }; | ||
| //#endregion | ||
| export { y as ApiForm, v as useApiFormContext }; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react-hook-form"),r=require("./api-form.cjs");Object.defineProperty(exports,"Controller",{enumerable:!0,get:()=>e.Controller});Object.defineProperty(exports,"useController",{enumerable:!0,get:()=>e.useController});Object.defineProperty(exports,"useFieldArray",{enumerable:!0,get:()=>e.useFieldArray});Object.defineProperty(exports,"useFormContext",{enumerable:!0,get:()=>e.useFormContext});Object.defineProperty(exports,"useWatch",{enumerable:!0,get:()=>e.useWatch});exports.ApiForm=r.ApiForm;exports.useApiFormContext=r.useApiFormContext; | ||
| Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./api-form.cjs`);let t=require(`react-hook-form`);exports.ApiForm=e.ApiForm,Object.defineProperty(exports,`Controller`,{enumerable:!0,get:function(){return t.Controller}}),exports.useApiFormContext=e.useApiFormContext,Object.defineProperty(exports,`useController`,{enumerable:!0,get:function(){return t.useController}}),Object.defineProperty(exports,`useFieldArray`,{enumerable:!0,get:function(){return t.useFieldArray}}),Object.defineProperty(exports,`useFormContext`,{enumerable:!0,get:function(){return t.useFormContext}}),Object.defineProperty(exports,`useWatch`,{enumerable:!0,get:function(){return t.useWatch}}); |
+3
-11
@@ -1,11 +0,3 @@ | ||
| import { Controller as e, useController as t, useFieldArray as l, useFormContext as m, useWatch as s } from "react-hook-form"; | ||
| import { ApiForm as n, useApiFormContext as p } from "./api-form.js"; | ||
| export { | ||
| n as ApiForm, | ||
| e as Controller, | ||
| p as useApiFormContext, | ||
| t as useController, | ||
| l as useFieldArray, | ||
| m as useFormContext, | ||
| s as useWatch | ||
| }; | ||
| import { ApiForm as e, useApiFormContext as t } from "./api-form.js"; | ||
| import { Controller as n, useController as r, useFieldArray as i, useFormContext as a, useWatch as o } from "react-hook-form"; | ||
| export { e as ApiForm, n as Controller, t as useApiFormContext, r as useController, i as useFieldArray, a as useFormContext, o as useWatch }; |
+9
-9
| { | ||
| "name": "@devup-api/hookform", | ||
| "version": "0.1.2", | ||
| "version": "0.1.3", | ||
| "license": "Apache-2.0", | ||
@@ -25,6 +25,6 @@ "type": "module", | ||
| "dependencies": { | ||
| "@devup-api/fetch": "^0.1.19", | ||
| "@devup-api/zod": "^0.1.2", | ||
| "@devup-api/fetch": "^0.1.20", | ||
| "@devup-api/zod": "^0.1.3", | ||
| "@hookform/resolvers": ">=5.2.2", | ||
| "react-hook-form": ">=7.71.1" | ||
| "react-hook-form": ">=7.72.1" | ||
| }, | ||
@@ -38,16 +38,16 @@ "peerDependencies": { | ||
| "devDependencies": { | ||
| "@tanstack/react-query": "^5.90", | ||
| "@tanstack/react-query": "^5.96", | ||
| "@testing-library/react": "^16.3", | ||
| "@types/node": "^25.2", | ||
| "@types/node": "^25.5", | ||
| "@types/react": "^19.2", | ||
| "bun-test-env-dom": "^1.0", | ||
| "happy-dom": "^20.6", | ||
| "happy-dom": "^20.8", | ||
| "react": "^19.2", | ||
| "react-dom": "^19.2", | ||
| "typescript": "^5.9", | ||
| "typescript": "^6.0", | ||
| "zod": "^4.3", | ||
| "rollup-plugin-preserve-directives": "^0.4", | ||
| "vite": "^7.3", | ||
| "vite": "^8.0", | ||
| "vite-plugin-dts": "^4.5" | ||
| } | ||
| } |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
23348
-2.03%398
-10.56%Updated
Updated
Updated