This repository contains a WebAssembly Component written in Typescript, which:
- Uses
axiosto perform a web request - Runs with a single entrypoint (
wasi:cli'sruninterface) - Works with
wash call - Works with WebAssembly ecosystem tooling (e.g. using
wasmtime runorjco run)
This example also showcases using external dependencies, in this case
the widely used HTTP request library axios.
![WARN] When building this project, ensure you are using a stable NodeJS release.
Use of node version management tools (ex.
nvmor more newer NVM compatible tools likefnm) are recommended -- a.nvmrcfile is included for easy use.
Building this project relies on the following installed software:
| Name | Description |
|---|---|
wash |
Wasmcloud Shell controls your wasmcloud host instances and enables building components |
npm |
Node Package Manager (NPM) which manages packages for for the NodeJS ecosystem |
node |
[NodeJS runtime][nodejs] (see .nvmrc for version) |
wkg |
(optional) wasm-pkg-tools project that makes it easy to pull down WIT definitions |
To simply build the project into a runnable WebAssembly module, you can use wash:
wash buildTo get into a rapid development loop, clone the repo and run wash dev:
wash devwash dev does many things for you:
- Starts the wasmCloud host that can run your WebAssembly component
- Builds this project (including necessary
npmscript targets) - Deploys an HTTP server on port 8000 and links it to your component
- Watches your code for changes and re-deploys when necessary
Since using jco doesn't have any built in mechanism to resolve dependencies, you can use wash build to retrieve
the WIT dependencies for this project:
wash build
If you'd like to use WebAssembly ecosystem tooling, you can use wkg:
wkg wit fetchNote
If you've used wash build on the project at least once, wit/deps is likely already populated.
Once this command runs, you should find files & folders in the wit/deps directory, similar to the following:
wit
├── component.wit
└── deps
├── wasi-cli-0.2.3
│ └── package.wit
├── wasi-clocks-0.2.3
│ └── package.wit
├── wasi-filesystem-0.2.3
│ └── package.wit
├── wasi-io-0.2.3
│ └── package.wit
├── wasi-random-0.2.3
│ └── package.wit
└── wasi-sockets-0.2.3
└── package.wit
8 directories, 7 files
To build and run the entire example (w/ the component served by jco), run the following:
npm install
npm run allNote
By default the example code makes a request to https://jsonplaceholder.typicode.com/todos/1
You should see output like the following:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Once wash dev is serving your component, to invoke the component and perform the HTTP request:
wash call wasmcloud:examples/invoke.callTo learn how to extend this example with additional capabilities, see the TypeScript Language Guide in the wasmCloud documentation.