@@ -14,43 +14,53 @@ import {
1414 getServiceFunction ,
1515 RouteHandler ,
1616} from './request' ;
17+ import { defaultResponseEncoder , ResponseEncoder } from './response' ;
18+
19+ export type { ResponseEncoder } from './response' ;
1720
1821const isHttpVerb = ( verb : string ) : verb is 'get' | 'put' | 'post' | 'delete' =>
1922 verb === 'get' || verb === 'put' || verb === 'post' || verb === 'delete' ;
2023
21- export function createServer < Spec extends ApiSpec > (
22- spec : Spec ,
23- configureExpressApplication : ( app : express . Application ) => {
24- [ ApiName in keyof Spec ] : {
25- [ Method in keyof Spec [ ApiName ] ] : RouteHandler < Spec [ ApiName ] [ Method ] > ;
26- } ;
27- } ,
28- ) {
29- const app : express . Application = express ( ) ;
30- const routes = configureExpressApplication ( app ) ;
31-
32- const router = express . Router ( ) ;
33- for ( const apiName of Object . keys ( spec ) ) {
34- const resource = spec [ apiName ] as Spec [ string ] ;
35- for ( const method of Object . keys ( resource ) ) {
36- if ( ! isHttpVerb ( method ) ) {
37- continue ;
24+ export const createServerWithResponseEncoder =
25+ ( encoder : ResponseEncoder ) =>
26+ < Spec extends ApiSpec > (
27+ spec : ApiSpec ,
28+ configureExpressApplication : ( app : express . Application ) => {
29+ [ ApiName in keyof Spec ] : {
30+ [ Method in keyof Spec [ ApiName ] ] : RouteHandler < Spec [ ApiName ] [ Method ] > ;
31+ } ;
32+ } ,
33+ ) => {
34+ const app : express . Application = express ( ) ;
35+ const routes = configureExpressApplication ( app ) ;
36+
37+ const router = express . Router ( ) ;
38+ for ( const apiName of Object . keys ( spec ) ) {
39+ const resource = spec [ apiName ] as Spec [ string ] ;
40+ for ( const method of Object . keys ( resource ) ) {
41+ if ( ! isHttpVerb ( method ) ) {
42+ continue ;
43+ }
44+ const httpRoute : HttpRoute = resource [ method ] ! ;
45+ const routeHandler = routes [ apiName ] ! [ method ] ! ;
46+ const expressRouteHandler = decodeRequestAndEncodeResponse (
47+ apiName ,
48+ httpRoute ,
49+ // FIXME: TS is complaining that `routeHandler` is not necessarily guaranteed to be a
50+ // `ServiceFunction`, because subtypes of Spec[string][string] can have arbitrary extra keys.
51+ getServiceFunction ( routeHandler as any ) ,
52+ encoder ,
53+ ) ;
54+ const handlers = [ ...getMiddleware ( routeHandler ) , expressRouteHandler ] ;
55+
56+ const expressPath = apiTsPathToExpress ( httpRoute . path ) ;
57+ router [ method ] ( expressPath , handlers ) ;
3858 }
39- const httpRoute : HttpRoute = resource [ method ] ! ;
40- const routeHandler = routes [ apiName ] ! [ method ] ! ;
41- const expressRouteHandler = decodeRequestAndEncodeResponse (
42- apiName ,
43- httpRoute as any , // TODO: wat
44- getServiceFunction ( routeHandler ) ,
45- ) ;
46- const handlers = [ ...getMiddleware ( routeHandler ) , expressRouteHandler ] ;
47-
48- const expressPath = apiTsPathToExpress ( httpRoute . path ) ;
49- router [ method ] ( expressPath , handlers ) ;
5059 }
51- }
5260
53- app . use ( router ) ;
61+ app . use ( router ) ;
62+
63+ return app ;
64+ } ;
5465
55- return app ;
56- }
66+ export const createServer = createServerWithResponseEncoder ( defaultResponseEncoder ) ;
0 commit comments