Skip to content

Commit 88c5cc4

Browse files
committed
simplify typing
1 parent 9ce9cee commit 88c5cc4

File tree

4 files changed

+47
-89
lines changed

4 files changed

+47
-89
lines changed

packages/clients/tanstack-query/src/react.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -309,39 +309,39 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
309309
},
310310

311311
useCreate: (options?: any) => {
312-
return useInternalMutation(schema, modelName, 'POST', 'create', options, true);
312+
return useInternalMutation(schema, modelName, 'POST', 'create', options);
313313
},
314314

315315
useCreateMany: (options?: any) => {
316-
return useInternalMutation(schema, modelName, 'POST', 'createMany', options, false);
316+
return useInternalMutation(schema, modelName, 'POST', 'createMany', options);
317317
},
318318

319319
useCreateManyAndReturn: (options?: any) => {
320-
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options, true);
320+
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options);
321321
},
322322

323323
useUpdate: (options?: any) => {
324-
return useInternalMutation(schema, modelName, 'PUT', 'update', options, true);
324+
return useInternalMutation(schema, modelName, 'PUT', 'update', options);
325325
},
326326

327327
useUpdateMany: (options?: any) => {
328-
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options, false);
328+
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options);
329329
},
330330

331331
useUpdateManyAndReturn: (options?: any) => {
332-
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options, true);
332+
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options);
333333
},
334334

335335
useUpsert: (options?: any) => {
336-
return useInternalMutation(schema, modelName, 'POST', 'upsert', options, true);
336+
return useInternalMutation(schema, modelName, 'POST', 'upsert', options);
337337
},
338338

339339
useDelete: (options?: any) => {
340-
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options, true);
340+
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options);
341341
},
342342

343343
useDeleteMany: (options?: any) => {
344-
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options, false);
344+
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
345345
},
346346

