Skip to content

Commit cd8f6b9

Browse files
author
Eugene Yaroslavtsev
authored
fix: [nestjs integration] fastify HTTP adapter detection now works correctly for response headers (#938)
* fix: [nestjs integration] fastify HTTP adapter detection now works correctly for response headers - fixed a bug where Fastify responses were incorrectly processed as Express due to overlapping method names. - introduced type guards to differentiate between Express and Fastify response objects. - bumping @trigger.dev/nestjs patch version to 2.3.19 * fix TS type check build error
1 parent a49b659 commit cd8f6b9

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

.changeset/lemon-jobs-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/nestjs": patch
3+
---
4+
5+
fix: [nestjs integration] fastify HTTP adapter detection now works correctly for response headers

packages/nestjs/src/index.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "@nestjs/common";
1717
import { Headers as StandardHeaders, Request as StandardRequest } from "@remix-run/web-fetch";
1818
import { TriggerClient, TriggerClientOptions } from "@trigger.dev/sdk";
19-
import type { Response } from "express";
19+
import type { Response as ExpressResponse } from "express";
2020
import type { FastifyReply } from "fastify";
2121

2222
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE, ASYNC_OPTIONS_TYPE } =
@@ -192,15 +192,24 @@ function createControllerByPath(customProvider: InjectionToken, path: string) {
192192
throw new NotFoundException({ error: "Not found" });
193193
}
194194

195-
if (typeof res.status === "function") {
196-
// express
197-
(res as Response).status(response.status);
198-
(res as Response).set(response.headers);
199-
} else if (typeof res.code === "function") {
200-
// fastify
201-
(res as FastifyReply).code(response.status);
195+
/**
196+
* NestJS users mostly use either Express or Fastify, but they have
197+
* different response object APIs, so we need to figure out which one
198+
* is being used and set the status code and headers accordingly.
199+
*/
200+
if (isExpressResponse(res)) {
201+
res.status(response.status);
202+
203+
if (response.headers) {
204+
// Merges the headers, so no need to iterate over them
205+
res.set(response.headers);
206+
}
207+
} else if (isFastifyReply(res)) {
208+
res.code(response.status);
209+
202210
if (response.headers) {
203-
(res as FastifyReply).headers(response.headers);
211+
// Same merge behaviour as Express
212+
res.headers(response.headers);
204213
}
205214
} else {
206215
throw new InternalServerErrorException(
@@ -214,3 +223,23 @@ function createControllerByPath(customProvider: InjectionToken, path: string) {
214223

215224
return TriggerDevController;
216225
}
226+
227+
/**
228+
* Type guard for Express with unique checks
229+
*/
230+
function isExpressResponse(res: unknown): res is ExpressResponse {
231+
return (
232+
typeof (res as ExpressResponse)?.status === "function" &&
233+
typeof (res as ExpressResponse)?.render === "function"
234+
);
235+
}
236+
237+
/**
238+
* Type guard for Fastify with unique checks
239+
*/
240+
function isFastifyReply(res: unknown): res is FastifyReply {
241+
return (
242+
typeof (res as FastifyReply)?.code === "function" &&
243+
typeof (res as FastifyReply)?.headers === "function"
244+
);
245+
}

0 commit comments

Comments
 (0)