Skip to content

Commit 0efb6dd

Browse files
committed
bump templates to use v3.0.0-rc1 and depend on itty router directly
Signed-off-by: karthik2804 <[email protected]>
1 parent 5e8d91e commit 0efb6dd

File tree

10 files changed

+40
-129
lines changed

10 files changed

+40
-129
lines changed

templates/http-js/content/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"description": "{{project-description}}",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/{{project-name | kebab_case}}.wasm",
8-
"test": "echo \"Error: no test specified\" && exit 1",
9-
"postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined"
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/{{project-name | kebab_case}}.wasm",
8+
"test": "echo \"Error: no test specified\" && exit 1"
109
},
1110
"keywords": [],
1211
"author": "",
@@ -18,6 +17,7 @@
1817
"@fermyon/knitwit": "0.3.0"
1918
},
2019
"dependencies": {
21-
"@fermyon/spin-sdk": "^2.4.0"
20+
"@fermyon/spin-sdk": "^3.0.0-rc1",
21+
"itty-router": "^5.0.18"
2222
}
2323
}

templates/http-js/content/spin.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ component = "{{project-name | kebab_case}}"
1414
source = "dist/{{project-name | kebab_case}}.wasm"
1515
exclude_files = ["**/node_modules"]
1616
[component.{{project-name | kebab_case}}.build]
17-
command = "npm install && npm run build"
18-
watch = ["src/**/*.ts", "package.json"]
17+
command = ["npm install", "npm run build"]
18+
watch = ["src/**/*.js"]
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import { ResponseBuilder, Router } from "@fermyon/spin-sdk";
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
23

3-
let router = Router();
4-
// Route that will be called for the default route:
5-
router.get("/", (_, req, res) => { handleDefaultRoute(req, res) });
6-
// Modify this route or add additional ones to implement the component's API:
7-
router.get("/hello/:name", (metadata, req, res) => { handleHelloRoute(req, res, metadata.params.name) });
8-
// Default route that will be called for any routes not handled above:
9-
router.all("*", (_, req, res) => { notFound(req, res) });
4+
let router = AutoRouter();
105

11-
async function handleDefaultRoute(req, res) {
12-
res.send("hello universe");
13-
}
6+
// Route ordering matters, the first route that matches will be used
7+
// Any route that does not return will be treated as a middleware
8+
// Any unmatched route will return a 404
9+
router
10+
.get("/", () => new Response("hello universe"))
11+
.get('/hello/:name', ({ name }) => `Hello, ${name}!`)
1412

15-
async function handleHelloRoute(req, res, name) {
16-
res.send(`hello ${name}`);
17-
}
18-
async function notFound(req, res) {
19-
res.status(404);
20-
res.send("not found");
21-
}
22-
23-
export async function handler(req, res) {
24-
await router.handleRequest(req, res);
25-
}
13+
//@ts-ignore
14+
addEventListener('fetch', async (event) => {
15+
event.respondWith(router.fetch(event.request));
16+
});

templates/http-js/content/src/spin.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

templates/http-js/content/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.js',
5+
entry: './src/index.js',
66
experiments: {
77
outputModule: true,
88
},

templates/http-ts/content/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"description": "{{project-description}}",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/{{ project-name | kebab_case }}.wasm",
8-
"test": "echo \"Error: no test specified\" && exit 1",
9-
"postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined"
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/{{ project-name | kebab_case }}.wasm",
8+
"test": "echo \"Error: no test specified\" && exit 1"
109
},
1110
"keywords": [],
1211
"author": "",
@@ -20,6 +19,7 @@
2019
"@fermyon/knitwit": "0.3.0"
2120
},
2221
"dependencies": {
23-
"@fermyon/spin-sdk": "^2.4.0"
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18"
2424
}
2525
}

templates/http-ts/content/spin.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ component = "{{project-name | kebab_case}}"
1414
source = "dist/{{project-name | kebab_case}}.wasm"
1515
exclude_files = ["**/node_modules"]
1616
[component.{{project-name | kebab_case}}.build]
17-
command = "npm install && npm run build"
18-
watch = ["src/**/*.ts", "package.json"]
17+
command = ["npm install", "npm run build"]
18+
watch = ["src/**/*.ts"]
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
import { ResponseBuilder, Router } from "@fermyon/spin-sdk";
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
23

3-
let router = Router();
4-
// Route that will be called for the default route:
5-
router.get("/", (_, req, res) => { handleDefaultRoute(req, res) });
6-
// Modify this route or add additional ones to implement the component's API:
7-
router.get("/hello/:name", (metadata, req, res) => { handleHelloRoute(req, res, metadata.params.name) });
8-
// Default route that will be called for any routes not handled above:
9-
router.all("*", (_, req, res) => { notFound(req, res) });
4+
let router = AutoRouter();
105

11-
async function handleDefaultRoute(req: Request, res: ResponseBuilder) {
12-
res.send("hello universe");
13-
}
6+
// Route ordering matters, the first route that matches will be used
7+
// Any route that does not return will be treated as a middleware
8+
// Any unmatched route will return a 404
9+
router
10+
.get("/", () => new Response("hello universe"))
11+
.get('/hello/:name', ({ name }) => `Hello, ${name}!`)
1412

15-
async function handleHelloRoute(req: Request, res: ResponseBuilder, name: string) {
16-
res.send(`hello ${name}`);
17-
}
18-
19-
async function notFound(req: Request, res: ResponseBuilder) {
20-
res.status(404);
21-
res.send("not found");
22-
}
23-
24-
export async function handler(req: Request, res: ResponseBuilder) {
25-
await router.handleRequest(req, res);
26-
}
13+
//@ts-ignore
14+
addEventListener('fetch', async (event: FetchEvent) => {
15+
event.respondWith(router.fetch(event.request));
16+
});

templates/http-ts/content/src/spin.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

templates/http-ts/content/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.ts',
5+
entry: './src/index.ts',
66
experiments: {
77
outputModule: true,
88
},

0 commit comments

Comments
 (0)