@firtoz/router-toolkit 8.0.1 → 9.0.1
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 +127 -71
- package/dist/{chunk-HX57TC2S.js → chunk-F4324Q33.js} +2 -2
- package/dist/chunk-F4324Q33.js.map +1 -0
- package/dist/chunk-SBJFTOWW.js +3 -0
- package/dist/{chunk-2RLEUOSR.js.map → chunk-SBJFTOWW.js.map} +1 -1
- package/dist/{chunk-5MOCOBGV.js → chunk-UF5QHE5K.js} +2 -2
- package/dist/chunk-UF5QHE5K.js.map +1 -0
- package/dist/chunk-XMGRKSHM.js +183 -0
- package/dist/chunk-XMGRKSHM.js.map +1 -0
- package/dist/formAction.d.ts +41 -15
- package/dist/formAction.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +1 -1
- package/dist/useDynamicFetcher.d.ts +18 -8
- package/dist/useDynamicFetcher.js +1 -1
- package/dist/useDynamicSubmitter.d.ts +300 -133
- package/dist/useDynamicSubmitter.js +1 -1
- package/package.json +4 -4
- package/src/formAction.ts +41 -15
- package/src/types/index.ts +0 -1
- package/src/useDynamicFetcher.ts +18 -8
- package/src/useDynamicSubmitter.tsx +323 -101
- package/dist/chunk-2RLEUOSR.js +0 -3
- package/dist/chunk-5MOCOBGV.js.map +0 -1
- package/dist/chunk-HX57TC2S.js.map +0 -1
- package/dist/chunk-JJN6GBJL.js +0 -55
- package/dist/chunk-JJN6GBJL.js.map +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { useMemo, useRef, useCallback, useEffect } from 'react';
|
|
2
|
+
import { href, useFetcher } from 'react-router';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
// src/useDynamicSubmitter.tsx
|
|
6
|
+
var SubmitterSupersededError = class extends Error {
|
|
7
|
+
constructor(message = "This submission was superseded by a newer submit before it completed.") {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "SubmitterSupersededError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var SubmitterUnmountedError = class extends Error {
|
|
13
|
+
constructor(message = "The submitter was unmounted before this submission completed.") {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "SubmitterUnmountedError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function dynamicSubmitterFetcherKey(resolvedHref, keySuffix) {
|
|
19
|
+
const base = `submitter-${resolvedHref}`;
|
|
20
|
+
if (keySuffix === void 0 || keySuffix === "") {
|
|
21
|
+
return base;
|
|
22
|
+
}
|
|
23
|
+
return `${base}::${encodeURIComponent(keySuffix)}`;
|
|
24
|
+
}
|
|
25
|
+
function isSubmitterOptions(x) {
|
|
26
|
+
if (x === null || typeof x !== "object") return false;
|
|
27
|
+
const keys = Object.keys(x);
|
|
28
|
+
if (keys.length === 0) return false;
|
|
29
|
+
return keys.every((k) => k === "keySuffix");
|
|
30
|
+
}
|
|
31
|
+
function parseUseDynamicSubmitterRestArgs(args) {
|
|
32
|
+
if (args.length === 0) {
|
|
33
|
+
return { hrefArgs: [], options: {} };
|
|
34
|
+
}
|
|
35
|
+
const last = args[args.length - 1];
|
|
36
|
+
if (args.length >= 2 && isSubmitterOptions(last)) {
|
|
37
|
+
return { hrefArgs: [...args.slice(0, -1)], options: last };
|
|
38
|
+
}
|
|
39
|
+
if (args.length === 1 && isSubmitterOptions(args[0])) {
|
|
40
|
+
return { hrefArgs: [], options: args[0] };
|
|
41
|
+
}
|
|
42
|
+
return { hrefArgs: [...args], options: {} };
|
|
43
|
+
}
|
|
44
|
+
var submitterKeyBuckets = /* @__PURE__ */ new Map();
|
|
45
|
+
function getSubmitterKeyBucket(key) {
|
|
46
|
+
let b = submitterKeyBuckets.get(key);
|
|
47
|
+
if (!b) {
|
|
48
|
+
b = { submitGen: 0, pending: null };
|
|
49
|
+
submitterKeyBuckets.set(key, b);
|
|
50
|
+
}
|
|
51
|
+
return b;
|
|
52
|
+
}
|
|
53
|
+
var nextSubmitterOwnerId = 1;
|
|
54
|
+
function allocateSubmitterOwnerId() {
|
|
55
|
+
return nextSubmitterOwnerId++;
|
|
56
|
+
}
|
|
57
|
+
function useDynamicSubmitter(path, ...rest) {
|
|
58
|
+
const { hrefArgs, options } = parseUseDynamicSubmitterRestArgs(rest);
|
|
59
|
+
const keySuffix = options.keySuffix;
|
|
60
|
+
const url = useMemo(() => {
|
|
61
|
+
return href(path, ...hrefArgs);
|
|
62
|
+
}, [path, keySuffix, ...hrefArgs]);
|
|
63
|
+
const fetcherKey = useMemo(
|
|
64
|
+
() => dynamicSubmitterFetcherKey(url, keySuffix),
|
|
65
|
+
[url, keySuffix]
|
|
66
|
+
);
|
|
67
|
+
const fetcher = useFetcher({
|
|
68
|
+
key: fetcherKey
|
|
69
|
+
});
|
|
70
|
+
const fetcherRef = useRef(fetcher);
|
|
71
|
+
fetcherRef.current = fetcher;
|
|
72
|
+
const ownerIdRef = useRef(allocateSubmitterOwnerId());
|
|
73
|
+
const prevStateRef = useRef(fetcher.state);
|
|
74
|
+
const beginSubmit = useCallback(
|
|
75
|
+
(runSubmit) => {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
const bucket = getSubmitterKeyBucket(fetcherKey);
|
|
78
|
+
const prevPending = bucket.pending;
|
|
79
|
+
if (prevPending) {
|
|
80
|
+
prevPending.reject(new SubmitterSupersededError());
|
|
81
|
+
}
|
|
82
|
+
bucket.submitGen += 1;
|
|
83
|
+
const gen = bucket.submitGen;
|
|
84
|
+
bucket.pending = {
|
|
85
|
+
gen,
|
|
86
|
+
ownerId: ownerIdRef.current,
|
|
87
|
+
reject,
|
|
88
|
+
finishIdle: (data, error) => {
|
|
89
|
+
if (data !== void 0) {
|
|
90
|
+
resolve(data);
|
|
91
|
+
} else {
|
|
92
|
+
reject(error ?? new Error("Submission failed"));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
runSubmit();
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
[fetcherKey]
|
|
100
|
+
);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
return () => {
|
|
103
|
+
const bucket = getSubmitterKeyBucket(fetcherKey);
|
|
104
|
+
const pending = bucket.pending;
|
|
105
|
+
if (pending && pending.ownerId === ownerIdRef.current) {
|
|
106
|
+
bucket.pending = null;
|
|
107
|
+
pending.reject(new SubmitterUnmountedError());
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}, [fetcherKey]);
|
|
111
|
+
const fetcherError = fetcher.error;
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
const prev = prevStateRef.current;
|
|
114
|
+
prevStateRef.current = fetcher.state;
|
|
115
|
+
const wasWorking = prev === "submitting" || prev === "loading";
|
|
116
|
+
if (!wasWorking || fetcher.state !== "idle") {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const bucket = getSubmitterKeyBucket(fetcherKey);
|
|
120
|
+
const p = bucket.pending;
|
|
121
|
+
if (!p || p.gen !== bucket.submitGen) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
bucket.pending = null;
|
|
125
|
+
p.finishIdle(fetcher.data, fetcherError);
|
|
126
|
+
}, [fetcherKey, fetcher.state, fetcher.data, fetcherError]);
|
|
127
|
+
const submit = useCallback(
|
|
128
|
+
(target, options2) => {
|
|
129
|
+
return beginSubmit(() => {
|
|
130
|
+
const f = fetcherRef.current;
|
|
131
|
+
void f.submit(target, {
|
|
132
|
+
...options2,
|
|
133
|
+
method: options2?.method ?? "POST",
|
|
134
|
+
action: url,
|
|
135
|
+
encType: "multipart/form-data"
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
[beginSubmit, url]
|
|
140
|
+
);
|
|
141
|
+
const submitJson = useCallback(
|
|
142
|
+
(data, options2 = {}) => {
|
|
143
|
+
return beginSubmit(() => {
|
|
144
|
+
const f = fetcherRef.current;
|
|
145
|
+
void f.submit(
|
|
146
|
+
data,
|
|
147
|
+
{
|
|
148
|
+
...options2,
|
|
149
|
+
method: options2.method ?? "POST",
|
|
150
|
+
action: url,
|
|
151
|
+
encType: "application/json"
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
[beginSubmit, url]
|
|
157
|
+
);
|
|
158
|
+
const fetcherFormRef = useRef(fetcher.Form);
|
|
159
|
+
fetcherFormRef.current = fetcher.Form;
|
|
160
|
+
const Form = useCallback(
|
|
161
|
+
({ method = "POST", ...props }) => {
|
|
162
|
+
const OriginalForm = fetcherFormRef.current;
|
|
163
|
+
return /* @__PURE__ */ jsx(OriginalForm, { action: url, method, ...props });
|
|
164
|
+
},
|
|
165
|
+
[url]
|
|
166
|
+
);
|
|
167
|
+
return useMemo(
|
|
168
|
+
() => ({
|
|
169
|
+
submit,
|
|
170
|
+
submitJson,
|
|
171
|
+
Form,
|
|
172
|
+
fetcherKey
|
|
173
|
+
}),
|
|
174
|
+
[submit, submitJson, Form, fetcherKey]
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
function useDynamicSubmitterFetcher(submitter) {
|
|
178
|
+
return useFetcher({ key: submitter.fetcherKey });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export { SubmitterSupersededError, SubmitterUnmountedError, dynamicSubmitterFetcherKey, useDynamicSubmitter, useDynamicSubmitterFetcher };
|
|
182
|
+
//# sourceMappingURL=chunk-XMGRKSHM.js.map
|
|
183
|
+
//# sourceMappingURL=chunk-XMGRKSHM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useDynamicSubmitter.tsx"],"names":["options"],"mappings":";;;;;AAoIO,IAAM,wBAAA,GAAN,cAAuC,KAAA,CAAM;AAAA,EAEnD,WAAA,CACC,UAAU,uEAAA,EACT;AACD,IAAA,KAAA,CAAM,OAAO,CAAA;AAJd,IAAA,IAAA,CAAkB,IAA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,GAAG;AAAA,GACL;AAEA,EAAA,OAAO,OAAA;AAAA,IACN,OAAO;AAAA,MACN,MAAA;AAAA,MACA,UAAA;AAAA,MACA,IAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,IACA,CAAC,MAAA,EAAQ,UAAA,EAAY,IAAA,EAAM,UAAU;AAAA,GACtC;AACD;AAQO,SAAS,2BACf,SAAA,EACC;AACD,EAAA,OAAO,UAAA,CAA4B,EAAE,GAAA,EAAK,SAAA,CAAU,YAAY,CAAA;AACjE","file":"chunk-XMGRKSHM.js","sourcesContent":["/**\n * @fileoverview Type-safe dynamic form submission hook for React Router 7\n *\n * This module provides a hook that creates a type-safe fetcher for submitting forms\n * to dynamic routes with full TypeScript inference for the form schema and route params.\n *\n * **Awaiting results:** `submit` and `submitJson` return a `Promise` that resolves with the\n * action payload after the submission completes (fetcher leaves `submitting` / `loading` for\n * `idle`). The hook does **not** expose `data` or `state`—use the promise result (and local\n * React state) for outcomes and loading UX. For declarative UI tied to the same fetcher, use\n * {@link useDynamicSubmitterFetcher} (or {@link dynamicSubmitterFetcherKey} with `useFetcher`).\n * Use **one awaited submit at\n * a time** per hook instance; React Router’s\n * single fetcher replaces an in-flight request when you submit again. If you call `submit` or\n * `submitJson` again before the previous promise settles, the previous promise is **rejected**\n * with {@link SubmitterSupersededError}. That applies **per React Router fetcher key** (same\n * resolved URL and {@link UseDynamicSubmitterOptions.keySuffix}): two hook instances that share\n * the same key also supersede one another’s in-flight promise. Use distinct `keySuffix` values\n * when you need independent overlapping submissions to the same route. For many overlapping\n * operations, use `ConcurrentSubmitterProvider` / `useConcurrentSubmitter` instead.\n *\n * **Unmount:** If the component unmounts while a returned promise is still pending, that\n * promise is **rejected** with {@link SubmitterUnmountedError}.\n *\n * **Local `useState` vs {@link useDynamicSubmitterFetcher}:** For programmatic\n * `await submitter.submitJson`, a local pending flag and `try` / `finally` is often enough for\n * disabled buttons and matches the promise-first API. Use {@link useDynamicSubmitterFetcher} when\n * you want declarative `fetcher.state` / `fetcher.data` in JSX (especially with `submitter.Form`\n * or inline errors). The package README documents trade-offs in more detail.\n *\n * @example\n * ### Route Setup (`app/routes/admin.posts.$id.tsx`)\n *\n * First, set up your route with the required exports:\n *\n * ```typescript\n * import { z } from \"zod\";\n * import { formAction, type RoutePath } from \"@firtoz/router-toolkit\";\n * import { success, fail } from \"@firtoz/maybe-error\";\n *\n * // Export the route path for type inference\n * export const route: RoutePath<\"/admin/posts/:id\"> = \"/admin/posts/:id\";\n *\n * // Define the form schema\n * export const formSchema = z.object({\n * title: z.string().min(1, \"Title is required\"),\n * content: z.string().min(10, \"Content must be at least 10 characters\"),\n * published: z.boolean().optional().default(false),\n * });\n *\n * // Create the action using formAction\n * export const action = formAction({\n * schema: formSchema,\n * handler: async ({ request, params }, formData) => {\n * const postId = params.id;\n * const updated = await db.posts.update({\n * where: { id: postId },\n * data: formData,\n * });\n * return success(updated);\n * },\n * });\n * ```\n *\n * @example\n * ### Using the hook in a component\n *\n * ```tsx\n * import { useDynamicSubmitter } from \"@firtoz/router-toolkit\";\n *\n * function EditPostForm({ postId }: { postId: string }) {\n * // Type-safe submitter with full inference\n * const submitter = useDynamicSubmitter<typeof import(\"./admin.posts.$id\")>(\n * \"/admin/posts/:id\",\n * { id: postId }\n * );\n *\n * const [pending, setPending] = useState(false);\n *\n * // Option 1: Submit as JSON (recommended for programmatic submissions)\n * // Defaults to POST if no options provided\n * const handleSubmitJson = async () => {\n * setPending(true);\n * try {\n * const data = await submitter.submitJson({\n * title: \"My Post\",\n * content: \"Post content here\",\n * published: true,\n * });\n * if (data.success) {\n * console.log(\"Saved\");\n * }\n * } finally {\n * setPending(false);\n * }\n * };\n *\n * // Option 2: Submit with FormData or SubmitTarget\n * const handleSubmit = async (formData: FormData) => {\n * await submitter.submit(formData, { method: \"POST\" });\n * };\n *\n * // Option 3: Use the Form component (defaults to POST); pair with useDynamicSubmitterFetcher(submitter) if you need reactive state\n * return (\n * <submitter.Form>\n * <input name=\"title\" />\n * <textarea name=\"content\" />\n * <button type=\"submit\" disabled={pending}>Save</button>\n * </submitter.Form>\n * );\n * }\n * ```\n */\n\n// biome-ignore lint/style/useImportType: We need to import React here.\nimport React, { useCallback, useEffect, useMemo, useRef } from \"react\";\nimport {\n\ttype FetcherFormProps,\n\ttype HTMLFormMethod,\n\thref,\n\ttype SubmitOptions,\n\ttype SubmitTarget,\n\tuseFetcher,\n} from \"react-router\";\nimport type { z } from \"zod\";\nimport type { HrefArgs } from \"./types/HrefArgs\";\nimport type { RouteWithActionModule } from \"./types/RouteWithActionModule\";\n\n/**\n * Thrown when a new `submit` or `submitJson` runs before a prior returned promise has settled.\n * The new submission proceeds; catch this error if overlapping calls are expected.\n */\nexport class SubmitterSupersededError extends Error {\n\toverride readonly name = \"SubmitterSupersededError\";\n\tconstructor(\n\t\tmessage = \"This submission was superseded by a newer submit before it completed.\",\n\t) {\n\t\tsuper(message);\n\t}\n}\n\n/**\n * Thrown when the component that owns the submitter unmounts before a `submit` /\n * `submitJson` promise has settled.\n */\nexport class SubmitterUnmountedError extends Error {\n\toverride readonly name = \"SubmitterUnmountedError\";\n\tconstructor(\n\t\tmessage = \"The submitter was unmounted before this submission completed.\",\n\t) {\n\t\tsuper(message);\n\t}\n}\n\n/**\n * Action payload type on the fetcher (same shape React Router puts on `fetcher.data` after the action runs).\n * Includes `undefined` while idle or in flight—use {@link SubmitterSettledData} for the value after\n * `await submitter.submit` / `await submitter.submitJson`.\n */\nexport type DynamicSubmitterData<TInfo extends RouteWithActionModule> =\n\tReturnType<typeof useFetcher<TInfo[\"action\"]>>[\"data\"];\n\n/**\n * Payload type after a successful `await submitter.submit` / `await submitter.submitJson`.\n * Omits `undefined` from {@link DynamicSubmitterData}: the promise only resolves when `fetcher.data`\n * is defined (otherwise it rejects). Inner success values may still be void / optional `result` for\n * `MaybeError<undefined>` from `formAction` + `success()`.\n */\nexport type SubmitterSettledData<TInfo extends RouteWithActionModule> =\n\tNonNullable<DynamicSubmitterData<TInfo>>;\n\n/**\n * Options for {@link useDynamicSubmitter}.\n */\nexport type UseDynamicSubmitterOptions = {\n\t/**\n\t * Appended to the default fetcher key so multiple submitters can target the same resolved URL\n\t * without sharing React Router fetcher state. Omit to use the default key for that URL.\n\t */\n\tkeySuffix?: string;\n};\n\n/**\n * React Router `useFetcher` key used by {@link useDynamicSubmitter} for a resolved href.\n * Pass the same string as {@link UseDynamicSubmitterResult.fetcherKey} (or call with the same\n * `resolvedHref` and `keySuffix` as the submitter) so a parallel `useFetcher({ key })` observes\n * the same submission lifecycle.\n *\n * When `keySuffix` is set, it is encoded and joined with a fixed delimiter so arbitrary strings\n * are safe in the key.\n */\nexport function dynamicSubmitterFetcherKey(\n\tresolvedHref: string,\n\tkeySuffix?: string,\n): string {\n\tconst base = `submitter-${resolvedHref}`;\n\tif (keySuffix === undefined || keySuffix === \"\") {\n\t\treturn base;\n\t}\n\treturn `${base}::${encodeURIComponent(keySuffix)}`;\n}\n\nfunction isSubmitterOptions(x: unknown): x is UseDynamicSubmitterOptions {\n\tif (x === null || typeof x !== \"object\") return false;\n\tconst keys = Object.keys(x as object);\n\tif (keys.length === 0) return false;\n\treturn keys.every((k) => k === \"keySuffix\");\n}\n\nfunction parseUseDynamicSubmitterRestArgs(args: readonly unknown[]): {\n\threfArgs: unknown[];\n\toptions: UseDynamicSubmitterOptions;\n} {\n\tif (args.length === 0) {\n\t\treturn { hrefArgs: [], options: {} };\n\t}\n\tconst last = args[args.length - 1];\n\tif (args.length >= 2 && isSubmitterOptions(last)) {\n\t\treturn { hrefArgs: [...args.slice(0, -1)], options: last };\n\t}\n\tif (args.length === 1 && isSubmitterOptions(args[0])) {\n\t\treturn { hrefArgs: [], options: args[0] };\n\t}\n\treturn { hrefArgs: [...args], options: {} };\n}\n\ntype UseDynamicSubmitterRest<R extends RouteWithActionModule[\"route\"]> =\n\tHrefArgs<R> extends readonly []\n\t\t? [options?: UseDynamicSubmitterOptions]\n\t\t: [...hrefArgs: HrefArgs<R>, options?: UseDynamicSubmitterOptions];\n\ntype PendingAwait = {\n\tgen: number;\n\townerId: number;\n\treject: (reason: unknown) => void;\n\t/** Called when the shared fetcher reaches `idle` for this submission generation. */\n\tfinishIdle: (data: unknown, error: unknown | undefined) => void;\n};\n\ntype SubmitterKeyBucket = {\n\tsubmitGen: number;\n\tpending: PendingAwait | null;\n};\n\nconst submitterKeyBuckets = new Map<string, SubmitterKeyBucket>();\n\nfunction getSubmitterKeyBucket(key: string): SubmitterKeyBucket {\n\tlet b = submitterKeyBuckets.get(key);\n\tif (!b) {\n\t\tb = { submitGen: 0, pending: null };\n\t\tsubmitterKeyBuckets.set(key, b);\n\t}\n\treturn b;\n}\n\nlet nextSubmitterOwnerId = 1;\nfunction allocateSubmitterOwnerId(): number {\n\treturn nextSubmitterOwnerId++;\n}\n\n/**\n * Function type for submitting form data with a SubmitTarget.\n *\n * Accepts the form schema data combined with SubmitTarget (FormData, HTMLFormElement, etc.)\n * Use this when you have a FormData object or form element reference.\n *\n * @example\n * ```typescript\n * // With FormData\n * submitter.submit(formData, { method: \"POST\" });\n *\n * // With form element reference\n * submitter.submit(formRef.current, { method: \"POST\" });\n * ```\n */\ntype SubmitFunc<TModule extends RouteWithActionModule> = (\n\ttarget: z.infer<TModule[\"formSchema\"]> & SubmitTarget,\n\toptions: Omit<SubmitOptions, \"action\" | \"method\" | \"encType\"> & {\n\t\tmethod: Exclude<SubmitOptions[\"method\"], \"GET\">;\n\t},\n) => Promise<SubmitterSettledData<TModule>>;\n\n/**\n * Options for submitJson function.\n * Method defaults to \"POST\" if not specified.\n */\ntype SubmitJsonOptions = Omit<\n\tSubmitOptions,\n\t\"action\" | \"method\" | \"encType\"\n> & {\n\tmethod?: Exclude<SubmitOptions[\"method\"], \"GET\">;\n};\n\n/**\n * Function type for submitting form data as JSON.\n *\n * Accepts only the inferred form schema type (plain object).\n * Automatically serializes the data as JSON. This is the recommended\n * approach for programmatic form submissions.\n *\n * Options are optional and default to `{ method: \"POST\" }`.\n *\n * @example\n * ```typescript\n * // Submit a plain object - fully type-safe (defaults to POST)\n * await submitter.submitJson({\n * email: \"user@example.com\",\n * password: \"secret123\",\n * rememberMe: true,\n * });\n *\n * // Or specify a different method\n * await submitter.submitJson(data, { method: \"PUT\" });\n * ```\n */\ntype SubmitJsonFunc<TModule extends RouteWithActionModule> = (\n\tdata: z.infer<TModule[\"formSchema\"]>,\n\toptions?: SubmitJsonOptions,\n) => Promise<SubmitterSettledData<TModule>>;\n\n/**\n * Form component type with pre-bound action URL.\n *\n * Renders a form element that automatically submits to the correct route.\n * Method defaults to \"POST\" if not specified.\n *\n * @example\n * ```typescript\n * // Defaults to POST\n * <submitter.Form>\n * <input name=\"title\" />\n * <button type=\"submit\">Submit</button>\n * </submitter.Form>\n *\n * // Or specify a different method\n * <submitter.Form method=\"PUT\">\n * ...\n * </submitter.Form>\n * ```\n */\ntype SubmitForm = (\n\tprops: Omit<\n\t\tFetcherFormProps & React.RefAttributes<HTMLFormElement>,\n\t\t\"action\" | \"method\"\n\t> & {\n\t\tmethod?: Exclude<SubmitOptions[\"method\"], \"GET\">;\n\t},\n) => React.ReactElement;\n\n/**\n * Stable object returned by {@link useDynamicSubmitter}: `submit`, `submitJson`, `Form`, and\n * `fetcherKey`. The reference is memoized and does not change when the internal fetcher’s\n * `state` / `data` update.\n */\nexport type UseDynamicSubmitterResult<TInfo extends RouteWithActionModule> = {\n\tsubmit: SubmitFunc<TInfo>;\n\tsubmitJson: SubmitJsonFunc<TInfo>;\n\tForm: SubmitForm;\n\t/** Pass to {@link useDynamicSubmitterFetcher} or `useFetcher({ key })` for reactive `state` / `data`. */\n\tfetcherKey: string;\n};\n\n/**\n * Creates a type-safe fetcher for submitting forms to dynamic routes.\n *\n * This hook provides full TypeScript inference for:\n * - Route parameters (from the route path)\n * - Form data schema (from the route's formSchema export)\n * - Action response type (from the route's action export)\n *\n * @template TInfo - The route module type (use `typeof import(\"./route-file\")`)\n *\n * @param path - The route path (must match the route's `route` export)\n * @param rest - Route parameters (if any), then optional {@link UseDynamicSubmitterOptions}. For\n * static routes, you may pass only options as the second argument (e.g. `{ keySuffix: \"a\" }`).\n * Options are recognized only when the object contains exclusively the `keySuffix` key (do not use\n * a route param object whose only field is named `keySuffix` unless it is meant as options).\n *\n * @returns Stable `{ submit, submitJson, Form, fetcherKey }`. Await the promises for action results;\n * use {@link useDynamicSubmitterFetcher} or local state for reactive loading/data.\n *\n * @example\n * ### Basic usage with route parameters\n *\n * ```typescript\n * // In your route file (app/routes/users.$userId.settings.tsx):\n * export const route: RoutePath<\"/users/:userId/settings\"> = \"/users/:userId/settings\";\n * export const formSchema = z.object({\n * displayName: z.string().min(2),\n * email: z.string().email(),\n * notifications: z.boolean().default(true),\n * });\n * export const action = formAction({ schema: formSchema, handler: ... });\n *\n * // In your component:\n * const submitter = useDynamicSubmitter<typeof import(\"./users.$userId.settings\")>(\n * \"/users/:userId/settings\",\n * { userId: \"123\" }\n * );\n *\n * const data = await submitter.submitJson({\n * displayName: \"John Doe\",\n * email: \"john@example.com\",\n * notifications: true,\n * });\n *\n * if (data.success) {\n * console.log(\"Settings updated!\");\n * }\n * ```\n */\nexport function useDynamicSubmitter<TInfo extends RouteWithActionModule>(\n\tpath: TInfo[\"route\"],\n\t...rest: UseDynamicSubmitterRest<TInfo[\"route\"]>\n): UseDynamicSubmitterResult<TInfo> {\n\tconst { hrefArgs, options } = parseUseDynamicSubmitterRestArgs(rest);\n\tconst keySuffix = options.keySuffix;\n\n\tconst url = useMemo(() => {\n\t\t// biome-ignore lint/suspicious/noExplicitAny: Intentional\n\t\treturn href(path, ...(hrefArgs as any));\n\t}, [path, keySuffix, ...(hrefArgs as unknown[])]);\n\n\tconst fetcherKey = useMemo(\n\t\t() => dynamicSubmitterFetcherKey(url, keySuffix),\n\t\t[url, keySuffix],\n\t);\n\n\tconst fetcher = useFetcher<TInfo[\"action\"]>({\n\t\tkey: fetcherKey,\n\t});\n\n\tconst fetcherRef = useRef(fetcher);\n\tfetcherRef.current = fetcher;\n\n\tconst ownerIdRef = useRef(allocateSubmitterOwnerId());\n\tconst prevStateRef = useRef(fetcher.state);\n\n\tconst beginSubmit = useCallback(\n\t\t(runSubmit: () => void) => {\n\t\t\treturn new Promise<SubmitterSettledData<TInfo>>((resolve, reject) => {\n\t\t\t\tconst bucket = getSubmitterKeyBucket(fetcherKey);\n\t\t\t\tconst prevPending = bucket.pending;\n\t\t\t\tif (prevPending) {\n\t\t\t\t\tprevPending.reject(new SubmitterSupersededError());\n\t\t\t\t}\n\t\t\t\tbucket.submitGen += 1;\n\t\t\t\tconst gen = bucket.submitGen;\n\t\t\t\tbucket.pending = {\n\t\t\t\t\tgen,\n\t\t\t\t\townerId: ownerIdRef.current,\n\t\t\t\t\treject,\n\t\t\t\t\tfinishIdle: (data, error) => {\n\t\t\t\t\t\tif (data !== undefined) {\n\t\t\t\t\t\t\tresolve(data as SubmitterSettledData<TInfo>);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(error ?? new Error(\"Submission failed\"));\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t\trunSubmit();\n\t\t\t});\n\t\t},\n\t\t[fetcherKey],\n\t);\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tconst bucket = getSubmitterKeyBucket(fetcherKey);\n\t\t\tconst pending = bucket.pending;\n\t\t\tif (pending && pending.ownerId === ownerIdRef.current) {\n\t\t\t\tbucket.pending = null;\n\t\t\t\tpending.reject(new SubmitterUnmountedError());\n\t\t\t}\n\t\t};\n\t}, [fetcherKey]);\n\n\tconst fetcherError = (fetcher as { error?: unknown }).error;\n\n\tuseEffect(() => {\n\t\tconst prev = prevStateRef.current;\n\t\tprevStateRef.current = fetcher.state;\n\t\tconst wasWorking = prev === \"submitting\" || prev === \"loading\";\n\t\tif (!wasWorking || fetcher.state !== \"idle\") {\n\t\t\treturn;\n\t\t}\n\t\tconst bucket = getSubmitterKeyBucket(fetcherKey);\n\t\tconst p = bucket.pending;\n\t\tif (!p || p.gen !== bucket.submitGen) {\n\t\t\treturn;\n\t\t}\n\t\tbucket.pending = null;\n\t\tp.finishIdle(fetcher.data, fetcherError);\n\t}, [fetcherKey, fetcher.state, fetcher.data, fetcherError]);\n\n\tconst submit: SubmitFunc<TInfo> = useCallback(\n\t\t(target, options) => {\n\t\t\treturn beginSubmit(() => {\n\t\t\t\tconst f = fetcherRef.current;\n\t\t\t\tvoid f.submit(target, {\n\t\t\t\t\t...options,\n\t\t\t\t\tmethod: (options?.method ?? \"POST\") as HTMLFormMethod,\n\t\t\t\t\taction: url,\n\t\t\t\t\tencType: \"multipart/form-data\",\n\t\t\t\t} as Parameters<typeof f.submit>[1]);\n\t\t\t});\n\t\t},\n\t\t[beginSubmit, url],\n\t);\n\n\tconst submitJson: SubmitJsonFunc<TInfo> = useCallback(\n\t\t(data, options = {}) => {\n\t\t\treturn beginSubmit(() => {\n\t\t\t\tconst f = fetcherRef.current;\n\t\t\t\tvoid f.submit(\n\t\t\t\t\tdata as SubmitTarget,\n\t\t\t\t\t{\n\t\t\t\t\t\t...options,\n\t\t\t\t\t\tmethod: (options.method ?? \"POST\") as HTMLFormMethod,\n\t\t\t\t\t\taction: url,\n\t\t\t\t\t\tencType: \"application/json\",\n\t\t\t\t\t} as Parameters<typeof f.submit>[1],\n\t\t\t\t);\n\t\t\t});\n\t\t},\n\t\t[beginSubmit, url],\n\t);\n\n\tconst fetcherFormRef = useRef(fetcher.Form);\n\tfetcherFormRef.current = fetcher.Form;\n\n\tconst Form: SubmitForm = useCallback(\n\t\t({ method = \"POST\", ...props }) => {\n\t\t\tconst OriginalForm = fetcherFormRef.current;\n\t\t\treturn <OriginalForm action={url} method={method} {...props} />;\n\t\t},\n\t\t[url],\n\t);\n\n\treturn useMemo(\n\t\t() => ({\n\t\t\tsubmit,\n\t\t\tsubmitJson,\n\t\t\tForm,\n\t\t\tfetcherKey,\n\t\t}),\n\t\t[submit, submitJson, Form, fetcherKey],\n\t);\n}\n\n/**\n * React Router `useFetcher` bound to the same key as {@link useDynamicSubmitter}, so `state` /\n * `data` reflect the same submissions as `submitter.submit` / `submitter.Form`.\n *\n * Call at component top level next to `useDynamicSubmitter`.\n */\nexport function useDynamicSubmitterFetcher<TInfo extends RouteWithActionModule>(\n\tsubmitter: UseDynamicSubmitterResult<TInfo>,\n) {\n\treturn useFetcher<TInfo[\"action\"]>({ key: submitter.fetcherKey });\n}\n"]}
|
package/dist/formAction.d.ts
CHANGED
|
@@ -56,13 +56,26 @@ import { z } from 'zod';
|
|
|
56
56
|
* @example
|
|
57
57
|
* ### Using with useDynamicSubmitter
|
|
58
58
|
*
|
|
59
|
-
* The route above can be used with `useDynamicSubmitter` for type-safe form submissions
|
|
59
|
+
* The route above can be used with `useDynamicSubmitter` for type-safe form submissions.
|
|
60
|
+
* The hook exposes {@link UseDynamicSubmitterResult.fetcherKey} (built with
|
|
61
|
+
* {@link dynamicSubmitterFetcherKey}) so a parallel `useFetcher` stays aligned; prefer
|
|
62
|
+
* {@link useDynamicSubmitterFetcher} instead of hand-rolling the key.
|
|
63
|
+
*
|
|
64
|
+
* The **optional** {@link useDynamicSubmitterFetcher} below is only for declarative UI that reads
|
|
65
|
+
* `fetcher.state` / `fetcher.data` in render (same submission as `submitter`). For promise-first
|
|
66
|
+
* flows, omit it and use `await submitter.submitJson(...)` plus local `useState` for pending.
|
|
67
|
+
* Use {@link UseDynamicSubmitterOptions.keySuffix} when two submitters target the same URL and
|
|
68
|
+
* must not share fetcher state.
|
|
60
69
|
*
|
|
61
70
|
* ```tsx
|
|
62
|
-
* import {
|
|
71
|
+
* import {
|
|
72
|
+
* useDynamicSubmitter,
|
|
73
|
+
* useDynamicSubmitterFetcher,
|
|
74
|
+
* } from "@firtoz/router-toolkit";
|
|
63
75
|
*
|
|
64
76
|
* function LoginForm() {
|
|
65
77
|
* const submitter = useDynamicSubmitter<typeof import("./auth.login")>("/auth/login");
|
|
78
|
+
* const fetcher = useDynamicSubmitterFetcher(submitter);
|
|
66
79
|
*
|
|
67
80
|
* // Option 1: Submit as JSON (defaults to POST)
|
|
68
81
|
* const handleLoginJson = async () => {
|
|
@@ -73,7 +86,7 @@ import { z } from 'zod';
|
|
|
73
86
|
* });
|
|
74
87
|
* };
|
|
75
88
|
*
|
|
76
|
-
* // Option 2:
|
|
89
|
+
* // Option 2: Form + useDynamicSubmitterFetcher for reactive state/data
|
|
77
90
|
* return (
|
|
78
91
|
* <submitter.Form>
|
|
79
92
|
* <input name="email" type="email" placeholder="Email" />
|
|
@@ -81,16 +94,16 @@ import { z } from 'zod';
|
|
|
81
94
|
* <label>
|
|
82
95
|
* <input name="rememberMe" type="checkbox" /> Remember me
|
|
83
96
|
* </label>
|
|
84
|
-
* <button disabled={
|
|
85
|
-
* {
|
|
97
|
+
* <button type="submit" disabled={fetcher.state === "submitting"}>
|
|
98
|
+
* {fetcher.state === "submitting" ? "Logging in..." : "Login"}
|
|
86
99
|
* </button>
|
|
87
100
|
*
|
|
88
|
-
* {
|
|
101
|
+
* {fetcher.data && !fetcher.data.success && (
|
|
89
102
|
* <div className="error">
|
|
90
|
-
* {
|
|
103
|
+
* {fetcher.data.error.type === "validation"
|
|
91
104
|
* ? "Please check your inputs"
|
|
92
|
-
* :
|
|
93
|
-
* ?
|
|
105
|
+
* : fetcher.data.error.type === "handler"
|
|
106
|
+
* ? fetcher.data.error.error
|
|
94
107
|
* : "An unexpected error occurred"}
|
|
95
108
|
* </div>
|
|
96
109
|
* )}
|
|
@@ -143,21 +156,21 @@ import { z } from 'zod';
|
|
|
143
156
|
*
|
|
144
157
|
* ```tsx
|
|
145
158
|
* import { useDynamicFetcher, useDynamicSubmitter } from "@firtoz/router-toolkit";
|
|
146
|
-
* import { useEffect } from "react";
|
|
159
|
+
* import { useEffect, useState } from "react";
|
|
147
160
|
*
|
|
148
161
|
* function PostEditor({ postId }: { postId: string }) {
|
|
149
|
-
* // Fetch post data
|
|
150
162
|
* const fetcher = useDynamicFetcher<typeof import("./admin.posts.$id")>(
|
|
151
163
|
* "/admin/posts/:id",
|
|
152
164
|
* { id: postId }
|
|
153
165
|
* );
|
|
154
166
|
*
|
|
155
|
-
* // Submit updates
|
|
156
167
|
* const submitter = useDynamicSubmitter<typeof import("./admin.posts.$id")>(
|
|
157
168
|
* "/admin/posts/:id",
|
|
158
169
|
* { id: postId }
|
|
159
170
|
* );
|
|
160
171
|
*
|
|
172
|
+
* const [saving, setSaving] = useState(false);
|
|
173
|
+
*
|
|
161
174
|
* useEffect(() => {
|
|
162
175
|
* fetcher.load();
|
|
163
176
|
* }, [fetcher.load]);
|
|
@@ -169,15 +182,28 @@ import { z } from 'zod';
|
|
|
169
182
|
* const post = fetcher.data?.post;
|
|
170
183
|
*
|
|
171
184
|
* return (
|
|
172
|
-
* <submitter.Form
|
|
185
|
+
* <submitter.Form
|
|
186
|
+
* method="PUT"
|
|
187
|
+
* onSubmit={async (e) => {
|
|
188
|
+
* e.preventDefault();
|
|
189
|
+
* setSaving(true);
|
|
190
|
+
* try {
|
|
191
|
+
* const fd = new FormData(e.currentTarget);
|
|
192
|
+
* await submitter.submit(fd, { method: "PUT" });
|
|
193
|
+
* fetcher.load();
|
|
194
|
+
* } finally {
|
|
195
|
+
* setSaving(false);
|
|
196
|
+
* }
|
|
197
|
+
* }}
|
|
198
|
+
* >
|
|
173
199
|
* <input name="title" defaultValue={post?.title} />
|
|
174
200
|
* <textarea name="content" defaultValue={post?.content} />
|
|
175
201
|
* <label>
|
|
176
202
|
* <input name="published" type="checkbox" defaultChecked={post?.published} />
|
|
177
203
|
* Published
|
|
178
204
|
* </label>
|
|
179
|
-
* <button disabled={
|
|
180
|
-
* {
|
|
205
|
+
* <button type="submit" disabled={saving}>
|
|
206
|
+
* {saving ? "Saving..." : "Save"}
|
|
181
207
|
* </button>
|
|
182
208
|
* </submitter.Form>
|
|
183
209
|
* );
|
package/dist/formAction.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { ConcurrentSubmitterContext, ConcurrentSubmitterProvider, FormDataSubmittedData, Operation, OperationStatus, SubmitFormDataOptions, SubmitJsonOptions, SubmitJsonResult } from './ConcurrentSubmitterProvider.js';
|
|
2
2
|
export { FormActionConfig, FormActionError, formAction } from './formAction.js';
|
|
3
|
-
export * from '@firtoz/maybe-error';
|
|
4
3
|
export { Func } from './types/Func.js';
|
|
5
4
|
export { HrefArgs } from './types/HrefArgs.js';
|
|
6
5
|
export { RegisterPages } from './types/RegisterPages.js';
|
|
@@ -10,9 +9,10 @@ export { RouteWithLoaderModule } from './types/RouteWithLoaderModule.js';
|
|
|
10
9
|
export { useCachedFetch } from './useCachedFetch.js';
|
|
11
10
|
export { UseConcurrentSubmitterReturn, useConcurrentSubmitter } from './useConcurrentSubmitter.js';
|
|
12
11
|
export { useDynamicFetcher } from './useDynamicFetcher.js';
|
|
13
|
-
export { useDynamicSubmitter } from './useDynamicSubmitter.js';
|
|
12
|
+
export { DynamicSubmitterData, SubmitterSettledData, SubmitterSupersededError, SubmitterUnmountedError, UseDynamicSubmitterOptions, UseDynamicSubmitterResult, dynamicSubmitterFetcherKey, useDynamicSubmitter, useDynamicSubmitterFetcher } from './useDynamicSubmitter.js';
|
|
14
13
|
export { useFetcherStateChanged } from './useFetcherStateChanged.js';
|
|
15
14
|
import 'react/jsx-runtime';
|
|
16
15
|
import 'react';
|
|
16
|
+
import '@firtoz/maybe-error';
|
|
17
17
|
import 'react-router';
|
|
18
18
|
import 'zod';
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import './chunk-
|
|
1
|
+
import './chunk-SBJFTOWW.js';
|
|
2
2
|
import './chunk-2W5QFQTE.js';
|
|
3
3
|
export { useCachedFetch } from './chunk-OQGTU34H.js';
|
|
4
4
|
export { useConcurrentSubmitter } from './chunk-QZDEBX3D.js';
|
|
5
|
-
export { useDynamicFetcher } from './chunk-
|
|
6
|
-
export { useDynamicSubmitter } from './chunk-
|
|
5
|
+
export { useDynamicFetcher } from './chunk-F4324Q33.js';
|
|
6
|
+
export { SubmitterSupersededError, SubmitterUnmountedError, dynamicSubmitterFetcherKey, useDynamicSubmitter, useDynamicSubmitterFetcher } from './chunk-XMGRKSHM.js';
|
|
7
7
|
export { useFetcherStateChanged } from './chunk-W5R7YYHR.js';
|
|
8
8
|
export { ConcurrentSubmitterContext, ConcurrentSubmitterProvider } from './chunk-ZC6U3MIB.js';
|
|
9
|
-
export { formAction } from './chunk-
|
|
9
|
+
export { formAction } from './chunk-UF5QHE5K.js';
|
|
10
10
|
import './chunk-YYJDSKJG.js';
|
|
11
11
|
import './chunk-ZMRGBYFH.js';
|
|
12
12
|
import './chunk-MKH4ZP5U.js';
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -103,28 +103,33 @@ import './types/Func.js';
|
|
|
103
103
|
*
|
|
104
104
|
* ```tsx
|
|
105
105
|
* import { useDynamicFetcher, useDynamicSubmitter } from "@firtoz/router-toolkit";
|
|
106
|
+
* import { useEffect, useState } from "react";
|
|
106
107
|
*
|
|
107
108
|
* function PostEditor({ postId }: { postId: string }) {
|
|
108
|
-
* // Fetch post data
|
|
109
109
|
* const fetcher = useDynamicFetcher<typeof import("./api.posts.$postId")>(
|
|
110
110
|
* "/api/posts/:postId",
|
|
111
111
|
* { postId }
|
|
112
112
|
* );
|
|
113
113
|
*
|
|
114
|
-
* // Submit updates
|
|
115
114
|
* const submitter = useDynamicSubmitter<typeof import("./api.posts.$postId")>(
|
|
116
115
|
* "/api/posts/:postId",
|
|
117
116
|
* { postId }
|
|
118
117
|
* );
|
|
119
118
|
*
|
|
119
|
+
* const [saving, setSaving] = useState(false);
|
|
120
|
+
*
|
|
120
121
|
* useEffect(() => {
|
|
121
122
|
* fetcher.load();
|
|
122
123
|
* }, [fetcher.load]);
|
|
123
124
|
*
|
|
124
125
|
* const handleSave = async (title: string, content: string) => {
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
126
|
+
* setSaving(true);
|
|
127
|
+
* try {
|
|
128
|
+
* await submitter.submitJson({ title, content }, { method: "PUT" });
|
|
129
|
+
* fetcher.load();
|
|
130
|
+
* } finally {
|
|
131
|
+
* setSaving(false);
|
|
132
|
+
* }
|
|
128
133
|
* };
|
|
129
134
|
*
|
|
130
135
|
* if (!fetcher.data) return <div>Loading...</div>;
|
|
@@ -133,17 +138,22 @@ import './types/Func.js';
|
|
|
133
138
|
* <form onSubmit={(e) => {
|
|
134
139
|
* e.preventDefault();
|
|
135
140
|
* const form = new FormData(e.currentTarget);
|
|
136
|
-
* handleSave(form.get("title") as string, form.get("content") as string);
|
|
141
|
+
* void handleSave(form.get("title") as string, form.get("content") as string);
|
|
137
142
|
* }}>
|
|
138
143
|
* <input name="title" defaultValue={fetcher.data.post.title} />
|
|
139
144
|
* <textarea name="content" defaultValue={fetcher.data.post.content} />
|
|
140
|
-
* <button disabled={
|
|
141
|
-
* {
|
|
145
|
+
* <button type="submit" disabled={saving}>
|
|
146
|
+
* {saving ? "Saving..." : "Save"}
|
|
142
147
|
* </button>
|
|
143
148
|
* </form>
|
|
144
149
|
* );
|
|
145
150
|
* }
|
|
146
151
|
* ```
|
|
152
|
+
*
|
|
153
|
+
* **Submitter UX:** Local `saving` around `await submitter.submitJson` fits a promise-first save +
|
|
154
|
+
* reload flow. For declarative `fetcher.state` / `fetcher.data` in JSX (e.g. with `submitter.Form`),
|
|
155
|
+
* use {@link useDynamicSubmitterFetcher} instead. The package README documents trade-offs under
|
|
156
|
+
* **useDynamicSubmitter** (heading “Local useState vs useDynamicSubmitterFetcher”).
|
|
147
157
|
*/
|
|
148
158
|
|
|
149
159
|
/**
|