Skip to content

Latest commit

 

History

History
138 lines (102 loc) · 4.39 KB

File metadata and controls

138 lines (102 loc) · 4.39 KB

TypeScript Axios example CLI

This repository contains a WebAssembly Component written in Typescript, which:

  • Uses axios to perform a web request
  • Runs with a single entrypoint (wasi:cli's run interface)
  • Works with wash call
  • Works with WebAssembly ecosystem tooling (e.g. using wasmtime run or jco run)

This example also showcases using external dependencies, in this case the widely used HTTP request library axios.

Dependencies

![WARN] When building this project, ensure you are using a stable NodeJS release.

Use of node version management tools (ex. nvm or more newer NVM compatible tools like fnm) are recommended -- a .nvmrc file 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

Quickstart

With wash

To simply build the project into a runnable WebAssembly module, you can use wash:

wash build

To get into a rapid development loop, clone the repo and run wash dev:

wash dev

wash dev does many things for you:

  • Starts the wasmCloud host that can run your WebAssembly component
  • Builds this project (including necessary npm script targets)
  • Deploys an HTTP server on port 8000 and links it to your component
  • Watches your code for changes and re-deploys when necessary

With jco

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 fetch

Note

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 all

Note

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
}

Send a request to the running component

Once wash dev is serving your component, to invoke the component and perform the HTTP request:

wash call wasmcloud:examples/invoke.call

Adding Capabilities

To learn how to extend this example with additional capabilities, see the TypeScript Language Guide in the wasmCloud documentation.