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

Function createMiddleware

packages/express/src/index.ts:54–92  ·  view source on GitHub ↗
(client: TriggerClient, path: string = "/api/trigger")

Source from the content-addressed store, hash-verified

52 * ```
53 */
54export function createMiddleware(client: TriggerClient, path: string = "/api/trigger") {
55 return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
56 if (req.path !== path) {
57 next();
58
59 return;
60 }
61
62 if (req.method === "HEAD") {
63 res.sendStatus(200);
64
65 return;
66 }
67
68 try {
69 const request = convertToStandardRequest(req);
70
71 const response = await client.handleRequest(request);
72
73 if (!response) {
74 res.status(404).json({ error: "Not found" });
75
76 return;
77 }
78
79 if (response.headers) {
80 for (const [key, value] of Object.entries(response.headers)) {
81 if (typeof value === "string") {
82 res.setHeader(key, value);
83 }
84 }
85 }
86
87 res.status(response.status).json(response.body);
88 } catch (error) {
89 next(error);
90 }
91 };
92}
93
94function convertToStandardRequest(req: express.Request): Request {
95 const { headers: nextHeaders, method } = req;

Callers 1

createExpressServerFunction · 0.70

Calls 3

jsonMethod · 0.80
convertToStandardRequestFunction · 0.70
handleRequestMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…