本文介绍了回送4在生成时生成openapi.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在环回 4 api 上生成一个 openapi.json 文件,而不必启动服务器并请求 openapi 端点.在回送文档中找不到任何方法.
I would like to generate an openapi.json file on a loopback 4 api without having to start the server and request the openapi endpoint. I could not find any way to do it in the loopback documentation.
有什么方法可以生成该文件吗?
Is there any way to generate that file ?
推荐答案
rest.server.ts @ loopback/rest/rest.server.ts中的方法getApiSpec().
Method getApiSpec() from rest.server.ts @loopback/rest/rest.server.ts.
index.ts中的添加方法:
add method in index.ts:
// Node module: @loopback/example-express-composition
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import { ExpressServer } from './server';
import { ApplicationConfig } from '@loopback/core';
import { RequestContext } from '@loopback/rest';
export { ExpressServer };
export async function main(options: ApplicationConfig = {}) {
const server = new ExpressServer(options);
await server.boot();
await server.start();
console.log('Server is running at http://127.0.0.1:3000');
}
export async function getOpenApiJSONtoFile(options: ApplicationConfig = {}) {
const server = new ExpressServer(options);
await server.boot();
return server.lbApp.restServer.getApiSpec()
}
并创建用于创建json的文件:
and create file for create json:
const application = require('./dist');
const fs = require('fs');
const config = {
rest: {
port: +process.env.PORT || 3000,
host: process.env.HOST || 'localhost',
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
// Use the LB4 application as a route. It should not be listening.
listenOnStart: false,
},
};
application.getOpenApiJSONtoFile(config)
.then(result => {
let data = JSON.stringify(result);
fs.writeFileSync('openapi.json', data);
})
.catch(err => { console.log(err) });
这篇关于回送4在生成时生成openapi.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!