@firtoz/router-toolkit 5.1.0 → 5.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firtoz/router-toolkit",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Type-safe React Router 7 framework mode helpers with enhanced fetching, form submission, and state management",
5
5
  "main": "./src/index.ts",
6
6
  "module": "./src/index.ts",
package/src/formAction.ts CHANGED
@@ -56,17 +56,18 @@
56
56
  * function LoginForm() {
57
57
  * const submitter = useDynamicSubmitter<typeof import("./auth.login")>("/auth/login");
58
58
  *
59
- * // Option 1: Submit as JSON (recommended for programmatic use)
59
+ * // Option 1: Submit as JSON (defaults to POST)
60
60
  * const handleLoginJson = async () => {
61
- * await submitter.submitJson(
62
- * { email: "user@example.com", password: "secret123", rememberMe: true },
63
- * { method: "POST" }
64
- * );
61
+ * await submitter.submitJson({
62
+ * email: "user@example.com",
63
+ * password: "secret123",
64
+ * rememberMe: true,
65
+ * });
65
66
  * };
66
67
  *
67
- * // Option 2: Use the Form component
68
+ * // Option 2: Use the Form component (defaults to POST)
68
69
  * return (
69
- * <submitter.Form method="POST">
70
+ * <submitter.Form>
70
71
  * <input name="email" type="email" placeholder="Email" />
71
72
  * <input name="password" type="password" placeholder="Password" />
72
73
  * <label>
@@ -55,11 +55,13 @@
55
55
  * // submitter.state is "idle" | "loading" | "submitting"
56
56
  *
57
57
  * // Option 1: Submit as JSON (recommended for programmatic submissions)
58
+ * // Defaults to POST if no options provided
58
59
  * const handleSubmitJson = async () => {
59
- * await submitter.submitJson(
60
- * { title: "My Post", content: "Post content here", published: true },
61
- * { method: "POST" }
62
- * );
60
+ * await submitter.submitJson({
61
+ * title: "My Post",
62
+ * content: "Post content here",
63
+ * published: true,
64
+ * });
63
65
  * };
64
66
  *
65
67
  * // Option 2: Submit with FormData or SubmitTarget
@@ -67,9 +69,9 @@
67
69
  * await submitter.submit(formData, { method: "POST" });
68
70
  * };
69
71
  *
70
- * // Option 3: Use the Form component
72
+ * // Option 3: Use the Form component (defaults to POST)
71
73
  * return (
72
- * <submitter.Form method="POST">
74
+ * <submitter.Form>
73
75
  * <input name="title" />
74
76
  * <textarea name="content" />
75
77
  * <button type="submit">Save</button>
@@ -98,7 +100,7 @@
98
100
  * }, [submitter.data]);
99
101
  *
100
102
  * return (
101
- * <submitter.Form method="POST">
103
+ * <submitter.Form>
102
104
  * <input name="email" type="email" />
103
105
  * <input name="password" type="password" />
104
106
  * <button disabled={submitter.state !== "idle"}>
@@ -160,6 +162,17 @@ type SubmitFunc<TModule extends RouteModule> = (
160
162
  },
161
163
  ) => Promise<void>;
162
164
 
165
+ /**
166
+ * Options for submitJson function.
167
+ * Method defaults to "POST" if not specified.
168
+ */
169
+ type SubmitJsonOptions = Omit<
170
+ SubmitOptions,
171
+ "action" | "method" | "encType"
172
+ > & {
173
+ method?: Exclude<SubmitOptions["method"], "GET">;
174
+ };
175
+
163
176
  /**
164
177
  * Function type for submitting form data as JSON.
165
178
  *
@@ -167,33 +180,44 @@ type SubmitFunc<TModule extends RouteModule> = (
167
180
  * Automatically serializes the data as JSON. This is the recommended
168
181
  * approach for programmatic form submissions.
169
182
  *
183
+ * Options are optional and default to `{ method: "POST" }`.
184
+ *
170
185
  * @example
171
186
  * ```typescript
172
- * // Submit a plain object - fully type-safe
173
- * await submitter.submitJson(
174
- * { email: "user@example.com", password: "secret123", rememberMe: true },
175
- * { method: "POST" }
176
- * );
187
+ * // Submit a plain object - fully type-safe (defaults to POST)
188
+ * await submitter.submitJson({
189
+ * email: "user@example.com",
190
+ * password: "secret123",
191
+ * rememberMe: true,
192
+ * });
193
+ *
194
+ * // Or specify a different method
195
+ * await submitter.submitJson(data, { method: "PUT" });
177
196
  * ```
178
197
  */
