-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
}); | ||
}); |
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> |
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,7 @@ | |
"mysql": "2.18.1", | ||
"nats": "1.4.9", | ||
"nodemon": "2.0.4", | ||
"nunjucks": "^3.2.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the reason to put that here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
"nyc": "15.1.0", | ||
"prettier": "2.0.5", | ||
"redis": "3.0.2", | ||
|
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; | ||
}; | ||
} |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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..