|
1 | 1 | import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
2 | 3 | import { Router } from '../../src/http/index.js'; |
3 | 4 | import type { IStore } from '../../src/store/Store.js'; |
4 | 5 | import type { |
5 | 6 | Env, |
| 7 | + InferResBody, |
| 8 | + InferResSchema, |
6 | 9 | IntersectAll, |
7 | 10 | MergeEnv, |
8 | 11 | Middleware, |
@@ -307,3 +310,113 @@ describe('includeRouter typing', () => { |
307 | 310 | }); |
308 | 311 | }); |
309 | 312 | }); |
| 313 | + |
| 314 | +describe('Response validation typing', () => { |
| 315 | + it('infers the output type for a standard response schema', () => { |
| 316 | + const responseSchema = z.object({ id: z.string(), name: z.string() }); |
| 317 | + type Config = { res: { body: typeof responseSchema } }; |
| 318 | + |
| 319 | + expectTypeOf<InferResBody<Config>>().toEqualTypeOf<{ |
| 320 | + id: string; |
| 321 | + name: string; |
| 322 | + }>(); |
| 323 | + }); |
| 324 | + |
| 325 | + it('infers the output type for a coerced response schema', () => { |
| 326 | + const responseSchema = z.object({ |
| 327 | + id: z.coerce.string(), |
| 328 | + name: z.string(), |
| 329 | + }); |
| 330 | + type Config = { res: { body: typeof responseSchema } }; |
| 331 | + |
| 332 | + expectTypeOf<InferResBody<Config>>().toEqualTypeOf<{ |
| 333 | + id: unknown; |
| 334 | + name: string; |
| 335 | + }>(); |
| 336 | + }); |
| 337 | + |
| 338 | + it('infers the output type for a transformed response schema', () => { |
| 339 | + const responseSchema = z |
| 340 | + .object({ |
| 341 | + id: z.number(), |
| 342 | + name: z.string(), |
| 343 | + }) |
| 344 | + .transform((r) => ({ |
| 345 | + ...r, |
| 346 | + id: String(r.id), |
| 347 | + })); |
| 348 | + |
| 349 | + type Config = { res: { body: typeof responseSchema } }; |
| 350 | + |
| 351 | + expectTypeOf<InferResBody<Config>>().toEqualTypeOf<{ |
| 352 | + id: number; |
| 353 | + name: string; |
| 354 | + }>(); |
| 355 | + }); |
| 356 | + |
| 357 | + it('rejects handler returning wrong types with a standard schema', () => { |
| 358 | + const app = new Router(); |
| 359 | + const responseSchema = z.object({ id: z.string(), name: z.string() }); |
| 360 | + |
| 361 | + app.get( |
| 362 | + '/users/:id', |
| 363 | + // @ts-expect-error - number is not assignable to string for id |
| 364 | + () => { |
| 365 | + return { id: 123, name: 'John' }; |
| 366 | + }, |
| 367 | + { validation: { res: { body: responseSchema } } } |
| 368 | + ); |
| 369 | + }); |
| 370 | + |
| 371 | + it('accepts handler returning pre-coercion types with z.coerce', () => { |
| 372 | + const app = new Router(); |
| 373 | + const responseSchema = z.object({ |
| 374 | + id: z.coerce.string(), |
| 375 | + name: z.string(), |
| 376 | + }); |
| 377 | + |
| 378 | + app.get( |
| 379 | + '/users/:id', |
| 380 | + () => { |
| 381 | + return { id: 123, name: 'John' }; |
| 382 | + }, |
| 383 | + { validation: { res: { body: responseSchema } } } |
| 384 | + ); |
| 385 | + }); |
| 386 | + |
| 387 | + it('accepts handler returning a Response object with response validation', () => { |
| 388 | + const app = new Router(); |
| 389 | + const responseSchema = z.object({ id: z.string(), name: z.string() }); |
| 390 | + |
| 391 | + app.get( |
| 392 | + '/users/:id', |
| 393 | + () => { |
| 394 | + return Response.json({ id: '123', name: 'John' }); |
| 395 | + }, |
| 396 | + { validation: { res: { body: responseSchema } } } |
| 397 | + ); |
| 398 | + }); |
| 399 | + |
| 400 | + it('infers validated response body as output type for a standard schema', () => { |
| 401 | + const responseSchema = z.object({ id: z.string(), name: z.string() }); |
| 402 | + type Config = { res: { body: typeof responseSchema } }; |
| 403 | + |
| 404 | + expectTypeOf<InferResSchema<Config>>().toEqualTypeOf<{ |
| 405 | + body: { id: string; name: string }; |
| 406 | + headers: undefined; |
| 407 | + }>(); |
| 408 | + }); |
| 409 | + |
| 410 | + it('infers validated response body as output type for a coerced schema', () => { |
| 411 | + const responseSchema = z.object({ |
| 412 | + id: z.coerce.string(), |
| 413 | + name: z.string(), |
| 414 | + }); |
| 415 | + type Config = { res: { body: typeof responseSchema } }; |
| 416 | + |
| 417 | + expectTypeOf<InferResSchema<Config>>().toEqualTypeOf<{ |
| 418 | + body: { id: string; name: string }; |
| 419 | + headers: undefined; |
| 420 | + }>(); |
| 421 | + }); |
| 422 | +}); |
0 commit comments