diff --git a/packages/astro/LICENSE b/packages/astro/LICENSE new file mode 100644 index 000000000..d2f5322a9 --- /dev/null +++ b/packages/astro/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2023 Trigger.dev + +Permission is hereby granted, free of +charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice +(including the next paragraph) shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/packages/astro/README.md b/packages/astro/README.md new file mode 100644 index 000000000..fdce5b0f9 --- /dev/null +++ b/packages/astro/README.md @@ -0,0 +1,34 @@ +# Trigger.dev native integration add-on for Astro + +This Astro integration project provides a `createAstroRoute` function that can be used to create an API endpoint route in your Astro project for integration with the hosted Trigger.dev platform. + +## Usage + +1. Install the package: + +```bash +npm install --save @trigger.dev/astro +``` + +Note: yes, right now this package isn't published to the npm registry, so you'll need to install it from my GitHub repository if you want to experiment with it. Hopefully this soon graduates into an official npm package from the Trigger.dev team 🙏🏼 + +2. Import the package in a `src/pages/api/trigger.js` file and define the route endpoint as follows: + +```js +import { createAstroRoute } from "triggerdev-astro-integration"; +import { client } from "../../../trigger.js"; + +export const post = createAstroRoute(client); +``` + +## More information + +See the [Trigger.dev Astro example project repository](https://github.com/lirantal/trigger.dev-astro-example) for a working example of this integration. + +## License + +MIT + +## Author + +(c) Liran Tal \ No newline at end of file diff --git a/packages/astro/astro.config.mjs b/packages/astro/astro.config.mjs new file mode 100644 index 000000000..484025096 --- /dev/null +++ b/packages/astro/astro.config.mjs @@ -0,0 +1,4 @@ +import { defineConfig } from "astro/config"; + +// https://astro.build/config +export default defineConfig({}); diff --git a/packages/astro/main.js b/packages/astro/main.js new file mode 100644 index 000000000..00ed8c6ea --- /dev/null +++ b/packages/astro/main.js @@ -0,0 +1,48 @@ +export function createAstroRoute(client) { + return async function astroRoute(ctx) { + if (ctx.request.method === "HEAD") { + return new Response(null, { status: 200 }); + } + + try { + // Prepare the request to be a fetch-compatible Request object: + const requestHeaders = ctx.request.headers; + const requestMethod = ctx.request.method; + const responseHeaders = Object.create(null); + + for (const [headerName, headerValue] of requestHeaders.entries()) { + responseHeaders[headerName] = headerValue; + } + + // Create a new Request object to be passed to the TriggerClient + // where we pass the clone the incoming request metadata such as + // headers, method, body. + const request = new Request("https://express.js/api/trigger", { + headers: responseHeaders, + method: requestMethod, + body: ctx.request.body ? ctx.request.body : ctx.request, + duplex: "half", + }); + + // This handshake handler knows how to authenticate requests, + // call the run() function of the job, and so on + const response = await client.handleRequest(request); + + if (!response) { + return new Response(JSON.stringify({ error: "Not found" }), { + status: 404, + }); + } + + // Optionally can do something with the job's finished + // execution's response body + return new Response(JSON.stringify(response.body), { + status: response.status, + }); + } catch (err) { + return new Response(JSON.stringify({ error: "Internal server error" }), { + status: 500, + }); + } + }; +} diff --git a/packages/astro/package.json b/packages/astro/package.json new file mode 100644 index 000000000..3c710d64d --- /dev/null +++ b/packages/astro/package.json @@ -0,0 +1,27 @@ +{ + "name": "@trigger.dev/astro", + "description": "An Astro-native integration for Trigger.dev background jobs platform", + "version": "1.0.0", + "type": "module", + "main": "main.js", + "scripts": {}, + "peerDependencies": { + "@trigger.dev/sdk": "workspace:^2.0.9", + "astro": "^2.10.7" + }, + "engines": { + "node": ">=18.0.0" + }, + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/triggerdev/trigger.dev.git" + }, + "bugs": { + "url": "https://github.com/triggerdev/trigger.dev/issues" + }, + "homepage": "https://github.com/triggerdev/trigger.dev#readme" +}