// (C) 2007-2019 GoodData Corporation import * as express from "express"; import * as bodyParser from "body-parser"; import * as session from "express-session"; import { buildProject } from "../schema/builder"; import { IMockProject } from "../model/MockProject"; import { IEndpoint } from "../endpoints/Endpoint"; import { ISchema } from "../schema/model/Schema"; import { IConfig } from "../model/Config"; export function createEndpoint( endpoint: IEndpoint, schema: ISchema, customConfig: IConfig = {}, ...args: any[] ): express.Application { const project: IMockProject = buildProject(schema); return createEndpointWithProject(endpoint, project, customConfig, ...args); } export function createEndpointWithProject( endpoint: IEndpoint, project: IMockProject, customConfig: IConfig = {}, ...args: any[] ): express.Application { const app: express.Application = express(); app.use(bodyParser.json({ limit: "50mb" })); app.use( session({ secret: "#sorryjako", resave: false, saveUninitialized: false, }), ); const config: IConfig = { pollCount: 2, ...customConfig, }; // express must be provided with already registered body parser! endpoint.register(app, project, config, ...args); // Custom error handler must be register at the end! app.use((err: any, _req: any, res: any, _next: any) => { console.error(err); // tslint:disable-line:no-console res.status(500).end(); }); return app; }