feat: add backend-mock app
This commit is contained in:
1
apps/backend-mock/src/core/decorator/index.ts
Normal file
1
apps/backend-mock/src/core/decorator/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './public';
|
||||
4
apps/backend-mock/src/core/decorator/public.ts
Normal file
4
apps/backend-mock/src/core/decorator/public.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
|
||||
export const IS_PUBLIC_KEY = 'isPublic';
|
||||
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
|
||||
40
apps/backend-mock/src/core/filter/http-exception.filter.ts
Normal file
40
apps/backend-mock/src/core/filter/http-exception.filter.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
@Catch(HttpException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const request = ctx.getRequest<Request>();
|
||||
const status =
|
||||
exception instanceof HttpException
|
||||
? exception.getStatus()
|
||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
const logFormat = `Request original url: ${request.originalUrl} Method: ${request.method} IP: ${request.ip} Status code: ${status} Response: ${exception.toString()}`;
|
||||
Logger.error(logFormat);
|
||||
|
||||
const resultMessage = exception.message as any;
|
||||
const message =
|
||||
resultMessage || `${status >= 500 ? 'Service Error' : 'Client Error'}`;
|
||||
|
||||
const errorResponse = {
|
||||
code: 1,
|
||||
error: resultMessage,
|
||||
message,
|
||||
status,
|
||||
url: request.originalUrl,
|
||||
};
|
||||
response.status(status);
|
||||
response.header('Content-Type', 'application/json; charset=utf-8');
|
||||
response.send(errorResponse);
|
||||
}
|
||||
}
|
||||
1
apps/backend-mock/src/core/filter/index.ts
Normal file
1
apps/backend-mock/src/core/filter/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './http-exception.filter';
|
||||
2
apps/backend-mock/src/core/guard/index.ts
Normal file
2
apps/backend-mock/src/core/guard/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './jwt-auth.guard';
|
||||
export * from './local-auth.guard';
|
||||
23
apps/backend-mock/src/core/guard/jwt-auth.guard.ts
Normal file
23
apps/backend-mock/src/core/guard/jwt-auth.guard.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
import { IS_PUBLIC_KEY } from '../decorator/index';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor(private reflector: Reflector) {
|
||||
super();
|
||||
}
|
||||
|
||||
canActivate(context: ExecutionContext) {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
if (isPublic) {
|
||||
return true;
|
||||
}
|
||||
return super.canActivate(context);
|
||||
}
|
||||
}
|
||||
5
apps/backend-mock/src/core/guard/local-auth.guard.ts
Normal file
5
apps/backend-mock/src/core/guard/local-auth.guard.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class LocalAuthGuard extends AuthGuard('local') {}
|
||||
1
apps/backend-mock/src/core/interceptor/index.ts
Normal file
1
apps/backend-mock/src/core/interceptor/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './transform.interceptor';
|
||||
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
Logger,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class TransformInterceptor implements NestInterceptor {
|
||||
public intercept(
|
||||
context: ExecutionContext,
|
||||
next: CallHandler,
|
||||
): Observable<any> {
|
||||
const req = context.getArgByIndex(1).req;
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
const logFormat = `
|
||||
Request original url: ${req.originalUrl}
|
||||
Method: ${req.method}
|
||||
IP: ${req.ip}
|
||||
User: ${JSON.stringify(req.user)}
|
||||
Response data: ${JSON.stringify(data)}
|
||||
`;
|
||||
Logger.debug(logFormat);
|
||||
return {
|
||||
code: 0,
|
||||
data,
|
||||
error: null,
|
||||
message: 'ok',
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
1
apps/backend-mock/src/core/pipe/index.ts
Normal file
1
apps/backend-mock/src/core/pipe/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './params.pipe';
|
||||
27
apps/backend-mock/src/core/pipe/params.pipe.ts
Normal file
27
apps/backend-mock/src/core/pipe/params.pipe.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpStatus,
|
||||
ValidationPipe,
|
||||
type ValidationPipeOptions,
|
||||
} from '@nestjs/common';
|
||||
|
||||
class ParamsValidationPipe extends ValidationPipe {
|
||||
constructor(options: ValidationPipeOptions = {}) {
|
||||
super({
|
||||
errorHttpStatusCode: HttpStatus.BAD_REQUEST,
|
||||
exceptionFactory: (errors) => {
|
||||
const message = Object.values(errors[0].constraints)[0];
|
||||
return new BadRequestException({
|
||||
message,
|
||||
status: HttpStatus.BAD_REQUEST,
|
||||
});
|
||||
},
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true,
|
||||
whitelist: true,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { ParamsValidationPipe };
|
||||
Reference in New Issue
Block a user