Skip to content

Commit

Permalink
feature(common) add @WithAlias decorator
Browse files Browse the repository at this point in the history
Add @WithAlias decorator which  allows arbitrary aliases
to be attached to Controller methods and later retrieved
in views and resolved to full route path.

Resolves nestjs#3743
  • Loading branch information
sjones6 committed Jul 19, 2020
1 parent b39cf9f commit 5f880e9
Show file tree
Hide file tree
Showing 18 changed files with 527 additions and 6 deletions.
54 changes: 54 additions & 0 deletions integration/hello-world/e2e/mvc.express.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { join } from 'path';
import { INestApplication } from '@nestjs/common';
import { ExpressAdapter } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import * as express from 'express';
import * as request from 'supertest';
import * as nunjucks from 'nunjucks';
import { ApplicationModule } from '../src/app.module';

interface IExpressNestApplication extends INestApplication {
setBaseViewsDir(string): IExpressNestApplication
setViewEngine(string): IExpressNestApplication
}

describe('Hello world MVC', () => {
let server;
let app: IExpressNestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();

const expressApp = express();
nunjucks.configure(join(__dirname, '..', 'src', 'views'), {
autoescape: true,
express: expressApp
});

app = module.createNestApplication<IExpressNestApplication>(new ExpressAdapter(expressApp));
app.setViewEngine('njk')
server = app.getHttpServer();
await app.init();
});

it(`/GET`, () => {
return request(server)
.get('/hello/mvc')
.expect(200)
.expect(/href="\/hello\/mvc/)
});

it(`/GET/:id`, () => {
const id = 5;
return request(server)
.get(`/hello/mvc/${id}`)
.expect(200)
.expect(new RegExp(`href="/hello/mvc/${id}`))
});

afterEach(async () => {
await app.close();
});
});
22 changes: 21 additions & 1 deletion integration/hello-world/src/hello/hello.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Header, Param } from '@nestjs/common';
import { Controller, Get, Header, Param, Render, WithAlias } from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { HelloService } from './hello.service';
import { UserByIdPipe } from './users/user-by-id.pipe';
Expand Down Expand Up @@ -30,4 +30,24 @@ export class HelloController {
): any {
return user;
}

@Get('mvc')
@Render('mvc')
mvc() {
return { message: 'Hello World!' }
}

@Get('mvc-alias')
@WithAlias('mvc')
@Render('mvc')
mvcAliased() {
return { message: 'Hello World!' }
}

@Get('mvc/:id')
@WithAlias('mvc-id')
@Render('mvc-id')
mvcAliasedWithId(@Param('id') id) {
return { message: 'Hello World!', id }
}
}
16 changes: 16 additions & 0 deletions integration/hello-world/src/views/mvc-id.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>App</title>
</head>

<body>
<h1>{{ message }}</h1>
<div>
<a href="{{ getUrl('mvc-id', { id: id }) }}">Aliased 'mvc-id' With ID: {{ getUrl('mvc-id', { id: id }) }}</a>
</div>
</body>

</html>
16 changes: 16 additions & 0 deletions integration/hello-world/src/views/mvc.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>App</title>
</head>

<body>
<h1>{{ message }}</h1>
<div>
<a href="{{ getUrl('mvc') }}">Aliased 'mvc' to {{ getUrl('mvc') }}</a>
</div>
</body>

</html>
213 changes: 213 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"gulp-sourcemaps": "2.6.5",
"gulp-typescript": "5.0.1",
"gulp-watch": "5.0.1",
"hbs": "^4.1.1",
"husky": "4.2.5",
"imports-loader": "1.1.0",
"json-loader": "0.5.7",
Expand All @@ -147,6 +148,7 @@
"mysql": "2.18.1",
"nats": "1.4.9",
"nodemon": "2.0.4",
"nunjucks": "^3.2.1",
"nyc": "15.1.0",
"prettier": "2.0.5",
"redis": "3.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export const HTTP_CODE_METADATA = '__httpCode__';
export const MODULE_PATH = '__module_path__';
export const HEADERS_METADATA = '__headers__';
export const REDIRECT_METADATA = '__redirect__';
export const ROUTE_ALIAS_METADATA = '__route_alias__';
1 change: 1 addition & 0 deletions packages/common/decorators/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './create-route-param-metadata.decorator';
export * from './render.decorator';
export * from './header.decorator';
export * from './redirect.decorator';
export * from './route-alias.decorator';
Loading

0 comments on commit 5f880e9

Please sign in to comment.