|
| 1 | +import { expectTypeOf } from 'vitest'; |
| 2 | +import type { Parameter } from '../lib/parameter.ts'; |
| 3 | +import type { ValidParameter } from '../lib/types.ts'; |
| 4 | + |
| 5 | +// Test that only matching path param names are allowed |
| 6 | +type PathParams = ValidParameter<'/users/{userId}'>; |
| 7 | +expectTypeOf<Parameter<'userId', 'path'>>().toExtend<PathParams>(); |
| 8 | + |
| 9 | +// @ts-expect-error path parameter with wrong name is not allowed |
| 10 | +expectTypeOf<Parameter<'notInRoute', 'path'>>().toExtend<PathParams>(); |
| 11 | + |
| 12 | +// Test that query parameters can be any string |
| 13 | +expectTypeOf<Parameter<'anyName', 'query'>>().toExtend< |
| 14 | + ValidParameter<'/users/{userId}'> |
| 15 | +>(); |
| 16 | + |
| 17 | +// Test that header parameters can be any string |
| 18 | +expectTypeOf<Parameter<'X-Custom-Header', 'header'>>().toExtend< |
| 19 | + ValidParameter<'/users/{userId}'> |
| 20 | +>(); |
| 21 | + |
| 22 | +// Test that cookie parameters can be any string |
| 23 | +expectTypeOf<Parameter<'sessionId', 'cookie'>>().toExtend< |
| 24 | + ValidParameter<'/users/{userId}'> |
| 25 | +>(); |
| 26 | + |
| 27 | +// Test multi-param routes |
| 28 | +type MultiParamRoute = ValidParameter<'/users/{userId}/notes/{noteId}'>; |
| 29 | +expectTypeOf<Parameter<'userId', 'path'>>().toExtend<MultiParamRoute>(); |
| 30 | +expectTypeOf<Parameter<'noteId', 'path'>>().toExtend<MultiParamRoute>(); |
| 31 | + |
| 32 | +// @ts-expect-error path parameter not in route |
| 33 | +expectTypeOf<Parameter<'otherParam', 'path'>>().toExtend<MultiParamRoute>(); |
| 34 | + |
| 35 | +// Test that non-path parameters work for any route |
| 36 | +expectTypeOf<Parameter<'limit', 'query'>>().toExtend<MultiParamRoute>(); |
| 37 | +expectTypeOf<Parameter<'offset', 'query'>>().toExtend<MultiParamRoute>(); |
0 commit comments