* Used to create a custom controller for NestJS with specific path to handle TriggerDev requests. * * @param customProvider The provider to use to inject the TriggerDev client * @param path The path to use for the controller
(customProvider: InjectionToken, path: string)
| 142 | * @param path The path to use for the controller |
| 143 | */ |
| 144 | function createControllerByPath(customProvider: InjectionToken, path: string) { |
| 145 | @Controller(path) |
| 146 | class TriggerDevController { |
| 147 | constructor( |
| 148 | @InjectTriggerDevClient(customProvider) |
| 149 | private readonly client: TriggerClient |
| 150 | ) {} |
| 151 | |
| 152 | @Head() |
| 153 | @HttpCode(200) |
| 154 | public empty() {} |
| 155 | |
| 156 | @Post() |
| 157 | public handleRequestPost( |
| 158 | @Res({ passthrough: true }) res: any, |
| 159 | @Headers() headers: unknown, |
| 160 | @Body() body?: unknown |
| 161 | ): Promise<unknown> { |
| 162 | return this.handleRequest(res, "POST", headers, body); |
| 163 | } |
| 164 | |
| 165 | /** |
| 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); |
no outgoing calls
no test coverage detected
searching dependent graphs…