Socket
Book a DemoInstallSign in
Socket

as-fetch

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

as-fetch

Fetch API brought to AssemblyScript

latest
Source
npmnpm
Version
2.1.4
Version published
Maintainers
2
Created
Source

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);

Keywords

assemblyscript

FAQs

Package last updated on 10 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts