import * as t from 'io-ts' ; import { createServerRouteRepository } from './create_server_route_repository' ; import { decodeRequestParams } from './decode_request_params' ; import { EndpointOf , ReturnOf , RouteRepositoryClient } from './typings' ; function assertType < TShape = never > ( value ) { return value ; } createServerRouteRepository ( ) . add ( { endpoint : 'any_endpoint' , handler : async ( { params } ) => { } , } ) ; createServerRouteRepository < { } , { } > ( ) . add ( { endpoint : 'endpoint_without_params' , handler : async ( resources ) => { assertType < { params : any ; } > ( resources ) ; } , } ) ; createServerRouteRepository < { } , { } > ( ) . add ( { endpoint : 'endpoint_with_params' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , } ) , handler : async ( resources ) => { assertType < { params : { path : { serviceName : string ; } ; } ; } > ( resources ) ; } , } ) ; createServerRouteRepository < { context : { getSpaceId : ( ) => string ; } ; } , { } > ( ) . add ( { endpoint : 'endpoint_with_params' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , } ) , handler : async ( { context } ) => { const spaceId = context . getSpaceId ( ) ; assertType < string > ( spaceId ) ; } , } ) ; createServerRouteRepository < { } , { options : { tags : string [ ] ; } ; } > ( ) . add ( { endpoint : 'endpoint_with_params' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , } ) , options : { tags : [ ] , } , handler : async ( resources ) => { assertType < { params : { path : { serviceName : string ; } ; } ; } > ( resources ) ; } , } ) ; const repository = createServerRouteRepository < { } , { } > ( ) . add ( { endpoint : 'endpoint_without_params' , handler : async ( ) => { return { noParamsForMe : true , } ; } , } ) . add ( { endpoint : 'endpoint_with_params' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , } ) , handler : async ( ) => { return { yesParamsForMe : true , } ; } , } ) . add ( { endpoint : 'endpoint_with_optional_params' , params : t . partial ( { query : t . partial ( { serviceName : t . string , } ) , } ) , handler : async ( ) => { return { someParamsForMe : true , } ; } , } ) ; type TestRepository = typeof repository ; assertType < Array < EndpointOf < TestRepository > > > ( [ 'endpoint_with_params' , 'endpoint_without_params' , 'endpoint_with_optional_params' , ] ) ; assertType < Array < EndpointOf < TestRepository > > > ( [ 'this_endpoint_does_not_exist' ] ) ; assertType < ReturnOf < TestRepository , 'endpoint_without_params' > > ( { noParamsForMe : true , } ) ; const noParamsInvalid = { paramsForMe : true , } ; type TestClient = RouteRepositoryClient < TestRepository , { timeout : number ; } > ; const client = { } as any ; client ( { endpoint : 'endpoint_without_params' , } ) ; client ( { endpoint : 'endpoint_without_params' , timeout : 1 , } ) ; client ( { endpoint : 'endpoint_without_params' , params : { } , timeout : 1 , } ) ; client ( { endpoint : 'endpoint_with_params' , params : { path : { } , } , timeout : 1 , } ) ; client ( { endpoint : 'endpoint_with_optional_params' , timeout : 1 , } ) ; client ( { endpoint : 'endpoint_with_optional_params' , timeout : 1 , params : { path : '' , } , } ) ; client ( { endpoint : 'endpoint_with_params' , params : { path : { serviceName : '' , } , } , timeout : 1 , } ) . then ( ( res ) => { assertType < { noParamsForMe : boolean ; } > ( res ) ; assertType < { yesParamsForMe : boolean ; } > ( res ) ; } ) ; assertType < { path : { serviceName : string ; } ; } > ( decodeRequestParams ( { params : { serviceName : 'serviceName' , } , body : undefined , query : undefined , } , t . type ( { path : t . type ( { serviceName : t . string } ) } ) ) ) ; assertType < { path : { serviceName : boolean ; } ; } > ( decodeRequestParams ( { params : { serviceName : 'serviceName' , } , body : undefined , query : undefined , } , t . type ( { path : t . type ( { serviceName : t . string } ) } ) ) ) ;