@@ -11,6 +11,8 @@ import type {
1111 TagTypesFromApi ,
1212} from '@reduxjs/toolkit/query'
1313import { createApi , fetchBaseQuery } from '@reduxjs/toolkit/query'
14+ import * as v from 'valibot'
15+ import type { Post } from './mocks/handlers'
1416
1517describe ( 'type tests' , ( ) => {
1618 test ( 'sensible defaults' , ( ) => {
@@ -373,5 +375,72 @@ describe('type tests', () => {
373375 > ( )
374376 } )
375377 } )
378+ describe ( 'endpoint schemas' , ( ) => {
379+ test ( 'schemas must match' , ( ) => {
380+ const postSchema = v . object ( {
381+ id : v . number ( ) ,
382+ title : v . string ( ) ,
383+ body : v . string ( ) ,
384+ } ) satisfies v . GenericSchema < Post >
385+ const errorSchema = v . object ( {
386+ status : v . number ( ) ,
387+ data : v . unknown ( ) ,
388+ } ) satisfies v . GenericSchema < FetchBaseQueryError >
389+ createApi ( {
390+ baseQuery : fetchBaseQuery ( { baseUrl : 'https://example.com' } ) ,
391+ endpoints : ( build ) => ( {
392+ query : build . query < Post , { id : number } > ( {
393+ query : ( { id } ) => `/post/${ id } ` ,
394+ argSchema : v . object ( { id : v . number ( ) } ) ,
395+ resultSchema : postSchema ,
396+ errorSchema,
397+ } ) ,
398+ bothMismatch : build . query < Post , { id : number } > ( {
399+ query : ( { id } ) => `/post/${ id } ` ,
400+ // @ts -expect-error wrong schema
401+ argSchema : v . object ( { id : v . string ( ) } ) ,
402+ // @ts -expect-error wrong schema
403+ resultSchema : v . object ( { id : v . string ( ) } ) ,
404+ // @ts -expect-error wrong schema
405+ errorSchema : v . object ( { status : v . string ( ) } ) ,
406+ } ) ,
407+ inputMismatch : build . query < Post , { id : number } > ( {
408+ query : ( { id } ) => `/post/${ id } ` ,
409+ // @ts -expect-error can't expect different input
410+ argSchema : v . object ( {
411+ id : v . pipe ( v . string ( ) , v . transform ( Number ) , v . number ( ) ) ,
412+ } ) ,
413+ // @ts -expect-error can't expect different input
414+ resultSchema : v . object ( {
415+ ...postSchema . entries ,
416+ id : v . pipe ( v . string ( ) , v . transform ( Number ) ) ,
417+ } ) satisfies v . GenericSchema < any , Post > ,
418+ // @ts -expect-error can't expect different input
419+ errorSchema : v . object ( {
420+ status : v . pipe ( v . string ( ) , v . transform ( Number ) ) ,
421+ data : v . unknown ( ) ,
422+ } ) satisfies v . GenericSchema < any , FetchBaseQueryError > ,
423+ } ) ,
424+ outputMismatch : build . query < Post , { id : number } > ( {
425+ query : ( { id } ) => `/post/${ id } ` ,
426+ // @ts -expect-error can't provide different output
427+ argSchema : v . object ( {
428+ id : v . pipe ( v . number ( ) , v . transform ( String ) ) ,
429+ } ) ,
430+ // @ts -expect-error can't provide different output
431+ resultSchema : v . object ( {
432+ ...postSchema . entries ,
433+ id : v . pipe ( v . number ( ) , v . transform ( String ) ) ,
434+ } ) satisfies v . GenericSchema < Post , any > ,
435+ // @ts -expect-error can't provide different output
436+ errorSchema : v . object ( {
437+ status : v . pipe ( v . number ( ) , v . transform ( String ) ) ,
438+ data : v . unknown ( ) ,
439+ } ) satisfies v . GenericSchema < FetchBaseQueryError , any > ,
440+ } ) ,
441+ } ) ,
442+ } )
443+ } )
444+ } )
376445 } )
377446} )
0 commit comments