Skip to content

Commit

Permalink
Merge pull request #24 from barnuri/controller-name-fix
Browse files Browse the repository at this point in the history
controller-name-fix
  • Loading branch information
barnuri committed Apr 30, 2024
2 parents efeed6f + 7a33873 commit 5e97f6c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/generators/GeneratorAbstract.ts
@@ -1,6 +1,6 @@
import { EditorInput } from '../models/editor/EditorInput';
import { deleteFilesByPath, fixPath, makeDirIfNotExist } from '../helpers/generatorHelpers';
import { getAllEditors, getApiPaths, getAllEditorInputsByEditors, capitalize, distinctByProp } from '../helpers';
import { getAllEditors, getApiPaths, getAllEditorInputsByEditors, capitalize, distinctByProp, cleanString } from '../helpers';
import { OpenApiDocument, Editor, ApiPath, EditorPrimitiveInput, EditorObjectInput } from '../models/index';
import GeneratorsOptions from '../models/GeneratorsOptions';
import { join } from 'path';
Expand All @@ -18,7 +18,6 @@ export abstract class GeneratorAbstract {
generatedFiles: string[];
filesNames: string[];
haveModels: boolean;
cleanRegex = /\/| |-|{|}|\.|_|\[|\]|,/g;
methodsNames: { [name: string]: number } = {};

constructor(public swagger: OpenApiDocument, public options: GeneratorsOptions) {
Expand Down Expand Up @@ -57,6 +56,7 @@ export abstract class GeneratorAbstract {
this.allPrimitiveEditorInput = this.allEditorInputs.filter(x => x.editorType === 'EditorPrimitiveInput').map(x => x as EditorPrimitiveInput);
this.allEnumsEditorInput = this.allPrimitiveEditorInput.filter(x => x.enumNames.length + x.enumsOptions.length + x.enumValues.length > 0);
this.haveModels = this.allObjectEditorInputs.length + this.allEnumsEditorInput.length > 0;
console.log('----- done parsing -----'.green());
}

async generate(): Promise<void> {
Expand All @@ -83,9 +83,14 @@ export abstract class GeneratorAbstract {
console.log('----- generating controllers -----'.cyan());
for (const controllerName of this.controllersNames) {
const controllerPaths = this.apiPaths.filter(x => x.controller.toLowerCase() === controllerName.toLowerCase());
console.log(`${controllerName} - ${controllerPaths.length}`);
const controllerDisplay = `${this.getControllerName(controllerName)} - methods: ${controllerPaths.length}`;
console.log(' ');
console.log('-'.repeat(controllerDisplay.length).cyan());
console.log(controllerDisplay);
console.log('-'.repeat(controllerDisplay.length).cyan());
await this.generateController(controllerName, controllerPaths);
}
console.log(' ');
}

private async generateModels() {
Expand Down Expand Up @@ -140,7 +145,7 @@ export abstract class GeneratorAbstract {
if (!name || name === 'undefined' || name === '') {
return undefined;
}
name = name.replace(this.cleanRegex, '');
name = cleanString(name);
return this.options.modelNamePrefix + capitalize(name) + this.options.modelNameSuffix.split('.')[0];
};
let fileName = getFileName();
Expand Down Expand Up @@ -205,7 +210,7 @@ export abstract class GeneratorAbstract {
getMethodName(controllerPath: ApiPath, prefix: string = '', suffix: string = '') {
const generateMethodName = () => {
let longName = controllerPath.method.toLowerCase() + capitalize(controllerPath.path);
longName = longName.replace(this.cleanRegex, '');
longName = cleanString(longName);
if (this.options.longMethodName) {
return longName;
}
Expand All @@ -217,10 +222,10 @@ export abstract class GeneratorAbstract {
? shortName
: shortName.substring(`/${controllerPath.controller}/`.length);
shortName = capitalize(shortName);
shortName = shortName.replace(this.cleanRegex, '');
shortName = cleanString(shortName);
shortName = controllerPath.method.toLowerCase() + capitalize(shortName);
} catch {}
return !shortName ? longName : shortName.replace(this.cleanRegex, '').trim();
return !shortName ? longName : cleanString(shortName);
};
const methodName = `${prefix}${generateMethodName()}${suffix}`;
const key = `${controllerPath.controller}_${methodName}`;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/index.ts
Expand Up @@ -33,7 +33,7 @@ export async function generate(options: GeneratorsOptions) {
console.log(`get swagger successfull`.green());
const constractor = options.type === 'server' ? serverGeneratorGetter(options.generator) : clientGeneratorGetter(options.generator);
const generator = new constractor(swagger, options);
console.log(`start ${generator.constructor.name}`);
console.log(`start ${generator.constructor.name}`.cyan());
await generator.generate();
}

Expand Down
7 changes: 3 additions & 4 deletions src/helpers/openApiHelper.ts
@@ -1,5 +1,6 @@
import { OpenApiPathParamInVals } from './../models/openapi/OpenApiDocument';
import { OpenApiDefinition, OpenApiDocument, OpenApiDefinitionReference, OpenApiDefinitionObject, OpenApiDefinitionsDictionary, ApiPath } from '../models';
import { cleanString } from '../helpers/utilsHelper';

export function getOpenApiDefinitionObject(
definition: OpenApiDefinition,
Expand Down Expand Up @@ -71,10 +72,8 @@ export function getApiPaths(openApiDocument: OpenApiDocument): ApiPath[] {
const oldBody = getParamsByType('body').find(_ => true);
const body = methodDetails.requestBody;
let response = (methodDetails.responses || {})['200'] || {};
let controller = ((methodDetails.tags || []).find(_ => true) || 'Default')
.replace(/ /g, '')
.replace(/\/|-|{|}|\.|_/g, '')
.trim();
let controller = ((methodDetails.tags || []).find(_ => true) || 'Default').trim();
controller = cleanString(controller);
paths.push({
controller,
method,
Expand Down
40 changes: 40 additions & 0 deletions src/helpers/utilsHelper.ts
Expand Up @@ -114,6 +114,46 @@ export function camelCase(str: string) {
.replace(/\s+/g, '');
}

export function cleanString(str: string): string {
const charsToRemove = [
'/',
' ',
'-',
'{',
'}',
'.',
'_',
'[',
']',
',',
'(',
')',
':',
';',
'?',
'!',
'@',
'#',
'$',
'%',
'^',
'&',
'*',
'+',
'=',
'|',
'\\',
'<',
'>',
'~',
'`',
];
for (const char of charsToRemove) {
str = str.replace(new RegExp(escapeRegExp(char), 'g'), '');
}
return str.trim();
}

function createDelimiterRegex(delimiters) {
// Escape special characters in each delimiter and join them with the "or" operator
const regexString = delimiters.map(delimiter => escapeRegExp(delimiter)).join('|');
Expand Down

0 comments on commit 5e97f6c

Please sign in to comment.