feat: introduce support for native astro integration package (#354)

* feat: introduce support for native astro integration package

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* fix: update license copyright to match other packages from trigger

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* fix: doc update to refer to the official astro package from trigger

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* fix: repo URLs update accordingly to official repo

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* fix: remove myself as author of the package to not confuse people

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* Remove package-lock because this is now in the pnpm monorepo

---------

Signed-off-by: Liran Tal <liran.tal@gmail.com>
Co-authored-by: Eric Allam <eallam@icloud.com>
This commit is contained in:
Liran Tal
2023-08-23 12:29:10 +03:00
committed by GitHub
parent 2b21f5ba8a
commit ee79ab30a9
5 changed files with 136 additions and 0 deletions
+23
View File
@@ -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.
+34
View File
@@ -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 <liran@lirantal.com>
+4
View File
@@ -0,0 +1,4 @@
import { defineConfig } from "astro/config";
// https://astro.build/config
export default defineConfig({});
+48
View File
@@ -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,
});
}
};
}
+27
View File
@@ -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"
}