Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions packages/express-wrapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,59 @@ export type { ResponseEncoder } from './response';
const isHttpVerb = (verb: string): verb is 'get' | 'put' | 'post' | 'delete' =>
verb === 'get' || verb === 'put' || verb === 'post' || verb === 'delete';

export const createServerWithResponseEncoder =
(encoder: ResponseEncoder) =>
<Spec extends ApiSpec>(
spec: ApiSpec,
configureExpressApplication: (app: express.Application) => {
[ApiName in keyof Spec]: {
[Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>;
};
},
) => {
const app: express.Application = express();
const routes = configureExpressApplication(app);

const router = express.Router();
for (const apiName of Object.keys(spec)) {
const resource = spec[apiName] as Spec[string];
for (const method of Object.keys(resource)) {
if (!isHttpVerb(method)) {
continue;
}
const httpRoute: HttpRoute = resource[method]!;
const routeHandler = routes[apiName]![method]!;
const expressRouteHandler = decodeRequestAndEncodeResponse(
apiName,
httpRoute,
// FIXME: TS is complaining that `routeHandler` is not necessarily guaranteed to be a
// `ServiceFunction`, because subtypes of Spec[string][string] can have arbitrary extra keys.
getServiceFunction(routeHandler as any),
encoder,
);
const handlers = [...getMiddleware(routeHandler), expressRouteHandler];
type CreateRouterProps<Spec extends ApiSpec> = {
spec: Spec;
routeHandlers: {
[ApiName in keyof Spec]: {
[Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>;
};
};
encoder?: ResponseEncoder;
};

const expressPath = apiTsPathToExpress(httpRoute.path);
router[method](expressPath, handlers);
export function routerForApiSpec<Spec extends ApiSpec>({
spec,
routeHandlers,
encoder = defaultResponseEncoder,
}: CreateRouterProps<Spec>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting choice of parameter type! (an object)

const router = express.Router();
for (const apiName of Object.keys(spec)) {
const resource = spec[apiName] as Spec[string];
for (const method of Object.keys(resource)) {
if (!isHttpVerb(method)) {
continue;
}
}
const httpRoute: HttpRoute = resource[method]!;
const routeHandler = routeHandlers[apiName]![method]!;
const expressRouteHandler = decodeRequestAndEncodeResponse(
apiName,
httpRoute,
// FIXME: TS is complaining that `routeHandler` is not necessarily guaranteed to be a
// `ServiceFunction`, because subtypes of Spec[string][string] can have arbitrary extra keys.
getServiceFunction(routeHandler as any),
encoder,
);
const handlers = [...getMiddleware(routeHandler), expressRouteHandler];

app.use(router);
const expressPath = apiTsPathToExpress(httpRoute.path);
router[method](expressPath, handlers);
}
}

return app;
};
return router;
}

export const createServer = createServerWithResponseEncoder(defaultResponseEncoder);
export const createServer = <Spec extends ApiSpec>(
spec: Spec,
configureExpressApplication: (app: express.Application) => {
[ApiName in keyof Spec]: {
[Method in keyof Spec[ApiName]]: RouteHandler<Spec[ApiName][Method]>;
};
},
) => {
const app = express();
const routeHandlers = configureExpressApplication(app);
const router = routerForApiSpec({ spec, routeHandlers });
app.use(router);
return app;
};