Skip to content

Commit

Permalink
feat(api): upgrade NestJS to 6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Mar 18, 2019
1 parent eb046f2 commit 8dbe9d9
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 256 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/app/auth/decorators/allow.decorator.ts
@@ -1,6 +1,6 @@
import { ReflectMetadata } from '@nestjs/common';
import { SetMetadata } from '@nestjs/common';

export const Allow = (...roles: AllowEnum[]) => ReflectMetadata('allow', roles);
export const Allow = (...roles: AllowEnum[]) => SetMetadata('allow', roles);

export enum AllowEnum {
PUBLIC = 'public',
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/auth/decorators/claims.decorator.ts
@@ -1,3 +1,3 @@
import { ReflectMetadata } from '@nestjs/common';
import { SetMetadata } from '@nestjs/common';

export const Claims = (...claims: string[]) => ReflectMetadata('claims', claims);
export const Claims = (...claims: string[]) => SetMetadata('claims', claims);
4 changes: 2 additions & 2 deletions apps/api/src/app/auth/decorators/roles.decorator.ts
@@ -1,6 +1,6 @@
import { ReflectMetadata } from '@nestjs/common';
import { SetMetadata } from '@nestjs/common';

export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
export const Roles = (...roles: string[]) => SetMetadata('roles', roles);

export enum RolesEnum {
SELF = 'SELF',
Expand Down
16 changes: 7 additions & 9 deletions apps/api/src/app/core/context/request-context.middleware.ts
Expand Up @@ -5,15 +5,13 @@ import { RequestContext } from './request-context';

@Injectable()
export class RequestContextMiddleware implements NestMiddleware {
resolve() {
return (req, res, next) => {
const requestContext = new RequestContext(req, res);
const session = cls.getNamespace(RequestContext.name) || cls.createNamespace(RequestContext.name);
use(req, res, next) {
const requestContext = new RequestContext(req, res);
const session = cls.getNamespace(RequestContext.name) || cls.createNamespace(RequestContext.name);

session.run(async () => {
session.set(RequestContext.name, requestContext);
next();
});
};
session.run(async () => {
session.set(RequestContext.name, requestContext);
next();
});
}
}
7 changes: 3 additions & 4 deletions apps/api/src/app/core/interceptors/exception.interceptor.ts
@@ -1,11 +1,10 @@
import { Injectable, NestInterceptor, ExecutionContext, HttpStatus } from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { CallHandler, ExecutionContext, HttpException, HttpStatus, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
return call$.pipe(catchError(err => throwError(new HttpException('Message', HttpStatus.BAD_GATEWAY))));
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(catchError(err => throwError(new HttpException('Message', HttpStatus.BAD_GATEWAY))));
}
}
6 changes: 3 additions & 3 deletions apps/api/src/app/core/interceptors/logging.interceptor.ts
@@ -1,19 +1,19 @@
import { NestInterceptor, ExecutionContext, Logger, Injectable } from '@nestjs/common';
import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
private readonly logger = new Logger(LoggingInterceptor.name, true);

intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
const { url, method, params, query, headers } = request;

this.logger.log(`Before: ${method} ${url} with :
params: ${JSON.stringify(params)}, with query: ${JSON.stringify(query)}`);

const now = Date.now();
return call$.pipe(tap(() => this.logger.log(`After: ${method} ${url} took ${Date.now() - now}ms`)));
return next.handle().pipe(tap(() => this.logger.log(`After: ${method} ${url} took ${Date.now() - now}ms`)));
}
}
6 changes: 3 additions & 3 deletions apps/api/src/app/core/interceptors/timeout.interceptor.ts
@@ -1,10 +1,10 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { timeout } from 'rxjs/operators';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
return call$.pipe(timeout(5000));
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(timeout(5000));
}
}
6 changes: 3 additions & 3 deletions apps/api/src/app/core/interceptors/transform.interceptor.ts
@@ -1,11 +1,11 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { classToPlain } from 'class-transformer';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
return call$.pipe(map(data => classToPlain(data, { excludePrefixes: ['_id'] })));
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map(data => classToPlain(data, { excludePrefixes: ['_id'] })));
}
}
6 changes: 3 additions & 3 deletions apps/api/src/app/core/interceptors/wrap.interceptor.ts
@@ -1,10 +1,10 @@
import { NestInterceptor, ExecutionContext, Injectable } from '@nestjs/common';
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class WrapInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, stream$: Observable<any>): Observable<any> {
return stream$.pipe(map(response => ({ status: 'success', data: response })));
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map(response => ({ status: 'success', data: response })));
}
}
3 changes: 1 addition & 2 deletions apps/api/src/main.hmr.ts
@@ -1,4 +1,4 @@
import { FastifyAdapter, NestFactory } from '@nestjs/core';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app/app.module';
Expand All @@ -9,7 +9,6 @@ import { environment as env } from '@env-api/environment';
declare const module: any;

async function bootstrap() {
// const app = await NestFactory.create(AppModule, new FastifyAdapter(), { cors: true });
const app = await NestFactory.create(AppModule, { cors: true });
const config: ConfigService = app.get(ConfigService);
app.use(helmet());
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/main.ts
@@ -1,4 +1,4 @@
import { FastifyAdapter, NestFactory } from '@nestjs/core';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app/app.module';
Expand All @@ -8,7 +8,6 @@ import { join } from 'path';
import { environment as env } from '@env-api/environment';

async function bootstrap() {
// const app = await NestFactory.create(AppModule, new FastifyAdapter(), { cors: true });
const app = await NestFactory.create(AppModule, { cors: true });
const config: ConfigService = app.get(ConfigService);
app.use(helmet());
Expand Down

0 comments on commit 8dbe9d9

Please sign in to comment.