347347
useCount: (options?: any) => {
@@ -387,7 +387,7 @@ export function useInternalQuery<TQueryFnData, TData>(
387387
queryKey,
388388
...useQuery({
389389
queryKey,
390-
queryFn: ({ signal }) => fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false),
390+
queryFn: ({ signal }) => fetcher<TQueryFnData>(reqUrl, { signal }, fetch),
391391
...options,
392392
}),
393393
};
@@ -410,7 +410,7 @@ export function useInternalSuspenseQuery<TQueryFnData, TData>(
410410
queryKey,
411411
...useSuspenseQuery({
412412
queryKey,
413-
queryFn: ({ signal }) => fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false),
413+
queryFn: ({ signal }) => fetcher<TQueryFnData>(reqUrl, { signal }, fetch),
414414
...options,
415415
}),
416416
};
@@ -436,12 +436,7 @@ export function useInternalInfiniteQuery<TQueryFnData, TData>(
436436
...useInfiniteQuery({
437437
queryKey,
438438
queryFn: ({ pageParam, signal }) => {
439-
return fetcher<TQueryFnData, false>(
440-
makeUrl(endpoint, model, operation, pageParam ?? args),
441-
{ signal },
442-
fetch,
443-
false,
444-
);
439+
return fetcher<TQueryFnData>(makeUrl(endpoint, model, operation, pageParam ?? args), { signal }, fetch);
445440
},
446441
initialPageParam: args,
447442
...options,
@@ -466,12 +461,7 @@ export function useInternalSuspenseInfiniteQuery<TQueryFnData, TData>(
466461
...useSuspenseInfiniteQuery({
467462
queryKey,
468463
queryFn: ({ pageParam, signal }) => {
469-
return fetcher<TQueryFnData, false>(
470-
makeUrl(endpoint, model, operation, pageParam ?? args),
471-
{ signal },
472-
fetch,
473-
false,
474-
);
464+
return fetcher<TQueryFnData>(makeUrl(endpoint, model, operation, pageParam ?? args), { signal }, fetch);
475465
},
476466
initialPageParam: args,
477467
...options,
@@ -490,18 +480,12 @@ export function useInternalSuspenseInfiniteQuery<TQueryFnData, TData>(
490480
* @param options The react-query options.
491481
* @param checkReadBack Whether to check for read back errors and return undefined if found.
492482
*/
493-
export function useInternalMutation<
494-
TArgs,
495-
R = any,
496-
C extends boolean = boolean,
497-
Result = C extends true ? R | undefined : R,
498-
>(
483+
export function useInternalMutation<TArgs, R = any>(
499484
schema: SchemaDef,
500485
model: string,
501486
method: 'POST' | 'PUT' | 'DELETE',
502487
operation: string,
503-
options?: Omit<UseMutationOptions<Result, DefaultError, TArgs>, 'mutationFn'> & ExtraMutationOptions,
504-
checkReadBack?: C,
488+
options?: Omit<UseMutationOptions<R, DefaultError, TArgs>, 'mutationFn'> & ExtraMutationOptions,
505489
) {
506490
const { endpoint, fetch, logging } = useHooksContext();
507491
const queryClient = useQueryClient();
@@ -517,7 +501,7 @@ export function useInternalMutation<
517501
body: marshal(data),
518502
}),
519503
};
520-
return fetcher<R, C>(reqUrl, fetchInit, fetch, checkReadBack) as Promise<Result>;
504+
return fetcher<R>(reqUrl, fetchInit, fetch) as Promise<R>;
521505
};
522506

523507
const finalOptions = { ...options, mutationFn };

packages/clients/tanstack-query/src/svelte.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -248,39 +248,39 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
248248
},
249249

250250
useCreate: (options?: any) => {
251-
return useInternalMutation(schema, modelName, 'POST', 'create', options, true);
251+
return useInternalMutation(schema, modelName, 'POST', 'create', options);
252252
},
253253

254254
useCreateMany: (options?: any) => {
255-
return useInternalMutation(schema, modelName, 'POST', 'createMany', options, false);
255+
return useInternalMutation(schema, modelName, 'POST', 'createMany', options);
256256
},
257257

258258
useCreateManyAndReturn: (options?: any) => {
259-
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options, true);
259+
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options);
260260
},
261261

262262
useUpdate: (options?: any) => {
263-
return useInternalMutation(schema, modelName, 'PUT', 'update', options, true);
263+
return useInternalMutation(schema, modelName, 'PUT', 'update', options);
264264
},
265265

266266
useUpdateMany: (options?: any) => {
267-
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options, false);
267+
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options);
268268
},
269269

270270
useUpdateManyAndReturn: (options?: any) => {
271-
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options, true);
271+
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options);
272272
},
273273

274274
useUpsert: (options?: any) => {
275-
return useInternalMutation(schema, modelName, 'POST', 'upsert', options, true);
275+
return useInternalMutation(schema, modelName, 'POST', 'upsert', options);
276276
},
277277

278278
useDelete: (options?: any) => {
279-
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options, true);
279+
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options);
280280
},
281281

282282
useDeleteMany: (options?: any) => {
283-
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options, false);
283+
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
284284
},
285285

286286
useCount: (options?: any) => {
@@ -313,7 +313,7 @@ export function useInternalQuery<TQueryFnData, TData>(
313313
optimisticUpdate: optionsValue?.optimisticUpdate !== false,
314314
});
315315
const queryFn: QueryFunction<TQueryFnData, QueryKey, unknown> = ({ signal }) =>
316-
fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false);
316+
fetcher<TQueryFnData>(reqUrl, { signal }, fetch);
317317

