Skip to content

Commit 5f880e9

Browse files
committed
feature(common) add @WithAlias decorator
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
1 parent b39cf9f commit 5f880e9

18 files changed

+527
-6
lines changed

Diff for: integration/hello-world/e2e/mvc.express.spec.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { join } from 'path';
2+
import { INestApplication } from '@nestjs/common';
3+
import { ExpressAdapter } from '@nestjs/platform-express';
4+
import { Test } from '@nestjs/testing';
5+
import * as express from 'express';
6+
import * as request from 'supertest';
7+
import * as nunjucks from 'nunjucks';
8+
import { ApplicationModule } from '../src/app.module';
9+
10+
interface IExpressNestApplication extends INestApplication {
11+
setBaseViewsDir(string): IExpressNestApplication
12+
setViewEngine(string): IExpressNestApplication
13+
}
14+
15+
describe('Hello world MVC', () => {
16+
let server;
17+
let app: IExpressNestApplication;
18+
19+
beforeEach(async () => {
20+
const module = await Test.createTestingModule({
21+
imports: [ApplicationModule],
22+
}).compile();
23+
24+
const expressApp = express();
25+
nunjucks.configure(join(__dirname, '..', 'src', 'views'), {
26+
autoescape: true,
27+
express: expressApp
28+
});
29+
30+
app = module.createNestApplication<IExpressNestApplication>(new ExpressAdapter(expressApp));
31+
app.setViewEngine('njk')
32+
server = app.getHttpServer();
33+
await app.init();
34+
});
35+
36+
it(`/GET`, () => {
37+
return request(server)
38+
.get('/hello/mvc')
39+
.expect(200)
40+
.expect(/href="\/hello\/mvc/)
41+
});
42+
43+
it(`/GET/:id`, () => {
44+
const id = 5;
45+
return request(server)
46+
.get(`/hello/mvc/${id}`)
47+
.expect(200)
48+
.expect(new RegExp(`href="/hello/mvc/${id}`))
49+
});
50+
51+
afterEach(async () => {
52+
await app.close();
53+
});
54+
});

Diff for: integration/hello-world/src/hello/hello.controller.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Header, Param } from '@nestjs/common';
1+
import { Controller, Get, Header, Param, Render, WithAlias } from '@nestjs/common';
22
import { Observable, of } from 'rxjs';
33
import { HelloService } from './hello.service';
44
import { UserByIdPipe } from './users/user-by-id.pipe';
@@ -30,4 +30,24 @@ export class HelloController {
3030
): any {
3131
return user;
3232
}
33+
34+
@Get('mvc')
35+
@Render('mvc')
36+
mvc() {
37+
return { message: 'Hello World!' }
38+
}
39+
40+
@Get('mvc-alias')
41+
@WithAlias('mvc')
42+
@Render('mvc')
43+
mvcAliased() {
44+
return { message: 'Hello World!' }
45+
}
46+
47+
@Get('mvc/:id')
48+
@WithAlias('mvc-id')
49+
@Render('mvc-id')
50+
mvcAliasedWithId(@Param('id') id) {
51+
return { message: 'Hello World!', id }
52+
}
3353
}

Diff for: integration/hello-world/src/views/mvc-id.njk

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>App</title>
7+
</head>
8+
9+
<body>
10+
<h1>{{ message }}</h1>
11+
<div>
12+
<a href="{{ getUrl('mvc-id', { id: id }) }}">Aliased 'mvc-id' With ID: {{ getUrl('mvc-id', { id: id }) }}</a>
13+
</div>
14+
</body>
15+
16+
</html>

Diff for: integration/hello-world/src/views/mvc.njk

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>App</title>
7+
</head>
8+
9+
<body>
10+
<h1>{{ message }}</h1>
11+
<div>
12+
<a href="{{ getUrl('mvc') }}">Aliased 'mvc' to {{ getUrl('mvc') }}</a>
13+
</div>
14+
</body>
15+
16+
</html>

Diff for: package-lock.json

+213
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
"gulp-sourcemaps": "2.6.5",
133133
"gulp-typescript": "5.0.1",
134134
"gulp-watch": "5.0.1",
135+
"hbs": "^4.1.1",
135136
"husky": "4.2.5",
136137
"imports-loader": "1.1.0",
137138
"json-loader": "0.5.7",
@@ -147,6 +148,7 @@
147148
"mysql": "2.18.1",
148149
"nats": "1.4.9",
149150
"nodemon": "2.0.4",
151+
"nunjucks": "^3.2.1",
150152
"nyc": "15.1.0",
151153
"prettier": "2.0.5",
152154
"redis": "3.0.2",

Diff for: packages/common/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export const HTTP_CODE_METADATA = '__httpCode__';
2626
export const MODULE_PATH = '__module_path__';
2727
export const HEADERS_METADATA = '__headers__';
2828
export const REDIRECT_METADATA = '__redirect__';
29+
export const ROUTE_ALIAS_METADATA = '__route_alias__';

Diff for: packages/common/decorators/http/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './create-route-param-metadata.decorator';
55
export * from './render.decorator';
66
export * from './header.decorator';
77
export * from './redirect.decorator';
8+
export * from './route-alias.decorator';

0 commit comments

Comments
 (0)