3.2.1 • Published 6 months ago

http-with-fetch v3.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 months ago

http-with-fetch

A tiny abstraction over building featureful wrappers over the fetch API.

Installation

npm i http-with-fetch

Usage

import { CredentialsManager, createApiClient } from "http-with-fetch";

class DefaultCredentialsManager implements CredentialsManager {
  async attach(init: RequestInit, url: string) {
    init.credentials = "include";
    return { init, url };
  }
  async isValid(_: Request, res: Response) {
    if (res.status === 401) {
      return false;
    }
    return true;
  }
}

const visitor = {
  async ok(response: Response) {
    return response.json();
  },
  async fail(request: Request, response: Response) {
    let error = await response.json();
    if (process.env.NODE_ENV !== "production") {
      console.error("API Call Failed: ", error, request);
    }
    throw new Error(error);
  },
};

const credentials = new DefaultCredentialsManager();

const client = createApiClient({ credentials, visitor }, false);

const list = client.get("/resource", { limit: 10 });
3.2.1

6 months ago

3.2.0

10 months ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.0

7 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago