Skip to content

Commit

Permalink
fix(app): Nestjs controllers class inheritance
Browse files Browse the repository at this point in the history
fix #1140
  • Loading branch information
vogloblinsky committed Feb 9, 2022
1 parent af3752e commit 06dad8c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/app/compiler/angular/deps/controller-dep.factory.ts
Expand Up @@ -15,7 +15,7 @@ export class ControllerDepFactory {
): IControllerDep {
const sourceCode = srcFile.getText();
const hash = crypto.createHash('sha512').update(sourceCode).digest('hex');
let infos: IControllerDep = {
const infos: IControllerDep = {
name,
id: 'controller-' + name + '-' + hash,
file: file,
Expand All @@ -32,6 +32,9 @@ export class ControllerDepFactory {
infos.prefix = properties[0].text;
}
}
if (IO.extends) {
infos.extends = IO.extends;
}
return infos;
}
}
Expand All @@ -45,4 +48,5 @@ export interface IControllerDep extends IDep {
methodsClass: Array<any>;
deprecated: boolean;
deprecationMessage: string;
extends?: any;
}
7 changes: 6 additions & 1 deletion src/utils/extends-merger.util.ts
Expand Up @@ -8,6 +8,7 @@ export class ExtendsMerger {
private classes;
private injectables;
private directives;
private controllers;

private static instance: ExtendsMerger;
private constructor() {}
Expand All @@ -23,6 +24,7 @@ export class ExtendsMerger {
this.classes = deps.classes;
this.injectables = deps.injectables;
this.directives = deps.directives;
this.controllers = deps.controllers;

const mergeExtendedProperties = component => {
let ext;
Expand Down Expand Up @@ -121,6 +123,7 @@ export class ExtendsMerger {

this.components.forEach(mergeExtendedProperties);
this.directives.forEach(mergeExtendedProperties);
this.controllers.forEach(mergeExtendedProperties);

const mergeExtendedClasses = el => {
let ext;
Expand Down Expand Up @@ -155,6 +158,7 @@ export class ExtendsMerger {
this.classes.forEach(mergeExtendedClasses);
this.injectables.forEach(mergeExtendedClasses);
this.directives.forEach(mergeExtendedClasses);
this.controllers.forEach(mergeExtendedClasses);

return deps;
}
Expand Down Expand Up @@ -189,7 +193,8 @@ export class ExtendsMerger {
this.components,
this.classes,
this.injectables,
this.directives
this.directives,
this.controllers
);
const result = find(mergedData, { name: name } as any);
return result || false;
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/nest-app/src/app.controller.ts
@@ -1,11 +1,12 @@
import { Get, Controller } from '@nestjs/common';
import { AppService } from './app.service';
import { ControllerBase } from './controller.base';

/**
* The main app controller
*/
@Controller()
export class AppController {
export class AppController extends ControllerBase {
constructor(private readonly appService: AppService) {}

@Get()
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/nest-app/src/controller.base.ts
@@ -0,0 +1,8 @@
import { Get } from '@nestjs/common';
export class ControllerBase {
protected controllerBaseString = 'How are you?';
@Get('how-are-you')
getHowAreYou(): string {
return this.controllerBaseString;
}
}
7 changes: 7 additions & 0 deletions test/src/cli/cli-nest.spec.ts
Expand Up @@ -59,5 +59,12 @@ describe('CLI nest projects support', () => {
'@OneToMany(type &#x3D;&gt; ArticleEntity, article &#x3D;&gt; article.author)'
);
});

it('it should contain a controller page with inheritance', () => {
const file = read(`${distFolder}/controllers/AppController.html`);
expect(file).to.contain(
'code><a href="../classes/ControllerBase.html" target="_self" >ControllerBase'
);
});
});
});

0 comments on commit 06dad8c

Please sign in to comment.