@@ -56,16 +56,19 @@ test("typed error returns exact object with isDefined=true", () => {
5656test ( "context method works" , ( ) => {
5757 const fn = zagora ( )
5858 . context ( { db : "mock" } )
59- . input ( z . string ( ) )
59+ . input ( z . number ( ) )
6060 . output ( z . string ( ) )
6161 . handler ( ( { context } , input ) => {
62+ expectTypeOf ( input ) . toEqualTypeOf < number > ( ) ;
63+ expectTypeOf ( context ) . toEqualTypeOf < { db : string } > ( ) ;
64+
6265 return `${ input } -${ context . db } ` ;
6366 } )
6467 . callable ( ) ;
6568
66- const res = fn ( "bar" ) ;
69+ const res = fn ( 1001 ) ;
6770 if ( res . ok ) {
68- expect ( res . data ) . toBe ( "bar -mock" ) ;
71+ expect ( res . data ) . toBe ( "1001 -mock" ) ;
6972 } else {
7073 expect ( false , "Expected success" ) . toBe ( true ) ;
7174 }
@@ -149,7 +152,11 @@ test("async input schema", async () => {
149152 const fn = zagora ( )
150153 . input ( asyncSchema )
151154 . output ( z . string ( ) )
152- . handler ( ( _ , input ) => input . toUpperCase ( ) )
155+ . handler ( ( _ , input ) => {
156+ expectTypeOf ( input ) . toEqualTypeOf < string > ( ) ;
157+
158+ return input . toUpperCase ( ) ;
159+ } )
153160 . callable ( ) ;
154161
155162 const res = await fn ( "ab" ) ;
@@ -261,6 +268,7 @@ test("Error.cause is set on wrapped errors", () => {
261268 . input ( z . string ( ) )
262269 . output ( z . string ( ) )
263270 . handler ( ( _ , input ) => {
271+ expectTypeOf ( input ) . toEqualTypeOf < string > ( ) ;
264272 if ( input === "fail" ) {
265273 throw originalError ;
266274 }
@@ -278,21 +286,25 @@ test("Error.cause is set on wrapped errors", () => {
278286 }
279287} ) ;
280288
281- test ( "both tuple and object access formats work" , ( ) => {
282- const fn = zagora ( )
283- . input ( z . string ( ) )
284- . output ( z . string ( ) )
285- . handler ( ( _ , input ) => input )
286- . callable ( ) ;
287-
288- const res = fn ( "foo" ) ;
289-
290- if ( res . ok ) {
291- expect ( res . data ) . toBe ( "foo" ) ;
292- } else {
293- expect ( false , "Expected success" ) . toBe ( true ) ;
294- }
295- } ) ;
289+ // TODO: fix to support array schemas, and not treat it as tuple schemas!
290+ // test("input schema array of string should work", () => {
291+ // const fn = zagora()
292+ // .input(z.array(z.string()))
293+ // .output(z.string())
294+ // .handler((_, input) => {
295+ // expectTypeOf(input).toEqualTypeOf<string[]>();
296+ // return input.map((x) => x.toUpperCase());
297+ // })
298+ // .callable();
299+
300+ // const res = fn(["foo", "bar"]);
301+
302+ // if (res.ok) {
303+ // expect(res.data).toBe(["foo", "bar"]);
304+ // } else {
305+ // expect(false, "Expected success").toBe(true);
306+ // }
307+ // });
296308
297309test ( "input validation failure" , ( ) => {
298310 const fn = zagora ( )
@@ -331,6 +343,8 @@ test("no error schema works", () => {
331343 . input ( z . string ( ) )
332344 . output ( z . string ( ) )
333345 . handler ( ( _ , input ) => {
346+ expectTypeOf ( input ) . toEqualTypeOf < string > ( ) ;
347+
334348 if ( input === "fail" ) {
335349 throw new Error ( "Untyped" ) ;
336350 }
@@ -350,7 +364,12 @@ test("tuple input arguments", () => {
350364 const fn = zagora ( )
351365 . input ( z . tuple ( [ z . string ( ) , z . number ( ) ] ) )
352366 . output ( z . string ( ) )
353- . handler ( ( _ , str , num ) => `${ str } -${ num } ` )
367+ . handler ( ( _ , str , num ) => {
368+ expectTypeOf ( str ) . toEqualTypeOf < string > ( ) ;
369+ expectTypeOf ( num ) . toEqualTypeOf < number > ( ) ;
370+
371+ return `${ str } -${ num } ` ;
372+ } )
354373 . callable ( ) ;
355374
356375 const res = fn ( "hello" , 42 ) ;
@@ -368,6 +387,8 @@ test("thrown ZagoraError passed through", () => {
368387 . input ( z . string ( ) )
369388 . output ( z . string ( ) )
370389 . handler ( ( _ , input ) => {
390+ expectTypeOf ( input ) . toEqualTypeOf < string > ( ) ;
391+
371392 if ( input === "throw now" ) {
372393 throw customErr ;
373394 }
@@ -386,9 +407,14 @@ test("thrown ZagoraError passed through", () => {
386407test ( "multiple procedures (calculator) from single instance (autoCallable:true)" , ( ) => {
387408 const za = zagora ( { autoCallable : true , disableOptions : true } ) ;
388409
389- const add = za
390- . input ( z . tuple ( [ z . number ( ) , z . number ( ) ] ) )
391- . handler ( ( a , b ) => a + b ) ;
410+ const add = za . input ( z . tuple ( [ z . number ( ) , z . number ( ) ] ) ) . handler ( ( a , b ) => {
411+ expectTypeOf ( { a, b } ) . toEqualTypeOf < {
412+ a : number ;
413+ b : number ;
414+ } > ( ) ;
415+
416+ return a + b ;
417+ } ) ;
392418
393419 const subtract = za
394420 . input ( z . tuple ( [ z . number ( ) , z . number ( ) ] ) )
@@ -490,7 +516,14 @@ test("handle optional/default valaues in object schemas", async () => {
490516 . output ( SuccessSchema ) ;
491517
492518 const getPrices = getPricesContract . handler (
493- async ( { errors : err } , { speed, num, includeDetails } ) => {
519+ async ( { errors : err } , input ) => {
520+ const { speed, num, includeDetails } = input ;
521+ expectTypeOf ( input ) . toEqualTypeOf < {
522+ speed : "slow" | "normal" | "fast" ;
523+ num : number ;
524+ includeDetails : boolean ;
525+ } > ( ) ;
526+
494527 // Simulate rate limiting
495528 if ( num && num > 1000 ) {
496529 throw err . RATE_LIMIT ( {
@@ -595,6 +628,8 @@ test("basic in-memory caching/memoization", async () => {
595628
596629 let called = 0 ;
597630 const hello = za . input ( z . string ( ) ) . handler ( async ( _ , name ) => {
631+ expectTypeOf ( name ) . toEqualTypeOf < string > ( ) ;
632+
598633 called += 1 ;
599634 await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
600635 return `Hello, ${ name } !` ;
@@ -642,6 +677,10 @@ test("cache adapter passed through `.callable` method", async () => {
642677 . context ( { age : 10 } )
643678 . input ( z . string ( ) )
644679 . handler ( ( { context } ) => {
680+ expectTypeOf ( context ) . toEqualTypeOf < {
681+ age : number ;
682+ } > ;
683+
645684 called += 1 ;
646685 return context . age + called ;
647686 } )
@@ -734,7 +773,12 @@ test("basic env schema support through `.env` method", () => {
734773 process . env ,
735774 )
736775 . handler ( ( { env } , input ) => {
737- // env: { DATABASE_URL: string, SOME_SECRET: string, PORT: number }
776+ expectTypeOf ( env ) . toEqualTypeOf < {
777+ DATABASE_URL : string ;
778+ SOME_SECRET : string ;
779+ PORT : number ;
780+ } > ;
781+
738782 return `input=${ input } ;url=${ env . DATABASE_URL } ;secret=${ env . SOME_SECRET } ;PORT=${ env . PORT } ` ;
739783 } )
740784 . callable ( {
0 commit comments