Skip to content

Commit b5b844f

Browse files
committed
update upstash examples
Signed-off-by: karthik2804 <[email protected]>
1 parent 32e984e commit b5b844f

File tree

18 files changed

+3869
-176
lines changed

18 files changed

+3869
-176
lines changed

examples/upstash/qstash/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/upstash/qstash/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
This example demonstrates how to connect to Upstash QStash, publish a JSON message with a delay, and handle an HTTP request using the Spin SDK.
44

5-
## Install Dependencies
6-
Install the necessary npm packages:
7-
8-
```bash
9-
npm install
10-
```
11-
125
## Setup the Example
136

147
1. **Create an Upstash Account**
File renamed without changes.

examples/upstash/qstash/package-lock.json

Lines changed: 3724 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/upstash/qstash/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/qstash.wasm",
7+
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/qstash.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
@@ -15,10 +15,12 @@
1515
"ts-loader": "^9.4.1",
1616
"typescript": "^4.8.4",
1717
"webpack": "^5.74.0",
18-
"webpack-cli": "^4.10.0"
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
1920
},
2021
"dependencies": {
21-
"@fermyon/spin-sdk": "^2.0.0",
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18",
2224
"@upstash/qstash": "^2.5.5"
2325
}
2426
}

examples/upstash/qstash/spin.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ route = "/..."
1111
component = "qstash"
1212

1313
[component.qstash]
14-
source = "target/qstash.wasm"
14+
source = "dist/qstash.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ["https://qstash.upstash.io:443"]
1716
[component.qstash.build]
18-
command = "npm run build"
19-
watch = ["src/**/*.ts", "package.json"]
17+
command = ["npm install", "npm run build"]
18+
watch = ["src/**/*.ts"]
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import { ResponseBuilder } from '@fermyon/spin-sdk';
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
23
import { Client } from '@upstash/qstash';
34

45
const client = new Client({ token: '<upstash token>' });
6+
let router = AutoRouter();
57

6-
export async function handler(_req: Request, res: ResponseBuilder) {
7-
const resp = await client.publishJSON({
8-
url: '<>',
9-
body: { hello: 'world' },
10-
delay: 2,
11-
});
12-
console.log(resp);
13-
res.send('hello universe');
14-
}
8+
// Route ordering matters, the first route that matches will be used
9+
// Any route that does not return will be treated as a middleware
10+
// Any unmatched route will return a 404
11+
router
12+
.get("/", async () => {
13+
let resp = await client.publishJSON({
14+
url: '<>',
15+
body: { hello: 'world' },
16+
delay: 2,
17+
});
18+
19+
return new Response(JSON.stringify(resp), { status: 200 });
20+
})
21+
22+
//@ts-ignore
23+
addEventListener('fetch', async (event: FetchEvent) => {
24+
event.respondWith(router.fetch(event.request));
25+
});

examples/upstash/qstash/src/spin.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
const path = require('path');
2-
const SpinSdkPlugin = require('@fermyon/spin-sdk/plugins/webpack');
2+
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.ts',
6-
experiments: {
7-
outputModule: true,
8-
},
9-
module: {
10-
rules: [
11-
{
12-
test: /\.tsx?$/,
13-
use: 'ts-loader',
14-
exclude: /node_modules/,
15-
},
5+
entry: './src/index.ts',
6+
experiments: {
7+
outputModule: true,
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/,
13+
use: 'ts-loader',
14+
exclude: /node_modules/,
15+
},
16+
],
17+
},
18+
resolve: {
19+
extensions: ['.tsx', '.ts', '.js'],
20+
},
21+
output: {
22+
path: path.resolve(__dirname, './build'),
23+
filename: 'bundle.js',
24+
module: true,
25+
library: {
26+
type: "module",
27+
}
28+
},
29+
plugins: [
30+
new SpinSdkPlugin()
1631
],
17-
},
18-
resolve: {
19-
extensions: ['.tsx', '.ts', '.js'],
20-
},
21-
output: {
22-
path: path.resolve(__dirname, './'),
23-
filename: 'dist.js',
24-
module: true,
25-
library: {
26-
type: 'module',
32+
optimization: {
33+
minimize: false
2734
},
28-
},
29-
plugins: [new SpinSdkPlugin()],
30-
optimization: {
31-
minimize: false,
32-
},
33-
};
35+
};

examples/upstash/vector/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

0 commit comments

Comments
 (0)