我正在尝试找到一个解决方案,通过一个快速应用程序获得多个swaggerUi文档。
我正在使用:
"typescript": "^2.5.2",
"swagger-tools": "^0.10.1",
"express": "^4.15.3",
"express-openapi": "^1.0.1",
我的swagger doc文件部分是使用项目文件架构生成的。
我该怎么做?
编辑---
现在我正在这样初始化swaggerUi:
const openapi = Openapi.initialize({
paths: openApiPaths,
expressApp,
swaggerApiDoc,
});
const openApiSpec: any = openapi.apiDoc;
app.use(swaggerUI(openApiSpec));
其中openApiPaths
包含paths:{}
swagger doc
答案 0 :(得分:1)
迟了一年,但仍然有效:
async function initialize (app) {
const authenticationSpec = path.join(__dirname,'api/authentication.yaml');
const authenticationMiddleware = await useSwaggerMiddlewares(authenticationSpec, {
router: routerOptions,
validator: validatorOptions,
});
// NOTE the metadata must be mounted at root level!
app.use(authenticationMiddleware.metadata);
app.use('/v2/authentication', authenticationMiddleware.validator);
app.use('/v2/authentication', authenticationMiddleware.router);
app.use('/v2/authentication', authenticationMiddleware.ui);
const mainSpec = path.join(__dirname,'api/swagger.yaml');
const mainMiddleware = await useSwaggerMiddlewares(mainSpec, {
router: routerOptions,
validator: validatorOptions,
});
app.use(mainMiddleware.metadata);
app.use(mainMiddleware.validator);
app.use(mainMiddleware.router);
app.use(mainMiddleware.ui);
// Start the server
http.createServer(app).listen(serverPort, () => console.log(`server lintening on port ${serverPort}`);
}
initialize(app);
使用此处定义的useSwaggerMiddlewares
module.exports = function useSwaggerMiddlewares (swaggerSpec, {
router: routerOptions, validator: validatorOptions,
}) {
return new Promise((resolve, reject) => {
try {
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
const spec = fs.readFileSync(swaggerSpec, 'utf8');
const swaggerDoc = jsyaml.safeLoad(spec);
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, (middleware) => {
try {
return resolve({
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
metadata: middleware.swaggerMetadata(),
// Validate Swagger requests
validator: middleware.swaggerValidator(validatorOptions),
// Route validated requests to appropriate controller
router: middleware.swaggerRouter(routerOptions),
// Serve the Swagger documents and Swagger UI
ui: middleware.swaggerUi()
});
} catch (error) {
console.error(error);
return reject(error);
}
});
} catch (error) {
console.error(error);
return reject(error);
}
});
};
答案 1 :(得分:0)
您可以在单个快速服务器上托管多个api,您只需要确定
basePath
并不相互重叠。
参考:https://github.com/apigee-127/swagger-tools/issues/530
swaggerTools.initializeMiddleware(doc1, function (mw1) {
// Do stuff with first middleware
swaggerTools.initializeMiddleware(doc2, function (mw2) {
// Do stuff with second middleware
// Repeat...
});
});