@tanstack/start-client-core 1.120.3 → 1.120.4-alpha.10

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.
@@ -16,15 +16,17 @@ import type {
16
16
  } from '@tanstack/router-core'
17
17
  import type { Readable } from 'node:stream'
18
18
  import type {
19
- AnyMiddleware,
19
+ AnyFunctionMiddleware,
20
20
  AssignAllClientSendContext,
21
21
  AssignAllServerContext,
22
+ FunctionMiddlewareClientFnResult,
23
+ FunctionMiddlewareServerFnResult,
22
24
  IntersectAllValidatorInputs,
23
25
  IntersectAllValidatorOutputs,
24
- MiddlewareClientFnResult,
25
- MiddlewareServerFnResult,
26
26
  } from './createMiddleware'
27
27
 
28
+ type TODO = any
29
+
28
30
  export function createServerFn<
29
31
  TMethod extends Method,
30
32
  TServerFnResponseType extends ServerFnResponseType = 'data',
@@ -215,8 +217,8 @@ export function createServerFn<
215
217
  }
216
218
  }
217
219
 
218
- async function executeMiddleware(
219
- middlewares: Array<AnyMiddleware>,
220
+ export async function executeMiddleware(
221
+ middlewares: Array<AnyFunctionMiddleware>,
220
222
  env: 'client' | 'server',
221
223
  opts: ServerFnMiddlewareOptions,
222
224
  ): Promise<ServerFnMiddlewareResult> {
@@ -398,6 +400,7 @@ export type ServerFnReturnType<
398
400
  > = TServerFnResponseType extends 'raw'
399
401
  ? RawResponse | Promise<RawResponse>
400
402
  : Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>
403
+
401
404
  export type ServerFn<
402
405
  TMethod,
403
406
  TServerFnResponseType extends ServerFnResponseType,
@@ -442,7 +445,7 @@ export type ServerFnBaseOptions<
442
445
  method: TMethod
443
446
  response?: TServerFnResponseType
444
447
  validateClient?: boolean
445
- middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyMiddleware>>
448
+ middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>
446
449
  validator?: ConstrainValidator<TInput>
447
450
  extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>
448
451
  serverFn?: ServerFn<
@@ -485,7 +488,10 @@ export interface ServerFnMiddleware<
485
488
  TValidator,
486
489
  > {
487
490
  middleware: <const TNewMiddlewares = undefined>(
488
- middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,
491
+ middlewares: Constrain<
492
+ TNewMiddlewares,
493
+ ReadonlyArray<AnyFunctionMiddleware>
494
+ >,
489
495
  ) => ServerFnAfterMiddleware<
490
496
  TMethod,
491
497
  TServerFnResponseType,
@@ -812,12 +818,12 @@ export function extractFormDataContext(formData: FormData) {
812
818
  }
813
819
 
814
820
  export function flattenMiddlewares(
815
- middlewares: Array<AnyMiddleware>,
816
- ): Array<AnyMiddleware> {
817
- const seen = new Set<AnyMiddleware>()
818
- const flattened: Array<AnyMiddleware> = []
821
+ middlewares: Array<AnyFunctionMiddleware>,
822
+ ): Array<AnyFunctionMiddleware> {
823
+ const seen = new Set<AnyFunctionMiddleware>()
824
+ const flattened: Array<AnyFunctionMiddleware> = []
819
825
 
820
- const recurse = (middleware: Array<AnyMiddleware>) => {
826
+ const recurse = (middleware: Array<AnyFunctionMiddleware>) => {
821
827
  middleware.forEach((m) => {
822
828
  if (m.options.middleware) {
823
829
  recurse(m.options.middleware)
@@ -929,7 +935,7 @@ export function execValidator(
929
935
 
930
936
  export function serverFnBaseToMiddleware(
931
937
  options: ServerFnBaseOptions<any, any, any, any, any>,
932
- ): AnyMiddleware {
938
+ ): AnyFunctionMiddleware {
933
939
  return {
934
940
  _types: undefined!,
935
941
  options: {
@@ -973,16 +979,25 @@ export function serverFnBaseToMiddleware(
973
979
  // but not before serializing the context
974
980
  const res = await options.extractedFn?.(payload)
975
981
 
976
- return next(res) as unknown as MiddlewareClientFnResult<any, any, any>
982
+ return next(res) as unknown as FunctionMiddlewareClientFnResult<
983
+ any,
984
+ any,
985
+ any
986
+ >
977
987
  },
978
988
  server: async ({ next, ...ctx }) => {
979
989
  // Execute the server function
980
- const result = await options.serverFn?.(ctx)
990
+ const result = await options.serverFn?.(ctx as TODO)
981
991
 
982
992
  return next({
983
993
  ...ctx,
984
994
  result,
985
- } as any) as unknown as MiddlewareServerFnResult<any, any, any, any>
995
+ } as any) as unknown as FunctionMiddlewareServerFnResult<
996
+ any,
997
+ any,
998
+ any,
999
+ any
1000
+ >
986
1001
  },
987
1002
  },
988
1003
  }
package/src/index.tsx CHANGED
@@ -25,30 +25,30 @@ export {
25
25
  createMiddleware,
26
26
  type IntersectAllValidatorInputs,
27
27
  type IntersectAllValidatorOutputs,
28
- type MiddlewareServerFn,
29
- type AnyMiddleware,
30
- type MiddlewareOptions,
31
- type MiddlewareWithTypes,
32
- type MiddlewareValidator,
33
- type MiddlewareServer,
34
- type MiddlewareAfterClient,
35
- type MiddlewareAfterMiddleware,
36
- type MiddlewareAfterServer,
37
- type Middleware,
38
- type MiddlewareClientFnOptions,
39
- type MiddlewareClientFnResult,
40
- type MiddlewareClientNextFn,
41
- type ClientResultWithContext,
28
+ type FunctionMiddlewareServerFn,
29
+ type AnyFunctionMiddleware,
30
+ type FunctionMiddlewareOptions,
31
+ type FunctionMiddlewareWithTypes,
32
+ type FunctionMiddlewareValidator,
33
+ type FunctionMiddlewareServer,
34
+ type FunctionMiddlewareAfterClient,
35
+ type FunctionMiddlewareAfterServer,
36
+ type FunctionMiddleware,
37
+ type FunctionMiddlewareClientFnOptions,
38
+ type FunctionMiddlewareClientFnResult,
39
+ type FunctionMiddlewareClientNextFn,
40
+ type FunctionClientResultWithContext,
42
41
  type AssignAllClientContextBeforeNext,
43
42
  type AssignAllMiddleware,
44
43
  type AssignAllServerContext,
45
- type MiddlewareAfterValidator,
46
- type MiddlewareClientFn,
47
- type MiddlewareServerFnResult,
48
- type MiddlewareClient,
49
- type MiddlewareServerFnOptions,
50
- type MiddlewareServerNextFn,
51
- type ServerResultWithContext,
44
+ type FunctionMiddlewareAfterValidator,
45
+ type FunctionMiddlewareClientFn,
46
+ type FunctionMiddlewareServerFnResult,
47
+ type FunctionMiddlewareClient,
48
+ type FunctionMiddlewareServerFnOptions,
49
+ type FunctionMiddlewareServerNextFn,
50
+ type FunctionServerResultWithContext,
51
+ type AnyRequestMiddleware,
52
52
  } from './createMiddleware'
53
53
  export {
54
54
  registerGlobalMiddleware,
@@ -84,4 +84,5 @@ export {
84
84
  extractFormDataContext,
85
85
  flattenMiddlewares,
86
86
  serverFnStaticCache,
87
+ executeMiddleware,
87
88
  } from './createServerFn'
@@ -1,9 +1,9 @@
1
- import type { AnyMiddleware } from './createMiddleware'
1
+ import type { AnyFunctionMiddleware } from './createMiddleware'
2
2
 
3
- export const globalMiddleware: Array<AnyMiddleware> = []
3
+ export const globalMiddleware: Array<AnyFunctionMiddleware> = []
4
4
 
5
5
  export function registerGlobalMiddleware(options: {
6
- middleware: Array<AnyMiddleware>
6
+ middleware: Array<AnyFunctionMiddleware>
7
7
  }) {
8
8
  globalMiddleware.push(...options.middleware)
9
9
  }
@@ -60,21 +60,25 @@ test('createServerFn with validator', () => {
60
60
  })
61
61
 
62
62
  test('createServerFn with middleware and context', () => {
63
- const middleware1 = createMiddleware().server(({ next }) => {
64
- return next({ context: { a: 'a' } as const })
65
- })
63
+ const middleware1 = createMiddleware({ type: 'function' }).server(
64
+ ({ next }) => {
65
+ return next({ context: { a: 'a' } as const })
66
+ },
67
+ )
66
68
 
67
- const middleware2 = createMiddleware().server(({ next }) => {
68
- return next({ context: { b: 'b' } as const })
69
- })
69
+ const middleware2 = createMiddleware({ type: 'function' }).server(
70
+ ({ next }) => {
71
+ return next({ context: { b: 'b' } as const })
72
+ },
73
+ )
70
74
 
71
- const middleware3 = createMiddleware()
75
+ const middleware3 = createMiddleware({ type: 'function' })
72
76
  .middleware([middleware1, middleware2])
73
77
  .client(({ next }) => {
74
78
  return next({ context: { c: 'c' } as const })
75
79
  })
76
80
 
77
- const middleware4 = createMiddleware()
81
+ const middleware4 = createMiddleware({ type: 'function' })
78
82
  .middleware([middleware3])
79
83
  .client(({ context, next }) => {
80
84
  return next({ sendContext: context })
@@ -113,21 +117,24 @@ test('createServerFn with middleware and context', () => {
113
117
  })
114
118
 
115
119
  describe('createServerFn with middleware and validator', () => {
116
- const middleware1 = createMiddleware().validator(
120
+ const middleware1 = createMiddleware({ type: 'function' }).validator(
117
121
  (input: { readonly inputA: 'inputA' }) =>
118
122
  ({
119
123
  outputA: 'outputA',
120
124
  }) as const,
121
125
  )
122
126
 
123
- const middleware2 = createMiddleware().validator(
127
+ const middleware2 = createMiddleware({ type: 'function' }).validator(
124
128
  (input: { readonly inputB: 'inputB' }) =>
125
129
  ({
126
130
  outputB: 'outputB',
127
131
  }) as const,
128
132
  )
129
133
 
130
- const middleware3 = createMiddleware().middleware([middleware1, middleware2])
134
+ const middleware3 = createMiddleware({ type: 'function' }).middleware([
135
+ middleware1,
136
+ middleware2,
137
+ ])
131
138
 
132
139
  test(`response: 'data'`, () => {
133
140
  const fn = createServerFn({ method: 'GET', response: 'data' })
@@ -222,7 +229,7 @@ describe('createServerFn with middleware and validator', () => {
222
229
  })
223
230
 
224
231
  test('createServerFn overrides properties', () => {
225
- const middleware1 = createMiddleware()
232
+ const middleware1 = createMiddleware({ type: 'function' })
226
233
  .validator(
227
234
  () =>
228
235
  ({
@@ -246,7 +253,7 @@ test('createServerFn overrides properties', () => {
246
253
  return next({ sendContext: newContext, context: newContext })
247
254
  })
248
255
 
249
- const middleware2 = createMiddleware()
256
+ const middleware2 = createMiddleware({ type: 'function' })
250
257
  .middleware([middleware1])
251
258
  .validator(
252
259
  () =>
@@ -1,10 +1,11 @@
1
1
  import { expectTypeOf, test } from 'vitest'
2
2
  import { createMiddleware } from '../createMiddleware'
3
+ import type { RequestServerNextFn } from '../createMiddleware'
3
4
  import type { Constrain, Validator } from '@tanstack/router-core'
4
5
  import type { ConstrainValidator } from '../createServerFn'
5
6
 
6
7
  test('createServeMiddleware removes middleware after middleware,', () => {
7
- const middleware = createMiddleware()
8
+ const middleware = createMiddleware({ type: 'function' })
8
9
 
9
10
  expectTypeOf(middleware).toHaveProperty('middleware')
10
11
  expectTypeOf(middleware).toHaveProperty('server')
@@ -45,51 +46,55 @@ test('createServeMiddleware removes middleware after middleware,', () => {
45
46
  })
46
47
 
47
48
  test('createMiddleware merges server context', () => {
48
- const middleware1 = createMiddleware().server(async (options) => {
49
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
50
- expectTypeOf(options.data).toEqualTypeOf<undefined>()
51
- expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
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'>()
52
54
 
53
- const result = await options.next({ context: { a: true } })
55
+ const result = await options.next({ context: { a: true } })
54
56
 
55
- expectTypeOf(result).toEqualTypeOf<{
56
- 'use functions must return the result of next()': true
57
- _types: {
58
- context: {
59
- a: boolean
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
60
64
  }
65
+ context: { a: boolean }
61
66
  sendContext: undefined
62
- }
63
- context: { a: boolean }
64
- sendContext: undefined
65
- }>()
67
+ }>()
66
68
 
67
- return result
68
- })
69
+ return result
70
+ },
71
+ )
69
72
 
70
- const middleware2 = createMiddleware().server(async (options) => {
71
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
72
- expectTypeOf(options.data).toEqualTypeOf<undefined>()
73
- expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
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'>()
74
78
 
75
- const result = await options.next({ context: { b: 'test' } })
79
+ const result = await options.next({ context: { b: 'test' } })
76
80
 
77
- expectTypeOf(result).toEqualTypeOf<{
78
- 'use functions must return the result of next()': true
79
- _types: {
80
- context: {
81
- b: string
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
82
88
  }
89
+ context: { b: string }
83
90
  sendContext: undefined
84
- }
85
- context: { b: string }
86
- sendContext: undefined
87
- }>()
91
+ }>()
88
92
 
89
- return result
90
- })
93
+ return result
94
+ },
95
+ )
91
96
 
92
- const middleware3 = createMiddleware()
97
+ const middleware3 = createMiddleware({ type: 'function' })
93
98
  .middleware([middleware1, middleware2])
94
99
  .server(async (options) => {
95
100
  expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
@@ -111,7 +116,7 @@ test('createMiddleware merges server context', () => {
111
116
  return result
112
117
  })
113
118
 
114
- createMiddleware()
119
+ createMiddleware({ type: 'function' })
115
120
  .middleware([middleware3])
116
121
  .server(async (options) => {
117
122
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -139,37 +144,41 @@ test('createMiddleware merges server context', () => {
139
144
  })
140
145
 
141
146
  test('createMiddleware merges client context and sends to the server', () => {
142
- const middleware1 = createMiddleware().client(async (options) => {
143
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
147
+ const middleware1 = createMiddleware({ type: 'function' }).client(
148
+ async (options) => {
149
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
144
150
 
145
- const result = await options.next({ context: { a: true } })
151
+ const result = await options.next({ context: { a: true } })
146
152
 
147
- expectTypeOf(result).toEqualTypeOf<{
148
- 'use functions must return the result of next()': true
149
- context: { a: boolean }
150
- sendContext: undefined
151
- headers: HeadersInit
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
+ }>()
153
159
 
154
- return result
155
- })
160
+ return result
161
+ },
162
+ )
156
163
 
157
- const middleware2 = createMiddleware().client(async (options) => {
158
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
164
+ const middleware2 = createMiddleware({ type: 'function' }).client(
165
+ async (options) => {
166
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
159
167
 
160
- const result = await options.next({ context: { b: 'test' } })
168
+ const result = await options.next({ context: { b: 'test' } })
161
169
 
162
- expectTypeOf(result).toEqualTypeOf<{
163
- 'use functions must return the result of next()': true
164
- context: { b: string }
165
- sendContext: undefined
166
- headers: HeadersInit
167
- }>()
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
+ }>()
168
176
 
169
- return result
170
- })
177
+ return result
178
+ },
179
+ )
171
180
 
172
- const middleware3 = createMiddleware()
181
+ const middleware3 = createMiddleware({ type: 'function' })
173
182
  .middleware([middleware1, middleware2])
174
183
  .client(async (options) => {
175
184
  expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
@@ -186,7 +195,7 @@ test('createMiddleware merges client context and sends to the server', () => {
186
195
  return result
187
196
  })
188
197
 
189
- const middleware4 = createMiddleware()
198
+ const middleware4 = createMiddleware({ type: 'function' })
190
199
  .middleware([middleware3])
191
200
  .client(async (options) => {
192
201
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -209,7 +218,7 @@ test('createMiddleware merges client context and sends to the server', () => {
209
218
  return result
210
219
  })
211
220
 
212
- createMiddleware()
221
+ createMiddleware({ type: 'function' })
213
222
  .middleware([middleware4])
214
223
  .server(async (options) => {
215
224
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -242,7 +251,7 @@ test('createMiddleware merges client context and sends to the server', () => {
242
251
  })
243
252
 
244
253
  test('createMiddleware merges input', () => {
245
- const middleware1 = createMiddleware()
254
+ const middleware1 = createMiddleware({ type: 'function' })
246
255
  .validator(() => {
247
256
  return {
248
257
  a: 'a',
@@ -253,7 +262,7 @@ test('createMiddleware merges input', () => {
253
262
  return next()
254
263
  })
255
264
 
256
- const middleware2 = createMiddleware()
265
+ const middleware2 = createMiddleware({ type: 'function' })
257
266
  .middleware([middleware1])
258
267
  .validator(() => {
259
268
  return {
@@ -265,7 +274,7 @@ test('createMiddleware merges input', () => {
265
274
  return next()
266
275
  })
267
276
 
268
- createMiddleware()
277
+ createMiddleware({ type: 'function' })
269
278
  .middleware([middleware2])
270
279
  .validator(() => ({ c: 'c' }) as const)
271
280
  .server(({ next, data }) => {
@@ -279,7 +288,7 @@ test('createMiddleware merges input', () => {
279
288
  })
280
289
 
281
290
  test('createMiddleware merges server context and client context, sends server context to the client and merges ', () => {
282
- const middleware1 = createMiddleware()
291
+ const middleware1 = createMiddleware({ type: 'function' })
283
292
  .client(async (options) => {
284
293
  expectTypeOf(options.context).toEqualTypeOf<undefined>()
285
294
 
@@ -318,7 +327,7 @@ test('createMiddleware merges server context and client context, sends server co
318
327
  return result
319
328
  })
320
329
 
321
- const middleware2 = createMiddleware()
330
+ const middleware2 = createMiddleware({ type: 'function' })
322
331
  .client(async (options) => {
323
332
  expectTypeOf(options.context).toEqualTypeOf<undefined>()
324
333
 
@@ -357,7 +366,7 @@ test('createMiddleware merges server context and client context, sends server co
357
366
  return result
358
367
  })
359
368
 
360
- const middleware3 = createMiddleware()
369
+ const middleware3 = createMiddleware({ type: 'function' })
361
370
  .middleware([middleware1, middleware2])
362
371
  .client(async (options) => {
363
372
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -411,7 +420,7 @@ test('createMiddleware merges server context and client context, sends server co
411
420
  return result
412
421
  })
413
422
 
414
- const middleware4 = createMiddleware()
423
+ const middleware4 = createMiddleware({ type: 'function' })
415
424
  .middleware([middleware3])
416
425
  .client(async (options) => {
417
426
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -475,7 +484,7 @@ test('createMiddleware merges server context and client context, sends server co
475
484
  return result
476
485
  })
477
486
 
478
- createMiddleware()
487
+ createMiddleware({ type: 'function' })
479
488
  .middleware([middleware4])
480
489
  .client(async (options) => {
481
490
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -548,7 +557,7 @@ test('createMiddleware merges server context and client context, sends server co
548
557
  })
549
558
 
550
559
  test('createMiddleware sendContext cannot send a function', () => {
551
- createMiddleware()
560
+ createMiddleware({ type: 'function' })
552
561
  .client(({ next }) => {
553
562
  expectTypeOf(next<{ func: () => 'func' }>)
554
563
  .parameter(0)
@@ -570,7 +579,7 @@ test('createMiddleware sendContext cannot send a function', () => {
570
579
  })
571
580
 
572
581
  test('createMiddleware cannot validate function', () => {
573
- const validator = createMiddleware().validator<
582
+ const validator = createMiddleware({ type: 'function' }).validator<
574
583
  (input: { func: () => 'string' }) => { output: 'string' }
575
584
  >
576
585
 
@@ -585,7 +594,7 @@ test('createMiddleware cannot validate function', () => {
585
594
  })
586
595
 
587
596
  test('createMiddleware can validate Date', () => {
588
- const validator = createMiddleware().validator<
597
+ const validator = createMiddleware({ type: 'function' }).validator<
589
598
  (input: Date) => { output: 'string' }
590
599
  >
591
600
 
@@ -595,7 +604,7 @@ test('createMiddleware can validate Date', () => {
595
604
  })
596
605
 
597
606
  test('createMiddleware can validate FormData', () => {
598
- const validator = createMiddleware().validator<
607
+ const validator = createMiddleware({ type: 'function' }).validator<
599
608
  (input: FormData) => { output: 'string' }
600
609
  >
601
610
 
@@ -606,8 +615,22 @@ test('createMiddleware can validate FormData', () => {
606
615
  >()
607
616
  })
608
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
+
609
632
  test('createMiddleware validator infers unknown for default input type', () => {
610
- createMiddleware()
633
+ createMiddleware({ type: 'function' })
611
634
  .validator((input) => {
612
635
  expectTypeOf(input).toEqualTypeOf<unknown>()
613
636
 
@@ -621,3 +644,93 @@ test('createMiddleware validator infers unknown for default input type', () => {
621
644
  return next()
622
645
  })
623
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
+ })