-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.ts
33 lines (27 loc) · 1.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import 'source-map-support/register';
import path from 'path';
import OpenAPIBackend from 'openapi-backend';
import express from 'express';
import morgan from 'morgan';
import { Request, Response } from 'express';
const app = express();
app.use(express.json());
// define api
const api = new OpenAPIBackend({
definition: path.join(__dirname, '..', 'openapi.yml'),
handlers: {
validationFail: async (c, req: Request, res: Response) => res.status(400).json({ err: c.validation.errors }),
notFound: async (c, req: Request, res: Response) => res.status(404).json({ err: 'not found' }),
notImplemented: async (c, req: Request, res: Response) => {
const { status, mock } = c.api.mockResponseForOperation(c.operation.operationId);
return res.status(status).json(mock);
},
},
});
api.init();
// logging
app.use(morgan('combined'));
// use as express middleware
app.use((req, res) => api.handleRequest(req, req, res));
// start server
app.listen(9000, () => console.info('api listening at http://localhost:9000'));