@tanstack/start-client-core 1.132.0-alpha.7 → 1.132.0-alpha.9
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/dist/esm/createMiddleware.d.ts +35 -36
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +45 -37
- package/dist/esm/createServerFn.js +23 -12
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.d.ts +1 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js +5 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +1 -1
- package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +1 -1
- package/dist/esm/serverFnFetcher.js.map +1 -1
- package/package.json +3 -3
- package/src/createMiddleware.ts +101 -28
- package/src/createServerFn.ts +177 -52
- package/src/index.tsx +0 -9
- package/src/serializer/ServerFunctionSerializationAdapter.ts +7 -1
- package/src/serializer/getDefaultSerovalPlugins.ts +2 -2
- package/src/serverFnFetcher.ts +3 -2
- package/src/tests/createServerFn.test-d.ts +90 -4
- package/src/tests/createServerMiddleware.test-d.ts +11 -9
- package/dist/esm/serializer.d.ts +0 -16
- package/src/serializer.ts +0 -25
package/src/createServerFn.ts
CHANGED
|
@@ -3,17 +3,17 @@ import { mergeHeaders } from '@tanstack/router-core/ssr/client'
|
|
|
3
3
|
import { globalMiddleware } from './registerGlobalMiddleware'
|
|
4
4
|
|
|
5
5
|
import { getRouterInstance } from './getRouterInstance'
|
|
6
|
-
import type {
|
|
7
|
-
SerializerParse,
|
|
8
|
-
SerializerStringify,
|
|
9
|
-
SerializerStringifyBy,
|
|
10
|
-
} from './serializer'
|
|
11
6
|
import type {
|
|
12
7
|
AnyRouter,
|
|
13
8
|
AnyValidator,
|
|
14
9
|
Constrain,
|
|
15
10
|
Expand,
|
|
11
|
+
Register,
|
|
12
|
+
RegisteredSerializableInput,
|
|
16
13
|
ResolveValidatorInput,
|
|
14
|
+
ValidateSerializable,
|
|
15
|
+
ValidateSerializableInput,
|
|
16
|
+
ValidateSerializableInputResult,
|
|
17
17
|
Validator,
|
|
18
18
|
} from '@tanstack/router-core'
|
|
19
19
|
import type { JsonResponse } from '@tanstack/router-core/ssr/client'
|
|
@@ -31,6 +31,7 @@ import type {
|
|
|
31
31
|
type TODO = any
|
|
32
32
|
|
|
33
33
|
export function createServerFn<
|
|
34
|
+
TRegister extends Register,
|
|
34
35
|
TMethod extends Method,
|
|
35
36
|
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
36
37
|
TResponse = unknown,
|
|
@@ -42,14 +43,16 @@ export function createServerFn<
|
|
|
42
43
|
response?: TServerFnResponseType
|
|
43
44
|
},
|
|
44
45
|
__opts?: ServerFnBaseOptions<
|
|
46
|
+
TRegister,
|
|
45
47
|
TMethod,
|
|
46
48
|
TServerFnResponseType,
|
|
47
49
|
TResponse,
|
|
48
50
|
TMiddlewares,
|
|
49
51
|
TValidator
|
|
50
52
|
>,
|
|
51
|
-
): ServerFnBuilder<TMethod, TServerFnResponseType> {
|
|
53
|
+
): ServerFnBuilder<TRegister, TMethod, TServerFnResponseType> {
|
|
52
54
|
const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<
|
|
55
|
+
TRegister,
|
|
53
56
|
TMethod,
|
|
54
57
|
ServerFnResponseType,
|
|
55
58
|
TResponse,
|
|
@@ -61,33 +64,43 @@ export function createServerFn<
|
|
|
61
64
|
resolvedOptions.method = 'GET' as TMethod
|
|
62
65
|
}
|
|
63
66
|
|
|
64
|
-
|
|
67
|
+
const res: ServerFnBuilder<TRegister, TMethod, TServerFnResponseType> = {
|
|
65
68
|
options: resolvedOptions as any,
|
|
66
69
|
middleware: (middleware) => {
|
|
70
|
+
// multiple calls to `middleware()` merge the middlewares with the previously supplied ones
|
|
71
|
+
// this is primarily useful for letting users create their own abstractions on top of `createServerFn`
|
|
72
|
+
const newOptions = {
|
|
73
|
+
...resolvedOptions,
|
|
74
|
+
middleware: [...(resolvedOptions.middleware || []), ...middleware],
|
|
75
|
+
}
|
|
67
76
|
return createServerFn<
|
|
77
|
+
TRegister,
|
|
68
78
|
TMethod,
|
|
69
79
|
ServerFnResponseType,
|
|
70
80
|
TResponse,
|
|
71
81
|
TMiddlewares,
|
|
72
82
|
TValidator
|
|
73
|
-
>(undefined,
|
|
83
|
+
>(undefined, newOptions) as any
|
|
74
84
|
},
|
|
75
85
|
validator: (validator) => {
|
|
86
|
+
const newOptions = { ...resolvedOptions, validator: validator as any }
|
|
76
87
|
return createServerFn<
|
|
88
|
+
TRegister,
|
|
77
89
|
TMethod,
|
|
78
90
|
ServerFnResponseType,
|
|
79
91
|
TResponse,
|
|
80
92
|
TMiddlewares,
|
|
81
93
|
TValidator
|
|
82
|
-
>(undefined,
|
|
94
|
+
>(undefined, newOptions) as any
|
|
83
95
|
},
|
|
84
96
|
handler: (...args) => {
|
|
85
97
|
// This function signature changes due to AST transformations
|
|
86
98
|
// in the babel plugin. We need to cast it to the correct
|
|
87
99
|
// function signature post-transformation
|
|
88
100
|
const [extractedFn, serverFn] = args as unknown as [
|
|
89
|
-
CompiledFetcherFn<TResponse, TServerFnResponseType>,
|
|
101
|
+
CompiledFetcherFn<TRegister, TResponse, TServerFnResponseType>,
|
|
90
102
|
ServerFn<
|
|
103
|
+
TRegister,
|
|
91
104
|
TMethod,
|
|
92
105
|
TServerFnResponseType,
|
|
93
106
|
TMiddlewares,
|
|
@@ -98,15 +111,11 @@ export function createServerFn<
|
|
|
98
111
|
|
|
99
112
|
// Keep the original function around so we can use it
|
|
100
113
|
// in the server environment
|
|
101
|
-
|
|
102
|
-
...extractedFn,
|
|
103
|
-
extractedFn,
|
|
104
|
-
serverFn,
|
|
105
|
-
})
|
|
114
|
+
const newOptions = { ...resolvedOptions, extractedFn, serverFn }
|
|
106
115
|
|
|
107
116
|
const resolvedMiddleware = [
|
|
108
|
-
...(
|
|
109
|
-
serverFnBaseToMiddleware(
|
|
117
|
+
...(newOptions.middleware || []),
|
|
118
|
+
serverFnBaseToMiddleware(newOptions),
|
|
110
119
|
]
|
|
111
120
|
|
|
112
121
|
// We want to make sure the new function has the same
|
|
@@ -117,14 +126,14 @@ export function createServerFn<
|
|
|
117
126
|
// Start by executing the client-side middleware chain
|
|
118
127
|
return executeMiddleware(resolvedMiddleware, 'client', {
|
|
119
128
|
...extractedFn,
|
|
120
|
-
...
|
|
129
|
+
...newOptions,
|
|
121
130
|
data: opts?.data as any,
|
|
122
131
|
headers: opts?.headers,
|
|
123
132
|
signal: opts?.signal,
|
|
124
133
|
context: {},
|
|
125
134
|
router: getRouterInstance(),
|
|
126
135
|
}).then((d) => {
|
|
127
|
-
if (
|
|
136
|
+
if (newOptions.response === 'full') {
|
|
128
137
|
return d
|
|
129
138
|
}
|
|
130
139
|
if (d.error) throw d.error
|
|
@@ -156,6 +165,19 @@ export function createServerFn<
|
|
|
156
165
|
) as any
|
|
157
166
|
},
|
|
158
167
|
}
|
|
168
|
+
const fun = (options?: {
|
|
169
|
+
method?: TMethod
|
|
170
|
+
response?: TServerFnResponseType
|
|
171
|
+
}) => {
|
|
172
|
+
return {
|
|
173
|
+
...res,
|
|
174
|
+
options: {
|
|
175
|
+
...res.options,
|
|
176
|
+
...options,
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return Object.assign(fun, res)
|
|
159
181
|
}
|
|
160
182
|
|
|
161
183
|
export async function executeMiddleware(
|
|
@@ -230,6 +252,7 @@ export type CompiledFetcherFnOptions = {
|
|
|
230
252
|
}
|
|
231
253
|
|
|
232
254
|
export type Fetcher<
|
|
255
|
+
TRegister extends Register,
|
|
233
256
|
TMiddlewares,
|
|
234
257
|
TValidator,
|
|
235
258
|
TResponse,
|
|
@@ -237,12 +260,14 @@ export type Fetcher<
|
|
|
237
260
|
> =
|
|
238
261
|
undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>
|
|
239
262
|
? OptionalFetcher<
|
|
263
|
+
TRegister,
|
|
240
264
|
TMiddlewares,
|
|
241
265
|
TValidator,
|
|
242
266
|
TResponse,
|
|
243
267
|
TServerFnResponseType
|
|
244
268
|
>
|
|
245
269
|
: RequiredFetcher<
|
|
270
|
+
TRegister,
|
|
246
271
|
TMiddlewares,
|
|
247
272
|
TValidator,
|
|
248
273
|
TResponse,
|
|
@@ -262,16 +287,18 @@ export interface FetcherBase {
|
|
|
262
287
|
}
|
|
263
288
|
|
|
264
289
|
export type FetchResult<
|
|
290
|
+
TRegister extends Register,
|
|
265
291
|
TMiddlewares,
|
|
266
292
|
TResponse,
|
|
267
293
|
TServerFnResponseType extends ServerFnResponseType,
|
|
268
294
|
> = TServerFnResponseType extends 'raw'
|
|
269
295
|
? Promise<Response>
|
|
270
296
|
: TServerFnResponseType extends 'full'
|
|
271
|
-
? Promise<FullFetcherData<TMiddlewares, TResponse>>
|
|
272
|
-
: Promise<FetcherData<TResponse>>
|
|
297
|
+
? Promise<FullFetcherData<TRegister, TMiddlewares, TResponse>>
|
|
298
|
+
: Promise<FetcherData<TRegister, TResponse>>
|
|
273
299
|
|
|
274
300
|
export interface OptionalFetcher<
|
|
301
|
+
TRegister extends Register,
|
|
275
302
|
TMiddlewares,
|
|
276
303
|
TValidator,
|
|
277
304
|
TResponse,
|
|
@@ -279,10 +306,11 @@ export interface OptionalFetcher<
|
|
|
279
306
|
> extends FetcherBase {
|
|
280
307
|
(
|
|
281
308
|
options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>,
|
|
282
|
-
): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>
|
|
309
|
+
): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>
|
|
283
310
|
}
|
|
284
311
|
|
|
285
312
|
export interface RequiredFetcher<
|
|
313
|
+
TRegister extends Register,
|
|
286
314
|
TMiddlewares,
|
|
287
315
|
TValidator,
|
|
288
316
|
TResponse,
|
|
@@ -290,7 +318,7 @@ export interface RequiredFetcher<
|
|
|
290
318
|
> extends FetcherBase {
|
|
291
319
|
(
|
|
292
320
|
opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>,
|
|
293
|
-
): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>
|
|
321
|
+
): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>
|
|
294
322
|
}
|
|
295
323
|
|
|
296
324
|
export type FetcherBaseOptions = {
|
|
@@ -308,16 +336,20 @@ export interface RequiredFetcherDataOptions<TMiddlewares, TValidator>
|
|
|
308
336
|
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>
|
|
309
337
|
}
|
|
310
338
|
|
|
311
|
-
export interface FullFetcherData<
|
|
339
|
+
export interface FullFetcherData<
|
|
340
|
+
TRegister extends Register,
|
|
341
|
+
TMiddlewares,
|
|
342
|
+
TResponse,
|
|
343
|
+
> {
|
|
312
344
|
error: unknown
|
|
313
|
-
result: FetcherData<TResponse>
|
|
345
|
+
result: FetcherData<TRegister, TResponse>
|
|
314
346
|
context: AssignAllClientSendContext<TMiddlewares>
|
|
315
347
|
}
|
|
316
348
|
|
|
317
|
-
export type FetcherData<TResponse> =
|
|
349
|
+
export type FetcherData<TRegister extends Register, TResponse> =
|
|
318
350
|
TResponse extends JsonResponse<any>
|
|
319
|
-
?
|
|
320
|
-
:
|
|
351
|
+
? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>>
|
|
352
|
+
: ValidateSerializableInputResult<TRegister, TResponse>
|
|
321
353
|
|
|
322
354
|
export type RscStream<T> = {
|
|
323
355
|
__cacheState: T
|
|
@@ -330,13 +362,17 @@ export type ServerFnResponseType = 'data' | 'full' | 'raw'
|
|
|
330
362
|
export type RawResponse = Response | ReadableStream | Readable | null | string
|
|
331
363
|
|
|
332
364
|
export type ServerFnReturnType<
|
|
365
|
+
TRegister extends Register,
|
|
333
366
|
TServerFnResponseType extends ServerFnResponseType,
|
|
334
367
|
TResponse,
|
|
335
368
|
> = TServerFnResponseType extends 'raw'
|
|
336
369
|
? RawResponse | Promise<RawResponse>
|
|
337
|
-
:
|
|
370
|
+
:
|
|
371
|
+
| Promise<ValidateSerializableInput<TRegister, TResponse>>
|
|
372
|
+
| ValidateSerializableInput<TRegister, TResponse>
|
|
338
373
|
|
|
339
374
|
export type ServerFn<
|
|
375
|
+
TRegister extends Register,
|
|
340
376
|
TMethod,
|
|
341
377
|
TServerFnResponseType extends ServerFnResponseType,
|
|
342
378
|
TMiddlewares,
|
|
@@ -344,7 +380,7 @@ export type ServerFn<
|
|
|
344
380
|
TResponse,
|
|
345
381
|
> = (
|
|
346
382
|
ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,
|
|
347
|
-
) => ServerFnReturnType<TServerFnResponseType, TResponse>
|
|
383
|
+
) => ServerFnReturnType<TRegister, TServerFnResponseType, TResponse>
|
|
348
384
|
|
|
349
385
|
export interface ServerFnCtx<
|
|
350
386
|
TMethod,
|
|
@@ -360,17 +396,19 @@ export interface ServerFnCtx<
|
|
|
360
396
|
}
|
|
361
397
|
|
|
362
398
|
export type CompiledFetcherFn<
|
|
399
|
+
TRegister extends Register,
|
|
363
400
|
TResponse,
|
|
364
401
|
TServerFnResponseType extends ServerFnResponseType,
|
|
365
402
|
> = {
|
|
366
403
|
(
|
|
367
404
|
opts: CompiledFetcherFnOptions &
|
|
368
|
-
ServerFnBaseOptions<Method, TServerFnResponseType>,
|
|
405
|
+
ServerFnBaseOptions<TRegister, Method, TServerFnResponseType>,
|
|
369
406
|
): Promise<TResponse>
|
|
370
407
|
url: string
|
|
371
408
|
}
|
|
372
409
|
|
|
373
410
|
export type ServerFnBaseOptions<
|
|
411
|
+
TRegister extends Register,
|
|
374
412
|
TMethod extends Method = 'GET',
|
|
375
413
|
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
376
414
|
TResponse = unknown,
|
|
@@ -381,9 +419,10 @@ export type ServerFnBaseOptions<
|
|
|
381
419
|
response?: TServerFnResponseType
|
|
382
420
|
validateClient?: boolean
|
|
383
421
|
middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>
|
|
384
|
-
validator?: ConstrainValidator<TInput>
|
|
385
|
-
extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>
|
|
422
|
+
validator?: ConstrainValidator<TRegister, TInput>
|
|
423
|
+
extractedFn?: CompiledFetcherFn<TRegister, TResponse, TServerFnResponseType>
|
|
386
424
|
serverFn?: ServerFn<
|
|
425
|
+
TRegister,
|
|
387
426
|
TMethod,
|
|
388
427
|
TServerFnResponseType,
|
|
389
428
|
TMiddlewares,
|
|
@@ -393,27 +432,38 @@ export type ServerFnBaseOptions<
|
|
|
393
432
|
functionId: string
|
|
394
433
|
}
|
|
395
434
|
|
|
396
|
-
export type
|
|
435
|
+
export type ValidateValidatorInput<
|
|
436
|
+
TRegister extends Register,
|
|
437
|
+
TValidator,
|
|
438
|
+
> = ValidateSerializable<
|
|
397
439
|
ResolveValidatorInput<TValidator>,
|
|
398
|
-
|
|
440
|
+
RegisteredSerializableInput<TRegister> | FormData
|
|
399
441
|
>
|
|
400
442
|
|
|
401
|
-
export type
|
|
402
|
-
|
|
443
|
+
export type ValidateValidator<TRegister extends Register, TValidator> =
|
|
444
|
+
ValidateValidatorInput<TRegister, TValidator> extends infer TInput
|
|
403
445
|
? Validator<TInput, any>
|
|
404
446
|
: never
|
|
405
447
|
|
|
406
|
-
export type ConstrainValidator<TValidator> =
|
|
448
|
+
export type ConstrainValidator<TRegister extends Register, TValidator> =
|
|
407
449
|
| (unknown extends TValidator
|
|
408
450
|
? TValidator
|
|
409
|
-
: ResolveValidatorInput<TValidator> extends
|
|
451
|
+
: ResolveValidatorInput<TValidator> extends ValidateValidator<
|
|
452
|
+
TRegister,
|
|
453
|
+
TValidator
|
|
454
|
+
>
|
|
410
455
|
? TValidator
|
|
411
456
|
: never)
|
|
412
|
-
|
|
|
457
|
+
| ValidateValidator<TRegister, TValidator>
|
|
458
|
+
|
|
459
|
+
type ToTuple<T> =
|
|
460
|
+
T extends ReadonlyArray<infer U> ? T : T extends undefined ? [] : [T]
|
|
413
461
|
|
|
414
462
|
export interface ServerFnMiddleware<
|
|
463
|
+
TRegister extends Register,
|
|
415
464
|
TMethod extends Method,
|
|
416
465
|
TServerFnResponseType extends ServerFnResponseType,
|
|
466
|
+
TMiddlewares,
|
|
417
467
|
TValidator,
|
|
418
468
|
> {
|
|
419
469
|
middleware: <const TNewMiddlewares = undefined>(
|
|
@@ -422,28 +472,60 @@ export interface ServerFnMiddleware<
|
|
|
422
472
|
ReadonlyArray<AnyFunctionMiddleware>
|
|
423
473
|
>,
|
|
424
474
|
) => ServerFnAfterMiddleware<
|
|
475
|
+
TRegister,
|
|
425
476
|
TMethod,
|
|
426
477
|
TServerFnResponseType,
|
|
427
|
-
TNewMiddlewares,
|
|
478
|
+
[...ToTuple<TMiddlewares>, ...ToTuple<TNewMiddlewares>],
|
|
428
479
|
TValidator
|
|
429
480
|
>
|
|
430
481
|
}
|
|
431
482
|
|
|
432
483
|
export interface ServerFnAfterMiddleware<
|
|
484
|
+
TRegister extends Register,
|
|
433
485
|
TMethod extends Method,
|
|
434
486
|
TServerFnResponseType extends ServerFnResponseType,
|
|
435
487
|
TMiddlewares,
|
|
436
488
|
TValidator,
|
|
437
|
-
> extends
|
|
438
|
-
|
|
489
|
+
> extends ServerFnMiddleware<
|
|
490
|
+
TRegister,
|
|
491
|
+
TMethod,
|
|
492
|
+
TServerFnResponseType,
|
|
493
|
+
TMiddlewares,
|
|
494
|
+
undefined
|
|
495
|
+
>,
|
|
496
|
+
ServerFnValidator<TRegister, TMethod, TServerFnResponseType, TMiddlewares>,
|
|
497
|
+
ServerFnHandler<
|
|
498
|
+
TRegister,
|
|
499
|
+
TMethod,
|
|
500
|
+
TServerFnResponseType,
|
|
501
|
+
TMiddlewares,
|
|
502
|
+
TValidator
|
|
503
|
+
> {
|
|
504
|
+
<
|
|
505
|
+
TNewMethod extends Method = TMethod,
|
|
506
|
+
TNewServerFnResponseType extends
|
|
507
|
+
ServerFnResponseType = TServerFnResponseType,
|
|
508
|
+
>(options?: {
|
|
509
|
+
method?: TNewMethod
|
|
510
|
+
response?: TNewServerFnResponseType
|
|
511
|
+
}): ServerFnAfterMiddleware<
|
|
512
|
+
TRegister,
|
|
513
|
+
TNewMethod,
|
|
514
|
+
TNewServerFnResponseType,
|
|
515
|
+
TMiddlewares,
|
|
516
|
+
TValidator
|
|
517
|
+
>
|
|
518
|
+
}
|
|
439
519
|
|
|
440
520
|
export type ValidatorFn<
|
|
521
|
+
TRegister extends Register,
|
|
441
522
|
TMethod extends Method,
|
|
442
523
|
TServerFnResponseType extends ServerFnResponseType,
|
|
443
524
|
TMiddlewares,
|
|
444
525
|
> = <TValidator>(
|
|
445
|
-
validator: ConstrainValidator<TValidator>,
|
|
526
|
+
validator: ConstrainValidator<TRegister, TValidator>,
|
|
446
527
|
) => ServerFnAfterValidator<
|
|
528
|
+
TRegister,
|
|
447
529
|
TMethod,
|
|
448
530
|
TServerFnResponseType,
|
|
449
531
|
TMiddlewares,
|
|
@@ -451,27 +533,48 @@ export type ValidatorFn<
|
|
|
451
533
|
>
|
|
452
534
|
|
|
453
535
|
export interface ServerFnValidator<
|
|
536
|
+
TRegister extends Register,
|
|
454
537
|
TMethod extends Method,
|
|
455
538
|
TServerFnResponseType extends ServerFnResponseType,
|
|
456
539
|
TMiddlewares,
|
|
457
540
|
> {
|
|
458
|
-
validator: ValidatorFn<
|
|
541
|
+
validator: ValidatorFn<
|
|
542
|
+
TRegister,
|
|
543
|
+
TMethod,
|
|
544
|
+
TServerFnResponseType,
|
|
545
|
+
TMiddlewares
|
|
546
|
+
>
|
|
459
547
|
}
|
|
460
548
|
|
|
461
549
|
export interface ServerFnAfterValidator<
|
|
550
|
+
TRegister extends Register,
|
|
462
551
|
TMethod extends Method,
|
|
463
552
|
TServerFnResponseType extends ServerFnResponseType,
|
|
464
553
|
TMiddlewares,
|
|
465
554
|
TValidator,
|
|
466
|
-
> extends ServerFnMiddleware<
|
|
467
|
-
|
|
555
|
+
> extends ServerFnMiddleware<
|
|
556
|
+
TRegister,
|
|
557
|
+
TMethod,
|
|
558
|
+
TServerFnResponseType,
|
|
559
|
+
TMiddlewares,
|
|
560
|
+
TValidator
|
|
561
|
+
>,
|
|
562
|
+
ServerFnHandler<
|
|
563
|
+
TRegister,
|
|
564
|
+
TMethod,
|
|
565
|
+
TServerFnResponseType,
|
|
566
|
+
TMiddlewares,
|
|
567
|
+
TValidator
|
|
568
|
+
> {}
|
|
468
569
|
|
|
469
570
|
export interface ServerFnAfterTyper<
|
|
571
|
+
TRegister extends Register,
|
|
470
572
|
TMethod extends Method,
|
|
471
573
|
TServerFnResponseType extends ServerFnResponseType,
|
|
472
574
|
TMiddlewares,
|
|
473
575
|
TValidator,
|
|
474
576
|
> extends ServerFnHandler<
|
|
577
|
+
TRegister,
|
|
475
578
|
TMethod,
|
|
476
579
|
TServerFnResponseType,
|
|
477
580
|
TMiddlewares,
|
|
@@ -480,6 +583,7 @@ export interface ServerFnAfterTyper<
|
|
|
480
583
|
|
|
481
584
|
// Handler
|
|
482
585
|
export interface ServerFnHandler<
|
|
586
|
+
TRegister extends Register,
|
|
483
587
|
TMethod extends Method,
|
|
484
588
|
TServerFnResponseType extends ServerFnResponseType,
|
|
485
589
|
TMiddlewares,
|
|
@@ -487,22 +591,43 @@ export interface ServerFnHandler<
|
|
|
487
591
|
> {
|
|
488
592
|
handler: <TNewResponse>(
|
|
489
593
|
fn?: ServerFn<
|
|
594
|
+
TRegister,
|
|
490
595
|
TMethod,
|
|
491
596
|
TServerFnResponseType,
|
|
492
597
|
TMiddlewares,
|
|
493
598
|
TValidator,
|
|
494
599
|
TNewResponse
|
|
495
600
|
>,
|
|
496
|
-
) => Fetcher<
|
|
601
|
+
) => Fetcher<
|
|
602
|
+
TRegister,
|
|
603
|
+
TMiddlewares,
|
|
604
|
+
TValidator,
|
|
605
|
+
TNewResponse,
|
|
606
|
+
TServerFnResponseType
|
|
607
|
+
>
|
|
497
608
|
}
|
|
498
609
|
|
|
499
610
|
export interface ServerFnBuilder<
|
|
611
|
+
TRegister extends Register,
|
|
500
612
|
TMethod extends Method = 'GET',
|
|
501
613
|
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
502
|
-
> extends ServerFnMiddleware<
|
|
503
|
-
|
|
504
|
-
|
|
614
|
+
> extends ServerFnMiddleware<
|
|
615
|
+
TRegister,
|
|
616
|
+
TMethod,
|
|
617
|
+
TServerFnResponseType,
|
|
618
|
+
undefined,
|
|
619
|
+
undefined
|
|
620
|
+
>,
|
|
621
|
+
ServerFnValidator<TRegister, TMethod, TServerFnResponseType, undefined>,
|
|
622
|
+
ServerFnHandler<
|
|
623
|
+
TRegister,
|
|
624
|
+
TMethod,
|
|
625
|
+
TServerFnResponseType,
|
|
626
|
+
undefined,
|
|
627
|
+
undefined
|
|
628
|
+
> {
|
|
505
629
|
options: ServerFnBaseOptions<
|
|
630
|
+
TRegister,
|
|
506
631
|
TMethod,
|
|
507
632
|
TServerFnResponseType,
|
|
508
633
|
unknown,
|
|
@@ -627,7 +752,7 @@ export function execValidator(
|
|
|
627
752
|
}
|
|
628
753
|
|
|
629
754
|
export function serverFnBaseToMiddleware(
|
|
630
|
-
options: ServerFnBaseOptions<any, any, any, any, any>,
|
|
755
|
+
options: ServerFnBaseOptions<any, any, any, any, any, any>,
|
|
631
756
|
): AnyFunctionMiddleware {
|
|
632
757
|
return {
|
|
633
758
|
_types: undefined!,
|
package/src/index.tsx
CHANGED
|
@@ -5,15 +5,6 @@ export type {
|
|
|
5
5
|
|
|
6
6
|
export { hydrate, json, mergeHeaders } from '@tanstack/router-core/ssr/client'
|
|
7
7
|
|
|
8
|
-
export type {
|
|
9
|
-
Serializable,
|
|
10
|
-
SerializerParse,
|
|
11
|
-
SerializerParseBy,
|
|
12
|
-
SerializerStringify,
|
|
13
|
-
SerializerStringifyBy,
|
|
14
|
-
SerializerExtensions,
|
|
15
|
-
} from './serializer'
|
|
16
|
-
|
|
17
8
|
export {
|
|
18
9
|
createIsomorphicFn,
|
|
19
10
|
type IsomorphicFn,
|
|
@@ -4,7 +4,13 @@ import { TSS_SERVER_FUNCTION } from '../constants'
|
|
|
4
4
|
|
|
5
5
|
export const ServerFunctionSerializationAdapter = createSerializationAdapter({
|
|
6
6
|
key: '$TSS/serverfn',
|
|
7
|
-
test: (v): v is { functionId: string } =>
|
|
7
|
+
test: (v): v is { functionId: string } => {
|
|
8
|
+
if (typeof v !== 'object' || v === null) return false
|
|
9
|
+
|
|
10
|
+
if (!(TSS_SERVER_FUNCTION in v)) return false
|
|
11
|
+
|
|
12
|
+
return !!v[TSS_SERVER_FUNCTION]
|
|
13
|
+
},
|
|
8
14
|
toSerializable: ({ functionId }) => ({ functionId }),
|
|
9
15
|
fromSerializable: ({ functionId }) => createClientRpc(functionId),
|
|
10
16
|
})
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
} from '@tanstack/router-core'
|
|
6
6
|
import { FormDataPlugin } from 'seroval-plugins/web'
|
|
7
7
|
import { getRouterInstance } from '../getRouterInstance'
|
|
8
|
-
import type {
|
|
8
|
+
import type { AnySerializationAdapter } from '@tanstack/router-core'
|
|
9
9
|
|
|
10
10
|
import type { Plugin } from 'seroval'
|
|
11
11
|
|
|
@@ -18,7 +18,7 @@ export function getDefaultSerovalPlugins() {
|
|
|
18
18
|
const router = getRouterInstance()
|
|
19
19
|
invariant(router, 'Expected router instance to be available')
|
|
20
20
|
const adapters = router.options.serializationAdapters as
|
|
21
|
-
| Array<
|
|
21
|
+
| Array<AnySerializationAdapter>
|
|
22
22
|
| undefined
|
|
23
23
|
return [...(adapters?.map(makeSerovalPlugin) ?? []), ...defaultSerovalPlugins]
|
|
24
24
|
}
|
package/src/serverFnFetcher.ts
CHANGED
|
@@ -30,6 +30,7 @@ export async function serverFnFetcher(
|
|
|
30
30
|
any,
|
|
31
31
|
any,
|
|
32
32
|
any,
|
|
33
|
+
any,
|
|
33
34
|
any
|
|
34
35
|
> & {
|
|
35
36
|
headers: HeadersInit
|
|
@@ -100,7 +101,7 @@ export async function serverFnFetcher(
|
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
async function serializePayload(
|
|
103
|
-
opts: FunctionMiddlewareClientFnOptions<any, any, any, any>,
|
|
104
|
+
opts: FunctionMiddlewareClientFnOptions<any, any, any, any, any>,
|
|
104
105
|
) {
|
|
105
106
|
const payloadToSerialize: any = {}
|
|
106
107
|
if (opts.data) {
|
|
@@ -121,7 +122,7 @@ async function serialize(data: any) {
|
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
async function getFetcherRequestOptions(
|
|
124
|
-
opts: FunctionMiddlewareClientFnOptions<any, any, any, any>,
|
|
125
|
+
opts: FunctionMiddlewareClientFnOptions<any, any, any, any, any>,
|
|
125
126
|
) {
|
|
126
127
|
if (opts.method === 'POST') {
|
|
127
128
|
if (opts.data instanceof FormData) {
|