mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 11:55:42 +00:00
46 lines
922 B
TypeScript
46 lines
922 B
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Injectable,
|
|
NestInterceptor,
|
|
StreamableFile,
|
|
} from "@nestjs/common";
|
|
import { Readable } from "stream";
|
|
import { Observable } from "rxjs";
|
|
import { map } from "rxjs/operators";
|
|
|
|
export interface StandardResponse<T> {
|
|
success: true;
|
|
data: T;
|
|
timestamp: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class ResponseTransformInterceptor<T> implements NestInterceptor<
|
|
T,
|
|
StandardResponse<T> | T
|
|
> {
|
|
intercept(
|
|
_context: ExecutionContext,
|
|
next: CallHandler<T>,
|
|
): Observable<StandardResponse<T> | T> {
|
|
return next.handle().pipe(
|
|
map((data) => {
|
|
if (
|
|
data instanceof StreamableFile ||
|
|
data instanceof Buffer ||
|
|
data instanceof Readable
|
|
) {
|
|
return data;
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data,
|
|
timestamp: new Date().toISOString(),
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
}
|