Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/published-data/interceptors/fast-response.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from "@nestjs/common";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { Response } from "express";

@Injectable()
export class FastResponseInterceptor implements NestInterceptor {
constructor(
private readonly headers = { "Content-Type": "application/json" },
) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const res = context.switchToHttp().getResponse<Response>();

return next.handle().pipe(
map((data) => {
if (res.headersSent) return data;

res.set(this.headers);
res.send(JSON.stringify(data));
return null;
}),
);
}
}
9 changes: 7 additions & 2 deletions src/published-data/published-data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ import { PublishedDataService } from "./published-data.service";
import { PublishedData } from "./schemas/published-data.schema";
import { V3_FILTER_PIPE } from "./pipes/filter.pipe";
import { Filter } from "src/datasets/decorators/filter.decorator";
import { FastResponseInterceptor } from "./interceptors/fast-response.interceptor";

@ApiBearerAuth()
@ApiTags("published data")
@Controller("publisheddata")
@UseInterceptors(ClassSerializerInterceptor)
export class PublishedDataController {
constructor(
private readonly attachmentsService: AttachmentsService,
Expand Down Expand Up @@ -239,6 +239,7 @@ export class PublishedDataController {
description:
"This endpoint is deprecated and v4 endpoints should be used in the future",
})
@UseInterceptors(new FastResponseInterceptor(), ClassSerializerInterceptor)
@SerializeOptions({
type: PublishedDataObsoleteDto,
excludeExtraneousValues: true,
Expand Down Expand Up @@ -276,6 +277,7 @@ export class PublishedDataController {
isArray: true,
description: "Results with a published documents array",
})
@UseInterceptors(new FastResponseInterceptor(), ClassSerializerInterceptor)
@SerializeOptions({
type: PublishedDataObsoleteDto,
excludeExtraneousValues: true,
Expand Down Expand Up @@ -408,11 +410,12 @@ export class PublishedDataController {
status: HttpStatus.NOT_FOUND,
description: "PublishedData not found",
})
@Get("/:id")
@UseInterceptors(new FastResponseInterceptor(), ClassSerializerInterceptor)
@SerializeOptions({
type: PublishedDataObsoleteDto,
excludeExtraneousValues: true,
})
@Get("/:id")
async findOne(
@Param(new IdToDoiPipe(), RegisteredPipe)
filter: {
Expand Down Expand Up @@ -449,6 +452,7 @@ export class PublishedDataController {
isArray: false,
description: "Return updated published data",
})
@UseInterceptors(new FastResponseInterceptor(), ClassSerializerInterceptor)
@SerializeOptions({
type: PublishedDataObsoleteDto,
excludeExtraneousValues: true,
Expand Down Expand Up @@ -485,6 +489,7 @@ export class PublishedDataController {
isArray: false,
description: "Return removed published data",
})
@UseInterceptors(new FastResponseInterceptor(), ClassSerializerInterceptor)
@SerializeOptions({
type: PublishedDataObsoleteDto,
excludeExtraneousValues: true,
Expand Down
9 changes: 9 additions & 0 deletions src/published-data/published-data.v4.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Query,
Req,
UseGuards,
UseInterceptors,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import {
Expand Down Expand Up @@ -58,6 +59,7 @@ import {
} from "./schemas/published-data.schema";
import { V4_FILTER_PIPE } from "./pipes/filter.pipe";
import { ILimitsFilter } from "src/common/interfaces/common.interface";
import { FastResponseInterceptor } from "./interceptors/fast-response.interceptor";

@ApiBearerAuth()
@ApiTags("published data v4")
Expand Down Expand Up @@ -116,6 +118,7 @@ export class PublishedDataV4Controller {
isArray: true,
description: "Results with a published documents array",
})
@UseInterceptors(new FastResponseInterceptor())
async findAll(
@Req() request: Request,
@Query(...V4_FILTER_PIPE, RegisteredFilterPipe)
Expand Down Expand Up @@ -307,6 +310,7 @@ export class PublishedDataV4Controller {
status: HttpStatus.NOT_FOUND,
description: "PublishedData not found",
})
@UseInterceptors(new FastResponseInterceptor())
@Get("/:id")
async findOne(
@Req() request: Request,
Expand Down Expand Up @@ -334,6 +338,7 @@ export class PublishedDataV4Controller {
isArray: false,
description: "Return updated published data with id specified",
})
@UseInterceptors(new FastResponseInterceptor())
@Patch("/:id")
async update(
@Req() request: Request,
Expand Down Expand Up @@ -389,6 +394,7 @@ export class PublishedDataV4Controller {
isArray: false,
description: "Return published data with id specified after publishing",
})
@UseInterceptors(new FastResponseInterceptor())
@Post("/:id/publish")
async publish(
@Req() request: Request,
Expand Down Expand Up @@ -437,6 +443,7 @@ export class PublishedDataV4Controller {
isArray: false,
description: "Return amended data with id specified",
})
@UseInterceptors(new FastResponseInterceptor())
@Post("/:id/amend")
async amend(
@Req() request: Request,
Expand Down Expand Up @@ -504,6 +511,7 @@ export class PublishedDataV4Controller {
@CheckPolicies("publisheddata", (ability: AppAbility) =>
ability.can(Action.Delete, PublishedData),
)
@UseInterceptors(new FastResponseInterceptor())
@Delete("/:id")
async remove(
@Req() request: Request,
Expand Down Expand Up @@ -547,6 +555,7 @@ export class PublishedDataV4Controller {
@CheckPolicies("publisheddata", (ability: AppAbility) =>
ability.can(Action.Update, PublishedData),
)
@UseInterceptors(new FastResponseInterceptor())
@Post("/:id/register")
async register(
@Req() request: Request,
Expand Down
Loading