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

Loading…
Cancel
Save