Skip to content

Commit

Permalink
Merge pull request #1041 from itowlson/js-reference-links
Browse files Browse the repository at this point in the history
Add links to JavaScript SDK reference
  • Loading branch information
itowlson authored Nov 14, 2023
2 parents 056bd99 + c1a860c commit 5e684dc
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 18 deletions.
6 changes: 4 additions & 2 deletions content/spin/v2/http-trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ For a full Rust SDK reference, see the [Rust Spin SDK documentation](https://fer

{{ startTab "TypeScript"}}

[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/)

In JavaScript or TypeScript, the handler is identified by name. It must be called `handleRequest`. The way you declare it is slightly different between the two languages.

In **JavaScript**, `handleRequest` is declared as `export async function`. It takes a JsvaScript object representing the request, and returns a response object. The fields of these objects are exactly the same as in TypeScript:
In **JavaScript**, `handleRequest` is declared as `export async function`. It takes a JavaScript [object representing the request](https://fermyon.github.io/spin-js-sdk/interfaces/HttpRequest.html), and returns a [response object](https://fermyon.github.io/spin-js-sdk/interfaces/HttpResponse.html). The fields of these objects are exactly the same as in TypeScript:

```javascript
export async function handleRequest(request) {
Expand All @@ -258,7 +260,7 @@ export async function handleRequest(request) {
}
```

In **TypeScript**, `handleRequest` is declared as an `export const` of the `HandleRequest` function type - that is, a function literal rather than a function declaration. It takes a `HttpRequest` object, and returns a `HttpResponse` object, both defined in the `@fermyon/spin-sdk` package:
In **TypeScript**, `handleRequest` is declared as an `export const` of the [`HandleRequest` function type](https://fermyon.github.io/spin-js-sdk/types/HandleRequest.html) - that is, a function literal rather than a function declaration. It takes a [`HttpRequest` object](https://fermyon.github.io/spin-js-sdk/interfaces/HttpRequest.html), and returns a [`HttpResponse` object](https://fermyon.github.io/spin-js-sdk/interfaces/HttpResponse.html), both defined in the `@fermyon/spin-sdk` package:

```javascript
import { HandleRequest, HttpRequest, HttpResponse} from "@fermyon/spin-sdk"
Expand Down
6 changes: 4 additions & 2 deletions content/spin/v2/javascript-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ With JavaScript being a very popular language, Spin provides support for buildin
> All examples from this page can be found in [the JavaScript SDK repository on GitHub](https://github.com/fermyon/spin-js-sdk/tree/main/examples).
[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/)

In order to compile JavaScript programs to Spin components, you also need to install a Spin plugin `js2wasm` using the following command:

<!-- @selectiveCpy -->
Expand Down Expand Up @@ -390,7 +392,7 @@ Some NPM packages can be installed and used in the component. If a popular libra

### Suggested Libraries for Common Tasks

These are some of the suggested libraries that have been tested and confired to work with the SDK for common tasks.
These are some of the suggested libraries that have been tested and confirmed to work with the SDK for common tasks.

{{ details "HTML parsers" "- [node-html-parser](https://www.npmjs.com/package/node-html-parser)" }}

Expand All @@ -404,4 +406,4 @@ These are some of the suggested libraries that have been tested and confired to

- All `spin-sdk` related functions and methods (like `Config`, `Redis`, `Mysql`, `Pg`, `Kv` and `Sqlite`) can be called only inside the `handleRequest` function. This includes the usage of `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.
- Only a subset of the browser and `Node.js` APIs are implemented.
- The support for Crypto module is limited. The methods currently supported are `crypto.getRandomValues`, `crypto.subtle.digest`, `cryto.createHash` and `crypto.createHmac`
- The support for Crypto module is limited. The methods currently supported are `crypto.getRandomValues`, `crypto.subtle.digest`, `crypto.createHash` and `crypto.createHmac`
10 changes: 6 additions & 4 deletions content/spin/v2/kv-store-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ fn handle_request(_req: Request) -> Result<impl IntoResponse> {

{{ startTab "Typescript"}}

With Typescript, the key value functions can be accessed after opening a store using either the `Kv.open` or the `Kv.openDefault` methods which returns a handle to the store. For example:
[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/)

With Typescript, the key value functions can be accessed after opening a store using either [the `Kv.open` or the `Kv.openDefault` methods](https://fermyon.github.io/spin-js-sdk/variables/Kv.html) which returns a [handle to the store](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.KvStore.html). For example:

```javascript
import { HandleRequest, HttpRequest, HttpResponse, Kv } from "@fermyon/spin-sdk"
Expand All @@ -99,14 +101,14 @@ export const handleRequest: HandleRequest = async function (request: HttpRequest
**General Notes**
- The spinSdk object is always available at runtime. Code checking and completion are available in TypeScript at design time if the module imports anything from the @fermyon/spin-sdk package. For example:
`get` **Operation**
[`get` **Operation**](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.KvStore.html#get)
- The result is of the type `ArrayBuffer | null`
- If the key does not exist, `get` returns `null`
`set` **Operation**
[`set` **Operation**](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.KvStore.html#set)
- The value argument is of the type `ArrayBuffer | string`.
`setJson` and `getJson` **Operation**
[`setJson`](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.KvStore.html#setJson) and [`getJson` **Operation**](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.KvStore.html#getJson)
- JavaScript and TypeScript 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 an error is thrown (note that this is different behavior from `get`).
{{ blockEnd }}
Expand Down
2 changes: 2 additions & 0 deletions content/spin/v2/language-support-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ This page contains information about language support for Spin features:

{{ startTab "TypeScript"}}

[Visit the Spin SDK reference documentation.](https://fermyon.github.io/spin-js-sdk/)

| Feature | SDK Supported? |
|-----|-----|
| **Triggers** |
Expand Down
2 changes: 2 additions & 0 deletions content/spin/v2/rdbms-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ For full information about the MySQL and PostgreSQL APIs, see [the Spin SDK refe

{{ startTab "TypeScript"}}

[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/)

The code below is an [Outbound MySQL example](https://github.com/fermyon/spin-js-sdk/tree/main/examples/typescript/outbound_mysql). There is also an outbound [PostgreSQL example](https://github.com/fermyon/spin-js-sdk/tree/main/examples/typescript/outbound_pg) available.

```ts
Expand Down
4 changes: 3 additions & 1 deletion content/spin/v2/redis-outbound.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ You can find a complete Rust code example for using outbound Redis from an HTTP

{{ startTab "TypeScript"}}

Redis functions are available on the `Redis` object. The function names match the operations above, but you must pass the Redis instance address to _each_ operation as its first parameter. For example:
[**Want to go straight to the Spin SDK reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/)

Redis functions are available on [the `Redis` object](https://fermyon.github.io/spin-js-sdk/variables/Redis.html). The function names match the operations above, but you must pass the Redis instance address to _each_ operation as its first parameter. For example:

```javascript
import {Redis} from "@fermyon/spin-sdk"
Expand Down
14 changes: 8 additions & 6 deletions content/spin/v2/serverless-ai-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ The `infer_with_options` examples, operation:

{{ startTab "Typescript"}}

To use Serverless AI functions, the `Llm` module from the Spin SDK provides two methods: `infer` and `generateEmbeddings`. For example:
[**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/variables/Llm.html)

To use Serverless AI functions, [the `Llm` module](https://fermyon.github.io/spin-js-sdk/variables/Llm.html) from the Spin SDK provides two methods: `infer` and `generateEmbeddings`. For example:

```javascript
import { EmbeddingModels, HandleRequest, HttpRequest, HttpResponse, InferencingModels, Llm} from "@fermyon/spin-sdk"
Expand All @@ -132,15 +134,15 @@ export const handleRequest: HandleRequest = async function (request: HttpRequest
`infer` operation:

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

`generateEmbeddings` operation:

- It takes two arguments - model name and list of strings to generate the embeddings for.
- The model name is a string. There are enums for the inbuilt models (AllMiniLmL6V2) in `EmbeddingModels`.
- The return value is of the type `number[][]
- The model name is a string. There are enums for the inbuilt models (AllMiniLmL6V2) in [`EmbeddingModels`](https://fermyon.github.io/spin-js-sdk/enums/EmbeddingModels.html).
- The return value is an [`EmbeddingResult`](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.EmbeddingResult.html)

{{ blockEnd }}

Expand Down
6 changes: 4 additions & 2 deletions content/spin/v2/sqlite-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ struct ToDo {

{{ startTab "Typescript"}}

To use SQLite functions, use the `Sqlite.open` or `Sqlite.openDefault` function to obtain a `Connection` object. `Connection` provides the `execute` method as described above. For example:
[**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/variables/Sqlite.html)

To use SQLite functions, use [the `Sqlite.open` or `Sqlite.openDefault` function](https://fermyon.github.io/spin-js-sdk/variables/Sqlite.html) to obtain [a `SqliteStore` object](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.SqliteStore.html). `SqliteStore` provides the `execute` method as described above. For example:

```javascript
import {Sqlite} from "@fermyon/spin-sdk"
Expand All @@ -122,7 +124,7 @@ const json = JSON.stringify(result.rows);
* The `spinSdk` object is always available at runtime. Code checking and completion are available in TypeScript at design time if the module imports anything from the `@fermyon/spin-sdk` package.
* Parameters are JavaScript values (numbers, strings, byte arrays, or nulls). Spin infers the underlying SQL type.
* The `execute` function returns an object with `rows` and `columns` properties. `columns` is an array of strings representing column names. `rows` is an array of rows, each of which is an array of JavaScript values (as above) in the same order as `columns`.
* The `Connection` object doesn't surface the `close` function.
* The `SqliteStore` object doesn't surface the `close` function.
* If a Spin SDK function fails, it throws an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).

{{ blockEnd }}
Expand Down
4 changes: 3 additions & 1 deletion content/spin/v2/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ fn handle_spin_example(req: Request) -> Result<impl IntoResponse> {

{{ startTab "TypeScript"}}

The function is available in the `Config` package and is named `get`.
[**Want to go straight to the reference documentation?** Find it here.](https://fermyon.github.io/spin-js-sdk/variables/Config.html)

The function is available on [the `Config` object](https://fermyon.github.io/spin-js-sdk/variables/Config.html) and is named [`get`](https://fermyon.github.io/spin-js-sdk/interfaces/_internal_.SpinConfig.html#get).

> Note that the name is `Config` rather than `Variables`.
Expand Down

0 comments on commit 5e684dc

Please sign in to comment.