2.1.4 • Published 9 months ago

as-fetch v2.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Fetch

A fully asynchronous or optionally synchronous fetch implementation for AssemblyScript

Installation

npm install as-fetch

Setting up (Asynchronous Fetch)

Add the fetch imports to your instantiation file.

Raw Bindings

import { readFileSync } from "fs";

import { instantiate } from "./build/test.js";
import { FetchHandler } from "as-fetch/bindings.raw.esm.js";

const binary = readFileSync("./build/test.wasm");
const compiled = new WebAssembly.Module(binary);

// You can use your own implementation of fetch
const Fetch = new FetchHandler(fetch);

instantiate(compiled, { ...Fetch.imports }).then((exports) => {
  Fetch.init(exports, exports.main);
  exports.main();
});

ESM Bindings

import { main, responseHandler } from "./build/test.js";
// Fetch API must exist on the host
fetch.setResponseHandler = responseHandler;
main();

Setting up (Synchronous Fetch)

Add the transform to your asc command (e.g. in package.json)

--transform as-fetch/transform

Alternatively, add it to your asconfig.json

{
  "options": {
    "transform": ["as-fetch/transform"]
  }
}

Finally, add the fetch imports to your instantiation file.

Raw Bindings

import { readFileSync } from "fs";

import { instantiate } from "./build/test.js";
import { FetchHandler } from "as-fetch/bindings.raw.esm.js";

const binary = readFileSync("./build/test.wasm");
const compiled = new WebAssembly.Module(binary);

// You can use your own implementation of fetch
const Fetch = new FetchHandler(fetch);

instantiate(compiled, { ...Fetch.imports }).then((exports) => {
  Fetch.init(exports, exports.main);
  exports.main();
});

ESM Bindings are not supported for sync fetch. Use Raw instead

Usage

Asynchronous Fetch

import { fetch } from "as-fetch/assembly";

// Make sure to add this line! (For async only)
export { responseHandler } from "as-fetch/assembly";

fetch("http://api.quotable.io/random", {
  method: "GET",
  mode: "no-cors",
  headers: [],
  body: null,
}).then((response) => {
  let body = response.text();
  console.log("Ok: " + response.ok.toString());
  console.log("Status: " + response.status.toString());
  console.log("Status Text: " + response.statusText);
  console.log("Redirected: " + response.redirected.toString());
  console.log("Response: " + body);
});

Synchronous Fetch

import { fetchSync } from "as-fetch/sync";

const response = fetchSync("http://api.quotable.io/random", {
  method: "GET",
  mode: "no-cors",
  headers: [],
  body: null,
});

let body = response.text();
console.log("Ok: " + response.ok.toString());
console.log("Status: " + response.status.toString());
console.log("Status Text: " + response.statusText);
console.log("Redirected: " + response.redirected.toString());
console.log("Response: " + body);
2.1.2

9 months ago

2.0.3

10 months ago

2.1.1

9 months ago

2.0.2

10 months ago

2.1.4

9 months ago

2.0.5

10 months ago

2.1.3

9 months ago

2.0.4

10 months ago

2.0.7

10 months ago

2.0.6

10 months ago

2.0.9

10 months ago

2.0.8

10 months ago

2.1.0

9 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.1.3

2 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.1.2

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago