MCPcopy Index your code
hub / github.com/triggerdotdev/trigger.dev / createControllerByPath

Function createControllerByPath

packages/nestjs/src/index.ts:144–225  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

142 * @param path The path to use for the controller
143 */
144function 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);

Callers 2

registerMethod · 0.85
registerAsyncMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…