fix:拦截器文件存放错误修改

This commit is contained in:
3R-喜东东 2023-11-23 13:50:25 +08:00
parent ef90d0550b
commit fe94150b2b
2 changed files with 41 additions and 40 deletions

View File

@ -0,0 +1,40 @@
import {
ClassSerializerContextOptions,
ClassSerializerInterceptor,
PlainLiteralObject,
StreamableFile,
} from '@nestjs/common';
import { PinoLoggerOptions } from 'fastify/types/logger';
import { isArray, isNil, isObject } from 'lodash';
export class AppIntercepter extends ClassSerializerInterceptor {
serialize(
response: PlainLiteralObject | PlainLiteralObject[],
options: ClassSerializerContextOptions,
): PlainLiteralObject | PlainLiteralObject[] {
if ((!isObject(response) && !isArray(response)) || response instanceof StreamableFile) {
return response;
}
// 数组处理 - 如果是数组则对数组每一项元素序列化
if (isArray(response)) {
return (response as PlainLiteralObject[]).map((item) =>
!isObject(item) ? item : this.transformToPlain(item, options),
);
}
// 分页处理 - 对items中的每一项进行序列化
if ('meta' in response && 'items' in response) {
const items = !isNil(response.items) && isArray(response.items) ? response.items : [];
return {
...response,
items: (items as PinoLoggerOptions[]).map((item) =>
!isObject(item) ? item : this.transformToPlain(item, options),
),
};
}
return this.transformToPlain(response, options);
}
}

View File

@ -1,40 +1 @@
import { export * from './app.interceptor';
ClassSerializerContextOptions,
ClassSerializerInterceptor,
PlainLiteralObject,
StreamableFile,
} from '@nestjs/common';
import { PinoLoggerOptions } from 'fastify/types/logger';
import { isArray, isNil, isObject } from 'lodash';
export class AppIntercepter extends ClassSerializerInterceptor {
serialize(
response: PlainLiteralObject | PlainLiteralObject[],
options: ClassSerializerContextOptions,
): PlainLiteralObject | PlainLiteralObject[] {
if ((!isObject(response) && !isArray(response)) || response instanceof StreamableFile) {
return response;
}
// 数组处理 - 如果是数组则对数组每一项元素序列化
if (isArray(response)) {
return (response as PlainLiteralObject[]).map((item) =>
!isObject(item) ? item : this.transformToPlain(item, options),
);
}
// 分页处理 - 对items中的每一项进行序列化
if ('meta' in response && 'items' in response) {
const items = !isNil(response.items) && isArray(response.items) ? response.items : [];
return {
...response,
items: (items as PinoLoggerOptions[]).map((item) =>
!isObject(item) ? item : this.transformToPlain(item, options),
),
};
}
return this.transformToPlain(response, options);
}
}