forked from Yara724/api
32 lines
904 B
TypeScript
32 lines
904 B
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Injectable,
|
|
NestInterceptor,
|
|
} from "@nestjs/common";
|
|
import { Observable } from "rxjs";
|
|
import { normalizeUnicodeDigitsDeep } from "src/utils/unicode-digits";
|
|
|
|
/**
|
|
* Normalizes Unicode decimal digits in JSON request bodies to ASCII before
|
|
* validation and handlers run (Persian/Arabic-Indic → 0-9).
|
|
*/
|
|
@Injectable()
|
|
export class UnicodeDigitsNormalizeInterceptor implements NestInterceptor {
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
const req = context.switchToHttp().getRequest();
|
|
const contentType = String(req.headers?.["content-type"] ?? "");
|
|
|
|
if (
|
|
req.body &&
|
|
typeof req.body === "object" &&
|
|
!Buffer.isBuffer(req.body) &&
|
|
!contentType.includes("multipart/form-data")
|
|
) {
|
|
req.body = normalizeUnicodeDigitsDeep(req.body);
|
|
}
|
|
|
|
return next.handle();
|
|
}
|
|
}
|