chore: add email service and password service
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from "@nestjs/common";
|
||||
import { FastifyRequest } from "fastify";
|
||||
import { Observable, map } from "rxjs";
|
||||
|
||||
import { IPageFormat } from "../../common/interfaces/IPagination";
|
||||
|
||||
@Injectable()
|
||||
export class PaginationInterceptor implements NestInterceptor {
|
||||
private readonly logger = new Logger(PaginationInterceptor.name);
|
||||
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
const request = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
const query = request.query as any;
|
||||
|
||||
const page = parseInt(query.page as string, 10) || 1;
|
||||
const limit = parseInt(query.limit as string, 10) || 10;
|
||||
|
||||
const className = context.getClass().name;
|
||||
const handlerName = context.getHandler().name;
|
||||
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
if (data.paginate || data.count) {
|
||||
const { count, ...response } = data;
|
||||
this.logger.log(`paginate response from ${className}.${handlerName}`);
|
||||
const pager = this.formatPage(page, limit, count, request);
|
||||
|
||||
return {
|
||||
pager,
|
||||
...response,
|
||||
};
|
||||
}
|
||||
|
||||
return data;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
private formatPage(page: number, limit: number, totalItems: number, request: FastifyRequest): IPageFormat {
|
||||
const totalPages = Math.ceil(totalItems / limit);
|
||||
const prevPage = page === 1 ? false : page - 1;
|
||||
const nextPage = page >= totalPages ? false : page + 1;
|
||||
|
||||
const formatLink = (pageNum: number | boolean): string | boolean => {
|
||||
if (!pageNum) return false;
|
||||
const protocol = request.protocol;
|
||||
const hostname = request.hostname;
|
||||
const originalUrl = request.url;
|
||||
return `${protocol}://${hostname}${originalUrl.split("?")[0]}?page=${pageNum}&limit=${limit}`;
|
||||
};
|
||||
|
||||
return {
|
||||
page,
|
||||
limit,
|
||||
totalItems,
|
||||
totalPages,
|
||||
prevPage: formatLink(prevPage),
|
||||
nextPage: formatLink(nextPage),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
|
||||
import { Response } from "express";
|
||||
import { Observable } from "rxjs";
|
||||
import { map } from "rxjs/operators";
|
||||
|
||||
@Injectable()
|
||||
export class ResponseInterceptor implements NestInterceptor {
|
||||
//
|
||||
intercept(context: ExecutionContext, next: CallHandler<Record<string, unknown>>): Observable<unknown> {
|
||||
const ctx = context.switchToHttp().getResponse<Response>();
|
||||
const statusCode = ctx.statusCode;
|
||||
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
if (data && data.data !== undefined) {
|
||||
return {
|
||||
statusCode,
|
||||
success: true,
|
||||
data: data.data,
|
||||
};
|
||||
}
|
||||
return {
|
||||
statusCode,
|
||||
success: true,
|
||||
data,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// import { Injectable, Logger, NestMiddleware } from "@nestjs/common";
|
||||
// import { FastifyReply, FastifyRequest } from "fastify";
|
||||
|
||||
// @Injectable()
|
||||
// export class HTTPLoggerMiddleware implements NestMiddleware {
|
||||
// private readonly logger = new Logger(HTTPLoggerMiddleware.name);
|
||||
// // use(req: any, res: any, next: (error?: Error | any) => void) {}
|
||||
|
||||
// use(req: FastifyRequest, res: FastifyReply, next: NextFunction) {
|
||||
// const { ip, method, baseUrl } = req;
|
||||
// const userAgent = req.get("user-agent") || "";
|
||||
// const startAt = process.hrtime();
|
||||
|
||||
// res.on("finish", () => {
|
||||
// const { statusCode } = res;
|
||||
// const contentLength = res.get("content-length") || 0;
|
||||
// const dif = process.hrtime(startAt);
|
||||
// const responseTime = dif[0] * 1e3 + dif[1] * 1e-6;
|
||||
// this.logger.log(`${method} - ${baseUrl} - ${statusCode} - ${contentLength} - ${userAgent} - ${ip} - ${responseTime.toFixed(2)}ms`);
|
||||
// });
|
||||
|
||||
// next();
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user