318318
let mergedOpt: any;
319319
if (isStore(options)) {
@@ -360,12 +360,7 @@ export function useInternalInfiniteQuery<TQueryFnData, TData>(
360360
const argsValue = unwrapStore(args);
361361
const queryKey = getQueryKey(model, operation, argsValue, { infinite: true, optimisticUpdate: false });
362362
const queryFn: QueryFunction<TQueryFnData, QueryKey, unknown> = ({ pageParam, signal }) =>
363-
fetcher<TQueryFnData, false>(
364-
makeUrl(endpoint, model, operation, pageParam ?? argsValue),
365-
{ signal },
366-
fetch,
367-
false,
368-
);
363+
fetcher<TQueryFnData>(makeUrl(endpoint, model, operation, pageParam ?? argsValue), { signal }, fetch);
369364

370365
let mergedOpt: StoreOrVal<CreateInfiniteQueryOptions<TQueryFnData, DefaultError, InfiniteData<TData>>>;
371366
if (isStore(options)) {
@@ -406,18 +401,12 @@ export function useInternalInfiniteQuery<TQueryFnData, TData>(
406401
* @param options The svelte-query options.
407402
* @param checkReadBack Whether to check for read back errors and return undefined if found.
408403
*/
409-
export function useInternalMutation<
410-
TArgs,
411-
R = any,
412-
C extends boolean = boolean,
413-
Result = C extends true ? R | undefined : R,
414-
>(
404+
export function useInternalMutation<TArgs, R = any>(
415405
schema: SchemaDef,
416406
model: string,
417407
method: 'POST' | 'PUT' | 'DELETE',
418408
operation: string,
419-
options?: StoreOrVal<Omit<CreateMutationOptions<Result, DefaultError, TArgs>, 'mutationFn'> & ExtraMutationOptions>,
420-
checkReadBack?: C,
409+
options?: StoreOrVal<Omit<CreateMutationOptions<R, DefaultError, TArgs>, 'mutationFn'> & ExtraMutationOptions>,
421410
) {
422411
const { endpoint, fetch, logging } = getQuerySettings();
423412
const queryClient = useQueryClient();
@@ -434,10 +423,10 @@ export function useInternalMutation<
434423
body: marshal(data),
435424
}),
436425
};
437-
return fetcher<R, C>(reqUrl, fetchInit, fetch, checkReadBack) as Promise<Result>;
426+
return fetcher<R>(reqUrl, fetchInit, fetch) as Promise<R>;
438427
};
439428

440-
let mergedOpt: StoreOrVal<CreateMutationOptions<Result, DefaultError, TArgs>>;
429+
let mergedOpt: StoreOrVal<CreateMutationOptions<R, DefaultError, TArgs>>;
441430

442431
if (isStore(options)) {
443432
mergedOpt = derived([options], ([$opt]) => ({

packages/clients/tanstack-query/src/utils/common.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,12 @@ export type APIContext = {
125125
logging?: boolean;
126126
};
127127

128-
export async function fetcher<R, C extends boolean>(
129-
url: string,
130-
options?: RequestInit,
131-
customFetch?: FetchFn,
132-
checkReadBack?: C,
133-
): Promise<C extends true ? R | undefined : R> {
128+
export async function fetcher<R>(url: string, options?: RequestInit, customFetch?: FetchFn): Promise<R> {
134129
const _fetch = customFetch ?? fetch;
135130
const res = await _fetch(url, options);
136131
if (!res.ok) {
137132
const errData = unmarshal(await res.text());
138-
if (
139-
checkReadBack !== false &&
140-
errData.error?.rejectedByPolicy &&
141-
errData.error?.rejectReason === 'cannot-read-back'
142-
) {
133+
if (errData.error?.rejectedByPolicy && errData.error?.rejectReason === 'cannot-read-back') {
143134
// policy doesn't allow mutation result to be read back, just return undefined
144135
return undefined as any;
145136
}

packages/clients/tanstack-query/src/vue.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,39 +237,39 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
237237
},
238238

239239
useCreate: (options?: any) => {
240-
return useInternalMutation(schema, modelName, 'POST', 'create', options, true);
240+
return useInternalMutation(schema, modelName, 'POST', 'create', options);
241241
},
242242

243243
useCreateMany: (options?: any) => {
244-
return useInternalMutation(schema, modelName, 'POST', 'createMany', options, false);
244+
return useInternalMutation(schema, modelName, 'POST', 'createMany', options);
245245
},
246246

247247
useCreateManyAndReturn: (options?: any) => {
248-
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options, true);
248+
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options);
249249
},
250250

251251
useUpdate: (options?: any) => {
252-
return useInternalMutation(schema, modelName, 'PUT', 'update', options, true);
252+
return useInternalMutation(schema, modelName, 'PUT', 'update', options);
253253
},
254254

255255
useUpdateMany: (options?: any) => {
256-
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options, false);
256+
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options);
257257
},
258258

259259
useUpdateManyAndReturn: (options?: any) => {
260-
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options, true);
260+
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options);
261261
},
262262

263263
useUpsert: (options?: any) => {
264-
return useInternalMutation(schema, modelName, 'POST', 'upsert', options, true);
264+
return useInternalMutation(schema, modelName, 'POST', 'upsert', options);
265265
},
266266

267267
useDelete: (options?: any) => {
268-
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options, true);
268+
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options);
269269
},
270270

271271
useDeleteMany: (options?: any) => {
272-
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options, false);
272+
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
273273
},
274274

