-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
55 lines (47 loc) · 2.5 KB
/
app.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
import path from 'path';
import OpenAPIBackend, {Request} from 'openapi-backend';
import express from 'express';
import morgan from 'morgan';
import {Request as ExpressReq, Response as ExpressRes} from 'express';
import {deleteMovieById, getMovieById, getMovies, getVote, saveMovie} from "./repository";
import {ApiError} from "./model";
const app = express();
app.use(express.json());
// define api
const api = new OpenAPIBackend({
definition: path.join(__dirname, '..', 'openapi', 'api.yaml'),
quick: true,
validate: true,
handlers: {
getMovies: async (c, req: ExpressReq, res: ExpressRes) => transform(res, getMovies(parseInt(stringOrLast(c.request.params["limit"])))),
postMovie: async (c, req: ExpressReq, res: ExpressRes) => transform(res, saveMovie(req.body)),
getMovieById: async (c, req: ExpressReq, res: ExpressRes) => transform(res, getMovieById(parseInt(stringOrLast(c.request.params["id"])))),
deleteMovieById: async (c, req: ExpressReq, res: ExpressRes) => transform(res, deleteMovieById(parseInt(stringOrLast(c.request.params["id"])))),
getVote: async (c, req: ExpressReq, res: ExpressRes) => transform(res, getVote(stringOrLast(c.request.params["userId"]), parseInt(stringOrLast(c.request.query["movieId"])))),
validationFail: async (c, req: ExpressReq, res: ExpressRes) => res.status(400).json({message: c.validation.errors}),
notFound: async (c, req: ExpressReq, res: ExpressRes) => res.status(404).json({message: 'not found'}),
notImplemented: async (c, req: ExpressReq, res: ExpressRes) => {
const {status, mock} = c.api.mockResponseForOperation(c.operation.operationId || "unknown");
return res.status(status).json(mock);
},
},
});
api.init();
// logging
app.use(morgan('combined'));
// use as express middleware
app.use((req, res) => api.handleRequest(req as Request, req, res));
// start server
app.listen(process.env.PORT || 8080, () => console.info(`movies-api listening at http://localhost:${process.env.PORT || 8080}`));
function stringOrLast(data: string | string[]): string {
return Array.isArray(data) ? data[data.length - 1] : data
}
async function transform(res: ExpressRes, data: Promise<any | ApiError>): Promise<ExpressRes> {
const result = await data;
if (!result) {
return res.status(404).json({message: "Not found"});
} else if (typeof result === 'object' && "message" in result) {
return res.status(500).json(result)
}
return res.status(200).json(result)
}