-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (31 loc) · 976 Bytes
/
index.js
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
const { initializeMiddleware } = require("swagger-tools");
const connect = require("koa-connect");
const compose = require("koa-compose");
const setBody = async (ctx, next) => {
ctx.req.body = ctx.request.body;
await next();
};
const koaSwaggerValidator = async (spec, {
security,
validator,
router,
ui,
} = {}) => new Promise(resolve =>
initializeMiddleware(spec, ({
swaggerMetadata: Metadata,
swaggerSecurity: Security,
swaggerValidator: Validator,
swaggerRouter: Router,
swaggerUi: UI,
}) => {
let middleware = [ Metadata() ];
security && middleware.push(Security(security));
middleware.push(Validator(validator));
router && middleware.push(Router(router));
ui && middleware.push(UI(ui));
middleware = middleware.map(connect);
middleware.unshift(setBody);
resolve(compose(middleware));
})
);
module.exports = koaSwaggerValidator;