环回 4 在构建时生成 openapi.json
loopback 4 generate openapi.json on build
我想在环回 4 api 上生成一个 openapi.json 文件,而不必启动服务器并请求 openapi 端点。我在环回文档中找不到任何方法。
有什么方法可以生成该文件吗?
方法 getApiSpec() 来自 rest.server.ts @loopback/rest/rest.server.ts.
在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 创建文件:
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 api 上生成一个 openapi.json 文件,而不必启动服务器并请求 openapi 端点。我在环回文档中找不到任何方法。
有什么方法可以生成该文件吗?
方法 getApiSpec() 来自 rest.server.ts @loopback/rest/rest.server.ts.
在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 创建文件:
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) });