-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
36 lines (31 loc) · 994 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
35
36
const constructor = require('./constructor');
const docRoutes = require('../routes');
const symbols = require('fastify/lib/symbols');
function getSchemasRecursive(instance) {
const schemas = {};
Object.assign(schemas, instance.getSchemas());
const children = instance[symbols.kChildren];
if (children && children.length) {
children.forEach((child) => {
Object.assign(schemas, getSchemasRecursive(child));
});
}
return schemas;
}
module.exports = async (fastify, options = {}) => {
const routes = [];
if (options.exposeRoute) {
const prefix = options.routePrefix || '/documentation';
fastify.register(docRoutes, { prefix, options: options.exposeRoute });
}
fastify.addHook('onRoute', (routeOptions) => {
routes.push(routeOptions);
});
const openapi = constructor({
options,
getSchemas: getSchemasRecursive.bind(null, fastify),
getSchema: fastify.getSchema.bind(fastify),
routes,
});
fastify.decorate('oas', openapi);
};