@tanstack/start-client-core 1.132.0-alpha.8 → 1.132.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/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.js +2 -2
- package/dist/esm/client/ServerFunctionSerializationAdapter.js.map +1 -0
- package/dist/esm/client/createClientRpc.d.ts +6 -0
- package/dist/esm/{createClientRpc.js → client/createClientRpc.js} +4 -4
- package/dist/esm/client/createClientRpc.js.map +1 -0
- package/dist/esm/client/hydrateStart.d.ts +2 -0
- package/dist/esm/client/hydrateStart.js +29 -0
- package/dist/esm/client/hydrateStart.js.map +1 -0
- package/dist/esm/client/index.d.ts +2 -0
- package/dist/esm/client/index.js +7 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/{serverFnFetcher.js → client/serverFnFetcher.js} +6 -6
- package/dist/esm/client/serverFnFetcher.js.map +1 -0
- package/dist/esm/constants.d.ts +1 -0
- package/dist/esm/constants.js +4 -0
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/createMiddleware.d.ts +95 -86
- package/dist/esm/createMiddleware.js +5 -5
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +64 -61
- package/dist/esm/createServerFn.js +57 -30
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/createStart.d.ts +27 -0
- package/dist/esm/createStart.js +14 -0
- package/dist/esm/createStart.js.map +1 -0
- package/dist/esm/fake-start-entry.d.ts +5 -0
- package/dist/esm/fake-start-entry.js +11 -0
- package/dist/esm/fake-start-entry.js.map +1 -0
- package/dist/esm/getDefaultSerovalPlugins.d.ts +1 -0
- package/dist/esm/getDefaultSerovalPlugins.js +14 -0
- package/dist/esm/getDefaultSerovalPlugins.js.map +1 -0
- package/dist/esm/getGlobalStartContext.d.ts +3 -0
- package/dist/esm/getGlobalStartContext.js +15 -0
- package/dist/esm/getGlobalStartContext.js.map +1 -0
- package/dist/esm/getRouterInstance.d.ts +2 -1
- package/dist/esm/getRouterInstance.js +1 -1
- package/dist/esm/getRouterInstance.js.map +1 -1
- package/dist/esm/getServerContextAfterGlobalMiddlewares.d.ts +1 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js +10 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js.map +1 -0
- package/dist/esm/getStartOptions.d.ts +1 -0
- package/dist/esm/getStartOptions.js +7 -0
- package/dist/esm/getStartOptions.js.map +1 -0
- package/dist/esm/global.d.ts +7 -0
- package/dist/esm/index.d.ts +11 -6
- package/dist/esm/index.js +9 -9
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/serverRoute.d.ts +66 -0
- package/dist/esm/startEntry.d.ts +8 -0
- package/package.json +18 -5
- package/src/{serializer → client}/ServerFunctionSerializationAdapter.ts +2 -2
- package/src/{createClientRpc.ts → client/createClientRpc.ts} +5 -3
- package/src/client/hydrateStart.ts +33 -0
- package/src/client/index.ts +3 -0
- package/src/{serverFnFetcher.ts → client/serverFnFetcher.ts} +11 -16
- package/src/constants.ts +3 -0
- package/src/createMiddleware.ts +269 -255
- package/src/createServerFn.ts +277 -290
- package/src/createStart.ts +122 -0
- package/src/fake-start-entry.ts +4 -0
- package/src/getDefaultSerovalPlugins.ts +17 -0
- package/src/getGlobalStartContext.ts +18 -0
- package/src/getRouterInstance.ts +5 -4
- package/src/getServerContextAfterGlobalMiddlewares.ts +7 -0
- package/src/getStartOptions.ts +6 -0
- package/src/global.ts +9 -0
- package/src/index.tsx +12 -11
- package/src/serverRoute.ts +474 -0
- package/src/start-entry.d.ts +11 -0
- package/src/startEntry.ts +10 -0
- package/src/tests/createServerFn.test-d.ts +147 -123
- package/src/tests/createServerMiddleware.test-d.ts +22 -17
- package/dist/esm/createClientRpc.d.ts +0 -6
- package/dist/esm/createClientRpc.js.map +0 -1
- package/dist/esm/registerGlobalMiddleware.d.ts +0 -5
- package/dist/esm/registerGlobalMiddleware.js +0 -9
- package/dist/esm/registerGlobalMiddleware.js.map +0 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +0 -1
- package/dist/esm/serializer/getClientSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getClientSerovalPlugins.js +0 -13
- package/dist/esm/serializer/getClientSerovalPlugins.js.map +0 -1
- package/dist/esm/serializer/getDefaultSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getDefaultSerovalPlugins.js +0 -19
- package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +0 -1
- package/dist/esm/serverFnFetcher.js.map +0 -1
- package/src/registerGlobalMiddleware.ts +0 -9
- package/src/serializer/getClientSerovalPlugins.ts +0 -10
- package/src/serializer/getDefaultSerovalPlugins.ts +0 -24
- /package/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.d.ts +0 -0
- /package/dist/esm/{serverFnFetcher.d.ts → client/serverFnFetcher.d.ts} +0 -0
|
@@ -13,7 +13,7 @@ test('createServerFn method with autocomplete', () => {
|
|
|
13
13
|
test('createServerFn without middleware', () => {
|
|
14
14
|
expectTypeOf(createServerFn()).toHaveProperty('handler')
|
|
15
15
|
expectTypeOf(createServerFn()).toHaveProperty('middleware')
|
|
16
|
-
expectTypeOf(createServerFn()).toHaveProperty('
|
|
16
|
+
expectTypeOf(createServerFn()).toHaveProperty('inputValidator')
|
|
17
17
|
|
|
18
18
|
createServerFn({ method: 'GET' }).handler((options) => {
|
|
19
19
|
expectTypeOf(options).toEqualTypeOf<{
|
|
@@ -21,7 +21,6 @@ test('createServerFn without middleware', () => {
|
|
|
21
21
|
context: undefined
|
|
22
22
|
data: undefined
|
|
23
23
|
signal: AbortSignal
|
|
24
|
-
response: 'data'
|
|
25
24
|
}>()
|
|
26
25
|
})
|
|
27
26
|
})
|
|
@@ -29,13 +28,13 @@ test('createServerFn without middleware', () => {
|
|
|
29
28
|
test('createServerFn with validator', () => {
|
|
30
29
|
const fnAfterValidator = createServerFn({
|
|
31
30
|
method: 'GET',
|
|
32
|
-
}).
|
|
31
|
+
}).inputValidator((input: { input: string }) => ({
|
|
33
32
|
a: input.input,
|
|
34
33
|
}))
|
|
35
34
|
|
|
36
35
|
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
|
|
37
36
|
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
|
|
38
|
-
expectTypeOf(fnAfterValidator).not.toHaveProperty('
|
|
37
|
+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
|
|
39
38
|
|
|
40
39
|
const fn = fnAfterValidator.handler((options) => {
|
|
41
40
|
expectTypeOf(options).toEqualTypeOf<{
|
|
@@ -45,7 +44,6 @@ test('createServerFn with validator', () => {
|
|
|
45
44
|
a: string
|
|
46
45
|
}
|
|
47
46
|
signal: AbortSignal
|
|
48
|
-
response: 'data'
|
|
49
47
|
}>()
|
|
50
48
|
})
|
|
51
49
|
|
|
@@ -96,8 +94,7 @@ test('createServerFn with middleware and context', () => {
|
|
|
96
94
|
])
|
|
97
95
|
|
|
98
96
|
expectTypeOf(fnWithMiddleware).toHaveProperty('handler')
|
|
99
|
-
expectTypeOf(fnWithMiddleware).toHaveProperty('
|
|
100
|
-
expectTypeOf(fnWithMiddleware).not.toHaveProperty('middleware')
|
|
97
|
+
expectTypeOf(fnWithMiddleware).toHaveProperty('inputValidator')
|
|
101
98
|
|
|
102
99
|
fnWithMiddleware.handler((options) => {
|
|
103
100
|
expectTypeOf(options).toEqualTypeOf<{
|
|
@@ -110,20 +107,19 @@ test('createServerFn with middleware and context', () => {
|
|
|
110
107
|
}
|
|
111
108
|
data: undefined
|
|
112
109
|
signal: AbortSignal
|
|
113
|
-
response: 'data'
|
|
114
110
|
}>()
|
|
115
111
|
})
|
|
116
112
|
})
|
|
117
113
|
|
|
118
114
|
describe('createServerFn with middleware and validator', () => {
|
|
119
|
-
const middleware1 = createMiddleware({ type: 'function' }).
|
|
115
|
+
const middleware1 = createMiddleware({ type: 'function' }).inputValidator(
|
|
120
116
|
(input: { readonly inputA: 'inputA' }) =>
|
|
121
117
|
({
|
|
122
118
|
outputA: 'outputA',
|
|
123
119
|
}) as const,
|
|
124
120
|
)
|
|
125
121
|
|
|
126
|
-
const middleware2 = createMiddleware({ type: 'function' }).
|
|
122
|
+
const middleware2 = createMiddleware({ type: 'function' }).inputValidator(
|
|
127
123
|
(input: { readonly inputB: 'inputB' }) =>
|
|
128
124
|
({
|
|
129
125
|
outputB: 'outputB',
|
|
@@ -135,10 +131,10 @@ describe('createServerFn with middleware and validator', () => {
|
|
|
135
131
|
middleware2,
|
|
136
132
|
])
|
|
137
133
|
|
|
138
|
-
test(`response
|
|
139
|
-
const fn = createServerFn({ method: 'GET'
|
|
134
|
+
test(`response`, () => {
|
|
135
|
+
const fn = createServerFn({ method: 'GET' })
|
|
140
136
|
.middleware([middleware3])
|
|
141
|
-
.
|
|
137
|
+
.inputValidator(
|
|
142
138
|
(input: { readonly inputC: 'inputC' }) =>
|
|
143
139
|
({
|
|
144
140
|
outputC: 'outputC',
|
|
@@ -154,7 +150,6 @@ describe('createServerFn with middleware and validator', () => {
|
|
|
154
150
|
readonly outputC: 'outputC'
|
|
155
151
|
}
|
|
156
152
|
signal: AbortSignal
|
|
157
|
-
response: 'data'
|
|
158
153
|
}>()
|
|
159
154
|
|
|
160
155
|
return 'some-data' as const
|
|
@@ -177,57 +172,11 @@ describe('createServerFn with middleware and validator', () => {
|
|
|
177
172
|
}),
|
|
178
173
|
).returns.resolves.toEqualTypeOf<'some-data'>()
|
|
179
174
|
})
|
|
180
|
-
|
|
181
|
-
test(`response: 'full'`, () => {
|
|
182
|
-
const fn = createServerFn({ method: 'GET', response: 'full' })
|
|
183
|
-
.middleware([middleware3])
|
|
184
|
-
.validator(
|
|
185
|
-
(input: { readonly inputC: 'inputC' }) =>
|
|
186
|
-
({
|
|
187
|
-
outputC: 'outputC',
|
|
188
|
-
}) as const,
|
|
189
|
-
)
|
|
190
|
-
.handler((options) => {
|
|
191
|
-
expectTypeOf(options).toEqualTypeOf<{
|
|
192
|
-
method: 'GET'
|
|
193
|
-
context: undefined
|
|
194
|
-
data: {
|
|
195
|
-
readonly outputA: 'outputA'
|
|
196
|
-
readonly outputB: 'outputB'
|
|
197
|
-
readonly outputC: 'outputC'
|
|
198
|
-
}
|
|
199
|
-
signal: AbortSignal
|
|
200
|
-
response: 'full'
|
|
201
|
-
}>()
|
|
202
|
-
|
|
203
|
-
return 'some-data' as const
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
|
|
207
|
-
data: {
|
|
208
|
-
readonly inputA: 'inputA'
|
|
209
|
-
readonly inputB: 'inputB'
|
|
210
|
-
readonly inputC: 'inputC'
|
|
211
|
-
}
|
|
212
|
-
headers?: HeadersInit
|
|
213
|
-
signal?: AbortSignal
|
|
214
|
-
}>()
|
|
215
|
-
|
|
216
|
-
expectTypeOf(() =>
|
|
217
|
-
fn({
|
|
218
|
-
data: { inputA: 'inputA', inputB: 'inputB', inputC: 'inputC' },
|
|
219
|
-
}),
|
|
220
|
-
).returns.resolves.toEqualTypeOf<{
|
|
221
|
-
result: 'some-data'
|
|
222
|
-
context: undefined
|
|
223
|
-
error: unknown
|
|
224
|
-
}>()
|
|
225
|
-
})
|
|
226
175
|
})
|
|
227
176
|
|
|
228
177
|
test('createServerFn overrides properties', () => {
|
|
229
178
|
const middleware1 = createMiddleware({ type: 'function' })
|
|
230
|
-
.
|
|
179
|
+
.inputValidator(
|
|
231
180
|
() =>
|
|
232
181
|
({
|
|
233
182
|
input: 'a' as 'a' | 'b' | 'c',
|
|
@@ -252,7 +201,7 @@ test('createServerFn overrides properties', () => {
|
|
|
252
201
|
|
|
253
202
|
const middleware2 = createMiddleware({ type: 'function' })
|
|
254
203
|
.middleware([middleware1])
|
|
255
|
-
.
|
|
204
|
+
.inputValidator(
|
|
256
205
|
() =>
|
|
257
206
|
({
|
|
258
207
|
input: 'b' as 'b' | 'c',
|
|
@@ -275,7 +224,7 @@ test('createServerFn overrides properties', () => {
|
|
|
275
224
|
|
|
276
225
|
createServerFn()
|
|
277
226
|
.middleware([middleware2])
|
|
278
|
-
.
|
|
227
|
+
.inputValidator(
|
|
279
228
|
() =>
|
|
280
229
|
({
|
|
281
230
|
input: 'c',
|
|
@@ -291,28 +240,26 @@ test('createServerFn overrides properties', () => {
|
|
|
291
240
|
|
|
292
241
|
test('createServerFn where validator is a primitive', () => {
|
|
293
242
|
createServerFn({ method: 'GET' })
|
|
294
|
-
.
|
|
243
|
+
.inputValidator(() => 'c' as const)
|
|
295
244
|
.handler((options) => {
|
|
296
245
|
expectTypeOf(options).toEqualTypeOf<{
|
|
297
246
|
method: 'GET'
|
|
298
247
|
context: undefined
|
|
299
248
|
data: 'c'
|
|
300
249
|
signal: AbortSignal
|
|
301
|
-
response: 'data'
|
|
302
250
|
}>()
|
|
303
251
|
})
|
|
304
252
|
})
|
|
305
253
|
|
|
306
254
|
test('createServerFn where validator is optional if object is optional', () => {
|
|
307
255
|
const fn = createServerFn({ method: 'GET' })
|
|
308
|
-
.
|
|
256
|
+
.inputValidator((input: 'c' | undefined) => input)
|
|
309
257
|
.handler((options) => {
|
|
310
258
|
expectTypeOf(options).toEqualTypeOf<{
|
|
311
259
|
method: 'GET'
|
|
312
260
|
context: undefined
|
|
313
261
|
data: 'c' | undefined
|
|
314
262
|
signal: AbortSignal
|
|
315
|
-
response: 'data'
|
|
316
263
|
}>()
|
|
317
264
|
})
|
|
318
265
|
|
|
@@ -335,7 +282,6 @@ test('createServerFn where data is optional if there is no validator', () => {
|
|
|
335
282
|
context: undefined
|
|
336
283
|
data: undefined
|
|
337
284
|
signal: AbortSignal
|
|
338
|
-
response: 'data'
|
|
339
285
|
}>()
|
|
340
286
|
})
|
|
341
287
|
|
|
@@ -371,13 +317,14 @@ test('createServerFn cannot return function', () => {
|
|
|
371
317
|
expectTypeOf(createServerFn().handler<{ func: () => 'func' }>)
|
|
372
318
|
.parameter(0)
|
|
373
319
|
.returns.toEqualTypeOf<
|
|
320
|
+
| Response
|
|
374
321
|
| { func: 'Function is not serializable' }
|
|
375
322
|
| Promise<{ func: 'Function is not serializable' }>
|
|
376
323
|
>()
|
|
377
324
|
})
|
|
378
325
|
|
|
379
326
|
test('createServerFn cannot validate function', () => {
|
|
380
|
-
const validator = createServerFn().
|
|
327
|
+
const validator = createServerFn().inputValidator<
|
|
381
328
|
(input: { func: () => 'string' }) => { output: 'string' }
|
|
382
329
|
>
|
|
383
330
|
|
|
@@ -392,87 +339,49 @@ test('createServerFn cannot validate function', () => {
|
|
|
392
339
|
})
|
|
393
340
|
|
|
394
341
|
test('createServerFn can validate Date', () => {
|
|
395
|
-
const validator = createServerFn().
|
|
342
|
+
const validator = createServerFn().inputValidator<
|
|
396
343
|
(input: Date) => { output: 'string' }
|
|
397
344
|
>
|
|
398
345
|
|
|
399
346
|
expectTypeOf(validator)
|
|
400
347
|
.parameter(0)
|
|
401
348
|
.toEqualTypeOf<
|
|
402
|
-
ConstrainValidator<Register, (input: Date) => { output: 'string' }>
|
|
349
|
+
ConstrainValidator<Register, 'GET', (input: Date) => { output: 'string' }>
|
|
403
350
|
>()
|
|
404
351
|
})
|
|
405
352
|
|
|
406
353
|
test('createServerFn can validate FormData', () => {
|
|
407
|
-
const validator = createServerFn().
|
|
354
|
+
const validator = createServerFn({ method: 'POST' }).inputValidator<
|
|
355
|
+
(input: FormData) => { output: 'string' }
|
|
356
|
+
>
|
|
357
|
+
|
|
358
|
+
expectTypeOf(validator).parameter(0).parameter(0).toEqualTypeOf<FormData>()
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
test('createServerFn cannot validate FormData for GET', () => {
|
|
362
|
+
const validator = createServerFn({ method: 'GET' }).inputValidator<
|
|
408
363
|
(input: FormData) => { output: 'string' }
|
|
409
364
|
>
|
|
410
365
|
|
|
411
366
|
expectTypeOf(validator)
|
|
412
367
|
.parameter(0)
|
|
413
|
-
.
|
|
414
|
-
|
|
415
|
-
>()
|
|
368
|
+
.parameter(0)
|
|
369
|
+
.not.toEqualTypeOf<FormData>()
|
|
416
370
|
})
|
|
417
371
|
|
|
418
372
|
describe('response', () => {
|
|
419
|
-
describe('data', () => {
|
|
420
|
-
test(`response: 'data' is passed into handler without response being set`, () => {
|
|
421
|
-
createServerFn().handler((options) => {
|
|
422
|
-
expectTypeOf(options.response).toEqualTypeOf<'data'>()
|
|
423
|
-
})
|
|
424
|
-
})
|
|
425
|
-
|
|
426
|
-
test(`response: 'data' is passed into handler with explicit response: 'data'`, () => {
|
|
427
|
-
createServerFn({ response: 'data' }).handler((options) => {
|
|
428
|
-
expectTypeOf(options.response).toEqualTypeOf<'data'>()
|
|
429
|
-
})
|
|
430
|
-
})
|
|
431
|
-
})
|
|
432
|
-
describe('full', () => {
|
|
433
|
-
test(`response: 'full' is passed into handler`, () => {
|
|
434
|
-
createServerFn({ response: 'full' }).handler((options) => {
|
|
435
|
-
expectTypeOf(options.response).toEqualTypeOf<'full'>()
|
|
436
|
-
})
|
|
437
|
-
})
|
|
438
|
-
})
|
|
439
|
-
|
|
440
|
-
describe('raw', () => {
|
|
441
|
-
test(`response: 'raw' is passed into handler`, () => {
|
|
442
|
-
createServerFn({ response: 'raw' }).handler((options) => {
|
|
443
|
-
expectTypeOf(options.response).toEqualTypeOf<'raw'>()
|
|
444
|
-
return null
|
|
445
|
-
})
|
|
446
|
-
})
|
|
447
|
-
})
|
|
448
373
|
test(`client receives Response when Response is returned`, () => {
|
|
449
|
-
const fn = createServerFn(
|
|
374
|
+
const fn = createServerFn().handler(() => {
|
|
450
375
|
return new Response('Hello World')
|
|
451
376
|
})
|
|
452
377
|
|
|
453
378
|
expectTypeOf(fn()).toEqualTypeOf<Promise<Response>>()
|
|
454
379
|
})
|
|
455
|
-
|
|
456
|
-
test(`client receives Response when ReadableStream is returned`, () => {
|
|
457
|
-
const fn = createServerFn({ response: 'raw' }).handler(() => {
|
|
458
|
-
return new ReadableStream()
|
|
459
|
-
})
|
|
460
|
-
|
|
461
|
-
expectTypeOf(fn()).toEqualTypeOf<Promise<Response>>()
|
|
462
|
-
})
|
|
463
|
-
|
|
464
|
-
test(`client receives Response when string is returned`, () => {
|
|
465
|
-
const fn = createServerFn({ response: 'raw' }).handler(() => {
|
|
466
|
-
return 'hello'
|
|
467
|
-
})
|
|
468
|
-
|
|
469
|
-
expectTypeOf(fn()).toEqualTypeOf<Promise<Response>>()
|
|
470
|
-
})
|
|
471
380
|
})
|
|
472
381
|
|
|
473
382
|
test('createServerFn can be used as a mutation function', () => {
|
|
474
383
|
const serverFn = createServerFn()
|
|
475
|
-
.
|
|
384
|
+
.inputValidator((data: number) => data)
|
|
476
385
|
.handler(() => 'foo')
|
|
477
386
|
|
|
478
387
|
type MutationFunction<TData = unknown, TVariables = unknown> = (
|
|
@@ -489,7 +398,7 @@ test('createServerFn can be used as a mutation function', () => {
|
|
|
489
398
|
|
|
490
399
|
test('createServerFn validator infers unknown for default input type', () => {
|
|
491
400
|
const fn = createServerFn()
|
|
492
|
-
.
|
|
401
|
+
.inputValidator((input) => {
|
|
493
402
|
expectTypeOf(input).toEqualTypeOf<unknown>()
|
|
494
403
|
|
|
495
404
|
if (typeof input === 'number') return 'success' as const
|
|
@@ -513,3 +422,118 @@ test('createServerFn validator infers unknown for default input type', () => {
|
|
|
513
422
|
|
|
514
423
|
expectTypeOf(fn()).toEqualTypeOf<Promise<'failed' | 'success'>>()
|
|
515
424
|
})
|
|
425
|
+
|
|
426
|
+
test('incrementally building createServerFn with multiple middleware calls', () => {
|
|
427
|
+
const middleware1 = createMiddleware({ type: 'function' }).server(
|
|
428
|
+
({ next }) => {
|
|
429
|
+
return next({ context: { a: 'a' } as const })
|
|
430
|
+
},
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
const middleware2 = createMiddleware({ type: 'function' }).server(
|
|
434
|
+
({ next }) => {
|
|
435
|
+
return next({ context: { b: 'b' } as const })
|
|
436
|
+
},
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
const middleware3 = createMiddleware({ type: 'function' }).server(
|
|
440
|
+
({ next }) => {
|
|
441
|
+
return next({ context: { c: 'c' } as const })
|
|
442
|
+
},
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
const builderWithMw1 = createServerFn({ method: 'GET' }).middleware([
|
|
446
|
+
middleware1,
|
|
447
|
+
])
|
|
448
|
+
|
|
449
|
+
expectTypeOf(builderWithMw1).toHaveProperty('handler')
|
|
450
|
+
expectTypeOf(builderWithMw1).toHaveProperty('inputValidator')
|
|
451
|
+
expectTypeOf(builderWithMw1).toHaveProperty('middleware')
|
|
452
|
+
|
|
453
|
+
builderWithMw1.handler((options) => {
|
|
454
|
+
expectTypeOf(options).toEqualTypeOf<{
|
|
455
|
+
method: 'GET'
|
|
456
|
+
context: {
|
|
457
|
+
readonly a: 'a'
|
|
458
|
+
}
|
|
459
|
+
data: undefined
|
|
460
|
+
signal: AbortSignal
|
|
461
|
+
}>()
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
// overrides method
|
|
465
|
+
const builderWithMw2 = builderWithMw1({ method: 'POST' }).middleware([
|
|
466
|
+
middleware2,
|
|
467
|
+
])
|
|
468
|
+
|
|
469
|
+
expectTypeOf(builderWithMw2).toHaveProperty('handler')
|
|
470
|
+
expectTypeOf(builderWithMw2).toHaveProperty('inputValidator')
|
|
471
|
+
expectTypeOf(builderWithMw2).toHaveProperty('middleware')
|
|
472
|
+
|
|
473
|
+
builderWithMw2.handler((options) => {
|
|
474
|
+
expectTypeOf(options).toEqualTypeOf<{
|
|
475
|
+
method: 'POST'
|
|
476
|
+
context: {
|
|
477
|
+
readonly a: 'a'
|
|
478
|
+
readonly b: 'b'
|
|
479
|
+
}
|
|
480
|
+
data: undefined
|
|
481
|
+
signal: AbortSignal
|
|
482
|
+
}>()
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
// overrides method again
|
|
486
|
+
const builderWithMw3 = builderWithMw2({ method: 'GET' }).middleware([
|
|
487
|
+
middleware3,
|
|
488
|
+
])
|
|
489
|
+
|
|
490
|
+
expectTypeOf(builderWithMw3).toHaveProperty('handler')
|
|
491
|
+
expectTypeOf(builderWithMw3).toHaveProperty('inputValidator')
|
|
492
|
+
expectTypeOf(builderWithMw3).toHaveProperty('middleware')
|
|
493
|
+
|
|
494
|
+
builderWithMw3.handler((options) => {
|
|
495
|
+
expectTypeOf(options).toEqualTypeOf<{
|
|
496
|
+
method: 'GET'
|
|
497
|
+
context: {
|
|
498
|
+
readonly a: 'a'
|
|
499
|
+
readonly b: 'b'
|
|
500
|
+
readonly c: 'c'
|
|
501
|
+
}
|
|
502
|
+
data: undefined
|
|
503
|
+
signal: AbortSignal
|
|
504
|
+
}>()
|
|
505
|
+
})
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
test('compose middlewares and server function factories', () => {
|
|
509
|
+
const middleware1 = createMiddleware({ type: 'function' }).server(
|
|
510
|
+
({ next }) => {
|
|
511
|
+
return next({ context: { a: 'a' } as const })
|
|
512
|
+
},
|
|
513
|
+
)
|
|
514
|
+
|
|
515
|
+
const middleware2 = createMiddleware({ type: 'function' }).server(
|
|
516
|
+
({ next }) => {
|
|
517
|
+
return next({ context: { b: 'b' } as const })
|
|
518
|
+
},
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
const builderWithMw1 = createServerFn().middleware([middleware1])
|
|
522
|
+
|
|
523
|
+
const composedBuilder = createServerFn({ method: 'GET' }).middleware([
|
|
524
|
+
middleware2,
|
|
525
|
+
builderWithMw1,
|
|
526
|
+
])
|
|
527
|
+
|
|
528
|
+
composedBuilder.handler((options) => {
|
|
529
|
+
expectTypeOf(options).toEqualTypeOf<{
|
|
530
|
+
method: 'GET'
|
|
531
|
+
context: {
|
|
532
|
+
readonly a: 'a'
|
|
533
|
+
readonly b: 'b'
|
|
534
|
+
}
|
|
535
|
+
data: undefined
|
|
536
|
+
signal: AbortSignal
|
|
537
|
+
}>()
|
|
538
|
+
})
|
|
539
|
+
})
|
|
@@ -9,15 +9,15 @@ test('createServeMiddleware removes middleware after middleware,', () => {
|
|
|
9
9
|
|
|
10
10
|
expectTypeOf(middleware).toHaveProperty('middleware')
|
|
11
11
|
expectTypeOf(middleware).toHaveProperty('server')
|
|
12
|
-
expectTypeOf(middleware).toHaveProperty('
|
|
12
|
+
expectTypeOf(middleware).toHaveProperty('inputValidator')
|
|
13
13
|
|
|
14
14
|
const middlewareAfterMiddleware = middleware.middleware([])
|
|
15
15
|
|
|
16
|
-
expectTypeOf(middlewareAfterMiddleware).toHaveProperty('
|
|
16
|
+
expectTypeOf(middlewareAfterMiddleware).toHaveProperty('inputValidator')
|
|
17
17
|
expectTypeOf(middlewareAfterMiddleware).toHaveProperty('server')
|
|
18
18
|
expectTypeOf(middlewareAfterMiddleware).not.toHaveProperty('middleware')
|
|
19
19
|
|
|
20
|
-
const middlewareAfterInput = middleware.
|
|
20
|
+
const middlewareAfterInput = middleware.inputValidator(() => {})
|
|
21
21
|
|
|
22
22
|
expectTypeOf(middlewareAfterInput).toHaveProperty('server')
|
|
23
23
|
expectTypeOf(middlewareAfterInput).not.toHaveProperty('middleware')
|
|
@@ -252,7 +252,7 @@ test('createMiddleware merges client context and sends to the server', () => {
|
|
|
252
252
|
|
|
253
253
|
test('createMiddleware merges input', () => {
|
|
254
254
|
const middleware1 = createMiddleware({ type: 'function' })
|
|
255
|
-
.
|
|
255
|
+
.inputValidator(() => {
|
|
256
256
|
return {
|
|
257
257
|
a: 'a',
|
|
258
258
|
} as const
|
|
@@ -264,7 +264,7 @@ test('createMiddleware merges input', () => {
|
|
|
264
264
|
|
|
265
265
|
const middleware2 = createMiddleware({ type: 'function' })
|
|
266
266
|
.middleware([middleware1])
|
|
267
|
-
.
|
|
267
|
+
.inputValidator(() => {
|
|
268
268
|
return {
|
|
269
269
|
b: 'b',
|
|
270
270
|
} as const
|
|
@@ -276,7 +276,7 @@ test('createMiddleware merges input', () => {
|
|
|
276
276
|
|
|
277
277
|
createMiddleware({ type: 'function' })
|
|
278
278
|
.middleware([middleware2])
|
|
279
|
-
.
|
|
279
|
+
.inputValidator(() => ({ c: 'c' }) as const)
|
|
280
280
|
.server(({ next, data }) => {
|
|
281
281
|
expectTypeOf(data).toEqualTypeOf<{
|
|
282
282
|
readonly a: 'a'
|
|
@@ -579,7 +579,7 @@ test('createMiddleware sendContext cannot send a function', () => {
|
|
|
579
579
|
})
|
|
580
580
|
|
|
581
581
|
test('createMiddleware cannot validate function', () => {
|
|
582
|
-
const validator = createMiddleware({ type: 'function' }).
|
|
582
|
+
const validator = createMiddleware({ type: 'function' }).inputValidator<
|
|
583
583
|
(input: { func: () => 'string' }) => { output: 'string' }
|
|
584
584
|
>
|
|
585
585
|
|
|
@@ -588,37 +588,42 @@ test('createMiddleware cannot validate function', () => {
|
|
|
588
588
|
.toEqualTypeOf<
|
|
589
589
|
ConstrainValidator<
|
|
590
590
|
Register,
|
|
591
|
+
'GET',
|
|
591
592
|
(input: { func: () => 'string' }) => { output: 'string' }
|
|
592
593
|
>
|
|
593
594
|
>()
|
|
594
595
|
})
|
|
595
596
|
|
|
596
597
|
test('createMiddleware can validate Date', () => {
|
|
597
|
-
const validator = createMiddleware({ type: 'function' }).
|
|
598
|
+
const validator = createMiddleware({ type: 'function' }).inputValidator<
|
|
598
599
|
(input: Date) => { output: 'string' }
|
|
599
600
|
>
|
|
600
601
|
|
|
601
602
|
expectTypeOf(validator)
|
|
602
603
|
.parameter(0)
|
|
603
604
|
.toEqualTypeOf<
|
|
604
|
-
ConstrainValidator<Register, (input: Date) => { output: 'string' }>
|
|
605
|
+
ConstrainValidator<Register, 'GET', (input: Date) => { output: 'string' }>
|
|
605
606
|
>()
|
|
606
607
|
})
|
|
607
608
|
|
|
608
609
|
test('createMiddleware can validate FormData', () => {
|
|
609
|
-
const validator = createMiddleware({ type: 'function' }).
|
|
610
|
+
const validator = createMiddleware({ type: 'function' }).inputValidator<
|
|
610
611
|
(input: FormData) => { output: 'string' }
|
|
611
612
|
>
|
|
612
613
|
|
|
613
614
|
expectTypeOf(validator)
|
|
614
615
|
.parameter(0)
|
|
615
616
|
.toEqualTypeOf<
|
|
616
|
-
ConstrainValidator<
|
|
617
|
+
ConstrainValidator<
|
|
618
|
+
Register,
|
|
619
|
+
'GET',
|
|
620
|
+
(input: FormData) => { output: 'string' }
|
|
621
|
+
>
|
|
617
622
|
>()
|
|
618
623
|
})
|
|
619
624
|
|
|
620
625
|
test('createMiddleware merging from parent with undefined validator', () => {
|
|
621
|
-
const middleware1 = createMiddleware({ type: 'function' }).
|
|
626
|
+
const middleware1 = createMiddleware({ type: 'function' }).inputValidator(
|
|
622
627
|
(input: { test: string }) => input.test,
|
|
623
628
|
)
|
|
624
629
|
|
|
@@ -633,7 +638,7 @@ test('createMiddleware merging from parent with undefined validator', () => {
|
|
|
633
638
|
|
|
634
639
|
test('createMiddleware validator infers unknown for default input type', () => {
|
|
635
640
|
createMiddleware({ type: 'function' })
|
|
636
|
-
.
|
|
641
|
+
.inputValidator((input) => {
|
|
637
642
|
expectTypeOf(input).toEqualTypeOf<unknown>()
|
|
638
643
|
|
|
639
644
|
if (typeof input === 'number') return 'success' as const
|
|
@@ -651,7 +656,7 @@ test('createMiddleware with type request, no middleware or context', () => {
|
|
|
651
656
|
createMiddleware({ type: 'request' }).server(async (options) => {
|
|
652
657
|
expectTypeOf(options).toEqualTypeOf<{
|
|
653
658
|
request: Request
|
|
654
|
-
next: RequestServerNextFn<undefined>
|
|
659
|
+
next: RequestServerNextFn<{}, undefined>
|
|
655
660
|
pathname: string
|
|
656
661
|
context: undefined
|
|
657
662
|
}>()
|
|
@@ -673,7 +678,7 @@ test('createMiddleware with type request, no middleware with context', () => {
|
|
|
673
678
|
createMiddleware({ type: 'request' }).server(async (options) => {
|
|
674
679
|
expectTypeOf(options).toEqualTypeOf<{
|
|
675
680
|
request: Request
|
|
676
|
-
next: RequestServerNextFn<undefined>
|
|
681
|
+
next: RequestServerNextFn<{}, undefined>
|
|
677
682
|
pathname: string
|
|
678
683
|
context: undefined
|
|
679
684
|
}>()
|
|
@@ -696,7 +701,7 @@ test('createMiddleware with type request, middleware and context', () => {
|
|
|
696
701
|
async (options) => {
|
|
697
702
|
expectTypeOf(options).toEqualTypeOf<{
|
|
698
703
|
request: Request
|
|
699
|
-
next: RequestServerNextFn<undefined>
|
|
704
|
+
next: RequestServerNextFn<{}, undefined>
|
|
700
705
|
pathname: string
|
|
701
706
|
context: undefined
|
|
702
707
|
}>()
|
|
@@ -719,7 +724,7 @@ test('createMiddleware with type request, middleware and context', () => {
|
|
|
719
724
|
.server(async (options) => {
|
|
720
725
|
expectTypeOf(options).toEqualTypeOf<{
|
|
721
726
|
request: Request
|
|
722
|
-
next: RequestServerNextFn<undefined>
|
|
727
|
+
next: RequestServerNextFn<{}, undefined>
|
|
723
728
|
pathname: string
|
|
724
729
|
context: { a: string }
|
|
725
730
|
}>()
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createClientRpc.js","sources":["../../src/createClientRpc.ts"],"sourcesContent":["import { TSS_SERVER_FUNCTION } from './constants'\nimport { serverFnFetcher } from './serverFnFetcher'\n\nlet baseUrl: string\nfunction sanitizeBase(base: string) {\n return base.replace(/^\\/|\\/$/g, '')\n}\n\nexport const createClientRpc = (functionId: string) => {\n if (!baseUrl) {\n const sanitizedAppBase = sanitizeBase(process.env.TSS_APP_BASE || '/')\n const sanitizedServerBase = sanitizeBase(process.env.TSS_SERVER_FN_BASE!)\n baseUrl = `${sanitizedAppBase ? `/${sanitizedAppBase}` : ''}/${sanitizedServerBase}/`\n }\n const url = baseUrl + functionId\n\n const clientFn = (...args: Array<any>) => {\n return serverFnFetcher(url, args, fetch)\n }\n\n return Object.assign(clientFn, {\n url,\n functionId,\n [TSS_SERVER_FUNCTION]: true,\n })\n}\n"],"names":[],"mappings":";;AAGA,IAAI;AACJ,SAAS,aAAa,MAAc;AAClC,SAAO,KAAK,QAAQ,YAAY,EAAE;AACpC;AAEO,MAAM,kBAAkB,CAAC,eAAuB;AACrD,MAAI,CAAC,SAAS;AACZ,UAAM,mBAAmB,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AACrE,UAAM,sBAAsB,aAAa,QAAQ,IAAI,kBAAmB;AACxE,cAAU,GAAG,mBAAmB,IAAI,gBAAgB,KAAK,EAAE,IAAI,mBAAmB;AAAA,EACpF;AACA,QAAM,MAAM,UAAU;AAEtB,QAAM,WAAW,IAAI,SAAqB;AACxC,WAAO,gBAAgB,KAAK,MAAM,KAAK;AAAA,EACzC;AAEA,SAAO,OAAO,OAAO,UAAU;AAAA,IAC7B;AAAA,IACA;AAAA,IACA,CAAC,mBAAmB,GAAG;AAAA,EAAA,CACxB;AACH;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"registerGlobalMiddleware.js","sources":["../../src/registerGlobalMiddleware.ts"],"sourcesContent":["import type { AnyFunctionMiddleware } from './createMiddleware'\n\nexport const globalMiddleware: Array<AnyFunctionMiddleware> = []\n\nexport function registerGlobalMiddleware(options: {\n middleware: Array<AnyFunctionMiddleware>\n}) {\n globalMiddleware.push(...options.middleware)\n}\n"],"names":[],"mappings":"AAEO,MAAM,mBAAiD,CAAA;AAEvD,SAAS,yBAAyB,SAEtC;AACD,mBAAiB,KAAK,GAAG,QAAQ,UAAU;AAC7C;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ServerFunctionSerializationAdapter.js","sources":["../../../src/serializer/ServerFunctionSerializationAdapter.ts"],"sourcesContent":["import { createSerializationAdapter } from '@tanstack/router-core'\nimport { createClientRpc } from '../createClientRpc'\nimport { TSS_SERVER_FUNCTION } from '../constants'\n\nexport const ServerFunctionSerializationAdapter = createSerializationAdapter({\n key: '$TSS/serverfn',\n test: (v): v is { functionId: string } => {\n if (typeof v !== 'object' || v === null) return false\n\n if (!(TSS_SERVER_FUNCTION in v)) return false\n\n return !!v[TSS_SERVER_FUNCTION]\n },\n toSerializable: ({ functionId }) => ({ functionId }),\n fromSerializable: ({ functionId }) => createClientRpc(functionId),\n})\n"],"names":[],"mappings":";;;AAIO,MAAM,qCAAqC,2BAA2B;AAAA,EAC3E,KAAK;AAAA,EACL,MAAM,CAAC,MAAmC;AACxC,QAAI,OAAO,MAAM,YAAY,MAAM,KAAM,QAAO;AAEhD,QAAI,EAAE,uBAAuB,GAAI,QAAO;AAExC,WAAO,CAAC,CAAC,EAAE,mBAAmB;AAAA,EAChC;AAAA,EACA,gBAAgB,CAAC,EAAE,kBAAkB,EAAE,WAAA;AAAA,EACvC,kBAAkB,CAAC,EAAE,WAAA,MAAiB,gBAAgB,UAAU;AAClE,CAAC;"}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export declare function getClientSerovalPlugins(): (import('seroval').Plugin<Error, any> | import('seroval').Plugin<ReadableStream<any>, any> | import('seroval').Plugin<FormData, any> | import('seroval').Plugin<any, import('seroval').SerovalNode> | import('seroval').Plugin<{
|
|
2
|
-
functionId: string;
|
|
3
|
-
}, import('seroval').SerovalNode>)[];
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { makeSerovalPlugin } from "@tanstack/router-core";
|
|
2
|
-
import { getDefaultSerovalPlugins } from "./getDefaultSerovalPlugins.js";
|
|
3
|
-
import { ServerFunctionSerializationAdapter } from "./ServerFunctionSerializationAdapter.js";
|
|
4
|
-
function getClientSerovalPlugins() {
|
|
5
|
-
return [
|
|
6
|
-
...getDefaultSerovalPlugins(),
|
|
7
|
-
makeSerovalPlugin(ServerFunctionSerializationAdapter)
|
|
8
|
-
];
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
getClientSerovalPlugins
|
|
12
|
-
};
|
|
13
|
-
//# sourceMappingURL=getClientSerovalPlugins.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getClientSerovalPlugins.js","sources":["../../../src/serializer/getClientSerovalPlugins.ts"],"sourcesContent":["import { makeSerovalPlugin } from '@tanstack/router-core'\nimport { getDefaultSerovalPlugins } from './getDefaultSerovalPlugins'\nimport { ServerFunctionSerializationAdapter } from './ServerFunctionSerializationAdapter'\n\nexport function getClientSerovalPlugins() {\n return [\n ...getDefaultSerovalPlugins(),\n makeSerovalPlugin(ServerFunctionSerializationAdapter),\n ]\n}\n"],"names":[],"mappings":";;;AAIO,SAAS,0BAA0B;AACxC,SAAO;AAAA,IACL,GAAG,yBAAA;AAAA,IACH,kBAAkB,kCAAkC;AAAA,EAAA;AAExD;"}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { Plugin } from 'seroval';
|
|
2
|
-
export declare const defaultSerovalPlugins: (Plugin<Error, any> | Plugin<ReadableStream<any>, any> | Plugin<FormData, any>)[];
|
|
3
|
-
export declare function getDefaultSerovalPlugins(): (Plugin<Error, any> | Plugin<ReadableStream<any>, any> | Plugin<FormData, any> | Plugin<any, import('seroval').SerovalNode>)[];
|