Skip to content

Commit

Permalink
header + security token support, filters, create distinct controllers…
Browse files Browse the repository at this point in the history
… by a custom filed
  • Loading branch information
egandro committed Jun 30, 2017
1 parent 3c676a8 commit 1766f06
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 358 deletions.
105 changes: 99 additions & 6 deletions lib/generator/angular.client/angular.client.generator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const changeCase = require('change-case');

import { GeneratorConfigBasic } from '../../persistance/generatorconfig.basic';
import { GeneratorConfigAngularClient, AngularClientFilter } from '../../persistance/generatorconfig.angular.client';

import { ApiParser } from '../../swagger/apiparser.class';
import { ApiDescription } from '../../swagger/entites/apidesciption.class';
import { ControllerType } from '../../swagger/entites/controllertype.class';
import { ComplexType } from '../../swagger/entites/complextype.class';

import { RouteType } from '../../swagger/entites/routetype.class';

import { FileManger } from '../filemanager';
import { BaseGenerator } from '../basegenerator';
Expand Down Expand Up @@ -49,6 +51,7 @@ export class AngularClientGenerator extends BaseGenerator {
createProject: false,
projectName: 'angular-client',
ngModuleName: 'AngularClient',
controllerNames: [],
filter: angularClientFilter
};

Expand Down Expand Up @@ -84,7 +87,9 @@ export class AngularClientGenerator extends BaseGenerator {
projectName = SymbolNameMapper.lower(projectName);
let ngModuleName = this.generatorConfig.ngModuleName;

let controllers = this.applyControllerFilter(<any>this.generatorConfig.filter, api.controllers);
let controllers: ControllerType[] = this.createControllersFromRoute(ngModuleName, this.generatorConfig.controllerNames, api.routes);

controllers = this.applyControllerFilter(<any>this.generatorConfig.filter, controllers);
let complexTypes = this.applyReferenceTypeFilter(<any>this.generatorConfig.filter, api.complexTypes);


Expand Down Expand Up @@ -148,6 +153,48 @@ export class AngularClientGenerator extends BaseGenerator {
}
*/


protected createControllersFromRoute(defaultControllerName: string, controllerNames: string[], routes: RouteType[]): ControllerType[] {
let result: ControllerType[] = [];

if (controllerNames == null || controllerNames == undefined || controllerNames.length == 0) {
// we can't unflatten
let ctrl: ControllerType = new ControllerType();
ctrl.name = defaultControllerName;
for (let r = 0; r < routes.length; r++) {
let route = routes[r];
route.operationId = changeCase.camelCase(route.operationId);
ctrl.routes.push(route);
}

result.push(ctrl);
return result;
}

for (let c = 0; c < controllerNames.length; c++) {
let controllerName = controllerNames[c];
if (controllerName.toLowerCase().endsWith("controller")) {
controllerName = controllerName.substring(0, controllerName.length - "controller".length);
}

let ctrl: ControllerType = new ControllerType();
ctrl.name = controllerName;
for (let r = 0; r < routes.length; r++) {
let route = routes[r];
if (route.operationId.startsWith(controllerName)) {
route.operationId = route.operationId.substring(controllerName.length);
route.operationId = changeCase.camelCase(route.operationId);
ctrl.routes.push(route);
}
}

result.push(ctrl);
}


return result;
}

protected applyControllerFilter(filter: AngularClientFilter, controllers: ControllerType[]): ControllerType[] {
if (filter === undefined || filter === null) {
return controllers;
Expand All @@ -156,16 +203,62 @@ export class AngularClientGenerator extends BaseGenerator {
if (controllers === undefined || controllers === null || controllers.length === 0) {
return controllers;
}
console.log('applyControllerFilter not implemented');
return controllers;

let exculdeController: string[] = <any>filter.exculdeService;
let only: string[] = <any>filter.onlyService;

if (exculdeController === undefined || exculdeController === null) {
exculdeController = [];
}
if (only === undefined || only === null) {
only = [];
}

let result: ControllerType[] = [];
for (let k = 0; k < controllers.length; k++) {
let ctrl = controllers[k];
// excluded
if (only.length == 0 && exculdeController.indexOf(ctrl.name) != -1) {
continue;
}
if (only.length > 0 && only.indexOf(ctrl.name) == -1) {
continue;
}
result.push(ctrl);
}

return result;
}

protected applyReferenceTypeFilter(filter: AngularClientFilter, referenceTypes: ComplexType[]): ComplexType[] {
if (referenceTypes === undefined || referenceTypes === null || referenceTypes.length === 0) {
return referenceTypes;
}
console.log('applyReferenceTypeFilter not implemented');
return referenceTypes;

let exculdeEntity: string[] = <any>filter.exculdeEntity;
let only: string[] = <any>filter.onlyEntity;

if (exculdeEntity === undefined || exculdeEntity === null) {
exculdeEntity = [];
}
if (only === undefined || only === null) {
only = [];
}

let result: ComplexType[] = [];
for (let k = 0; k < referenceTypes.length; k++) {
let entity = referenceTypes[k];
// excluded
if (only.length == 0 && exculdeEntity.indexOf(entity.name) != -1) {
continue;
}
if (only.length > 0 && only.indexOf(entity.name) == -1) {
continue;
}
result.push(entity);
}

return result;
}

}
Expand Down
Loading

0 comments on commit 1766f06

Please sign in to comment.