@tanstack/start-client-core 1.114.4 → 1.114.5

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.
Files changed (62) hide show
  1. package/dist/cjs/createIsomorphicFn.cjs +7 -0
  2. package/dist/cjs/createIsomorphicFn.cjs.map +1 -0
  3. package/dist/cjs/createIsomorphicFn.d.cts +12 -0
  4. package/dist/cjs/createMiddleware.cjs +34 -0
  5. package/dist/cjs/createMiddleware.cjs.map +1 -0
  6. package/dist/cjs/createMiddleware.d.cts +131 -0
  7. package/dist/cjs/createServerFn.cjs +227 -0
  8. package/dist/cjs/createServerFn.cjs.map +1 -0
  9. package/dist/cjs/createServerFn.d.cts +152 -0
  10. package/dist/cjs/envOnly.cjs +7 -0
  11. package/dist/cjs/envOnly.cjs.map +1 -0
  12. package/dist/cjs/envOnly.d.cts +4 -0
  13. package/dist/cjs/index.cjs +22 -0
  14. package/dist/cjs/index.cjs.map +1 -1
  15. package/dist/cjs/index.d.cts +8 -0
  16. package/dist/cjs/json.cjs +14 -0
  17. package/dist/cjs/json.cjs.map +1 -0
  18. package/dist/cjs/json.d.cts +2 -0
  19. package/dist/cjs/registerGlobalMiddleware.cjs +9 -0
  20. package/dist/cjs/registerGlobalMiddleware.cjs.map +1 -0
  21. package/dist/cjs/registerGlobalMiddleware.d.cts +5 -0
  22. package/dist/cjs/tests/createIsomorphicFn.test-d.d.cts +1 -0
  23. package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +1 -0
  24. package/dist/cjs/tests/envOnly.test-d.d.cts +1 -0
  25. package/dist/cjs/tests/json.test.d.cts +1 -0
  26. package/dist/esm/createIsomorphicFn.d.ts +12 -0
  27. package/dist/esm/createIsomorphicFn.js +7 -0
  28. package/dist/esm/createIsomorphicFn.js.map +1 -0
  29. package/dist/esm/createMiddleware.d.ts +131 -0
  30. package/dist/esm/createMiddleware.js +34 -0
  31. package/dist/esm/createMiddleware.js.map +1 -0
  32. package/dist/esm/createServerFn.d.ts +152 -0
  33. package/dist/esm/createServerFn.js +206 -0
  34. package/dist/esm/createServerFn.js.map +1 -0
  35. package/dist/esm/envOnly.d.ts +4 -0
  36. package/dist/esm/envOnly.js +7 -0
  37. package/dist/esm/envOnly.js.map +1 -0
  38. package/dist/esm/index.d.ts +8 -0
  39. package/dist/esm/index.js +19 -0
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/json.d.ts +2 -0
  42. package/dist/esm/json.js +14 -0
  43. package/dist/esm/json.js.map +1 -0
  44. package/dist/esm/registerGlobalMiddleware.d.ts +5 -0
  45. package/dist/esm/registerGlobalMiddleware.js +9 -0
  46. package/dist/esm/registerGlobalMiddleware.js.map +1 -0
  47. package/dist/esm/tests/createIsomorphicFn.test-d.d.ts +1 -0
  48. package/dist/esm/tests/createServerMiddleware.test-d.d.ts +1 -0
  49. package/dist/esm/tests/envOnly.test-d.d.ts +1 -0
  50. package/dist/esm/tests/json.test.d.ts +1 -0
  51. package/package.json +2 -1
  52. package/src/createIsomorphicFn.ts +36 -0
  53. package/src/createMiddleware.ts +595 -0
  54. package/src/createServerFn.ts +700 -0
  55. package/src/envOnly.ts +9 -0
  56. package/src/index.tsx +73 -0
  57. package/src/json.ts +15 -0
  58. package/src/registerGlobalMiddleware.ts +9 -0
  59. package/src/tests/createIsomorphicFn.test-d.ts +72 -0
  60. package/src/tests/createServerMiddleware.test-d.ts +611 -0
  61. package/src/tests/envOnly.test-d.ts +34 -0
  62. package/src/tests/json.test.ts +37 -0
