* Forward the request to the TriggerDev client
(
res: any,
method: string,
requestHeaders: unknown,
requestBody?: unknown
)
| 166 | * Forward the request to the TriggerDev client |
| 167 | */ |
| 168 | public async handleRequest( |
| 169 | res: any, |
| 170 | method: string, |
| 171 | requestHeaders: unknown, |
| 172 | requestBody?: unknown |
| 173 | ): Promise<unknown> { |
| 174 | // try { |
| 175 | const headers = new StandardHeaders(); |
| 176 | |
| 177 | Object.entries(requestHeaders || {}).forEach(([key, value]) => { |
| 178 | headers.set(key, value as string); |
| 179 | }); |
| 180 | |
| 181 | // Create a new Request object (hardcode the url because it doesn't really matter what it is) |
| 182 | const standardRequest = new StandardRequest("https://nestjs.com/api/trigger", { |
| 183 | headers, |
| 184 | method, |
| 185 | // @ts-ignore |
| 186 | body: requestBody ? JSON.stringify(requestBody) : undefined, |
| 187 | }); |
| 188 | |
| 189 | const response = await this.client.handleRequest(standardRequest); |
| 190 | |
| 191 | if (!response) { |
| 192 | throw new NotFoundException({ error: "Not found" }); |
| 193 | } |
| 194 | |
| 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 | |
| 210 | if (response.headers) { |
| 211 | // Same merge behaviour as Express |
| 212 | res.headers(response.headers); |
| 213 | } |
| 214 | } else { |
| 215 | throw new InternalServerErrorException( |
| 216 | "Unable to indetify the framework to set the status code, are you using Express or Fastify?" |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | return response.body; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return TriggerDevController; |
no test coverage detected