Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(common) add @WithAlias route decorator #5117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, didn't see examples in integration tests that rendered html using the @Render decorator unless I'm mistaken. So, this tests that along with new changes..

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>
161 changes: 161 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"mysql": "2.18.1",
"nats": "1.4.9",
"nodemon": "2.0.4",
"nunjucks": "^3.2.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason to put that here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's for a new integration test. As it's in devDeps it should be fine

"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';
23 changes: 23 additions & 0 deletions packages/common/decorators/http/route-alias.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ROUTE_ALIAS_METADATA } from '../../constants';

/**
* Alias for the route, which can be resolved to the full route path
*
* For example: `@WithAlias('alias')`
*
* @param routeAlias alias for the route
*
* @see [Model-View-Controller](https://docs.nestjs.com/techniques.mvc)
*
* @publicApi
*/
export function WithAlias(routeAlias: string | Symbol): MethodDecorator {
return (
target: object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => {
Reflect.defineMetadata(ROUTE_ALIAS_METADATA, routeAlias, descriptor.value);
return descriptor;
};
}
17 changes: 17 additions & 0 deletions packages/common/test/decorators/route-alias.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import { WithAlias } from '../../decorators/http/route-alias.decorator';
import { ROUTE_ALIAS_METADATA } from '../../constants';

describe('@WithAlias', () => {
const alias = 'alias';

class Test {
@WithAlias(alias)
public static test() {}
}

it('should enhance method with expected alias string', () => {
const metadata = Reflect.getMetadata(ROUTE_ALIAS_METADATA, Test.test);
expect(metadata).to.be.eql(alias);
});
});
Loading