feat: add ApiEnvelopePayload carrier, update TransformInterceptor to handle it
This commit is contained in:
parent
659be4636c
commit
3a89cef76c
@ -13,6 +13,14 @@ export class ApiResponseMeta {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ApiEnvelopePayload<T> {
|
||||||
|
constructor(
|
||||||
|
public readonly data: T,
|
||||||
|
public readonly fromCache: boolean,
|
||||||
|
public readonly cachedAt: string | null,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
|
|
||||||
export class ApiResponse<T> {
|
export class ApiResponse<T> {
|
||||||
data: T;
|
data: T;
|
||||||
meta: ApiResponseMeta;
|
meta: ApiResponseMeta;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { ApiResponse } from '../dto/api-response.dto';
|
import { ApiEnvelopePayload, ApiResponse } from '../dto/api-response.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
|
export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
|
||||||
@ -9,6 +9,9 @@ export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T
|
|||||||
return next.handle().pipe(
|
return next.handle().pipe(
|
||||||
map((data) => {
|
map((data) => {
|
||||||
if (data instanceof ApiResponse) return data;
|
if (data instanceof ApiResponse) return data;
|
||||||
|
if (data instanceof ApiEnvelopePayload) {
|
||||||
|
return new ApiResponse(data.data, data.fromCache, data.cachedAt);
|
||||||
|
}
|
||||||
return new ApiResponse(data, false, null);
|
return new ApiResponse(data, false, null);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user