|
8 | 8 | ContractNoBodyType, |
9 | 9 | } from './dsl'; |
10 | 10 | import type { Equal, Expect } from './test-helpers'; |
| 11 | +import { Prettify } from './type-utils'; |
11 | 12 |
|
12 | 13 | const c = initContract(); |
13 | 14 |
|
@@ -615,6 +616,65 @@ describe('contract', () => { |
615 | 616 | >; |
616 | 617 | }); |
617 | 618 |
|
| 619 | + it('should merge metadata options from router to its routes', () => { |
| 620 | + const contract = c.router( |
| 621 | + { |
| 622 | + getPost: { |
| 623 | + method: 'GET', |
| 624 | + path: '/posts/:id', |
| 625 | + responses: { |
| 626 | + 200: c.type<{ id: number }>(), |
| 627 | + }, |
| 628 | + }, |
| 629 | + deletePost: { |
| 630 | + method: 'DELETE', |
| 631 | + path: '/posts/:id', |
| 632 | + body: c.type<undefined>(), |
| 633 | + metadata: { |
| 634 | + requireAuth: false, |
| 635 | + headerName: 'x-authorization', |
| 636 | + }, |
| 637 | + responses: { |
| 638 | + 200: c.type<{ id: number }>(), |
| 639 | + }, |
| 640 | + }, |
| 641 | + }, |
| 642 | + { |
| 643 | + metadata: { |
| 644 | + requireAuth: true, |
| 645 | + }, |
| 646 | + }, |
| 647 | + ); |
| 648 | + |
| 649 | + expect(contract.getPost.metadata).toStrictEqual({ |
| 650 | + requireAuth: true, |
| 651 | + }); |
| 652 | + |
| 653 | + expect(contract.deletePost.metadata).toStrictEqual({ |
| 654 | + requireAuth: false, |
| 655 | + headerName: 'x-authorization', |
| 656 | + }); |
| 657 | + |
| 658 | + type MetadataShape = Expect< |
| 659 | + Equal< |
| 660 | + typeof contract.getPost.metadata, |
| 661 | + { |
| 662 | + requireAuth: boolean; |
| 663 | + } |
| 664 | + > |
| 665 | + >; |
| 666 | + |
| 667 | + type MetadataShape2 = Expect< |
| 668 | + Equal< |
| 669 | + Prettify<typeof contract.deletePost.metadata>, |
| 670 | + { |
| 671 | + requireAuth: boolean; |
| 672 | + headerName: string; |
| 673 | + } |
| 674 | + > |
| 675 | + >; |
| 676 | + }); |
| 677 | + |
618 | 678 | describe('pathPrefix', () => { |
619 | 679 | it('Should recursively apply pathPrefix to path', () => { |
620 | 680 | const postsContractNested = c.router( |
|
0 commit comments