Skip to content

Commit 85f596a

Browse files
committed
update docs for the v4 of JS SDK with modularized packages
Signed-off-by: Karthik Ganeshram <[email protected]>
1 parent 1a78951 commit 85f596a

13 files changed

+65
-52
lines changed

content/v3/ai-sentiment-analysis-api-tutorial.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ impl FromStr for Sentiment {
451451
{{ startTab "TypeScript"}}
452452

453453
```typescript
454-
import { Llm, Kv } from "@fermyon/spin-sdk";
454+
import * as Llm from "@spinframework/spin-llm";
455+
import * as Kv from "@spinframework/spin-kv";
455456
import { AutoRouter } from 'itty-router';
456457

457458
interface SentimentAnalysisRequest {

content/v3/build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ It's normally convenient to put the detailed build instructions in `package.json
7878
```json
7979
{
8080
"scripts": {
81-
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/spin-http-js.wasm"
81+
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o target/spin-http-js.wasm"
8282
}
8383
}
8484
```

content/v3/http-trigger.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ For a full Rust SDK reference, see the [Rust Spin SDK documentation](https://doc
256256

257257
{{ startTab "TypeScript"}}
258258

259-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/)
259+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
260260
261261
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.
262262

content/v3/javascript-components.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ With JavaScript being a very popular language, Spin provides an SDK to support b
3131
3232
> All examples from this page can be found in [the JavaScript SDK repository on GitHub](https://github.com/spinframework/spin-js-sdk/tree/main/examples).
3333
34-
[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/)
34+
[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
3535

3636
## Installing Templates
3737

@@ -77,19 +77,18 @@ This creates a directory of the following structure:
7777

7878
```text
7979
hello-world
80-
├── config
81-
│ └── knitwit.json
8280
├── package.json
81+
├── README.md
8382
├── spin.toml
8483
├── src
85-
└── index.ts
84+
   └── index.ts
8685
├── tsconfig.json
8786
└── webpack.config.js
8887
```
8988

9089
The source for the component is present in `src/index.ts`. [Webpack](https://webpack.js.org) is used to bundle the component into a single `.js` file which will then be compiled to a `.wasm` module.
9190

92-
{{ details "Going from JavaScript to Wasm" "The JS source is compiled to a `wasm` module using the `j2w` node executable provided by the `@fermyon/spin-sdk` which is a wrapper around `ComponentizeJS`. The `knitwit.json` is the configuration file used by [knitwit](https://github.com/fermyon/knitwit) to manage the WebAssembly dependencies of each package."}}
91+
{{ details "Going from JavaScript to Wasm" "The JS source is compiled to a `wasm` module using the `j2w` node executable provided by the `@fermyon/spin-sdk` which is a wrapper around `ComponentizeJS` that is used to manage the dependencies."}}
9392

9493
## Building and Running the Template
9594

@@ -259,13 +258,13 @@ You must [add `http://self` or `http://self.alt` to the component's `allowed_out
259258
> You can find a complete example for using outbound Redis from an HTTP component
260259
> in the [spin-js-sdk repository on GitHub](https://github.com/spinframework/spin-js-sdk/blob/main/examples/spin-host-apis/spin-redis).
261260
262-
Using the Spin's JS SDK, you can use the Redis key/value store and to publish messages to Redis channels.
261+
We can install and use the `@spinframework/spin-redis` package to use the Redis key/value store and to publish messages to Redis channels.
263262

264263
Let's see how we can use the JS/TS SDK to connect to Redis:
265264

266265
```javascript
267266
import { AutoRouter } from 'itty-router';
268-
import { Redis } from '@fermyon/spin-sdk';
267+
import { Redis } from '@spinframework/spin-redis';
269268

270269
const encoder = new TextEncoder();
271270
const redisAddress = 'redis://localhost:6379/';
@@ -314,14 +313,20 @@ Spin has a key-value store built in. For information about using it from TypeScr
314313

315314
## Storing Data in SQLite
316315

316+
We can use the `@spinframework/spin-sqlite` package to interact with Spin's SQLite interface.
317+
317318
For more information about using SQLite from TypeScript/Javascript, see [SQLite storage](sqlite-api-guide).
318319

319320
## Storing Data in MySQL and PostgreSQL Relational Databases
320321

322+
We can use the `@spinframework/spin-mysql`, `@spinframework/spin-postgres` package to interact with Spin's RDBMS interfaces.
323+
321324
For more information about using relational databases from TypeScript/JavaScript, see [Relational Databases](rdbms-storage).
322325

323326
## AI Inferencing From JS/TS Components
324327

328+
We can use the `@spinframework/spin-llm` package to interact with Spin's LLM interface.
329+
325330
For more information about using Serverless AI from JS/TS, see the [Serverless AI](serverless-ai-api-guide) API guide.
326331

327332
## Node.js Compatibility
@@ -331,21 +336,24 @@ The SDK does not support the full specification of `Node.js`. A limited set of A
331336
<!-- @selectiveCpy -->
332337

333338
```bash
334-
$ npm install @fermyon/wasi-ext
339+
$ npm install @spinframework/wasi-ext
335340
```
336341

337342
Once installed, the plugin provided by it can be added to the webpack config:
338343

339344
```js
340-
const WasiExtPlugin = require("wasi-ext/plugin")
345+
import WasiExtPlugin from "@spinframework/wasi-ext/plugin/index.js";
341346

342-
module.exports = {
347+
export default config = () => {
343348
...
344-
plugins: [
345-
new WasiExtPlugin()
346-
],
347-
...
348-
};
349+
return {
350+
...
351+
plugins: [
352+
new WasiExtPlugin()
353+
],
354+
...
355+
}
356+
}
349357
```
350358

351359
This library only currently supports the following polyfills:

content/v3/key-value-store-tutorial.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ $ spin new -t http-rust spin-key-value
7575

7676
```bash
7777
$ spin new -t http-ts spin-key-value
78+
$ cd spin-key-value
79+
$ npm install @spinframework/spin-kv
7880

7981
# Reference: https://github.com/spinframework/spin-js-sdk/tree/main/examples/spin-host-apis/spin-kv
8082
```
@@ -234,15 +236,15 @@ fn handle_request(req: Request) -> anyhow::Result<impl IntoResponse> {
234236

235237
```typescript
236238
import { AutoRouter } from 'itty-router';
237-
import { Kv } from '@fermyon/spin-sdk';
239+
import { openDefault } from '@spinframework/spin-kv';
238240

239241
const decoder = new TextDecoder();
240242

241243
let router = AutoRouter();
242244

243245
router
244246
.all("*", async (req: Request) => {
245-
let store = Kv.openDefault();
247+
let store = openDefault();
246248
let status = 200;
247249
let body;
248250

content/v3/kv-store-api-guide.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@ fn handle_request(_req: Request) -> Result<impl IntoResponse> {
8383

8484
{{ startTab "Typescript"}}
8585

86-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/)
86+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
8787
88-
The key value functions can be accessed after opening a store using either [the `Kv.open` or the `Kv.openDefault` methods](https://spinframework.github.io/spin-js-sdk/stable/modules/Kv.html) which returns a [handle to the store](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Kv.Store.html). For example:
88+
The key value functions can be accessed after opening a store using either [the `open` or the `openDefault` functions](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-kv.html) which returns a [handle to the store](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-kv.Store.html). For example:
8989

9090
```ts
9191
import { AutoRouter } from 'itty-router';
92-
import { Kv } from '@fermyon/spin-sdk';
92+
import { openDefault } from '@spinframework/spin-kv';
9393

9494
let router = AutoRouter();
9595

9696
router
9797
.get("/", () => {
98-
let store = Kv.openDefault()
98+
let store = openDefault()
9999
store.set("mykey", "myvalue")
100100
return new Response(store.get("mykey") ?? "Key not found");
101101
})
@@ -109,14 +109,14 @@ addEventListener('fetch', async (event: FetchEvent) => {
109109
**General Notes**
110110
- The SDK doesn't surface the `close` operation. It automatically closes all stores at the end of the request; there's no way to close them early.
111111

112-
[`get` **Operation**](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Kv.Store.html#get)
112+
[`get` **Operation**](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-kv.Store.html#get)
113113
- The result is of the type `Uint8Array | null`
114114
- If the key does not exist, `get` returns `null`
115115

116-
[`set` **Operation**](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Kv.Store.html#set)
116+
[`set` **Operation**](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-kv.Store.html#set)
117117
- The value argument is of the type `Uint8Array | string | object`.
118118

119-
[`setJson`](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Kv.Store.html#setJson) and [`getJson` **Operation**](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Kv.Store.html#getJson)
119+
[`setJson`](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-kv.Store.html#setjson) and [`getJson` **Operation**](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-kv.Store.html#getjson)
120120
- Applications can store JavaScript objects using `setJson`; these are serialized within the store as JSON. These serialized objects can be retrieved and deserialized using `getJson`. If you call `getJson` on a key that doesn't exist then it returns an empty object.
121121

122122
{{ blockEnd }}

content/v3/language-support-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ This page contains information about language support for Spin features:
3737

3838
{{ startTab "TypeScript"}}
3939

40-
**[📄 Visit the JS/TS Spin SDK reference documentation](https://spinframework.github.io/spin-js-sdk/stable/) to see specific modules, functions, variables and syntax relating to the following TS/JS features.**
40+
**[📄 Visit the JS/TS Spin SDK reference documentation](https://spinframework.github.io/spin-js-sdk/) to see specific modules, functions, variables and syntax relating to the following TS/JS features.**
4141

4242
| Feature | SDK Supported? |
4343
|-----|-----|

content/v3/mqtt-outbound.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ You can find a complete Rust code example for using outbound MQTT from an HTTP c
5757

5858
{{ startTab "TypeScript"}}
5959

60-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/modules/Mqtt.html)
60+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-mqtt.html)
6161
62-
To access an MQTT server, use the `Mqtt.open` function.
62+
To access an MQTT server, use the `open` function.
6363

6464
```ts
65+
import { open, QoS } from "@spinframework/spin-mqtt";
66+
6567
let connection = Mqtt.open(address, username, password, keepAliveSecs);
6668
```
6769

@@ -72,7 +74,7 @@ let catPicture = new Uint8Array(await req.arraybuffer());
7274
connection.publish("pets", catPicture, QoS.AtleastOnce);
7375
```
7476

75-
For full details of the MQTT API, see the [Spin SDK reference documentation](https://spinframework.github.io/spin-js-sdk/stable/modules/Mqtt.html)
77+
For full details of the MQTT API, see the [Spin SDK reference documentation](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-mqtt.html)
7678

7779
You can find a complete Rust code example for using outbound MQTT from an HTTP component in the [Spin Rust SDK repository on GitHub](https://github.com/spinframework/spin-js-sdk/tree/main/examples/spin-host-apis/spin-mqtt).
7880

content/v3/rdbms-storage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ For full information about the MySQL and PostgreSQL APIs, see [the Spin SDK refe
7373

7474
{{ startTab "TypeScript"}}
7575

76-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/)
76+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
7777
7878
The code below is an [Outbound MySQL example](https://github.com/spinframework/spin-js-sdk/tree/main/examples/spin-host-apis/spin-mysql). There is also an outbound [PostgreSQL example](https://github.com/spinframework/spin-js-sdk/tree/main/examples/spin-host-apis/spin-postgres) available.
7979

8080
```ts
8181
// https://itty.dev/itty-router/routers/autorouter
8282
import { AutoRouter } from 'itty-router';
83-
import { Mysql } from '@fermyon/spin-sdk';
83+
import { open } from '@spinframework/spin-mysql';
8484

8585
// Connects as the root user without a password
8686
const DB_URL = "mysql://root:@127.0.0.1/spin_dev"
@@ -97,7 +97,7 @@ let router = AutoRouter();
9797

9898
router
9999
.get("/", () => {
100-
let conn = Mysql.open(DB_URL);
100+
let conn = open(DB_URL);
101101
conn.execute('delete from test where id=?', [4]);
102102
conn.execute('insert into test values (4,5)', []);
103103
let ret = conn.query('select * from test', []);

content/v3/redis-outbound.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ You can find a complete Rust code example for using outbound Redis from an HTTP
8989

9090
{{ startTab "TypeScript"}}
9191

92-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/)
92+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
9393
94-
Redis functions are available on [the `Redis` module](https://spinframework.github.io/spin-js-sdk/stable/modules/Redis.html). The function names match the operations above. For example:
94+
Redis functions are available on [the `Redis` module](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-redis.html). The function names match the operations above. For example:
9595

9696
```javascript
97-
import { Redis } from "@fermyon/spin-sdk"
97+
import { open } from "@spinframework/spin-redis"
9898

99-
let db = Redis.open("redis://localhost:6379")
99+
let db = open("redis://localhost:6379")
100100
let value = db.get(key);
101101
```
102102

content/v3/serverless-ai-api-guide.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ The `infer_with_options` examples, operation:
116116

117117
{{ startTab "Typescript"}}
118118

119-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/modules/Llm.html)
119+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
120120
121-
To use Serverless AI functions, [the `Llm` module](https://spinframework.github.io/spin-js-sdk/stable/modules/Llm.html) from the Spin SDK provides two methods: `infer` and `generateEmbeddings`. For example:
121+
To use Serverless AI functions, [the `@spinframework/spin-llm` pacakge](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-llm.html) provides two methods: `infer` and `generateEmbeddings`. For example:
122122

123123
```javascript
124124
import { AutoRouter } from 'itty-router';
125-
import { Llm } from '@fermyon/spin-sdk';
125+
import { inferencingModels, EmbeddingModels, infer, generateEmbeddings } from '@spinframework/spin-llm';
126126

127127
let router = AutoRouter();
128128

129129
router
130130
.get("/", () => {
131-
let embeddings = Llm.generateEmbeddings(Llm.EmbeddingModels.AllMiniLmL6V2, ["someString"])
131+
let embeddings = generateEmbeddings(EmbeddingModels.AllMiniLmL6V2, ["someString"])
132132
console.log(embeddings.embeddings)
133-
let result = Llm.infer(Llm.InferencingModels.Llama2Chat, prompt)
133+
let result = infer(InferencingModels.Llama2Chat, prompt)
134134

135135
return new Response(result.text);
136136
})
@@ -146,15 +146,15 @@ addEventListener('fetch', async (event: FetchEvent) => {
146146
`infer` operation:
147147

148148
- It takes in the following arguments - model name, prompt and a optional third parameter for inferencing options.
149-
- The model name is a string. There are enums for the inbuilt models (llama2-chat and codellama) in [`InferencingModels`](https://spinframework.github.io/spin-js-sdk/stable/enums/Llm.InferencingModels.html).
150-
- The optional third parameter which is an [InferencingOptions](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Llm.InferencingOptions.html) interface allows you to specify parameters such as `maxTokens`, `repeatPenalty`, `repeatPenaltyLastNTokenCount`, `temperature`, `topK`, `topP`.
151-
- The return value is an [`InferenceResult`](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Llm.EmbeddingResult.html).
149+
- The model name is a string. There are enums for the inbuilt models (llama2-chat and codellama) in [`InferencingModels`](https://spinframework.github.io/spin-js-sdk/enums/_spinframework_spin-llm.InferencingModels.html).
150+
- The optional third parameter which is an [InferencingOptions](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-llm.InferencingOptions.html) interface allows you to specify parameters such as `maxTokens`, `repeatPenalty`, `repeatPenaltyLastNTokenCount`, `temperature`, `topK`, `topP`.
151+
- The return value is an [`InferenceResult`](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-llm.InferenceResult.html).
152152

153153
`generateEmbeddings` operation:
154154

155155
- It takes two arguments - model name and list of strings to generate the embeddings for.
156-
- The model name is a string. There are enums for the inbuilt models (AllMiniLmL6V2) in [`EmbeddingModels`](https://spinframework.github.io/spin-js-sdk/stable/enums/Llm.EmbeddingModels.html).
157-
- The return value is an [`EmbeddingResult`](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Llm.EmbeddingResult.html)
156+
- The model name is a string. There are enums for the inbuilt models (AllMiniLmL6V2) in [`EmbeddingModels`](https://spinframework.github.io/spin-js-sdk/enums/_spinframework_spin-llm.EmbeddingModels.html).
157+
- The return value is an [`EmbeddingResult`](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-llm.EmbeddingResult.html)
158158

159159
{{ blockEnd }}
160160

content/v3/sqlite-api-guide.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ struct ToDo {
119119

120120
{{ startTab "Typescript"}}
121121

122-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/modules/Sqlite.html)
122+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/)
123123
124-
To use SQLite functions, use [the `Sqlite.open` or `Sqlite.openDefault` function](https://spinframework.github.io/spin-js-sdk/stable/modules/Sqlite.html) to obtain [a `SqliteConnection` object](https://spinframework.github.io/spin-js-sdk/stable/interfaces/Sqlite.SqliteConnection.html). `SqliteConnection` provides the `execute` method as described above. For example:
124+
To use SQLite functions, use [the `Sqlite.open` or `Sqlite.openDefault` function](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-sqlite.html) to obtain [a `SqliteConnection` object](https://spinframework.github.io/spin-js-sdk/interfaces/_spinframework_spin-sqlite.SqliteConnection.html). `SqliteConnection` provides the `execute` method as described above. For example:
125125

126126
```javascript
127127
import { AutoRouter } from 'itty-router';
128-
import { Sqlite } from '@fermyon/spin-sdk';
128+
import { Sqlite } from '@spinframework/spin-sqlite';
129129

130130
let router = AutoRouter();
131131
router

content/v3/variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async fn handle_api_call_with_token(_req: Request) -> anyhow::Result<impl IntoRe
134134

135135
{{ startTab "TypeScript"}}
136136

137-
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/stable/modules/Variables.html)
137+
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-js-sdk/modules/_spinframework_spin-variables.html)
138138
139139
```ts
140140
import { AutoRouter } from 'itty-router';

0 commit comments

Comments
 (0)