Giter Site home page Giter Site logo

fetch-api's Introduction

@sampullman/fetch-api

Small fetch wrapper for creating quick API wrappers


Instructions

Install

npm i -D @sampullman/fetch-api
yarn add @sampullman/fetch-api
pnpm i -D @sampullman/fetch-api

Configuration

FetchApi global configuration is passed to the constructor.

FetchApi({
  // API base URL prepended to requests
  baseUrl: '',

  // Default request timeout
  timeout: 10000,

   // Passed to JSON.stringify and used as fetch `body`.
   // Sets headers: { Accept: 'application/json', 'Content-Type': 'application/json' }
  data: null,

  // Request URL parameters, e.g. `{ 'a': 1 }`. Passed to `new URLSearchParams(params)`
  // Entries with undefined values are filtered out
  params: RequestParams,

  // Convenience for Basic Auth.
  // Sets headers['Authorization'] = `Basic ${btoa(`${auth.username}:${auth.password}`)}`
  auth: { username: 'test', password: 'password' }

  // Request interceptors
  requestInterceptors: [],

  // Response interceptors
  responseInterceptors: [],
});

Usage

Here is an example of basic usage that includes a response interceptor for handling 403 response codes and converting the body to json.

const api = new FetchApi({
  baseUrl: 'https://cool.api/',
  responseInterceptors: [
    async (res) => {
      const { status } = res;

      if (status === 403) {
        throw new Error('FORBIDDEN');
      }
      res.data = await res.json();
      return res;
    },
  ],
});

// Make a get request to 'https://cool.api/status/'
const status = api.request({ url: 'status/' });

Environment

fetch must be available. If you need to support older browsers or Node, use a polyfill such as whatwg-fetch

Example

See the example directory.

License

MIT License © 2021 Samuel Pullman

fetch-api's People

Contributors

sampullman avatar

Stargazers

 avatar

Watchers

 avatar  avatar

fetch-api's Issues

Improve publish workflow

The workflow for publishing is janky and undocumented.

Currently the rough steps are:

  • Make sure latest packages are installed
  • Build library and Vue plugin
  • Run library and plugin tests
  • Manually bump plugin version
  • Run pnpm run release for basic checks and library version bump
  • Run pnpm publish on library and plugin

Tasks

  • Add one of the release tools listed here
  • Investigate Github release automation
  • Add changelog generation

tslib not found

Vite fails to run when using vue3-fetch-api in a project that doesn't already include tslib

[vite] Internal server error: Failed to resolve import "tslib" from "node_modules/@sampullman/fetch-api/dist/fetch-api.mjs". Does the file exist?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.