179
198
  type SubmitJsonFunc<TModule extends RouteModule> = (
180
199
  data: z.infer<TModule["formSchema"]>,
181
- options: Omit<SubmitOptions, "action" | "method" | "encType"> & {
182
- method: Exclude<SubmitOptions["method"], "GET">;
183
- },
200
+ options?: SubmitJsonOptions,
184
201
  ) => Promise<void>;
185
202
 
186
203
  /**
187
204
  * Form component type with pre-bound action URL.
188
205
  *
189
206
  * Renders a form element that automatically submits to the correct route.
207
+ * Method defaults to "POST" if not specified.
190
208
  *
191
209
  * @example
192
210
  * ```typescript
193
- * <submitter.Form method="POST">
211
+ * // Defaults to POST
212
+ * <submitter.Form>
194
213
  * <input name="title" />
195
214
  * <button type="submit">Submit</button>
196
215
  * </submitter.Form>
216
+ *
217
+ * // Or specify a different method
218
+ * <submitter.Form method="PUT">
219
+ * ...
220
+ * </submitter.Form>
197
221
  * ```
198
222
  */
199
223
  type SubmitForm = (
@@ -201,7 +225,7 @@ type SubmitForm = (
201
225
  FetcherFormProps & React.RefAttributes<HTMLFormElement>,
202
226
  "action" | "method"
203
227
  > & {
204
- method: Exclude<SubmitOptions["method"], "GET">;
228
+ method?: Exclude<SubmitOptions["method"], "GET">;
205
229
  },
206
230
  ) => React.ReactElement;
207
231
 
@@ -245,12 +269,12 @@ type SubmitForm = (
245
269
  * { userId: "123" }
246
270
  * );
247
271
  *
248
- * // Submit using submitJson (type-safe, no FormData needed)
272
+ * // Submit using submitJson (type-safe, no FormData needed, defaults to POST)
249
273
  * await submitter.submitJson({
250
274
  * displayName: "John Doe",
251
275
  * email: "john@example.com",
252
276
  * notifications: true,
253
- * }, { method: "POST" });
277
+ * });
254
278
  *
255
279
  * // Check the response
256
280
  * if (submitter.data?.success) {
@@ -269,9 +293,9 @@ export const useDynamicSubmitter = <TInfo extends RouteModule>(
269
293
  > & {
270
294
  /** Submit with FormData or SubmitTarget (schema type & SubmitTarget) */
271
295
  submit: SubmitFunc<TInfo>;
272
- /** Submit a plain object as JSON (schema type only, recommended for programmatic use) */
296
+ /** Submit a plain object as JSON (schema type only, defaults to POST) */
273
297
  submitJson: SubmitJsonFunc<TInfo>;
274
- /** Pre-bound Form component with action URL already set */
298
+ /** Pre-bound Form component with action URL already set (defaults to POST) */
275
299
  Form: SubmitForm;
276
300
  } => {
277
301
  const url = useMemo(() => {
@@ -295,9 +319,10 @@ export const useDynamicSubmitter = <TInfo extends RouteModule>(
295
319
  );
296
320
 
297
321
  const submitJson: SubmitJsonFunc<TInfo> = useCallback(
298
- (data, options) => {
322
+ (data, options = {}) => {
299
323
  return fetcher.submit(data as SubmitTarget, {
300
324
  ...options,
325
+ method: options.method ?? "POST",
301
326
  action: url,
302
327
  encType: "application/json",
303
328
  });
@@ -308,8 +333,8 @@ export const useDynamicSubmitter = <TInfo extends RouteModule>(
308
333
  const OriginalForm = fetcher.Form;
309
334
 
310
335
  const Form: SubmitForm = useCallback(
311
- (props) => {
312
- return <OriginalForm action={url} {...props} />;
336
+ ({ method = "POST", ...props }) => {
337
+ return <OriginalForm action={url} method={method} {...props} />;
313
338
  },
314
339
  [url, OriginalForm],
315
340
  );