feat: Basic setup with auth
This commit is contained in:
8
common/exceptions/custom-exceptions.ts
Normal file
8
common/exceptions/custom-exceptions.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
|
||||
// Base exception
|
||||
export class BaseException extends HttpException {
|
||||
protected constructor(code: string, message: string, status: HttpStatus) {
|
||||
super({ code, message }, status);
|
||||
}
|
||||
}
|
||||
32
common/exceptions/exception-filter.ts
Normal file
32
common/exceptions/exception-filter.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
@Catch(HttpException) // What exception to catch
|
||||
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
|
||||
constructor(private readonly logger: Logger) {}
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const request: Request = ctx.getRequest();
|
||||
const response: Response = ctx.getResponse();
|
||||
const status = exception.getStatus();
|
||||
|
||||
if (status >= 500) {
|
||||
this.logger.warn({
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
message: exception.message,
|
||||
});
|
||||
}
|
||||
|
||||
response.status(status).json({
|
||||
message: exception.message,
|
||||
statusCode: status,
|
||||
});
|
||||
}
|
||||
}
|
||||
1
common/exceptions/index.ts
Normal file
1
common/exceptions/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './custom-exceptions';
|
||||
1
common/http/index.ts
Normal file
1
common/http/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './response';
|
||||
25
common/http/response.ts
Normal file
25
common/http/response.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export class MessageResponse {
|
||||
readonly success: boolean;
|
||||
readonly message: string;
|
||||
|
||||
constructor(message?: string) {
|
||||
this.success = true;
|
||||
this.message = message ?? 'Success';
|
||||
}
|
||||
}
|
||||
|
||||
export class DataResponse<T> extends MessageResponse {
|
||||
readonly data: T;
|
||||
|
||||
constructor(data: T, message?: string) {
|
||||
super(message);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
// Skipped
|
||||
export class GlobalErrorResponseDTO {
|
||||
success: boolean;
|
||||
message: string;
|
||||
statusCode: number;
|
||||
}
|
||||
0
common/interceptors/index.ts
Normal file
0
common/interceptors/index.ts
Normal file
72
common/interceptors/response.interceptor.ts
Normal file
72
common/interceptors/response.interceptor.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import {
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
ExecutionContext,
|
||||
CallHandler,
|
||||
} from '@nestjs/common';
|
||||
import { DataResponse, MessageResponse } from 'common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class ResponseInterceptor<T> implements NestInterceptor<T, any> {
|
||||
intercept(_: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
if (data instanceof MessageResponse) return data;
|
||||
else if (data instanceof DataResponse) return data;
|
||||
else if (typeof data === 'string') return new MessageResponse(data);
|
||||
return new DataResponse(data);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/* NOTE: How to access request
|
||||
*
|
||||
@Injectable()
|
||||
export class ResponseInterceptor<T> implements NestInterceptor<T, any> {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
req.userId = 'ram';
|
||||
return next.handle();
|
||||
// return next.handle().pipe(
|
||||
// map((data) => {
|
||||
// if (data instanceof MessageResponse) return data;
|
||||
// else if (data instanceof DataResponse) return data;
|
||||
// else if (typeof data === 'string') return new MessageResponse(data);
|
||||
// return new DataResponse(data);
|
||||
// }),
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
REQUEST
|
||||
↓
|
||||
Guards
|
||||
↓
|
||||
Interceptors (before)
|
||||
↓
|
||||
Pipes
|
||||
↓
|
||||
Controller method
|
||||
↓
|
||||
Interceptors (after) ← YOU ARE HERE
|
||||
↓
|
||||
Response
|
||||
|
||||
|
||||
3️⃣ What is next.handle() really?
|
||||
next.handle(): Observable<T>
|
||||
|
||||
|
||||
This is:
|
||||
|
||||
An RxJS stream of whatever the controller returns
|
||||
|
||||
Not the request
|
||||
|
||||
Not the response object
|
||||
|
||||
Just the return value
|
||||
* */
|
||||
Reference in New Issue
Block a user