Type alias PawnoteFetcher

PawnoteFetcher: ((url, options) => Promise<{
    headers: Record<string, string> | Headers;
    json: (<T>() => Promise<T>);
    text: (() => Promise<string>);
}>)

A fetcher that looks like the Fetch API so every fetcher applied to Pawnote will have the same API and should output the same thing.

Type declaration

    • (url, options): Promise<{
          headers: Record<string, string> | Headers;
          json: (<T>() => Promise<T>);
          text: (() => Promise<string>);
      }>
    • Parameters

      • url: string
      • options: {
            body?: string | FormData;
            headers?: Record<string, string> | Headers;
            method: "GET" | "POST";
            redirect?: "follow" | "manual";
        }
        • Optional body?: string | FormData

          Body of the request of type given in the "Content-Type" header.

        • Optional headers?: Record<string, string> | Headers

          Headers that should be appended to the request.

        • method: "GET" | "POST"
        • Optional redirect?: "follow" | "manual"

          Whether we should automatically handle the redirections or do it by hand.

      Returns Promise<{
          headers: Record<string, string> | Headers;
          json: (<T>() => Promise<T>);
          text: (() => Promise<string>);
      }>

Example

import type { PawnoteFetcher } from "pawnote";

// With the `fetch()` builtin, in TypeScript.
// This is actually the code for the default fetcher.
const fetcher: PawnoteFetcher = async (url, options) => {
const response = await fetch(url, {
method: options.method,
headers: options.headers,
redirect: options.redirect,
// Setting a body is not allowed on GET requests.
body: (options.method === "GET") ? void 0 : options.body
});

return {
headers: response.headers,
text: () => response.text(),
json: <T>() => response.json() as T
};
};