You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ details "Parts of the build script" "The build script calls out to [`webpack`](https://webpack.js.org/) and `j2w` which is a script provided by the `@fermyon/spin-sdk` package that utilizes [`ComponentizeJS`](https://github.com/bytecodealliance/ComponentizeJS)."}}
86
+
{{ details "Parts of the build script" "The build script calls out to [`webpack`](https://webpack.js.org/) and `j2w` which is a script provided by the `@fermyon/spin-sdk` package that utilizes [`ComponentizeJS`](https://github.com/bytecodealliance/ComponentizeJS).[`knitwit`](https://github.com/fermyon/knitwit) is a utility that helps combine `wit` worlds "}}
-[Storing Data in Redis From JS/TS Components](#storing-data-in-redis-from-jsts-components)
15
-
-[Routing in a Component](#routing-in-a-component)
16
15
-[Storing Data in the Spin Key-Value Store](#storing-data-in-the-spin-key-value-store)
17
16
-[Storing Data in SQLite](#storing-data-in-sqlite)
18
17
-[Storing Data in MySQL and PostgreSQL Relational Databases](#storing-data-in-mysql-and-postgresql-relational-databases)
@@ -76,13 +75,13 @@ This creates a directory of the following structure:
76
75
<!-- @nocpy -->
77
76
78
77
```text
79
-
hello-world/
80
-
├── knitwit.json
78
+
hello-world
79
+
├── config
80
+
│ └── knitwit.json
81
81
├── package.json
82
82
├── spin.toml
83
83
├── src
84
-
│ ├── index.ts
85
-
│ └── spin.ts
84
+
│ └── index.ts
86
85
├── tsconfig.json
87
86
└── webpack.config.js
88
87
```
@@ -134,47 +133,53 @@ for writing Spin components with the Spin JS/TS SDK.
134
133
> Make sure to read [the page describing the HTTP trigger](./http-trigger.md) for more
135
134
> details about building HTTP applications.
136
135
137
-
Building a Spin HTTP component using the JS/TS SDK means writing a single function
138
-
that takes an HTTP request and a Response Builder which can be used to return an HTTP response as a parameter.
136
+
Building a Spin HTTP component with the JavaScript/TypeScript SDK now involves adding an event listener for the `fetch` event. This event listener handles incoming HTTP requests and allows you to construct and return HTTP responses.
139
137
140
138
Below is a complete implementation for such a component in TypeScript:
- The `handler` function is the entry point for the Spin component.
154
-
- The execution of the function terminates once `res.send` or `res.end` is called.
154
+
```
155
155
156
156
## Sending Outbound HTTP Requests
157
157
158
158
If allowed, Spin components can send outbound HTTP requests.
159
159
Let's see an example of a component that makes a request to [an API that returns random animal facts](https://random-data-api.fermyon.app/animals/json)
The JavaScript/TypeScript SDK provides a router that makes it easier to handle routing within a component. The router is based on [`itty-router`](https://www.npmjs.com/package/itty-router). An additional function `handleRequest` has been implemented in the router to allow passing in the Spin HTTP request directly. An example usage of the router is given below:
Spin has a key-value store built in. For information about using it from TypeScript/JavaScript, see [the key-value store tutorial](key-value-store-tutorial).
@@ -381,5 +363,5 @@ These are some of the suggested libraries that have been tested and confirmed to
381
363
382
364
## Caveats
383
365
384
-
- All `spin-sdk` related functions and methods (like `Variables`, `Redis`, `Mysql`, `Pg`, `Kv` and `Sqlite`) can be called only inside the `handler` function. This includes `fetch`. Any attempts to use it outside the function will lead to an error. This is due to Wizer using only Wasmtime to execute the script at build time, which does not include any Spin SDK support.
366
+
- All `spin-sdk` related functions and methods (like `Variables`, `Redis`, `Mysql`, `Pg`, `Kv` and `Sqlite`) can be called only inside the fetch event handler. This includes `fetch`. Any attempts to use it outside the function will lead to an error. This is due to Wizer using only Wasmtime to execute the script at build time, which does not include any Spin SDK support.
385
367
- No crypto operation that involve handling private keys are supported.
0 commit comments