Skip to content

Commit

Permalink
ci(@nestjs) integrate with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Dec 21, 2017
1 parent 81be8a0 commit 52e0128
Show file tree
Hide file tree
Showing 645 changed files with 13,202 additions and 10,929 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
4 changes: 2 additions & 2 deletions examples/01-cats-app/e2e/cats/cats.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Cats', () => {

beforeAll(async () => {
const module = await Test.createTestingModule({
modules: [CatsModule],
modules: [CatsModule]
})
.overrideComponent(CatsService)
.useValue(catsService)
Expand All @@ -29,7 +29,7 @@ describe('Cats', () => {
.get('/cats')
.expect(200)
.expect({
data: catsService.findAll(),
data: catsService.findAll()
});
});

Expand Down
15 changes: 8 additions & 7 deletions examples/01-cats-app/src/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { CatsModule } from './cats/cats.module';
import { CatsController } from './cats/cats.controller';

@Module({
modules: [CatsModule],
modules: [CatsModule]
})
export class ApplicationModule implements NestModule {
configure(consumer: MiddlewaresConsumer): void {
consumer.apply(LoggerMiddleware)
.with('ApplicationModule')
.forRoutes(CatsController);
}
}
configure(consumer: MiddlewaresConsumer): void {
consumer
.apply(LoggerMiddleware)
.with('ApplicationModule')
.forRoutes(CatsController);
}
}
8 changes: 4 additions & 4 deletions examples/01-cats-app/src/modules/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ describe('CatsController', () => {

beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [CatsController],
components: [CatsService],
}).compile();
controllers: [CatsController],
components: [CatsService]
}).compile();

catsService = module.get<CatsService>(CatsService);
catsController = module.get<CatsController>(CatsController);
Expand All @@ -24,4 +24,4 @@ describe('CatsController', () => {
expect(await catsController.findAll()).toBe(result);
});
});
});
});
18 changes: 15 additions & 3 deletions examples/01-cats-app/src/modules/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Controller, Get, Post, Body, UseGuards, ReflectMetadata, UseInterceptors, Param } from '@nestjs/common';
import {
Controller,
Get,
Post,
Body,
UseGuards,
ReflectMetadata,
UseInterceptors,
Param
} from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
Expand Down Expand Up @@ -26,7 +35,10 @@ export class CatsController {
}

@Get(':id')
findOne(@Param('id', new ParseIntPipe()) id) {
findOne(
@Param('id', new ParseIntPipe())
id
) {
// logic
}
}
}
6 changes: 3 additions & 3 deletions examples/01-cats-app/src/modules/cats/cats.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

@Module({
controllers: [CatsController],
components: [CatsService],
controllers: [CatsController],
components: [CatsService]
})
export class CatsModule {}
export class CatsModule {}
2 changes: 1 addition & 1 deletion examples/01-cats-app/src/modules/cats/cats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export class CatsService {
findAll(): Cat[] {
return this.cats;
}
}
}
11 changes: 4 additions & 7 deletions examples/01-cats-app/src/modules/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { IsString, IsInt } from 'class-validator';

export class CreateCatDto {
@IsString()
readonly name: string;
@IsString() readonly name: string;

@IsInt()
readonly age: number;
@IsInt() readonly age: number;

@IsString()
readonly breed: string;
}
@IsString() readonly breed: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export interface Cat {
readonly name: string;
readonly age: number;
readonly breed: string;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ReflectMetadata } from '@nestjs/common';

export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export class ForbiddenException extends HttpException {
constructor() {
super('Forbidden', HttpStatus.FORBIDDEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class HttpExceptionFilter implements ExceptionFilter {

response.status(status).json({
statusCode: status,
message: `It's a message from the exception filter`,
message: `It's a message from the exception filter`
});
}
}
}
5 changes: 3 additions & 2 deletions examples/01-cats-app/src/modules/common/guards/roles.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class RolesGuard implements CanActivate {
}

const user = req.user;
const hasRole = () => !!user.roles.find((role) => !!roles.find((item) => item === role));
const hasRole = () =>
!!user.roles.find(role => !!roles.find(item => item === role));
return user && user.roles && hasRole();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import 'rxjs/add/observable/of';
export abstract class CacheInterceptor implements NestInterceptor {
protected abstract readonly isCached: () => boolean;

intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> {
intercept(
dataOrRequest,
context: ExecutionContext,
stream$: Observable<any>
): Observable<any> {
if (this.isCached()) {
return Observable.of([]);
}
return stream$;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Interceptor, NestInterceptor, ExecutionContext, HttpStatus } from '@nestjs/common';
import {
Interceptor,
NestInterceptor,
ExecutionContext,
HttpStatus
} from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

@Interceptor()
export class ExceptionInterceptor implements NestInterceptor {
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> {
return stream$.catch((err) => Observable.throw(
new HttpException('Exception interceptor message', HttpStatus.BAD_GATEWAY),
));
intercept(
dataOrRequest,
context: ExecutionContext,
stream$: Observable<any>
): Observable<any> {
return stream$.catch(err =>
Observable.throw(
new HttpException(
'Exception interceptor message',
HttpStatus.BAD_GATEWAY
)
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import 'rxjs/add/operator/do';

@Interceptor()
export class LoggingInterceptor implements NestInterceptor {
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> {
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`),
);
return stream$.do(() => console.log(`After... ${Date.now() - now}ms`));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { mixin } from '@nestjs/common';
import { CacheInterceptor } from './cache.interceptor';

export function mixinCacheInterceptor(isCached: () => boolean) {
return mixin(class extends CacheInterceptor {
protected readonly isCached = isCached;
});
}
return mixin(
class extends CacheInterceptor {
protected readonly isCached = isCached;
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ 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 }));
intercept(
dataOrRequest,
context: ExecutionContext,
stream$: Observable<any>
): Observable<any> {
return stream$.map(data => ({ data }));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export class LoggerMiddleware implements NestMiddleware {
console.log(`[${name}] Request...`); // [ApplicationModule] Request...
next();
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { HttpException } from '@nestjs/common';
import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
import {
PipeTransform,
Pipe,
ArgumentMetadata,
HttpStatus
} from '@nestjs/common';

@Pipe()
export class ParseIntPipe implements PipeTransform<string> {
Expand All @@ -10,4 +15,4 @@ export class ParseIntPipe implements PipeTransform<string> {
}
return val;
}
}
}
37 changes: 21 additions & 16 deletions examples/01-cats-app/src/modules/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { HttpException } from '@nestjs/common';
import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
import {
PipeTransform,
Pipe,
ArgumentMetadata,
HttpStatus
} from '@nestjs/common';
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';

@Pipe()
export class ValidationPipe implements PipeTransform<any> {
async transform(value, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
}
async transform(value, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}

private toValidate(metatype): boolean {
const types = [String, Boolean, Number, Array, Object];
return !types.find((type) => metatype === type);
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
}
}
return value;
}

private toValidate(metatype): boolean {
const types = [String, Boolean, Number, Array, Object];
return !types.find(type => metatype === type);
}
}
4 changes: 2 additions & 2 deletions examples/02-gateways/src/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Module } from '@nestjs/common';
import { EventsModule } from './events/events.module';

@Module({
modules: [EventsModule],
modules: [EventsModule]
})
export class ApplicationModule {}
export class ApplicationModule {}
Loading

0 comments on commit 52e0128

Please sign in to comment.