1- import type { DecoratedContractProcedure } from './procedure-decorated'
1+ import type { ContractProcedure } from './procedure'
2+ import type { ContractProcedureBuilder } from './procedure-builder'
3+ import type { ContractProcedureBuilderWithInput } from './procedure-builder-with-input'
4+ import type { ContractProcedureBuilderWithOutput } from './procedure-builder-with-output'
25import type { AdaptedContractRouter , ContractRouterBuilder } from './router-builder'
36import { z } from 'zod'
47import { ContractBuilder } from './builder'
5- import { ContractProcedure } from './procedure'
68
7- const schema = z . object ( {
8- value : z . string ( ) ,
9- } )
9+ const schema = z . object ( { value : z . string ( ) } )
1010
1111const baseErrorMap = {
1212 BASE : {
13- status : 500 ,
1413 data : z . object ( {
1514 message : z . string ( ) ,
1615 } ) ,
@@ -19,142 +18,72 @@ const baseErrorMap = {
1918
2019const builder = new ContractBuilder ( { errorMap : baseErrorMap , OutputSchema : undefined , InputSchema : undefined } )
2120
22- it ( 'also is a contract procedure' , ( ) => {
23- expectTypeOf ( builder ) . toMatchTypeOf < ContractProcedure < undefined , undefined , typeof baseErrorMap > > ( )
24- } )
25-
26- describe ( 'self chainable' , ( ) => {
27- describe ( 'errors' , ( ) => {
28- const errors = {
29- BAD : {
30- status : 500 ,
31- data : schema ,
32- } ,
33- ERROR2 : {
34- status : 401 ,
35- data : schema ,
36- } ,
37- } as const
38-
39- it ( 'should merge and strict with old one' , ( ) => {
40- expectTypeOf ( builder . errors ( errors ) ) . toEqualTypeOf <
41- ContractBuilder < typeof errors & typeof baseErrorMap >
42- > ( )
43- } )
44-
45- it ( 'should prevent redefine errorMap' , ( ) => {
46- // @ts -expect-error - not allow redefine errorMap
47- builder . errors ( { BASE : baseErrorMap . BASE } )
48- // @ts -expect-error - not allow redefine errorMap - even with undefined
49- builder . errors ( { BASE : undefined } )
50- } )
21+ describe ( 'ContractBuilder' , ( ) => {
22+ it ( 'is a contract procedure' , ( ) => {
23+ expectTypeOf ( builder ) . toMatchTypeOf < ContractProcedure < undefined , undefined , typeof baseErrorMap > > ( )
5124 } )
52- } )
53-
54- describe ( 'to ContractRouterBuilder' , ( ) => {
55- it ( 'prefix' , ( ) => {
56- expectTypeOf ( builder . prefix ( '/prefix' ) ) . toEqualTypeOf <
57- ContractRouterBuilder < typeof baseErrorMap >
58- > ( )
5925
60- // @ts -expect-error - invalid prefix
61- builder . prefix ( 1 )
62- // @ts -expect-error - invalid prefix
63- builder . prefix ( '' )
64- } )
26+ it ( '.errors' , ( ) => {
27+ const errors = { BAD_GATEWAY : { data : schema } } as const
6528
66- it ( 'tags' , ( ) => {
67- expectTypeOf ( builder . tag ( 'tag1' , 'tag2' ) ) . toEqualTypeOf <
68- ContractRouterBuilder < typeof baseErrorMap >
69- > ( )
29+ expectTypeOf ( builder . errors ( errors ) )
30+ . toEqualTypeOf < ContractBuilder < typeof baseErrorMap & typeof errors > > ( )
7031
71- // @ts -expect-error - invalid tag
72- builder . tag ( 1 )
73- // @ts -expect-error - invalid tag
74- builder . tag ( { } )
32+ // @ts -expect-error - not allow redefine error map
33+ builder . errors ( { BASE : baseErrorMap . BASE } )
7534 } )
76- } )
7735
78- describe ( 'to DecoratedContractProcedure' , ( ) => {
79- it ( 'route' , ( ) => {
80- expectTypeOf ( builder . route ( { method : 'GET' , path : '/path' } ) ) . toEqualTypeOf <
81- DecoratedContractProcedure < undefined , undefined , typeof baseErrorMap >
82- > ( )
83-
84- expectTypeOf ( builder . route ( { } ) ) . toEqualTypeOf <
85- DecoratedContractProcedure < undefined , undefined , typeof baseErrorMap >
86- > ( )
36+ it ( '.route' , ( ) => {
37+ expectTypeOf ( builder . route ( { method : 'GET' } ) ) . toEqualTypeOf < ContractProcedureBuilder < typeof baseErrorMap > > ( )
8738
8839 // @ts -expect-error - invalid method
8940 builder . route ( { method : 'HE' } )
90- // @ts -expect-error - invalid path
91- builder . route ( { method : 'GET' , path : '' } )
9241 } )
9342
94- it ( 'input' , ( ) => {
43+ it ( '. input' , ( ) => {
9544 expectTypeOf ( builder . input ( schema ) ) . toEqualTypeOf <
96- DecoratedContractProcedure < typeof schema , undefined , typeof baseErrorMap >
97- > ( )
98-
99- expectTypeOf ( builder . input ( schema , { value : 'example' } ) ) . toEqualTypeOf <
100- DecoratedContractProcedure < typeof schema , undefined , typeof baseErrorMap >
45+ ContractProcedureBuilderWithInput < typeof schema , typeof baseErrorMap >
10146 > ( )
102-
103- // @ts -expect-error - invalid schema
104- builder . input ( { } )
105-
106- // @ts -expect-error - invalid example
107- builder . input ( schema , { } )
10847 } )
10948
110- it ( 'output' , ( ) => {
49+ it ( '. output' , ( ) => {
11150 expectTypeOf ( builder . output ( schema ) ) . toEqualTypeOf <
112- DecoratedContractProcedure < undefined , typeof schema , typeof baseErrorMap >
113- > ( )
114-
115- expectTypeOf ( builder . output ( schema , { value : 'example' } ) ) . toEqualTypeOf <
116- DecoratedContractProcedure < undefined , typeof schema , typeof baseErrorMap >
51+ ContractProcedureBuilderWithOutput < typeof schema , typeof baseErrorMap >
11752 > ( )
53+ } )
11854
119- // @ts -expect-error - invalid schema
120- builder . output ( { } )
55+ it ( '.prefix' , ( ) => {
56+ expectTypeOf ( builder . prefix ( '/api' ) ) . toEqualTypeOf < ContractRouterBuilder < typeof baseErrorMap > > ( )
12157
122- // @ts -expect-error - invalid example
123- builder . output ( schema , { } )
58+ // @ts -expect-error - invalid prefix
59+ builder . prefix ( 1 )
12460 } )
125- } )
12661
127- describe ( 'to router' , ( ) => {
128- const errors = {
129- CONFLICT : {
130- status : 400 ,
131- data : z . object ( {
132- message : z . string ( ) ,
133- } ) ,
134- } ,
135- }
136-
137- const router = { a : { b : {
138- c : new ContractProcedure ( { InputSchema : undefined , OutputSchema : undefined , errorMap : errors } ) ,
139- } } }
140-
141- it ( 'adapt all procedures' , ( ) => {
142- expectTypeOf ( builder . router ( router ) ) . toEqualTypeOf < AdaptedContractRouter < typeof router , typeof baseErrorMap > > ( )
143- expectTypeOf ( builder . router ( { } ) ) . toEqualTypeOf < Record < never , never > > ( )
62+ it ( '.tag' , ( ) => {
63+ expectTypeOf ( builder . tag ( 'tag1' , 'tag2' ) ) . toEqualTypeOf < ContractRouterBuilder < typeof baseErrorMap > > ( )
14464
145- // @ts -expect-error - invalid router
146- builder . router ( { a : 1 } )
65+ // @ts -expect-error - invalid tag
66+ builder . tag ( 1 )
14767 } )
14868
149- it ( 'throw on conflict error map ' , ( ) => {
150- builder . router ( { ping : { } as ContractProcedure < any , any , { BASE : typeof baseErrorMap [ 'BASE' ] } > } )
151- // @ts -expect-error conflict
152- builder . router ( { ping : { } as ContractProcedure < any , any , { BASE : { message : string } } > } )
153- } )
69+ it ( '.router ' , ( ) => {
70+ const router = {
71+ ping : { } as ContractProcedure < undefined , typeof schema , typeof baseErrorMap > ,
72+ pong : { } as ContractProcedure < typeof schema , undefined , Record < never , never > > ,
73+ }
15474
155- it ( 'only required partial match error map' , ( ) => {
156- expectTypeOf ( builder . router ( { ping : { } as ContractProcedure < any , any , { OTHER : { status : number } } > } ) ) . toEqualTypeOf < {
157- ping : DecoratedContractProcedure < any , any , { OTHER : { status : number } } & typeof baseErrorMap >
158- } > ( )
75+ expectTypeOf ( builder . router ( router ) ) . toEqualTypeOf < AdaptedContractRouter < typeof router , typeof baseErrorMap > > ( )
76+
77+ const invalidErrorMap = {
78+ BASE : {
79+ ...baseErrorMap . BASE ,
80+ status : 400 ,
81+ } ,
82+ }
83+
84+ builder . router ( {
85+ // @ts -expect-error - error map is not match
86+ ping : { } as ContractProcedure < undefined , typeof schema , typeof invalidErrorMap > ,
87+ } )
15988 } )
16089} )
0 commit comments