Skip to content

Commit ee79ab3

Browse files
lirantalericallam
andauthored
feat: introduce support for native astro integration package (#354)
* feat: introduce support for native astro integration package Signed-off-by: Liran Tal <[email protected]> * fix: update license copyright to match other packages from trigger Signed-off-by: Liran Tal <[email protected]> * fix: doc update to refer to the official astro package from trigger Signed-off-by: Liran Tal <[email protected]> * fix: repo URLs update accordingly to official repo Signed-off-by: Liran Tal <[email protected]> * fix: remove myself as author of the package to not confuse people Signed-off-by: Liran Tal <[email protected]> * Remove package-lock because this is now in the pnpm monorepo --------- Signed-off-by: Liran Tal <[email protected]> Co-authored-by: Eric Allam <[email protected]>
1 parent 2b21f5b commit ee79ab3

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

packages/astro/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Trigger.dev
4+
5+
Permission is hereby granted, free of
6+
charge, to any person obtaining a copy of this software and associated
7+
documentation files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use, copy, modify, merge,
9+
publish, distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to the
11+
following conditions:
12+
13+
The above copyright notice and this permission notice
14+
(including the next paragraph) shall be included in all copies or substantial
15+
portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.

packages/astro/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Trigger.dev native integration add-on for Astro
2+
3+
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.
4+
5+
## Usage
6+
7+
1. Install the package:
8+
9+
```bash
10+
npm install --save @trigger.dev/astro
11+
```
12+
13+
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 🙏🏼
14+
15+
2. Import the package in a `src/pages/api/trigger.js` file and define the route endpoint as follows:
16+
17+
```js
18+
import { createAstroRoute } from "triggerdev-astro-integration";
19+
import { client } from "../../../trigger.js";
20+
21+
export const post = createAstroRoute(client);
22+
```
23+
24+
## More information
25+
26+
See the [Trigger.dev Astro example project repository](https://github.com/lirantal/trigger.dev-astro-example) for a working example of this integration.
27+
28+
## License
29+
30+
MIT
31+
32+
## Author
33+
34+
(c) Liran Tal <[email protected]>

packages/astro/astro.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { defineConfig } from "astro/config";
2+
3+
// https://astro.build/config
4+
export default defineConfig({});

packages/astro/main.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export function createAstroRoute(client) {
2+
return async function astroRoute(ctx) {
3+
if (ctx.request.method === "HEAD") {
4+
return new Response(null, { status: 200 });
5+
}
6+
7+
try {
8+
// Prepare the request to be a fetch-compatible Request object:
9+
const requestHeaders = ctx.request.headers;
10+
const requestMethod = ctx.request.method;
11+
const responseHeaders = Object.create(null);
12+
13+
for (const [headerName, headerValue] of requestHeaders.entries()) {
14+
responseHeaders[headerName] = headerValue;
15+
}
16+
17+
// Create a new Request object to be passed to the TriggerClient
18+
// where we pass the clone the incoming request metadata such as
19+
// headers, method, body.
20+
const request = new Request("https://express.js/api/trigger", {
21+
headers: responseHeaders,
22+
method: requestMethod,
23+
body: ctx.request.body ? ctx.request.body : ctx.request,
24+
duplex: "half",
25+
});
26+
27+
// This handshake handler knows how to authenticate requests,
28+
// call the run() function of the job, and so on
29+
const response = await client.handleRequest(request);
30+
31+
if (!response) {
32+
return new Response(JSON.stringify({ error: "Not found" }), {
33+
status: 404,
34+
});
35+
}
36+
37+
// Optionally can do something with the job's finished
38+
// execution's response body
39+
return new Response(JSON.stringify(response.body), {
40+
status: response.status,
41+
});
42+
} catch (err) {
43+
return new Response(JSON.stringify({ error: "Internal server error" }), {
44+
status: 500,
45+
});
46+
}
47+
};
48+
}

packages/astro/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@trigger.dev/astro",
3+
"description": "An Astro-native integration for Trigger.dev background jobs platform",
4+
"version": "1.0.0",
5+
"type": "module",
6+
"main": "main.js",
7+
"scripts": {},
8+
"peerDependencies": {
9+
"@trigger.dev/sdk": "workspace:^2.0.9",
10+
"astro": "^2.10.7"
11+
},
12+
"engines": {
13+
"node": ">=18.0.0"
14+
},
15+
"license": "MIT",
16+
"publishConfig": {
17+
"access": "public"
18+
},
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/triggerdev/trigger.dev.git"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/triggerdev/trigger.dev/issues"
25+
},
26+
"homepage": "https://github.com/triggerdev/trigger.dev#readme"
27+
}

0 commit comments

Comments
 (0)