@tanstack/start-client-core 1.20.3-alpha.1

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 (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -0
  3. package/dist/cjs/createIsomorphicFn.cjs +7 -0
  4. package/dist/cjs/createIsomorphicFn.cjs.map +1 -0
  5. package/dist/cjs/createIsomorphicFn.d.cts +12 -0
  6. package/dist/cjs/createMiddleware.cjs +37 -0
  7. package/dist/cjs/createMiddleware.cjs.map +1 -0
  8. package/dist/cjs/createMiddleware.d.cts +175 -0
  9. package/dist/cjs/createServerFn.cjs +378 -0
  10. package/dist/cjs/createServerFn.cjs.map +1 -0
  11. package/dist/cjs/createServerFn.d.cts +159 -0
  12. package/dist/cjs/envOnly.cjs +7 -0
  13. package/dist/cjs/envOnly.cjs.map +1 -0
  14. package/dist/cjs/envOnly.d.cts +4 -0
  15. package/dist/cjs/headers.cjs +30 -0
  16. package/dist/cjs/headers.cjs.map +1 -0
  17. package/dist/cjs/headers.d.cts +5 -0
  18. package/dist/cjs/index.cjs +33 -0
  19. package/dist/cjs/index.cjs.map +1 -0
  20. package/dist/cjs/index.d.cts +11 -0
  21. package/dist/cjs/json.cjs +14 -0
  22. package/dist/cjs/json.cjs.map +1 -0
  23. package/dist/cjs/json.d.cts +2 -0
  24. package/dist/cjs/registerGlobalMiddleware.cjs +9 -0
  25. package/dist/cjs/registerGlobalMiddleware.cjs.map +1 -0
  26. package/dist/cjs/registerGlobalMiddleware.d.cts +5 -0
  27. package/dist/cjs/serializer.cjs +152 -0
  28. package/dist/cjs/serializer.cjs.map +1 -0
  29. package/dist/cjs/serializer.d.cts +2 -0
  30. package/dist/cjs/ssr-client.cjs +130 -0
  31. package/dist/cjs/ssr-client.cjs.map +1 -0
  32. package/dist/cjs/ssr-client.d.cts +64 -0
  33. package/dist/cjs/tests/createIsomorphicFn.test-d.d.cts +1 -0
  34. package/dist/cjs/tests/createServerFn.test-d.d.cts +1 -0
  35. package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +1 -0
  36. package/dist/cjs/tests/envOnly.test-d.d.cts +1 -0
  37. package/dist/cjs/tests/json.test.d.cts +1 -0
  38. package/dist/cjs/tests/transformer.test.d.cts +1 -0
  39. package/dist/esm/createIsomorphicFn.d.ts +12 -0
  40. package/dist/esm/createIsomorphicFn.js +7 -0
  41. package/dist/esm/createIsomorphicFn.js.map +1 -0
  42. package/dist/esm/createMiddleware.d.ts +175 -0
  43. package/dist/esm/createMiddleware.js +37 -0
  44. package/dist/esm/createMiddleware.js.map +1 -0
  45. package/dist/esm/createServerFn.d.ts +159 -0
  46. package/dist/esm/createServerFn.js +356 -0
  47. package/dist/esm/createServerFn.js.map +1 -0
  48. package/dist/esm/envOnly.d.ts +4 -0
  49. package/dist/esm/envOnly.js +7 -0
  50. package/dist/esm/envOnly.js.map +1 -0
  51. package/dist/esm/headers.d.ts +5 -0
  52. package/dist/esm/headers.js +30 -0
  53. package/dist/esm/headers.js.map +1 -0
  54. package/dist/esm/index.d.ts +11 -0
  55. package/dist/esm/index.js +30 -0
  56. package/dist/esm/index.js.map +1 -0
  57. package/dist/esm/json.d.ts +2 -0
  58. package/dist/esm/json.js +14 -0
  59. package/dist/esm/json.js.map +1 -0
  60. package/dist/esm/registerGlobalMiddleware.d.ts +5 -0
  61. package/dist/esm/registerGlobalMiddleware.js +9 -0
  62. package/dist/esm/registerGlobalMiddleware.js.map +1 -0
  63. package/dist/esm/serializer.d.ts +2 -0
  64. package/dist/esm/serializer.js +152 -0
  65. package/dist/esm/serializer.js.map +1 -0
  66. package/dist/esm/ssr-client.d.ts +64 -0
  67. package/dist/esm/ssr-client.js +130 -0
  68. package/dist/esm/ssr-client.js.map +1 -0
  69. package/dist/esm/tests/createIsomorphicFn.test-d.d.ts +1 -0
  70. package/dist/esm/tests/createServerFn.test-d.d.ts +1 -0
  71. package/dist/esm/tests/createServerMiddleware.test-d.d.ts +1 -0
  72. package/dist/esm/tests/envOnly.test-d.d.ts +1 -0
  73. package/dist/esm/tests/json.test.d.ts +1 -0
  74. package/dist/esm/tests/transformer.test.d.ts +1 -0
  75. package/package.json +56 -0
  76. package/src/createIsomorphicFn.ts +36 -0
  77. package/src/createMiddleware.ts +706 -0
  78. package/src/createServerFn.ts +1004 -0
  79. package/src/envOnly.ts +9 -0
  80. package/src/headers.ts +50 -0
  81. package/src/index.tsx +88 -0
  82. package/src/json.ts +15 -0
  83. package/src/registerGlobalMiddleware.ts +9 -0
  84. package/src/serializer.ts +177 -0
  85. package/src/ssr-client.tsx +243 -0
  86. package/src/tests/createIsomorphicFn.test-d.ts +72 -0
  87. package/src/tests/createServerFn.test-d.ts +519 -0
  88. package/src/tests/createServerMiddleware.test-d.ts +736 -0
  89. package/src/tests/envOnly.test-d.ts +34 -0
  90. package/src/tests/json.test.ts +37 -0
  91. package/src/tests/transformer.test.tsx +147 -0
@@ -0,0 +1,736 @@
1
+ import { expectTypeOf, test } from 'vitest'
2
+ import { createMiddleware } from '../createMiddleware'
3
+ import type { RequestServerNextFn } from '../createMiddleware'
4
+ import type { Constrain, Validator } from '@tanstack/router-core'
5
+ import type { ConstrainValidator } from '../createServerFn'
6
+
7
+ test('createServeMiddleware removes middleware after middleware,', () => {
8
+ const middleware = createMiddleware({ type: 'function' })
9
+
10
+ expectTypeOf(middleware).toHaveProperty('middleware')
11
+ expectTypeOf(middleware).toHaveProperty('server')
12
+ expectTypeOf(middleware).toHaveProperty('validator')
13
+
14
+ const middlewareAfterMiddleware = middleware.middleware([])
15
+
16
+ expectTypeOf(middlewareAfterMiddleware).toHaveProperty('validator')
17
+ expectTypeOf(middlewareAfterMiddleware).toHaveProperty('server')
18
+ expectTypeOf(middlewareAfterMiddleware).not.toHaveProperty('middleware')
19
+
20
+ const middlewareAfterInput = middleware.validator(() => {})
21
+
22
+ expectTypeOf(middlewareAfterInput).toHaveProperty('server')
23
+ expectTypeOf(middlewareAfterInput).not.toHaveProperty('middleware')
24
+
25
+ const middlewareAfterServer = middleware.server(async (options) => {
26
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
27
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
28
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
29
+
30
+ const result = await options.next({
31
+ context: {
32
+ a: 'a',
33
+ },
34
+ })
35
+
36
+ expectTypeOf(result.context).toEqualTypeOf<{ a: string }>()
37
+
38
+ expectTypeOf(result.sendContext).toEqualTypeOf<undefined>()
39
+
40
+ return result
41
+ })
42
+
43
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('server')
44
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('input')
45
+ expectTypeOf(middlewareAfterServer).not.toHaveProperty('middleware')
46
+ })
47
+
48
+ test('createMiddleware merges server context', () => {
49
+ const middleware1 = createMiddleware({ type: 'function' }).server(
50
+ async (options) => {
51
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
52
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
53
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
54
+
55
+ const result = await options.next({ context: { a: true } })
56
+
57
+ expectTypeOf(result).toEqualTypeOf<{
58
+ 'use functions must return the result of next()': true
59
+ _types: {
60
+ context: {
61
+ a: boolean
62
+ }
63
+ sendContext: undefined
64
+ }
65
+ context: { a: boolean }
66
+ sendContext: undefined
67
+ }>()
68
+
69
+ return result
70
+ },
71
+ )
72
+
73
+ const middleware2 = createMiddleware({ type: 'function' }).server(
74
+ async (options) => {
75
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
76
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
77
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
78
+
79
+ const result = await options.next({ context: { b: 'test' } })
80
+
81
+ expectTypeOf(result).toEqualTypeOf<{
82
+ 'use functions must return the result of next()': true
83
+ _types: {
84
+ context: {
85
+ b: string
86
+ }
87
+ sendContext: undefined
88
+ }
89
+ context: { b: string }
90
+ sendContext: undefined
91
+ }>()
92
+
93
+ return result
94
+ },
95
+ )
96
+
97
+ const middleware3 = createMiddleware({ type: 'function' })
98
+ .middleware([middleware1, middleware2])
99
+ .server(async (options) => {
100
+ expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
101
+
102
+ const result = await options.next({ context: { c: 0 } })
103
+
104
+ expectTypeOf(result).toEqualTypeOf<{
105
+ 'use functions must return the result of next()': true
106
+ _types: {
107
+ context: {
108
+ c: number
109
+ }
110
+ sendContext: undefined
111
+ }
112
+ context: { a: boolean; b: string; c: number }
113
+ sendContext: undefined
114
+ }>()
115
+
116
+ return result
117
+ })
118
+
119
+ createMiddleware({ type: 'function' })
120
+ .middleware([middleware3])
121
+ .server(async (options) => {
122
+ expectTypeOf(options.context).toEqualTypeOf<{
123
+ a: boolean
124
+ b: string
125
+ c: number
126
+ }>()
127
+
128
+ const result = await options.next({ context: { d: 5 } })
129
+
130
+ expectTypeOf(result).toEqualTypeOf<{
131
+ 'use functions must return the result of next()': true
132
+ _types: {
133
+ context: {
134
+ d: number
135
+ }
136
+ sendContext: undefined
137
+ }
138
+ context: { a: boolean; b: string; c: number; d: number }
139
+ sendContext: undefined
140
+ }>()
141
+
142
+ return result
143
+ })
144
+ })
145
+
146
+ test('createMiddleware merges client context and sends to the server', () => {
147
+ const middleware1 = createMiddleware({ type: 'function' }).client(
148
+ async (options) => {
149
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
150
+
151
+ const result = await options.next({ context: { a: true } })
152
+
153
+ expectTypeOf(result).toEqualTypeOf<{
154
+ 'use functions must return the result of next()': true
155
+ context: { a: boolean }
156
+ sendContext: undefined
157
+ headers: HeadersInit
158
+ }>()
159
+
160
+ return result
161
+ },
162
+ )
163
+
164
+ const middleware2 = createMiddleware({ type: 'function' }).client(
165
+ async (options) => {
166
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
167
+
168
+ const result = await options.next({ context: { b: 'test' } })
169
+
170
+ expectTypeOf(result).toEqualTypeOf<{
171
+ 'use functions must return the result of next()': true
172
+ context: { b: string }
173
+ sendContext: undefined
174
+ headers: HeadersInit
175
+ }>()
176
+
177
+ return result
178
+ },
179
+ )
180
+
181
+ const middleware3 = createMiddleware({ type: 'function' })
182
+ .middleware([middleware1, middleware2])
183
+ .client(async (options) => {
184
+ expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
185
+
186
+ const result = await options.next({ context: { c: 0 } })
187
+
188
+ expectTypeOf(result).toEqualTypeOf<{
189
+ 'use functions must return the result of next()': true
190
+ context: { a: boolean; b: string; c: number }
191
+ sendContext: undefined
192
+ headers: HeadersInit
193
+ }>()
194
+
195
+ return result
196
+ })
197
+
198
+ const middleware4 = createMiddleware({ type: 'function' })
199
+ .middleware([middleware3])
200
+ .client(async (options) => {
201
+ expectTypeOf(options.context).toEqualTypeOf<{
202
+ a: boolean
203
+ b: string
204
+ c: number
205
+ }>()
206
+
207
+ const result = await options.next({
208
+ sendContext: { ...options.context, d: 5 },
209
+ })
210
+
211
+ expectTypeOf(result).toEqualTypeOf<{
212
+ 'use functions must return the result of next()': true
213
+ context: { a: boolean; b: string; c: number }
214
+ sendContext: { a: boolean; b: string; c: number; d: number }
215
+ headers: HeadersInit
216
+ }>()
217
+
218
+ return result
219
+ })
220
+
221
+ createMiddleware({ type: 'function' })
222
+ .middleware([middleware4])
223
+ .server(async (options) => {
224
+ expectTypeOf(options.context).toEqualTypeOf<{
225
+ a: boolean
226
+ b: string
227
+ c: number
228
+ d: number
229
+ }>()
230
+
231
+ const result = await options.next({
232
+ context: {
233
+ e: 'e',
234
+ },
235
+ })
236
+
237
+ expectTypeOf(result).toEqualTypeOf<{
238
+ 'use functions must return the result of next()': true
239
+ _types: {
240
+ context: {
241
+ e: string
242
+ }
243
+ sendContext: undefined
244
+ }
245
+ context: { a: boolean; b: string; c: number; d: number; e: string }
246
+ sendContext: undefined
247
+ }>()
248
+
249
+ return result
250
+ })
251
+ })
252
+
253
+ test('createMiddleware merges input', () => {
254
+ const middleware1 = createMiddleware({ type: 'function' })
255
+ .validator(() => {
256
+ return {
257
+ a: 'a',
258
+ } as const
259
+ })
260
+ .server(({ data, next }) => {
261
+ expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a' }>()
262
+ return next()
263
+ })
264
+
265
+ const middleware2 = createMiddleware({ type: 'function' })
266
+ .middleware([middleware1])
267
+ .validator(() => {
268
+ return {
269
+ b: 'b',
270
+ } as const
271
+ })
272
+ .server(({ data, next }) => {
273
+ expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a'; readonly b: 'b' }>
274
+ return next()
275
+ })
276
+
277
+ createMiddleware({ type: 'function' })
278
+ .middleware([middleware2])
279
+ .validator(() => ({ c: 'c' }) as const)
280
+ .server(({ next, data }) => {
281
+ expectTypeOf(data).toEqualTypeOf<{
282
+ readonly a: 'a'
283
+ readonly b: 'b'
284
+ readonly c: 'c'
285
+ }>
286
+ return next()
287
+ })
288
+ })
289
+
290
+ test('createMiddleware merges server context and client context, sends server context to the client and merges ', () => {
291
+ const middleware1 = createMiddleware({ type: 'function' })
292
+ .client(async (options) => {
293
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
294
+
295
+ const result = await options.next({
296
+ context: { fromClient1: 'fromClient1' },
297
+ })
298
+
299
+ expectTypeOf(result).toEqualTypeOf<{
300
+ 'use functions must return the result of next()': true
301
+ context: { fromClient1: string }
302
+ sendContext: undefined
303
+ headers: HeadersInit
304
+ }>()
305
+
306
+ return result
307
+ })
308
+ .server(async (options) => {
309
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
310
+
311
+ const result = await options.next({
312
+ context: { fromServer1: 'fromServer1' },
313
+ })
314
+
315
+ expectTypeOf(result).toEqualTypeOf<{
316
+ 'use functions must return the result of next()': true
317
+ _types: {
318
+ context: {
319
+ fromServer1: string
320
+ }
321
+ sendContext: undefined
322
+ }
323
+ context: { fromServer1: string }
324
+ sendContext: undefined
325
+ }>()
326
+
327
+ return result
328
+ })
329
+
330
+ const middleware2 = createMiddleware({ type: 'function' })
331
+ .client(async (options) => {
332
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
333
+
334
+ const result = await options.next({
335
+ context: { fromClient2: 'fromClient2' },
336
+ })
337
+
338
+ expectTypeOf(result).toEqualTypeOf<{
339
+ 'use functions must return the result of next()': true
340
+ context: { fromClient2: string }
341
+ sendContext: undefined
342
+ headers: HeadersInit
343
+ }>()
344
+
345
+ return result
346
+ })
347
+ .server(async (options) => {
348
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
349
+
350
+ const result = await options.next({
351
+ context: { fromServer2: 'fromServer2' },
352
+ })
353
+
354
+ expectTypeOf(result).toEqualTypeOf<{
355
+ 'use functions must return the result of next()': true
356
+ _types: {
357
+ context: {
358
+ fromServer2: string
359
+ }
360
+ sendContext: undefined
361
+ }
362
+ context: { fromServer2: string }
363
+ sendContext: undefined
364
+ }>()
365
+
366
+ return result
367
+ })
368
+
369
+ const middleware3 = createMiddleware({ type: 'function' })
370
+ .middleware([middleware1, middleware2])
371
+ .client(async (options) => {
372
+ expectTypeOf(options.context).toEqualTypeOf<{
373
+ fromClient1: string
374
+ fromClient2: string
375
+ }>()
376
+
377
+ const result = await options.next({
378
+ context: { fromClient3: 'fromClient3' },
379
+ })
380
+
381
+ expectTypeOf(result).toEqualTypeOf<{
382
+ 'use functions must return the result of next()': true
383
+ context: {
384
+ fromClient1: string
385
+ fromClient2: string
386
+ fromClient3: string
387
+ }
388
+ sendContext: undefined
389
+ headers: HeadersInit
390
+ }>()
391
+
392
+ return result
393
+ })
394
+ .server(async (options) => {
395
+ expectTypeOf(options.context).toEqualTypeOf<{
396
+ fromServer1: string
397
+ fromServer2: string
398
+ }>()
399
+
400
+ const result = await options.next({
401
+ context: { fromServer3: 'fromServer3' },
402
+ })
403
+
404
+ expectTypeOf(result).toEqualTypeOf<{
405
+ 'use functions must return the result of next()': true
406
+ _types: {
407
+ context: {
408
+ fromServer3: string
409
+ }
410
+ sendContext: undefined
411
+ }
412
+ context: {
413
+ fromServer1: string
414
+ fromServer2: string
415
+ fromServer3: string
416
+ }
417
+ sendContext: undefined
418
+ }>()
419
+
420
+ return result
421
+ })
422
+
423
+ const middleware4 = createMiddleware({ type: 'function' })
424
+ .middleware([middleware3])
425
+ .client(async (options) => {
426
+ expectTypeOf(options.context).toEqualTypeOf<{
427
+ fromClient1: string
428
+ fromClient2: string
429
+ fromClient3: string
430
+ }>()
431
+
432
+ const result = await options.next({
433
+ context: { fromClient4: 'fromClient4' },
434
+ sendContext: { toServer1: 'toServer1' },
435
+ })
436
+
437
+ expectTypeOf(result).toEqualTypeOf<{
438
+ 'use functions must return the result of next()': true
439
+ context: {
440
+ fromClient1: string
441
+ fromClient2: string
442
+ fromClient3: string
443
+ fromClient4: string
444
+ }
445
+ sendContext: { toServer1: 'toServer1' }
446
+ headers: HeadersInit
447
+ }>()
448
+
449
+ return result
450
+ })
451
+ .server(async (options) => {
452
+ expectTypeOf(options.context).toEqualTypeOf<{
453
+ fromServer1: string
454
+ fromServer2: string
455
+ fromServer3: string
456
+ toServer1: 'toServer1'
457
+ }>()
458
+
459
+ const result = await options.next({
460
+ context: { fromServer4: 'fromServer4' },
461
+ sendContext: { toClient1: 'toClient1' },
462
+ })
463
+
464
+ expectTypeOf(result).toEqualTypeOf<{
465
+ 'use functions must return the result of next()': true
466
+ _types: {
467
+ context: {
468
+ fromServer4: string
469
+ }
470
+ sendContext: {
471
+ toClient1: 'toClient1'
472
+ }
473
+ }
474
+ context: {
475
+ fromServer1: string
476
+ fromServer2: string
477
+ fromServer3: string
478
+ fromServer4: string
479
+ toServer1: 'toServer1'
480
+ }
481
+ sendContext: { toClient1: 'toClient1' }
482
+ }>()
483
+
484
+ return result
485
+ })
486
+
487
+ createMiddleware({ type: 'function' })
488
+ .middleware([middleware4])
489
+ .client(async (options) => {
490
+ expectTypeOf(options.context).toEqualTypeOf<{
491
+ fromClient1: string
492
+ fromClient2: string
493
+ fromClient3: string
494
+ fromClient4: string
495
+ }>()
496
+
497
+ const result = await options.next({
498
+ context: { fromClient5: 'fromClient5' },
499
+ sendContext: { toServer2: 'toServer2' },
500
+ })
501
+
502
+ expectTypeOf(result).toEqualTypeOf<{
503
+ 'use functions must return the result of next()': true
504
+ context: {
505
+ fromClient1: string
506
+ fromClient2: string
507
+ fromClient3: string
508
+ fromClient4: string
509
+ fromClient5: string
510
+ toClient1: 'toClient1'
511
+ }
512
+ sendContext: { toServer1: 'toServer1'; toServer2: 'toServer2' }
513
+ headers: HeadersInit
514
+ }>()
515
+
516
+ return result
517
+ })
518
+ .server(async (options) => {
519
+ expectTypeOf(options.context).toEqualTypeOf<{
520
+ fromServer1: string
521
+ fromServer2: string
522
+ fromServer3: string
523
+ fromServer4: string
524
+ toServer1: 'toServer1'
525
+ toServer2: 'toServer2'
526
+ }>()
527
+
528
+ const result = await options.next({
529
+ context: { fromServer5: 'fromServer5' },
530
+ sendContext: { toClient2: 'toClient2' },
531
+ })
532
+
533
+ expectTypeOf(result).toEqualTypeOf<{
534
+ 'use functions must return the result of next()': true
535
+ _types: {
536
+ context: {
537
+ fromServer5: string
538
+ }
539
+ sendContext: {
540
+ toClient2: 'toClient2'
541
+ }
542
+ }
543
+ context: {
544
+ fromServer1: string
545
+ fromServer2: string
546
+ fromServer3: string
547
+ fromServer4: string
548
+ fromServer5: string
549
+ toServer1: 'toServer1'
550
+ toServer2: 'toServer2'
551
+ }
552
+ sendContext: { toClient1: 'toClient1'; toClient2: 'toClient2' }
553
+ }>()
554
+
555
+ return result
556
+ })
557
+ })
558
+
559
+ test('createMiddleware sendContext cannot send a function', () => {
560
+ createMiddleware({ type: 'function' })
561
+ .client(({ next }) => {
562
+ expectTypeOf(next<{ func: () => 'func' }>)
563
+ .parameter(0)
564
+ .exclude<undefined>()
565
+ .toHaveProperty('sendContext')
566
+ .toEqualTypeOf<{ func: 'Function is not serializable' } | undefined>()
567
+
568
+ return next()
569
+ })
570
+ .server(({ next }) => {
571
+ expectTypeOf(next<undefined, { func: () => 'func' }>)
572
+ .parameter(0)
573
+ .exclude<undefined>()
574
+ .toHaveProperty('sendContext')
575
+ .toEqualTypeOf<{ func: 'Function is not serializable' } | undefined>()
576
+
577
+ return next()
578
+ })
579
+ })
580
+
581
+ test('createMiddleware cannot validate function', () => {
582
+ const validator = createMiddleware({ type: 'function' }).validator<
583
+ (input: { func: () => 'string' }) => { output: 'string' }
584
+ >
585
+
586
+ expectTypeOf(validator)
587
+ .parameter(0)
588
+ .toEqualTypeOf<
589
+ Constrain<
590
+ (input: { func: () => 'string' }) => { output: 'string' },
591
+ Validator<{ func: 'Function is not serializable' }, any>
592
+ >
593
+ >()
594
+ })
595
+
596
+ test('createMiddleware can validate Date', () => {
597
+ const validator = createMiddleware({ type: 'function' }).validator<
598
+ (input: Date) => { output: 'string' }
599
+ >
600
+
601
+ expectTypeOf(validator)
602
+ .parameter(0)
603
+ .toEqualTypeOf<ConstrainValidator<(input: Date) => { output: 'string' }>>()
604
+ })
605
+
606
+ test('createMiddleware can validate FormData', () => {
607
+ const validator = createMiddleware({ type: 'function' }).validator<
608
+ (input: FormData) => { output: 'string' }
609
+ >
610
+
611
+ expectTypeOf(validator)
612
+ .parameter(0)
613
+ .toEqualTypeOf<
614
+ ConstrainValidator<(input: FormData) => { output: 'string' }>
615
+ >()
616
+ })
617
+
618
+ test('createMiddleware merging from parent with undefined validator', () => {
619
+ const middleware1 = createMiddleware({ type: 'function' }).validator(
620
+ (input: { test: string }) => input.test,
621
+ )
622
+
623
+ createMiddleware({ type: 'function' })
624
+ .middleware([middleware1])
625
+ .server((ctx) => {
626
+ expectTypeOf(ctx.data).toEqualTypeOf<string>()
627
+
628
+ return ctx.next()
629
+ })
630
+ })
631
+
632
+ test('createMiddleware validator infers unknown for default input type', () => {
633
+ createMiddleware({ type: 'function' })
634
+ .validator((input) => {
635
+ expectTypeOf(input).toEqualTypeOf<unknown>()
636
+
637
+ if (typeof input === 'number') return 'success' as const
638
+
639
+ return 'failed' as const
640
+ })
641
+ .server(({ data, next }) => {
642
+ expectTypeOf(data).toEqualTypeOf<'success' | 'failed'>()
643
+
644
+ return next()
645
+ })
646
+ })
647
+
648
+ test('createMiddleware with type request, no middleware or context', () => {
649
+ createMiddleware({ type: 'request' }).server(async (options) => {
650
+ expectTypeOf(options).toEqualTypeOf<{
651
+ request: Request
652
+ next: RequestServerNextFn<undefined>
653
+ pathname: string
654
+ context: undefined
655
+ }>()
656
+
657
+ const result = await options.next()
658
+
659
+ expectTypeOf(result).toEqualTypeOf<{
660
+ context: undefined
661
+ pathname: string
662
+ request: Request
663
+ response: Response
664
+ }>()
665
+
666
+ return result
667
+ })
668
+ })
669
+
670
+ test('createMiddleware with type request, no middleware with context', () => {
671
+ createMiddleware({ type: 'request' }).server(async (options) => {
672
+ expectTypeOf(options).toEqualTypeOf<{
673
+ request: Request
674
+ next: RequestServerNextFn<undefined>
675
+ pathname: string
676
+ context: undefined
677
+ }>()
678
+
679
+ const result = await options.next({ context: { a: 'a' } })
680
+
681
+ expectTypeOf(result).toEqualTypeOf<{
682
+ context: { a: string }
683
+ pathname: string
684
+ request: Request
685
+ response: Response
686
+ }>()
687
+
688
+ return result
689
+ })
690
+ })
691
+
692
+ test('createMiddleware with type request, middleware and context', () => {
693
+ const middleware1 = createMiddleware({ type: 'request' }).server(
694
+ async (options) => {
695
+ expectTypeOf(options).toEqualTypeOf<{
696
+ request: Request
697
+ next: RequestServerNextFn<undefined>
698
+ pathname: string
699
+ context: undefined
700
+ }>()
701
+
702
+ const result = await options.next({ context: { a: 'a' } })
703
+
704
+ expectTypeOf(result).toEqualTypeOf<{
705
+ context: { a: string }
706
+ pathname: string
707
+ request: Request
708
+ response: Response
709
+ }>()
710
+
711
+ return result
712
+ },
713
+ )
714
+
715
+ createMiddleware({ type: 'request' })
716
+ .middleware([middleware1])
717
+ .server(async (options) => {
718
+ expectTypeOf(options).toEqualTypeOf<{
719
+ request: Request
720
+ next: RequestServerNextFn<undefined>
721
+ pathname: string
722
+ context: { a: string }
723
+ }>()
724
+
725
+ const result = await options.next({ context: { b: 'b' } })
726
+
727
+ expectTypeOf(result).toEqualTypeOf<{
728
+ context: { a: string; b: string }
729
+ pathname: string
730
+ request: Request
731
+ response: Response
732
+ }>()
733
+
734
+ return result
735
+ })
736
+ })