Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

refactor: simplify swagger generation by utilizing shared utility function

Pranav Cc07d2c1f4323
diff --git a/packages/nocodb/src/services/api-docs/shared/swaggerUtils.ts b/packages/nocodb/src/services/api-docs/shared/swaggerUtils.ts
new file mode 100644
index 0000000000..4ff3a698cd
--- /dev/null
+++ b/packages/nocodb/src/services/api-docs/shared/swaggerUtils.ts
@@ -0,0 +1,201 @@
+import { ViewTypes } from 'nocodb-sdk';
+import type {
+ Base,
+ FormViewColumn,
+ GalleryViewColumn,
+ GridViewColumn,
+ Model,
+ Source,
+ View,
+} from '~/models';
+import type { NcContext } from '~/interface/config';
+import Noco from '~/Noco';
+
+export interface SwaggerView {
+ view: View;
+ columns: Array<GridViewColumn | GalleryViewColumn | FormViewColumn>;
+}
+
+export interface SwaggerGenerationContext {
+ context: NcContext;
+ base: Base;
+ models: Model[];
+ ncMeta?: any;
+}
+
+export interface SwaggerGenerationResult {
+ sourcesMap: Map<string, Source>;
+ tableNamesMap: Map<string, string>;
+ swaggerViews: Map<string, SwaggerView[]>;
+}
+
+/**
+ * Prepares common data structures for swagger generation
+ * - Fetches sources once and creates a map for efficient lookup
+ * - Pre-constructs table names with source prefixes and handles duplicates
+ * - Prepares views data for each model
+ */
+export async function prepareSwaggerGenerationData({
+ context,
+ base,
+ models,
+ ncMeta = Noco.ncMeta,
+}: SwaggerGenerationContext): Promise<SwaggerGenerationResult> {
+ // Fetch sources once for the entire base to avoid repeated queries
+ const sources = await base.getSources(false, ncMeta);
+ const sourcesMap = new Map<string, Source>(
+ sources.map((source) => [source.id, source]),
+ );
+
+ // Pre-construct table names for all models to avoid repeated construction and handle duplicates
+ const tableNamesMap = new Map<string, string>();
+ const usedTableNames = new Set<string>();
+
+ for (const model of models) {
+ const source = sourcesMap.get(model.source_id);
+ const sourcePrefix = source?.isMeta()
+ ? ''
+ : `${source?.alias || 'Source'}_`;
+ const tableName = `${sourcePrefix}${model.title}`;
+
+ // Handle duplicate table names by adding a number suffix
+ let finalTableName = tableName;
+ let counter = 1;
+ while (usedTableNames.has(finalTableName)) {
+ finalTableName = `${tableName}_${counter}`;
+ counter++;
+ }
+
+ usedTableNames.add(finalTableName);
+ tableNamesMap.set(model.id, finalTableName);
+ }
+
+ // Prepare views data for all models
+ const swaggerViews = new Map<string, SwaggerView[]>();
+
+ for (const model of models) {
+ const views: SwaggerView[] = [];
+
+ for (const view of (await model.getViews(context, false, ncMeta)) || []) {
+ if (view.type !== ViewTypes.GRID) continue;
+ views.push({
+ view,
+ columns: await view.getColumns(context, ncMeta),
+ });
+ }
+
+ swaggerViews.set(model.id, views);
+ }
+
+ return {
+ sourcesMap,
+ tableNamesMap,
+ swaggerViews,
+ };
+}
+
+/**
+ * Generic swagger generation function that can be used by all versions
+ */
+export async function generateSwagger<TSwaggerColumn, TSwaggerView>(
+ generationContext: SwaggerGenerationContext,
+ swaggerBase: any,
+ getSwaggerColumnMetas: (
+ context: NcContext,
+ columns: any[],
+ base: Base,
+ ncMeta?: any,
+ ) => Promise<TSwaggerColumn[]>,
+ getPaths: (
+ context: NcContext,
+ params: {
+ base?: Base;
+ model: Model;
+ columns: TSwaggerColumn[];
+ views: TSwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
+ },
+ ncMeta?: any,
+ ) => Promise<any>,
+ getSchemas: (
+ context: NcContext,
+ params: {
+ base?: Base;
+ model: Model;
+ columns: TSwaggerColumn[];
+ views: TSwaggerView[];
+ sourcesMap: Map<string, Source>;
+ tableName: string;
+ },
+ ncMeta?: any,
+ ) => Promise<any>,
+ transformViews?: (swaggerViews: SwaggerView[]) => TSwaggerView[],
+) {
+ const { context, base, models, ncMeta = Noco.ncMeta } = generationContext;
+
+ // base swagger object
+ const swaggerObj = {
+ ...swaggerBase,
+ paths: {},
+ components: {
+ ...swaggerBase.components,
+ schemas: { ...swaggerBase.components.schemas },
+ },
+ };
+
+ // Prepare common data structures
+ const { sourcesMap, tableNamesMap, swaggerViews } =
+ await prepareSwaggerGenerationData(generationContext);
+
+ // iterate and populate swagger schema and path for models and views
+ for (const model of models) {
+ let paths = {};
+
+ const columns = await getSwaggerColumnMetas(
+ context,
+ await model.getColumns(context, ncMeta),
+ base,
+ ncMeta,
+ );
+
+ const modelViews = swaggerViews.get(model.id) || [];
+ const views = transformViews
+ ? transformViews(modelViews)
+ : (modelViews as any);
+
+ // skip mm tables
+ if (!model.mm) {
+ paths = await getPaths(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
+ }
+
+ const schemas = await getSchemas(
+ context,
+ {
+ base,
+ model,
+ columns,
+ views,
+ sourcesMap,
+ tableName: tableNamesMap.get(model.id),
+ },
+ ncMeta,
+ );
+
+ Object.assign(swaggerObj.paths, paths);
+ Object.assign(swaggerObj.components.schemas, schemas);
+ }
+
+ return swaggerObj;
+}
diff --git a/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts b/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
index d1578fe3da..40352f04e8 100644
--- a/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
+++ b/packages/nocodb/src/services/api-docs/swagger/getSwaggerJSON.ts
@@ -1,16 +1,9 @@
-import { ViewTypes } from 'nocodb-sdk';
+import { generateSwagger } from '../shared/swaggerUtils';
import swaggerBase from './swagger-base.json';
import getPaths from './getPaths';
import getSchemas from './getSchemas';
import getSwaggerColumnMetas from './getSwaggerColumnMetas';
-import type {
- Base,
- FormViewColumn,
- GalleryViewColumn,
- GridViewColumn,
- Model,
- View,
-} from '~/models';
+import type { Base, Model } from '~/models';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';
@@ -20,100 +13,11 @@ export default async function getSwaggerJSON(
models: Model[],
ncMeta = Noco.ncMeta,
) {
- // base swagger object
- const swaggerObj = {
- ...swaggerBase,
- paths: {},
- components: {
- ...swaggerBase.components,
- schemas: { ...swaggerBase.components.schemas },
- },
- };
-
- // Fetch sources once for the entire base to avoid repeated queries
- const sources = await base.getSources(false, ncMeta);
- const sourcesMap = new Map(sources.map((source) => [source.id, source]));
-
- // Pre-construct table names for all models to avoid repeated construction and handle duplicates
- const tableNamesMap = new Map<string, string>();
- const usedTableNames = new Set<string>();
-
- for (const model of models) {
- const source = sourcesMap.get(model.source_id);
- const sourcePrefix = source?.isMeta()
- ? ''
- : `${source?.alias || 'Source'}_`;
- const tableName = `${sourcePrefix}${model.title}`;
-
- // Handle duplicate table names by adding a number suffix
- let finalTableName = tableName;
- let counter = 1;
- while (usedTableNames.has(finalTableName)) {
- finalTableName = `${tableName}_${counter}`;
- counter++;
- }
-
- usedTableNames.add(finalTableName);
- tableNamesMap.set(model.id, finalTableName);
- }
-
- // iterate and populate swagger schema and path for models and views
- for (const model of models) {
- let paths = {};
-
- const columns = await getSwaggerColumnMetas(
- context,
- await model.getColumns(context, ncMeta),
- base,
- ncMeta,
- );
-
- const views: SwaggerView[] = [];
-
- for (const view of (await model.getViews(context, false, ncMeta)) || []) {
- if (view.type !== ViewTypes.GRID) continue;
- views.push({
- view,
- columns: await view.getColumns(context, ncMeta),
- });
- }
-
- // skip mm tables
- if (!model.mm)
- paths = await getPaths(
- context,
- {
- base,
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- const schemas = await getSchemas(
- context,
- {
- base,
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- Object.assign(swaggerObj.paths, paths);
- Object.assign(swaggerObj.components.schemas, schemas);
- }
-
- return swaggerObj;
-}
-
-export interface SwaggerView {
- view: View;
- columns: Array<GridViewColumn | GalleryViewColumn | FormViewColumn>;
+ return generateSwagger(
+ { context, base, models, ncMeta },
+ swaggerBase,
+ getSwaggerColumnMetas,
+ getPaths,
+ getSchemas,
+ );
}
diff --git a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
index 9a79225ba4..b110dbd1e2 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV2/getSwaggerJSONV2.ts
@@ -1,16 +1,9 @@
-import { ViewTypes } from 'nocodb-sdk';
+import { generateSwagger } from '../shared/swaggerUtils';
import swaggerBase from './swagger-base.json';
import getPaths from './getPaths';
import getSchemas from './getSchemas';
import getSwaggerColumnMetas from './getSwaggerColumnMetas';
-import type {
- Base,
- FormViewColumn,
- GalleryViewColumn,
- GridViewColumn,
- Model,
- View,
-} from '~/models';
+import type { Base, Model } from '~/models';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';
@@ -20,99 +13,11 @@ export default async function getSwaggerJSONV2(
models: Model[],
ncMeta = Noco.ncMeta,
) {
- // base swagger object
- const swaggerObj = {
- ...swaggerBase,
- paths: {},
- components: {
- ...swaggerBase.components,
- schemas: { ...swaggerBase.components.schemas },
- },
- };
-
- // Fetch sources once for the entire base to avoid repeated queries
- const sources = await base.getSources(false, ncMeta);
- const sourcesMap = new Map(sources.map((source) => [source.id, source]));
-
- // Pre-construct table names for all models to avoid repeated construction and handle duplicates
- const tableNamesMap = new Map<string, string>();
- const usedTableNames = new Set<string>();
-
- for (const model of models) {
- const source = sourcesMap.get(model.source_id);
- const sourcePrefix = source?.isMeta()
- ? ''
- : `${source?.alias || 'Source'}_`;
- const tableName = `${sourcePrefix}${model.title}`;
-
- // Handle duplicate table names by adding a number suffix
- let finalTableName = tableName;
- let counter = 1;
- while (usedTableNames.has(finalTableName)) {
- finalTableName = `${tableName}_${counter}`;
- counter++;
- }
-
- usedTableNames.add(finalTableName);
- tableNamesMap.set(model.id, finalTableName);
- }
-
- // iterate and populate swagger schema and path for models and views
- for (const model of models) {
- let paths = {};
-
- const columns = await getSwaggerColumnMetas(
- context,
- await model.getColumns(context, ncMeta),
- base,
- ncMeta,
- );
-
- const views: SwaggerView[] = [];
-
- for (const view of (await model.getViews(context, false, ncMeta)) || []) {
- if (view.type !== ViewTypes.GRID) continue;
- views.push({
- view,
- columns: await view.getColumns(context, ncMeta),
- });
- }
-
- // skip mm tables
- if (!model.mm)
- paths = await getPaths(
- context,
- {
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- const schemas = await getSchemas(
- context,
- {
- base,
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- Object.assign(swaggerObj.paths, paths);
- Object.assign(swaggerObj.components.schemas, schemas);
- }
-
- return swaggerObj;
-}
-
-export interface SwaggerView {
- view: View;
- columns: Array<GridViewColumn | GalleryViewColumn | FormViewColumn>;
+ return generateSwagger(
+ { context, base, models, ncMeta },
+ swaggerBase,
+ getSwaggerColumnMetas,
+ getPaths,
+ getSchemas,
+ );
}
diff --git a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
index b077c4c6ae..8238194161 100644
--- a/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
+++ b/packages/nocodb/src/services/api-docs/swaggerV3/getSwaggerJSONV3.ts
@@ -1,16 +1,9 @@
-import { ViewTypes } from 'nocodb-sdk';
+import { generateSwagger } from '../shared/swaggerUtils';
import swaggerBase from './swagger-base-v3.json';
import getPathsV3 from './getPathsV3';
import getSchemasV3 from './getSchemasV3';
import getSwaggerColumnMetasV3 from './getSwaggerColumnMetasV3';
-import type {
- Base,
- FormViewColumn,
- GalleryViewColumn,
- GridViewColumn,
- Model,
- View,
-} from '~/models';
+import type { Base, Model } from '~/models';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';
@@ -20,100 +13,11 @@ export default async function getSwaggerJSONV3(
models: Model[],
ncMeta = Noco.ncMeta,
) {
- // base swagger object
- const swaggerObj = {
- ...swaggerBase,
- paths: {},
- components: {
- ...swaggerBase.components,
- schemas: { ...swaggerBase.components.schemas },
- },
- };
-
- // Fetch sources once for the entire base to avoid repeated queries
- const sources = await base.getSources(false, ncMeta);
- const sourcesMap = new Map(sources.map((source) => [source.id, source]));
-
- // Pre-construct table names for all models to avoid repeated construction and handle duplicates
- const tableNamesMap = new Map<string, string>();
- const usedTableNames = new Set<string>();
-
- for (const model of models) {
- const source = sourcesMap.get(model.source_id);
- const sourcePrefix = source?.isMeta()
- ? ''
- : `${source?.alias || 'Source'}_`;
- const tableName = `${sourcePrefix}${model.title}`;
-
- // Handle duplicate table names by adding a number suffix
- let finalTableName = tableName;
- let counter = 1;
- while (usedTableNames.has(finalTableName)) {
- finalTableName = `${tableName}_${counter}`;
- counter++;
- }
-
- usedTableNames.add(finalTableName);
- tableNamesMap.set(model.id, finalTableName);
- }
-
- // iterate and populate swagger schema and path for models and views
- for (const model of models) {
- let paths = {};
-
- const columns = await getSwaggerColumnMetasV3(
- context,
- await model.getColumns(context, ncMeta),
- base,
- ncMeta,
- );
-
- const views: SwaggerView[] = [];
-
- for (const view of (await model.getViews(context, false, ncMeta)) || []) {
- if (view.type !== ViewTypes.GRID) continue;
- views.push({
- view,
- columns: await view.getColumns(context, ncMeta),
- });
- }
-
- // skip mm tables
- if (!model.mm)
- paths = await getPathsV3(
- context,
- {
- base,
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- const schemas = await getSchemasV3(
- context,
- {
- base,
- model,
- columns,
- views,
- sourcesMap,
- tableName: tableNamesMap.get(model.id),
- },
- ncMeta,
- );
-
- Object.assign(swaggerObj.paths, paths);
- Object.assign(swaggerObj.components.schemas, schemas);
- }
-
- return swaggerObj;
-}
-
-export interface SwaggerView {
- view: View;
- columns: Array<GridViewColumn | GalleryViewColumn | FormViewColumn>;
+ return generateSwagger(
+ { context, base, models, ncMeta },
+ swaggerBase,
+ getSwaggerColumnMetasV3,
+ getPathsV3,
+ getSchemasV3,
+ );
}