(ctx)
| 3 | |
| 4 | export function createAstroRoute(client: TriggerClient) { |
| 5 | const POST: APIRoute = async (ctx) => { |
| 6 | if (ctx.request.method === "HEAD") { |
| 7 | return new Response(null, { status: 200 }); |
| 8 | } |
| 9 | |
| 10 | try { |
| 11 | // Prepare the request to be a fetch-compatible Request object: |
| 12 | const requestHeaders = ctx.request.headers; |
| 13 | const requestMethod = ctx.request.method; |
| 14 | const responseHeaders: Record<string, string> = {}; |
| 15 | |
| 16 | for (const [headerName, headerValue] of requestHeaders.entries()) { |
| 17 | responseHeaders[headerName] = headerValue; |
| 18 | } |
| 19 | |
| 20 | // Create a new Request object to be passed to the TriggerClient |
| 21 | // where we pass the clone the incoming request metadata such as |
| 22 | // headers, method, body. |
| 23 | const request = new Request("https://express.js/api/trigger", { |
| 24 | headers: responseHeaders, |
| 25 | method: requestMethod, |
| 26 | body: ctx.request.body, |
| 27 | // @ts-ignore |
| 28 | duplex: "half", |
| 29 | }); |
| 30 | |
| 31 | // This handshake handler knows how to authenticate requests, |
| 32 | // call the run() function of the job, and so on |
| 33 | const response = await client.handleRequest(request); |
| 34 | |
| 35 | if (!response) { |
| 36 | return new Response(JSON.stringify({ error: "Not found" }), { |
| 37 | status: 404, |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | // Optionally can do something with the job's finished |
| 42 | // execution's response body |
| 43 | return new Response(JSON.stringify(response.body), { |
| 44 | status: response.status, |
| 45 | headers: response.headers, |
| 46 | }); |
| 47 | } catch (err) { |
| 48 | return new Response(JSON.stringify({ error: "Internal server error" }), { |
| 49 | status: 500, |
| 50 | }); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | return { |
| 55 | POST, |
nothing calls this directly
no test coverage detected
searching dependent graphs…