Skip to content

Commit

Permalink
update templates and readme
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
  • Loading branch information
karthik2804 committed Aug 1, 2024
1 parent 6b4ba12 commit c533887
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This is an SDK for Javascript and Typescript based on [ComponentizeJS](https://g
The templates can be installed with the following command:

```bash
spin templates install --update --git https://github.com/fermyon/spin-js-sdk --branch feat/sdk-v2
spin templates install --update --git https://github.com/fermyon/spin-js-sdk
```

## Creating and building a new app
Expand All @@ -33,5 +33,5 @@ spin up
Currently pre-release versions are published on NPM and can be installed using the following command:

```bash
npm install @fermyon/spin-sdk@next
npm install @fermyon/spin-sdk@latest
```
5 changes: 5 additions & 0 deletions templates/http-js/content/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
target
.spin/
dist.js
9 changes: 9 additions & 0 deletions templates/http-js/content/knitwit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 1,
"project": {
"worlds": [
"spin-http"
]
},
"packages": {}
}
23 changes: 23 additions & 0 deletions templates/http-js/content/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "{{project-name | kebab_case}}",
"version": "1.0.0",
"description": "{{project-description}}",
"main": "index.js",
"scripts": {
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm",
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "knitwit"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"mkdirp": "^3.0.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"@fermyon/knitwit": "https://github.com/fermyon/knitwit"
},
"dependencies": {
"@fermyon/spin-sdk": "^2.0.0-alpha.3"
}
}
18 changes: 18 additions & 0 deletions templates/http-js/content/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
spin_manifest_version = 2

[application]
authors = ["{{authors}}"]
description = "{{project-description}}"
name = "{{project-name}}"
version = "0.1.0"

[[trigger.http]]
route = "{{http-path}}"
component = "{{project-name | kebab_case}}"

[component.{{project-name | kebab_case}}]
source = "target/{{project-name | kebab_case}}.wasm"
exclude_files = ["**/node_modules"]
[component.{{project-name | kebab_case}}.build]
command = "npm run build"
watch = ["src/**/*.ts", "package.json"]
6 changes: 6 additions & 0 deletions templates/http-js/content/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ResponseBuilder } from "@fermyon/spin-sdk";

export async function handler(req, res) {
console.log(req);
res.send("hello universe");
}
33 changes: 33 additions & 0 deletions templates/http-js/content/src/spin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is a wrapper around the actual handler function defined in `index.js` and attaches it to
// the fetchEvent. If you prefer to directly target the fetchEvent, you can
// modify this file

import { ResponseBuilder } from "@fermyon/spin-sdk";
import { handler } from ".";

addEventListener('fetch', (event) => {
handleEvent(event);
});

async function handleEvent(event) {

let resolve, reject;
let responsePromise = new Promise(async (res, rej) => {
resolve = res;
reject = rej;
});
event.respondWith(responsePromise);

let res = new ResponseBuilder(resolve);

try {
// In case you want to do some work after the response is sent
// uncomment the line below and comment out the line with
// await handler(event.request, res)
// event.waitUntil(handler(event.request, res))
await handler(event.request, res)
} catch (e) {
res.status(500);
res.send(`error in handler: ${e}`);
}
}
23 changes: 23 additions & 0 deletions templates/http-js/content/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")

module.exports = {
entry: './src/spin.js',
experiments: {
outputModule: true,
},
output: {
path: path.resolve(__dirname, './'),
filename: 'dist.js',
module: true,
library: {
type: "module",
}
},
plugins: [
new SpinSdkPlugin()
],
optimization: {
minimize: false
},
};
11 changes: 11 additions & 0 deletions templates/http-js/metadata/snippets/component.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[trigger.http]]
route = "{{http-path}}"
component = "{{project-name | kebab_case}}"

[component.{{project-name | kebab_case}}]
source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm"
allowed_outbound_hosts = []

[component.{{project-name | kebab_case}}.build]
command = "npm run build"
workdir = "{{ output-path }}"
13 changes: 13 additions & 0 deletions templates/http-js/metadata/spin-template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
manifest_version = "1"
id = "http-js"
description = "HTTP request handler using Typescript"

[add_component]
skip_files = ["spin.toml"]
[add_component.snippets]
component = "component.txt"

[parameters]
project-description = { type = "string", prompt = "Description", default = "" }
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }
# enable-experimental = { type = "string", prompt = "enable experimental interfaces [y/N]", default = "N", pattern = "^[yYnN]$" }
4 changes: 4 additions & 0 deletions templates/http-ts/content/src/spin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// This file is a wrapper around the actual handler function defined in `index.ts` and attaches it to
// the fetchEvent. If you prefer to directly target the fetchEvent, you can
// modify this file

import { ResponseBuilder } from "@fermyon/spin-sdk";
import { handler } from ".";

Expand Down
12 changes: 12 additions & 0 deletions templates/http-ts/metadata/snippets/component.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
manifest_version = "1"
id = "http-js"
description = "HTTP request handler using Javascript"

[add_component]
skip_files = ["spin.toml"]
[add_component.snippets]
component = "component.txt"

[parameters]
project-description = { type = "string", prompt = "Description", default = "" }
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }

0 comments on commit c533887

Please sign in to comment.