275275
useCount: (options?: any) => {
@@ -308,7 +308,7 @@ export function useInternalQuery<TQueryFnData, TData>(
308308
queryFn: ({ queryKey, signal }: any) => {
309309
const [_prefix, _model, _op, args] = queryKey;
310310
const reqUrl = makeUrl(endpoint, model, operation, args);
311-
return fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false);
311+
return fetcher<TQueryFnData>(reqUrl, { signal }, fetch);
312312
},
313313
...restOptions,
314314
};
@@ -340,7 +340,7 @@ export function useInternalInfiniteQuery<TQueryFnData, TData>(
340340
queryFn: ({ queryKey, signal }: any) => {
341341
const [_prefix, _model, _op, args] = queryKey;
342342
const reqUrl = makeUrl(endpoint, model, operation, args);
343-
return fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false);
343+
return fetcher<TQueryFnData>(reqUrl, { signal }, fetch);
344344
},
345345
initialPageParam: argsValue,
346346
...optionsValue,
@@ -362,20 +362,14 @@ export function useInternalInfiniteQuery<TQueryFnData, TData>(
362362
* @param options The vue-query options.
363363
* @param checkReadBack Whether to check for read back errors and return undefined if found.
364364
*/
365-
export function useInternalMutation<
366-
TArgs,
367-
R = any,
368-
C extends boolean = boolean,
369-
Result = C extends true ? R | undefined : R,
370-
>(
365+
export function useInternalMutation<TArgs, R = any>(
371366
schema: SchemaDef,
372367
model: string,
373368
method: 'POST' | 'PUT' | 'DELETE',
374369
operation: string,
375370
options?: MaybeRefOrGetter<
376-
Omit<UnwrapRef<UseMutationOptions<Result, DefaultError, TArgs>>, 'mutationFn'> & ExtraMutationOptions
371+
Omit<UnwrapRef<UseMutationOptions<R, DefaultError, TArgs>>, 'mutationFn'> & ExtraMutationOptions
377372
>,
378-
checkReadBack?: C,
379373
) {
380374
const { endpoint, fetch, logging } = getQuerySettings();
381375
const queryClient = useQueryClient();
@@ -391,7 +385,7 @@ export function useInternalMutation<
391385
body: marshal(data),
392386
}),
393387
};
394-
return fetcher<R, C>(reqUrl, fetchInit, fetch, checkReadBack) as Promise<Result>;
388+
return fetcher<R>(reqUrl, fetchInit, fetch) as Promise<R>;
395389
};
396390

397391
const optionsValue = toValue(options);

0 commit comments

Comments
 (0)