forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(@nestjs) update examples, fix interceptors bug
- Loading branch information
1 parent
8de7791
commit 0a6034f
Showing
55 changed files
with
9,003 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as express from 'express'; | ||
import * as bodyParser from 'body-parser'; | ||
import * as request from 'supertest'; | ||
import { Test } from '@nestjs/testing'; | ||
import { CatsModule } from '../../src/modules/cats/cats.module'; | ||
import { CatsService } from '../../src/modules/cats/cats.service'; | ||
|
||
describe('Cats', () => { | ||
const server = express(); | ||
server.use(bodyParser.json()); | ||
|
||
const catsService = { findAll: () => ['test'] }; | ||
|
||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
modules: [CatsModule], | ||
}) | ||
.overrideComponent(CatsService).useValue(catsService) | ||
.compile(); | ||
|
||
const app = module.createNestApplication(server); | ||
await app.init(); | ||
}); | ||
|
||
it(`/GET cats`, () => { | ||
return request(server) | ||
.get('/cats') | ||
.expect(200) | ||
.expect({ | ||
data: catsService.findAll(), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "<rootDir>/../node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "/e2e/.*\\.(e2e-test|e2e-spec).(ts|tsx|js)$", | ||
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"], | ||
"coverageReporters": ["json", "lcov"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$", | ||
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"], | ||
"coverageReporters": ["json", "lcov"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/01-cats-app/src/modules/cats/cats.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { CatsController } from './cats.controller'; | ||
import { CatsService } from './cats.service'; | ||
|
||
describe('CatsController', () => { | ||
let catsController: CatsController; | ||
let catsService: CatsService; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
controllers: [CatsController], | ||
components: [CatsService], | ||
}).compile(); | ||
|
||
catsService = module.get<CatsService>(CatsService); | ||
catsController = module.get<CatsController>(CatsController); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should return an array of cats', async () => { | ||
const result = ['test']; | ||
jest.spyOn(catsService, 'findAll').mockImplementation(() => result); | ||
|
||
expect(await catsController.findAll()).toBe(result); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
examples/01-cats-app/src/modules/common/decorators/roles.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReflectMetadata } from '@nestjs/common'; | ||
|
||
export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles); |
11 changes: 0 additions & 11 deletions
11
examples/01-cats-app/src/modules/common/guards/breed.guard.ts
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
examples/01-cats-app/src/modules/common/guards/roles.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Guard, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
@Guard() | ||
export class RolesGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector) {} | ||
|
||
canActivate(req, context: ExecutionContext): boolean { | ||
const { parent, handler } = context; | ||
const roles = this.reflector.get<string[]>('roles', handler); | ||
if (!roles) { | ||
return true; | ||
} | ||
|
||
const user = req.user; | ||
const hasRole = () => !!user.roles.find((role) => !!roles.find((item) => item === role)); | ||
return user && user.roles && hasRole(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/01-cats-app/src/modules/common/interceptors/cache.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import 'rxjs/add/observable/of'; | ||
|
||
@Interceptor() | ||
export class CacheInterceptor implements NestInterceptor { | ||
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> { | ||
const isCached = true; | ||
if (isCached) { | ||
return Observable.of([]); | ||
} | ||
return stream$; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/01-cats-app/src/modules/common/interceptors/logging.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import 'rxjs/add/operator/do'; | ||
|
||
@Interceptor() | ||
export class LoggingInterceptor implements NestInterceptor { | ||
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> { | ||
console.log('Before...'); | ||
const now = Date.now(); | ||
|
||
return stream$.do( | ||
() => console.log(`After... ${Date.now() - now}ms`), | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/01-cats-app/src/modules/common/interceptors/transform.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import 'rxjs/add/operator/map'; | ||
|
||
@Interceptor() | ||
export class TransformInterceptor implements NestInterceptor { | ||
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> { | ||
return stream$.map((data) => ({ data })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# dependencies | ||
/node_modules | ||
|
||
# IDE | ||
/.idea | ||
/.awcache | ||
/.vscode | ||
|
||
# misc | ||
npm-debug.log | ||
|
||
# example | ||
/quick-start | ||
|
||
# tests | ||
/test | ||
/coverage | ||
/.nyc_output | ||
|
||
# dist | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<html> | ||
<head> | ||
<script src="socket.io.js"></script> | ||
<script> | ||
const socket = io('http://localhost:81'); | ||
socket.on('connect', function() { | ||
console.log('Connected'); | ||
socket.emit('events', { test: 'test' }); | ||
}); | ||
socket.on('events', function(data) { | ||
console.log('event', data); | ||
}); | ||
socket.on('exception', function(data) { | ||
console.log('event', data); | ||
}); | ||
socket.on('disconnect', function() { | ||
console.log('Disconnected'); | ||
}); | ||
</script> | ||
</head> | ||
<body></body> | ||
</html> |
Oops, something went wrong.