@@ -0,0 +1,595 @@
1
+ import type {
2
+ ConstrainValidator,
3
+ Method,
4
+ ServerFnResponseType,
5
+ ServerFnTypeOrTypeFn,
6
+ } from './createServerFn'
7
+ import type {
8
+ Assign,
9
+ Constrain,
10
+ Expand,
11
+ IntersectAssign,
12
+ ResolveValidatorInput,
13
+ ResolveValidatorOutput,
14
+ SerializerStringify,
15
+ } from '@tanstack/router-core'
16
+
17
+ export type AssignAllMiddleware<
18
+ TMiddlewares,
19
+ TType extends keyof AnyMiddleware['_types'],
20
+ TAcc = undefined,
21
+ > = TMiddlewares extends readonly [
22
+ infer TMiddleware extends AnyMiddleware,
23
+ ...infer TRest,
24
+ ]
25
+ ? AssignAllMiddleware<
26
+ TRest,
27
+ TType,
28
+ Assign<TAcc, TMiddleware['_types'][TType]>
29
+ >
30
+ : TAcc
31
+
32
+ /**
33
+ * Recursively resolve the client context type produced by a sequence of middleware
34
+ */
35
+ export type AssignAllClientContextBeforeNext<
36
+ TMiddlewares,
37
+ TClientContext = undefined,
38
+ > = unknown extends TClientContext
39
+ ? TClientContext
40
+ : Assign<
41
+ AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>,
42
+ TClientContext
43
+ >
44
+
45
+ export type AssignAllClientSendContext<
46
+ TMiddlewares,
47
+ TSendContext = undefined,
48
+ > = unknown extends TSendContext
49
+ ? TSendContext
50
+ : Assign<
51
+ AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,
52
+ TSendContext
53
+ >
54
+
55
+ export type AssignAllClientContextAfterNext<
56
+ TMiddlewares,
57
+ TClientContext = undefined,
58
+ TSendContext = undefined,
59
+ > = unknown extends TClientContext
60
+ ? Assign<TClientContext, TSendContext>
61
+ : Assign<
62
+ AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>,
63
+ Assign<TClientContext, TSendContext>
64
+ >
65
+
66
+ /**
67
+ * Recursively resolve the server context type produced by a sequence of middleware
68
+ */
69
+ export type AssignAllServerContext<
70
+ TMiddlewares,
71
+ TSendContext = undefined,
72
+ TServerContext = undefined,
73
+ > = unknown extends TSendContext
74
+ ? Assign<TSendContext, TServerContext>
75
+ : Assign<
76
+ AssignAllMiddleware<TMiddlewares, 'allServerContext'>,
77
+ Assign<TSendContext, TServerContext>
78
+ >
79
+
80
+ export type AssignAllServerSendContext<
81
+ TMiddlewares,
82
+ TSendContext = undefined,
83
+ > = unknown extends TSendContext
84
+ ? TSendContext
85
+ : Assign<
86
+ AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,
87
+ TSendContext
88
+ >
89
+
90
+ export type IntersectAllMiddleware<
91
+ TMiddlewares,
92
+ TType extends keyof AnyMiddleware['_types'],
93
+ TAcc = undefined,
94
+ > = TMiddlewares extends readonly [
95
+ infer TMiddleware extends AnyMiddleware,
96
+ ...infer TRest,
97
+ ]
98
+ ? IntersectAllMiddleware<
99
+ TRest,
100
+ TType,
101
+ IntersectAssign<TAcc, TMiddleware['_types'][TType]>
102
+ >
103
+ : TAcc
104
+
105
+ /**
106
+ * Recursively resolve the input type produced by a sequence of middleware
107
+ */
108
+ export type IntersectAllValidatorInputs<TMiddlewares, TValidator> =
109
+ unknown extends TValidator
110
+ ? TValidator
111
+ : IntersectAssign<
112
+ IntersectAllMiddleware<TMiddlewares, 'allInput'>,
113
+ TValidator extends undefined
114
+ ? undefined
115
+ : ResolveValidatorInput<TValidator>
116
+ >
117
+ /**
118
+ * Recursively merge the output type produced by a sequence of middleware
119
+ */
120
+ export type IntersectAllValidatorOutputs<TMiddlewares, TValidator> =
121
+ unknown extends TValidator
122
+ ? TValidator
123
+ : IntersectAssign<
124
+ IntersectAllMiddleware<TMiddlewares, 'allOutput'>,
125
+ TValidator extends undefined
126
+ ? undefined
127
+ : ResolveValidatorOutput<TValidator>
128
+ >
129
+
130
+ export interface MiddlewareOptions<
131
+ in out TMiddlewares,
132
+ in out TValidator,
133
+ in out TServerContext,
134
+ in out TClientContext,
135
+ in out TServerFnResponseType extends ServerFnResponseType,
136
+ > {
137
+ validateClient?: boolean
138
+ middleware?: TMiddlewares
139
+ validator?: ConstrainValidator<TValidator>
140
+ client?: MiddlewareClientFn<
141
+ TMiddlewares,
142
+ TValidator,
143
+ TServerContext,
144
+ TClientContext,
145
+ TServerFnResponseType
146
+ >
147
+ server?: MiddlewareServerFn<
148
+ TMiddlewares,
149
+ TValidator,
150
+ TServerContext,
151
+ unknown,
152
+ unknown,
153
+ TServerFnResponseType
154
+ >
155
+ }
156
+
157
+ export type MiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <
158
+ TNewServerContext = undefined,
159
+ TSendContext = undefined,
160
+ >(ctx?: {
161
+ context?: TNewServerContext
162
+ sendContext?: SerializerStringify<TSendContext>
163
+ }) => Promise<
164
+ ServerResultWithContext<
165
+ TMiddlewares,
166
+ TServerSendContext,
167
+ TNewServerContext,
168
+ TSendContext
169
+ >
170
+ >
171
+
172
+ export interface MiddlewareServerFnOptions<
173
+ in out TMiddlewares,
174
+ in out TValidator,
175
+ in out TServerSendContext,
176
+ in out TServerFnResponseType,
177
+ > {
178
+ data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>
179
+ context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>
180
+ next: MiddlewareServerNextFn<TMiddlewares, TServerSendContext>
181
+ response: TServerFnResponseType
182
+ method: Method
183
+ filename: string
184
+ functionId: string
185
+ signal: AbortSignal
186
+ }
187
+
188
+ export type MiddlewareServerFn<
189
+ TMiddlewares,
190
+ TValidator,
191
+ TServerSendContext,
192
+ TNewServerContext,
193
+ TSendContext,
194
+ TServerFnResponseType extends ServerFnResponseType,
195
+ > = (
196
+ options: MiddlewareServerFnOptions<
197
+ TMiddlewares,
198
+ TValidator,
199
+ TServerSendContext,
200
+ TServerFnResponseType
201
+ >,
202
+ ) => MiddlewareServerFnResult<
203
+ TMiddlewares,
204
+ TServerSendContext,
205
+ TNewServerContext,
206
+ TSendContext
207
+ >
208
+
209
+ export type MiddlewareServerFnResult<
210
+ TMiddlewares,
211
+ TServerSendContext,
212
+ TServerContext,
213
+ TSendContext,
214
+ > =
215
+ | Promise<
216
+ ServerResultWithContext<
217
+ TMiddlewares,
218
+ TServerSendContext,
219
+ TServerContext,
220
+ TSendContext
221
+ >
222
+ >
223
+ | ServerResultWithContext<
224
+ TMiddlewares,
225
+ TServerSendContext,
226
+ TServerContext,
227
+ TSendContext
228
+ >
229
+
230
+ export type MiddlewareClientNextFn<TMiddlewares> = <
231
+ TSendContext = undefined,
232
+ TNewClientContext = undefined,
233
+ >(ctx?: {
234
+ context?: TNewClientContext
235
+ sendContext?: SerializerStringify<TSendContext>
236
+ headers?: HeadersInit
237
+ }) => Promise<
238
+ ClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>
239
+ >
240
+
241
+ export interface MiddlewareClientFnOptions<
242
+ in out TMiddlewares,
243
+ in out TValidator,
244
+ in out TServerFnResponseType extends ServerFnResponseType,
245
+ > {
246
+ data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>
247
+ context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>
248
+ sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>
249
+ method: Method
250
+ response: TServerFnResponseType
251
+ signal: AbortSignal
252
+ next: MiddlewareClientNextFn<TMiddlewares>
253
+ filename: string
254
+ functionId: string
255
+ type: ServerFnTypeOrTypeFn<
256
+ Method,
257
+ TServerFnResponseType,
258
+ TMiddlewares,
259
+ TValidator
260
+ >
261
+ }
262
+
263
+ export type MiddlewareClientFn<
264
+ TMiddlewares,
265
+ TValidator,
266
+ TSendContext,
267
+ TClientContext,
268
+ TServerFnResponseType extends ServerFnResponseType,
269
+ > = (
270
+ options: MiddlewareClientFnOptions<
271
+ TMiddlewares,
272
+ TValidator,
273
+ TServerFnResponseType
274
+ >,
275
+ ) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>
276
+
277
+ export type MiddlewareClientFnResult<
278
+ TMiddlewares,
279
+ TSendContext,
280
+ TClientContext,
281
+ > =
282
+ | Promise<ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>>
283
+ | ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>
284
+
285
+ export type ServerResultWithContext<
286
+ in out TMiddlewares,
287
+ in out TServerSendContext,
288
+ in out TServerContext,
289
+ in out TSendContext,
290
+ > = {
291
+ 'use functions must return the result of next()': true
292
+ _types: {
293
+ context: TServerContext
294
+ sendContext: TSendContext
295
+ }
296
+ context: Expand<
297
+ AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>
298
+ >
299
+ sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>
300
+ }
301
+
302
+ export type ClientResultWithContext<
303
+ in out TMiddlewares,
304
+ in out TSendContext,
305
+ in out TClientContext,
306
+ > = {
307
+ 'use functions must return the result of next()': true
308
+ context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>
309
+ sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>
310
+ headers: HeadersInit
311
+ }
312
+
313
+ export type AnyMiddleware = MiddlewareWithTypes<
314
+ any,
315
+ any,
316
+ any,
317
+ any,
318
+ any,
319
+ any,
320
+ any
321
+ >
322
+
323
+ export interface MiddlewareTypes<
324
+ in out TMiddlewares,
325
+ in out TValidator,
326
+ in out TServerContext,
327
+ in out TServerSendContext,
328
+ in out TClientContext,
329
+ in out TClientSendContext,
330
+ > {
331
+ middlewares: TMiddlewares
332
+ input: ResolveValidatorInput<TValidator>
333
+ allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>
334
+ output: ResolveValidatorOutput<TValidator>
335
+ allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>
336
+ clientContext: TClientContext
337
+ allClientContextBeforeNext: AssignAllClientContextBeforeNext<
338
+ TMiddlewares,
339
+ TClientContext
340
+ >
341
+ allClientContextAfterNext: AssignAllClientContextAfterNext<
342
+ TMiddlewares,
343
+ TClientContext,
344
+ TClientSendContext
345
+ >
346
+ serverContext: TServerContext
347
+ serverSendContext: TServerSendContext
348
+ allServerSendContext: AssignAllServerSendContext<
349
+ TMiddlewares,
350
+ TServerSendContext
351
+ >
352
+ allServerContext: AssignAllServerContext<
353
+ TMiddlewares,
354
+ TServerSendContext,
355
+ TServerContext
356
+ >
357
+ clientSendContext: TClientSendContext
358
+ allClientSendContext: AssignAllClientSendContext<
359
+ TMiddlewares,
360
+ TClientSendContext
361
+ >
362
+ validator: TValidator
363
+ }
364
+
365
+ export interface MiddlewareWithTypes<
366
+ TMiddlewares,
367
+ TValidator,
368
+ TServerContext,
369
+ TServerSendContext,
370
+ TClientContext,
371
+ TClientSendContext,
372
+ TServerFnResponseType extends ServerFnResponseType,
373
+ > {
374
+ _types: MiddlewareTypes<
375
+ TMiddlewares,
376
+ TValidator,
377
+ TServerContext,
378
+ TServerSendContext,
379
+ TClientContext,
380
+ TClientSendContext
381
+ >
382
+ options: MiddlewareOptions<
383
+ TMiddlewares,
384
+ TValidator,
385
+ TServerContext,
386
+ TClientContext,
387
+ TServerFnResponseType
388
+ >
389
+ }
390
+
391
+ export interface MiddlewareAfterValidator<
392
+ TMiddlewares,
393
+ TValidator,
394
+ TServerFnResponseType extends ServerFnResponseType,
395
+ > extends MiddlewareWithTypes<
396
+ TMiddlewares,
397
+ TValidator,
398
+ undefined,
399
+ undefined,
400
+ undefined,
401
+ undefined,
402
+ ServerFnResponseType
403
+ >,
404
+ MiddlewareServer<
405
+ TMiddlewares,
406
+ TValidator,
407
+ undefined,
408
+ undefined,
409
+ TServerFnResponseType
410
+ >,
411
+ MiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}
412
+
413
+ export interface MiddlewareValidator<
414
+ TMiddlewares,
415
+ TServerFnResponseType extends ServerFnResponseType,
416
+ > {
417
+ validator: <TNewValidator>(
418
+ input: ConstrainValidator<TNewValidator>,
419
+ ) => MiddlewareAfterValidator<
420
+ TMiddlewares,
421
+ TNewValidator,
422
+ TServerFnResponseType
423
+ >
424
+ }
425
+
426
+ export interface MiddlewareAfterServer<
427
+ TMiddlewares,
428
+ TValidator,
429
+ TServerContext,
430
+ TServerSendContext,
431
+ TClientContext,
432
+ TClientSendContext,
433
+ TServerFnResponseType extends ServerFnResponseType,
434
+ > extends MiddlewareWithTypes<
435
+ TMiddlewares,
436
+ TValidator,
437
+ TServerContext,
438
+ TServerSendContext,
439
+ TClientContext,
440
+ TClientSendContext,
441
+ TServerFnResponseType
442
+ > {}
443
+
444
+ export interface MiddlewareServer<
445
+ TMiddlewares,
446
+ TValidator,
447
+ TServerSendContext,
448
+ TClientContext,
449
+ TServerFnResponseType extends ServerFnResponseType,
450
+ > {
451
+ server: <TNewServerContext = undefined, TSendContext = undefined>(
452
+ server: MiddlewareServerFn<
453
+ TMiddlewares,
454
+ TValidator,
455
+ TServerSendContext,
456
+ TNewServerContext,
457
+ TSendContext,
458
+ TServerFnResponseType
459
+ >,
460
+ ) => MiddlewareAfterServer<
461
+ TMiddlewares,
462
+ TValidator,
463
+ TNewServerContext,
464
+ TServerSendContext,
465
+ TClientContext,
466
+ TSendContext,
467
+ ServerFnResponseType
468
+ >
469
+ }
470
+
471
+ export interface MiddlewareAfterClient<
472
+ TMiddlewares,
473
+ TValidator,
474
+ TServerSendContext,
475
+ TClientContext,
476
+ TServerFnResponseType extends ServerFnResponseType,
477
+ > extends MiddlewareWithTypes<
478
+ TMiddlewares,
479
+ TValidator,
480
+ undefined,
481
+ TServerSendContext,
482
+ TClientContext,
483
+ undefined,
484
+ TServerFnResponseType
485
+ >,
486
+ MiddlewareServer<
487
+ TMiddlewares,
488
+ TValidator,
489
+ TServerSendContext,
490
+ TClientContext,
491
+ TServerFnResponseType
492
+ > {}
493
+
494
+ export interface MiddlewareClient<
495
+ TMiddlewares,
496
+ TValidator,
497
+ TServerFnResponseType extends ServerFnResponseType,
498
+ > {
499
+ client: <TSendServerContext = undefined, TNewClientContext = undefined>(
500
+ client: MiddlewareClientFn<
501
+ TMiddlewares,
502
+ TValidator,
503
+ TSendServerContext,
504
+ TNewClientContext,
505
+ TServerFnResponseType
506
+ >,
507
+ ) => MiddlewareAfterClient<
508
+ TMiddlewares,
509
+ TValidator,
510
+ TSendServerContext,
511
+ TNewClientContext,
512
+ ServerFnResponseType
513
+ >
514
+ }
515
+
516
+ export interface MiddlewareAfterMiddleware<
517
+ TMiddlewares,
518
+ TServerFnResponseType extends ServerFnResponseType,
519
+ > extends MiddlewareWithTypes<
520
+ TMiddlewares,
521
+ undefined,
522
+ undefined,
523
+ undefined,
524
+ undefined,
525
+ undefined,
526
+ TServerFnResponseType
527
+ >,
528
+ MiddlewareServer<
529
+ TMiddlewares,
530
+ undefined,
531
+ undefined,
532
+ undefined,
533
+ TServerFnResponseType
534
+ >,
535
+ MiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,
536
+ MiddlewareValidator<TMiddlewares, TServerFnResponseType> {}
537
+
538
+ export interface Middleware<TServerFnResponseType extends ServerFnResponseType>
539
+ extends MiddlewareAfterMiddleware<unknown, TServerFnResponseType> {
540
+ middleware: <const TNewMiddlewares = undefined>(
541
+ middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,
542
+ ) => MiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>
543
+ }
544
+
545
+ export function createMiddleware(
546
+ options?: {
547
+ validateClient?: boolean
548
+ },
549
+ __opts?: MiddlewareOptions<
550
+ unknown,
551
+ undefined,
552
+ undefined,
553
+ undefined,
554
+ ServerFnResponseType
555
+ >,
556
+ ): Middleware<ServerFnResponseType> {
557
+ // const resolvedOptions = (__opts || options) as MiddlewareOptions<
558
+ const resolvedOptions =
559
+ __opts ||
560
+ ((options || {}) as MiddlewareOptions<
561
+ unknown,
562
+ undefined,
563
+ undefined,
564
+ undefined,
565
+ ServerFnResponseType
566
+ >)
567
+
568
+ return {
569
+ options: resolvedOptions as any,
570
+ middleware: (middleware: any) => {
571
+ return createMiddleware(
572
+ undefined,
573
+ Object.assign(resolvedOptions, { middleware }),
574
+ ) as any
575
+ },
576
+ validator: (validator: any) => {
577
+ return createMiddleware(
578
+ undefined,
579
+ Object.assign(resolvedOptions, { validator }),
580
+ ) as any
581
+ },
582
+ client: (client: any) => {
583
+ return createMiddleware(
584
+ undefined,
585
+ Object.assign(resolvedOptions, { client }),
586
+ ) as any
587
+ },
588
+ server: (server: any) => {
589
+ return createMiddleware(
590
+ undefined,
591
+ Object.assign(resolvedOptions, { server }),
592
+ ) as any
593
+ },
594
+ } as unknown as Middleware<ServerFnResponseType>
595
+ }