@firtoz/router-toolkit 6.0.0 → 7.0.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": "6.0.0",
3
+ "version": "7.0.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",
@@ -176,7 +176,7 @@ export function ConcurrentSubmitterProvider({
176
176
  setOperationsState((prev) => {
177
177
  const op = prev[id];
178
178
  if (!op) return prev;
179
- const { resolve, reject, ...rest } = op;
179
+ const { resolve, reject, pendingSubmit: _p, ...rest } = op;
180
180
  if (error !== undefined) {
181
181
  reject(error);
182
182
  return {
@@ -187,7 +187,6 @@ export function ConcurrentSubmitterProvider({
187
187
  error,
188
188
  resolve,
189
189
  reject,
190
- pendingSubmit: undefined,
191
190
  },
192
191
  };
193
192
  }
@@ -200,7 +199,6 @@ export function ConcurrentSubmitterProvider({
200
199
  data,
201
200
  resolve,
202
201
  reject,
203
- pendingSubmit: undefined,
204
202
  },
205
203
  };
206
204
  });
@@ -22,6 +22,75 @@ export type { ActionResult };
22
22
 
23
23
  type RouteParams<R extends keyof RegisterPages> = RegisterPages[R]["params"];
24
24
 
25
+ // Mirror HrefArgs logic: empty params → omit args; optional params → args optional; required → args required
26
+ type Equal<X, Y> =
27
+ (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
28
+ ? true
29
+ : false;
30
+
31
+ type HasNoParams<R extends keyof RegisterPages> =
32
+ Equal<
33
+ RouteParams<R>,
34
+ // biome-ignore lint/complexity/noBannedTypes: Intentionally empty for route with no params
35
+ {}
36
+ > extends true
37
+ ? true
38
+ : false;
39
+
40
+ type HasOptionalParams<R extends keyof RegisterPages> =
41
+ HasNoParams<R> extends true
42
+ ? false
43
+ : Partial<RouteParams<R>> extends RouteParams<R>
44
+ ? true
45
+ : false;
46
+
47
+ // Args immediately after path (when needed), matching useDynamicFetcher/useDynamicSubmitter.
48
+ // Order: (path, args?, data, options?) and (path, args?, formData, submittedData?, options?).
49
+ type SubmitJsonFn<TInfo extends RouteWithActionModule> =
50
+ HasNoParams<TInfo["route"]> extends true
51
+ ? (
52
+ path: TInfo["route"],
53
+ data: z.infer<TInfo["formSchema"]>,
54
+ options?: SubmitJsonOptions,
55
+ ) => SubmitJsonResult<ActionResult<TInfo>>
56
+ : HasOptionalParams<TInfo["route"]> extends true
57
+ ? (
58
+ path: TInfo["route"],
59
+ args: RouteParams<TInfo["route"]> | undefined,
60
+ data: z.infer<TInfo["formSchema"]>,
61
+ options?: SubmitJsonOptions,
62
+ ) => SubmitJsonResult<ActionResult<TInfo>>
63
+ : (
64
+ path: TInfo["route"],
65
+ args: RouteParams<TInfo["route"]>,
66
+ data: z.infer<TInfo["formSchema"]>,
67
+ options?: SubmitJsonOptions,
68
+ ) => SubmitJsonResult<ActionResult<TInfo>>;
69
+
70
+ type SubmitFormDataFn<TInfo extends RouteWithActionModule> =
71
+ HasNoParams<TInfo["route"]> extends true
72
+ ? (
73
+ path: TInfo["route"],
74
+ formData: FormData,
75
+ submittedData?: FormDataSubmittedData,
76
+ options?: SubmitFormDataOptions,
77
+ ) => SubmitJsonResult<ActionResult<TInfo>>
78
+ : HasOptionalParams<TInfo["route"]> extends true
79
+ ? (
80
+ path: TInfo["route"],
81
+ args: RouteParams<TInfo["route"]> | undefined,
82
+ formData: FormData,
83
+ submittedData?: FormDataSubmittedData,
84
+ options?: SubmitFormDataOptions,
85
+ ) => SubmitJsonResult<ActionResult<TInfo>>
86
+ : (
87
+ path: TInfo["route"],
88
+ args: RouteParams<TInfo["route"]>,
89
+ formData: FormData,
90
+ submittedData?: FormDataSubmittedData,
91
+ options?: SubmitFormDataOptions,
92
+ ) => SubmitJsonResult<ActionResult<TInfo>>;
93
+
25
94
  export type UseConcurrentSubmitterReturn<TInfo extends RouteWithActionModule> =
26
95
  {
27
96
  operations: Record<
@@ -31,19 +100,8 @@ export type UseConcurrentSubmitterReturn<TInfo extends RouteWithActionModule> =
31
100
  z.infer<TInfo["formSchema"]> | FormDataSubmittedData
32
101
  >
33
102
  >;
34
- submitJson: (
35
- path: TInfo["route"],
36
- args: RouteParams<TInfo["route"]> | undefined,
37
- data: z.infer<TInfo["formSchema"]>,
38
- options?: SubmitJsonOptions,
39
- ) => SubmitJsonResult<ActionResult<TInfo>>;
40
- submitFormData: (
41
- path: TInfo["route"],
42
- args: RouteParams<TInfo["route"]> | undefined,
43
- formData: FormData,
44
- submittedData?: FormDataSubmittedData,
45
- options?: SubmitFormDataOptions,
46
- ) => SubmitJsonResult<ActionResult<TInfo>>;
103
+ submitJson: SubmitJsonFn<TInfo>;
104
+ submitFormData: SubmitFormDataFn<TInfo>;
47
105
  };
48
106
 
49
107
  export function useConcurrentSubmitter<
@@ -56,24 +114,75 @@ export function useConcurrentSubmitter<
56
114
  );
57
115
  }
58
116
 
117
+ function isRouteParams(obj: unknown): obj is Record<string, string> {
118
+ return (
119
+ typeof obj === "object" &&
120
+ obj !== null &&
121
+ !Array.isArray(obj) &&
122
+ obj instanceof FormData === false &&
123
+ Object.values(obj as Record<string, unknown>).every(
124
+ (v) => typeof v === "string",
125
+ )
126
+ );
127
+ }
128
+
59
129
  const submitJson = useCallback(
60
- (
61
- path: string,
62
- args: Record<string, string> | undefined,
63
- data: unknown,
64
- options?: SubmitJsonOptions,
65
- ) => ctx.addJsonSubmission(path, args, data, options),
130
+ (path: string, ...rest: unknown[]) => {
131
+ if (rest.length === 1) {
132
+ return ctx.addJsonSubmission(path, undefined, rest[0], undefined);
133
+ }
134
+ if (rest.length === 2 && rest[0] === undefined) {
135
+ return ctx.addJsonSubmission(path, undefined, rest[1], undefined);
136
+ }
137
+ if (rest.length === 2 && !isRouteParams(rest[0])) {
138
+ const [data, options] = rest as [
139
+ Record<string, unknown>,
140
+ SubmitJsonOptions | undefined,
141
+ ];
142
+ return ctx.addJsonSubmission(path, undefined, data, options);
143
+ }
144
+ const [args, data, options] = rest as [
145
+ Record<string, string> | undefined,
146
+ Record<string, unknown>,
147
+ SubmitJsonOptions | undefined,
148
+ ];
149
+ return ctx.addJsonSubmission(path, args, data, options);
150
+ },
66
151
  [ctx],
67
152
  ) as UseConcurrentSubmitterReturn<TInfo>["submitJson"];
68
153
 
69
154
  const submitFormData = useCallback(
70
- (
71
- path: string,
72
- args: Record<string, string> | undefined,
73
- formData: FormData,
74
- submittedData: FormDataSubmittedData = {},
75
- options?: SubmitFormDataOptions,
76
- ) => ctx.addFormSubmission(path, args, formData, submittedData, options),
155
+ (path: string, ...rest: unknown[]) => {
156
+ const second = rest[0];
157
+ if (second instanceof FormData) {
158
+ const formData = second;
159
+ const submittedData =
160
+ rest.length >= 2 ? (rest[1] as FormDataSubmittedData) : {};
161
+ const options =
162
+ rest.length === 3 ? (rest[2] as SubmitFormDataOptions) : undefined;
163
+ return ctx.addFormSubmission(
164
+ path,
165
+ undefined,
166
+ formData,
167
+ submittedData,
168
+ options,
169
+ );
170
+ }
171
+ const args = second as Record<string, string>;
172
+ const formData = rest[1] as FormData;
173
+ const submittedData =
174
+ rest.length >= 3 ? (rest[2] as FormDataSubmittedData) : {};
175
+ const options = (rest.length === 4 ? rest[3] : undefined) as
176
+ | SubmitFormDataOptions
177
+ | undefined;
178
+ return ctx.addFormSubmission(
179
+ path,
180
+ args,
181
+ formData,
182
+ submittedData,
183
+ options,
184
+ );
185
+ },
77
186
  [ctx],
78
187
  ) as UseConcurrentSubmitterReturn<TInfo>["submitFormData"];
79
188