-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.ts
66 lines (59 loc) · 1.63 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'source-map-support/register';
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: {
openapi: '3.0.1',
info: {
title: 'My API',
version: '1.0.0',
},
paths: {
'/pets': {
get: {
operationId: 'getPets',
responses: {
200: { description: 'ok' },
},
},
},
'/pets/{id}': {
get: {
operationId: 'getPetById',
responses: {
200: { description: 'ok' },
},
},
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: {
type: 'integer',
},
},
],
},
},
},
handlers: {
getPets: async (c, req: Request, res: Response) => res.status(200).json({ operationId: c.operation.operationId }),
getPetById: async (c, req: Request, res: Response) =>
res.status(200).json({ operationId: c.operation.operationId }),
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' }),
},
});
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'));