- helmet 安全头 + CORS_ORIGIN 白名单 + Swagger 仅非生产 - @nestjs/throttler 全局限流(60s/100 次) - @nestjs/schedule 定时清理过期 token 与旧日志 - API 时间统一 Unix 毫秒(TransformInterceptor 递归转换) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
26 lines
1.0 KiB
TypeScript
26 lines
1.0 KiB
TypeScript
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
|
||
import { Observable } from 'rxjs';
|
||
import { map } from 'rxjs/operators';
|
||
|
||
/** 递归将 Date 转为 Unix 毫秒 number(API 时间契约:统一毫秒时间戳)。 */
|
||
function toUnixMs(value: unknown): unknown {
|
||
if (value instanceof Date) return value.getTime();
|
||
if (Array.isArray(value)) return value.map(toUnixMs);
|
||
if (value && typeof value === 'object') {
|
||
const out: Record<string, unknown> = {};
|
||
for (const [k, v] of Object.entries(value as Record<string, unknown>)) out[k] = toUnixMs(v);
|
||
return out;
|
||
}
|
||
return value;
|
||
}
|
||
|
||
/**
|
||
* 统一响应封装:{ code, message, data },成功 code = 0;时间统一 Unix 毫秒。
|
||
*/
|
||
@Injectable()
|
||
export class TransformInterceptor<T> implements NestInterceptor<T, unknown> {
|
||
intercept(_context: ExecutionContext, next: CallHandler<T>): Observable<unknown> {
|
||
return next.handle().pipe(map((data) => ({ code: 0, message: 'ok', data: toUnixMs(data ?? null) })));
|
||
}
|
||
}
|