feat: add admin module
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { map, Observable } from 'rxjs';
|
||||
import { AppResponse, PaginatedResponse } from '../responses';
|
||||
|
||||
@Injectable()
|
||||
export class ResponseInterceptor implements NestInterceptor {
|
||||
intercept(
|
||||
context: ExecutionContext,
|
||||
next: CallHandler<any>,
|
||||
): Observable<any> | Promise<Observable<any>> {
|
||||
return next.handle().pipe(
|
||||
map((response) => {
|
||||
if (response instanceof PaginatedResponse) {
|
||||
return {
|
||||
success: true,
|
||||
data: response.data,
|
||||
pagination: {
|
||||
...response.pagination,
|
||||
totalPages: Math.ceil(
|
||||
response.pagination.total / response.pagination.limit,
|
||||
),
|
||||
},
|
||||
...(response.message && {
|
||||
message: response.message,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
if (response instanceof AppResponse) {
|
||||
return {
|
||||
success: true,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
data: response.data,
|
||||
...(response.message && {
|
||||
message: response.message,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
data: response,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export class AppResponse<T> {
|
||||
constructor(
|
||||
public readonly data: T,
|
||||
public readonly message?: string,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './app-response';
|
||||
export * from './paginated-response';
|
||||
@@ -0,0 +1,13 @@
|
||||
export interface PaginationMeta {
|
||||
page: number;
|
||||
limit: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export class PaginatedResponse<T> {
|
||||
constructor(
|
||||
public readonly data: T[],
|
||||
public readonly pagination: PaginationMeta,
|
||||
public readonly message?: string,
|
||||
) {}
|
||||
}
|
||||
Reference in New Issue
Block a user