Browse Source

fix: asynchronous RestApiBuilder

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
1f2672d317
  1. 38
      packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

38
packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import autoBind from 'auto-bind';
import debug from 'debug';
@ -43,6 +44,10 @@ import { MetaTable } from '../../utils/globals';
const log = debug('nc:api:rest');
const NC_CUSTOM_ROUTE_KEY = '__xc_custom';
const globAsync = promisify(glob);
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
export class RestApiBuilder extends BaseApiBuilder<Noco> {
public readonly type = 'rest';
private controllers: {
@ -371,7 +376,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
});
if (tables?.length) {
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: swaggerDoc,
});
} else {
@ -995,7 +1000,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}
if (args.tableNames && args.tableNames.length) {
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: swaggerDoc,
});
} else {
@ -1099,7 +1104,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
await this.xcTablesRowDelete(tn, extras);
delete this.routers[tn];
this.swaggerUpdate({
await this.swaggerUpdate({
deleteTags: [tn],
});
} catch (e) {
@ -1122,7 +1127,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
delete this.routers[viewName];
this.swaggerUpdate({
await this.swaggerUpdate({
deleteTags: [viewName],
});
} catch (e) {
@ -2367,7 +2372,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.log(`onFunctionCreate : Update function and procedure routes`);
this.procedureCtrl.functionsSet(functions);
this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes);
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: { paths: this.procedureCtrl.getSwaggerObj().paths },
});
}
@ -2387,7 +2392,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.routers.___procedure.stack.length
);
this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes);
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: this.procedureCtrl.getSwaggerObj(),
deleteTags: ['Procedures', 'Functions'],
});
@ -2422,7 +2427,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.log(`onProcedureCreate : Update function and procedure routes`);
this.procedureCtrl.proceduresSet(procedures);
this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes);
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: { paths: this.procedureCtrl.getSwaggerObj().paths },
});
}
@ -2442,7 +2447,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.routers.___procedure.stack.length
);
this.procedureCtrl.mapRoutes(this.routers.___procedure, this.customRoutes);
this.swaggerUpdate({
await this.swaggerUpdate({
addApis: this.procedureCtrl.getSwaggerObj(),
deleteTags: ['Procedures', 'Functions'],
});
@ -2602,8 +2607,8 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.getDbAlias(),
'swagger'
);
mkdirp.sync(swaggerFilePath);
fs.writeFileSync(
await mkdirp(swaggerFilePath);
await writeFileAsync(
path.join(swaggerFilePath, 'swagger.json'),
JSON.stringify(swaggerDoc)
);
@ -2648,8 +2653,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
scheme,
scheme === 'http' ? 'https' : 'http',
];
glob
.sync(
await globAsync(
path.join(
this.app.getToolDir(),
'nc',
@ -2659,8 +2663,8 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
'swagger.json'
)
)
.forEach((jsonFile) => {
const swaggerJson = JSON.parse(fs.readFileSync(jsonFile, 'utf8'));
.forEach(async (jsonFile) => {
const swaggerJson = JSON.parse(await readFileAsync(jsonFile, 'utf8'));
swaggerBaseDocument.tags.push(...swaggerJson.tags);
Object.assign(swaggerBaseDocument.paths, swaggerJson.paths);
Object.assign(
@ -2709,7 +2713,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}
// @ts-ignore
private swaggerUpdate(args: {
private async swaggerUpdate(args: {
deleteApis?: any;
addApis?: any;
deleteTags?: string[];
@ -2729,7 +2733,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
'swagger'
);
const swaggerJson = JSON.parse(
fs.readFileSync(path.join(swaggerFilePath, 'swagger.json'), 'utf8')
await readFileAsync(path.join(swaggerFilePath, 'swagger.json'), 'utf8')
);
/* remove tags, paths and keys */
@ -2795,7 +2799,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
}
}
fs.writeFileSync(
await writeFileAsync(
path.join(swaggerFilePath, 'swagger.json'),
JSON.stringify(swaggerJson)
);

Loading…
Cancel
Save