cr
This commit is contained in:
@@ -3,16 +3,16 @@ import {
|
||||
NestInterceptor,
|
||||
ExecutionContext,
|
||||
CallHandler,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiResponse, createSuccessResponse } from '../types/api-response.type';
|
||||
} from "@nestjs/common";
|
||||
import { Observable } from "rxjs";
|
||||
import { map } from "rxjs/operators";
|
||||
import { ApiResponse, createSuccessResponse } from "../types/api-response.type";
|
||||
|
||||
/**
|
||||
* Response interceptor that wraps all successful responses
|
||||
* in the standard ApiResponse format
|
||||
*/
|
||||
import { I18nService, I18nContext } from 'nestjs-i18n';
|
||||
import { I18nService, I18nContext } from "nestjs-i18n";
|
||||
|
||||
@Injectable()
|
||||
export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
@@ -34,17 +34,17 @@ export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
let lang = i18nContext?.lang;
|
||||
|
||||
if (!lang) {
|
||||
const acceptLanguage = request.headers['accept-language'];
|
||||
const xLang = request.headers['x-lang'];
|
||||
const acceptLanguage = request.headers["accept-language"];
|
||||
const xLang = request.headers["x-lang"];
|
||||
|
||||
if (xLang) {
|
||||
lang = Array.isArray(xLang) ? xLang[0] : xLang;
|
||||
} else if (acceptLanguage) {
|
||||
lang = acceptLanguage.split(',')[0].split(';')[0].split('-')[0];
|
||||
lang = acceptLanguage.split(",")[0].split(";")[0].split("-")[0];
|
||||
}
|
||||
}
|
||||
|
||||
lang = lang || 'en';
|
||||
lang = lang || "en";
|
||||
|
||||
// If data is already an ApiResponse, we should still translate its 'data' property
|
||||
// But first let's just do it directly on 'data' below before returning
|
||||
@@ -68,7 +68,7 @@ export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
}
|
||||
}
|
||||
|
||||
const message = this.i18n.translate('common.success', {
|
||||
const message = this.i18n.translate("common.success", {
|
||||
lang,
|
||||
});
|
||||
|
||||
@@ -79,7 +79,7 @@ export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
}
|
||||
|
||||
private translateReasons(data: any, lang: string) {
|
||||
if (!data || typeof data !== 'object') {
|
||||
if (!data || typeof data !== "object") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -91,44 +91,44 @@ export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
Object.keys(data).forEach((key) => {
|
||||
const val = data[key];
|
||||
if (
|
||||
(key === 'reasons' ||
|
||||
key === 'decision_reasons' ||
|
||||
key === 'reasoning_factors') &&
|
||||
(key === "reasons" ||
|
||||
key === "decision_reasons" ||
|
||||
key === "reasoning_factors") &&
|
||||
Array.isArray(val)
|
||||
) {
|
||||
data[key] = val.map((r: any) => {
|
||||
if (typeof r !== 'string') return r;
|
||||
if (typeof r !== "string") return r;
|
||||
const translationKey = `predictions.reasons.${r}`;
|
||||
const translated = this.i18n.translate(translationKey, {
|
||||
lang,
|
||||
});
|
||||
return translated === translationKey ? r : translated;
|
||||
});
|
||||
} else if (key === 'reason' && typeof val === 'string') {
|
||||
} else if (key === "reason" && typeof val === "string") {
|
||||
const translationKey = `predictions.reasons.${val}`;
|
||||
const translated = this.i18n.translate(translationKey, {
|
||||
lang,
|
||||
});
|
||||
data[key] = translated === translationKey ? val : translated;
|
||||
} else if (key === 'flags' && Array.isArray(val)) {
|
||||
} else if (key === "flags" && Array.isArray(val)) {
|
||||
data[key] = val.map((r: any) => {
|
||||
if (typeof r !== 'string') return r;
|
||||
if (typeof r !== "string") return r;
|
||||
const translationKey = `predictions.flags.${r}`;
|
||||
const translated = this.i18n.translate(translationKey, {
|
||||
lang,
|
||||
});
|
||||
return translated === translationKey ? r : translated;
|
||||
});
|
||||
} else if (key === 'warnings' && Array.isArray(val)) {
|
||||
} else if (key === "warnings" && Array.isArray(val)) {
|
||||
data[key] = val.map((r: any) => {
|
||||
if (typeof r !== 'string') return r;
|
||||
if (typeof r !== "string") return r;
|
||||
const translationKey = `predictions.warnings.${r}`;
|
||||
const translated = this.i18n.translate(translationKey, {
|
||||
lang,
|
||||
});
|
||||
return translated === translationKey ? r : translated;
|
||||
});
|
||||
} else if (typeof val === 'object' && val !== null) {
|
||||
} else if (typeof val === "object" && val !== null) {
|
||||
this.translateReasons(val, lang);
|
||||
}
|
||||
});
|
||||
@@ -137,11 +137,11 @@ export class ResponseInterceptor<T> implements NestInterceptor<
|
||||
private isApiResponse(data: unknown): boolean {
|
||||
return (
|
||||
data !== null &&
|
||||
typeof data === 'object' &&
|
||||
'success' in data &&
|
||||
'status' in data &&
|
||||
'message' in data &&
|
||||
'data' in data
|
||||
typeof data === "object" &&
|
||||
"success" in data &&
|
||||
"status" in data &&
|
||||
"message" in data &&
|
||||
"data" in data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user