Skip to content

Commit

Permalink
refactor: use NestifyRequest instead of Request object from express
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Nov 20, 2023
1 parent 252bfa1 commit e153113
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/common/@types/typings/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare global {
export interface Request {
user?: UserEntity
realIp?: string
idx?: string
body: Record<string, any>
ip: string
ips: string[]
Expand Down
4 changes: 2 additions & 2 deletions src/common/database/mikro-orm.encrypted.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { EntityProperty, Platform } from "@mikro-orm/core";
import { Type, ValidationError } from "@mikro-orm/core";
import { decrypt, encrypt } from "helper-fns";
import { decrypt, encrypt, isString } from "helper-fns";

export class EncryptedType extends Type {
private readonly encKey = process.env.ENC_KEY;
private readonly encIV = process.env.ENC_IV;

convertToDatabaseValue(value: string, _platform: Platform): string {
if (value && !(typeof value.valueOf() === "string"))
if (value && !(isString(value.valueOf())))
throw ValidationError.invalidType(EncryptedType, value, "JS");

return encrypt({ text: value.toString(), config: { key: this.encKey, iv: this.encIV } });
Expand Down
10 changes: 5 additions & 5 deletions src/common/decorators/validation/transform.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transform } from "class-transformer";
import DOMPurify from "isomorphic-dompurify";
import { isArray } from "helper-fns";
import { isArray, isString } from "helper-fns";

/**
* It trims the value of a property and replaces multiple spaces with a single space
Expand Down Expand Up @@ -51,17 +51,17 @@ export function Sanitize(): PropertyDecorator {
({ value }) => {
if (isArray(value)) {
return value.map((v) => {
if (typeof v === "string")
if (isString(v))
return DOMPurify.sanitize(v);

return v;
}) as string[];
})
}

if (typeof value === "string")
if (isString(value))
return DOMPurify.sanitize(value);

return value as string;
return value;
},
{ toClassOnly: true },
);
Expand Down
3 changes: 1 addition & 2 deletions src/common/filters/all-exception-translatable.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { I18nContext } from "nestjs-i18n";
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
async catch(exception: HttpException, host: ArgumentsHost) {
const context = host.switchToHttp();
const response = context.getResponse<NestifyResponse>();
const response = host.switchToHttp().getResponse<NestifyResponse>();
const statusCode = exception.getStatus();

let message = exception.getResponse() as {
Expand Down
3 changes: 1 addition & 2 deletions src/common/filters/query-failed.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { Catch, HttpStatus } from "@nestjs/common";
@Catch(ServerException)
export class QueryFailedFilter implements ExceptionFilter {
catch(exception: DriverException, host: ArgumentsHost) {
const context = host.switchToHttp();
const response = context.getResponse<NestifyResponse>();
const response = host.switchToHttp().getResponse<NestifyResponse>();

const status
= exception.name && exception.name.startsWith("UQ")
Expand Down
4 changes: 2 additions & 2 deletions src/common/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class AuthGuard implements CanActivate {
constructor(private readonly jwt: JwtService) {}

canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const request = context.switchToHttp().getRequest<NestifyRequest>();

const token: string = request.headers.authorization;
const token = request.headers.authorization;

if (!token)
throw new UnauthorizedException(translate("exception.apiUnauthorizedResponse"));
Expand Down
4 changes: 2 additions & 2 deletions src/common/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { IGNORE_CACHING_META } from "@common/constant";
@Injectable()
export class HttpCacheInterceptor extends CacheInterceptor {
protected isRequestCacheable(context: ExecutionContext): boolean {
const http = context.switchToHttp();
const request = http.getRequest();

const request = context.switchToHttp().getRequest<NestifyRequest>();

const ignoreCaching: boolean = this.reflector.get(
IGNORE_CACHING_META,
Expand Down
4 changes: 2 additions & 2 deletions src/common/interceptors/logger.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class LoggingInterceptor implements NestInterceptor {
* @returns Observable of unknown type
*/
intercept(context: ExecutionContext, call$: CallHandler): Observable<unknown> {
const request: Request = context.switchToHttp().getRequest();
const request = context.switchToHttp().getRequest<NestifyRequest>();
const { method, url, body, headers } = request;
const logContext = `${this.userPrefix}${this.ctxPrefix} - ${method} - ${url}`;
const message = `Request - ${method} - ${url}`;
Expand Down Expand Up @@ -92,7 +92,7 @@ export class LoggingInterceptor implements NestInterceptor {
* @param context details about the current request
*/
private logError(error: Error, context: ExecutionContext): void {
const request: Request = context.switchToHttp().getRequest<Request>();
const request = context.switchToHttp().getRequest<NestifyRequest>();
const { method, url, body } = request;

if (error instanceof HttpException) {
Expand Down

0 comments on commit e153113

Please sign in to comment.