All Projects β†’ VitorLuizC β†’ Create Request

VitorLuizC / Create Request

Licence: mit
Apply interceptors to `fetch` and create a custom request function.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Create Request

Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+21005.88%)
Mutual labels:  promise, http-client, fetch, fetch-api
Wretch
A tiny wrapper built around fetch with an intuitive syntax. 🍬
Stars: ✭ 2,285 (+6620.59%)
Mutual labels:  promise, http-client, fetch, request
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (+179.41%)
Mutual labels:  http-client, fetch, fetch-api
Mfetch
mfetch will provide you with a strong ability to request resource management
Stars: ✭ 90 (+164.71%)
Mutual labels:  promise, fetch, request
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (-14.71%)
Mutual labels:  fetch, promise, http-client
Frisbee
πŸ• Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+2952.94%)
Mutual labels:  fetch, request, fetch-api
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+3026.47%)
Mutual labels:  http-client, fetch, fetch-api
Ky
🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
Stars: ✭ 7,047 (+20626.47%)
Mutual labels:  http-client, fetch, request
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-55.88%)
Mutual labels:  fetch, promise, request
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (+44.12%)
Mutual labels:  fetch, request, fetch-api
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (-38.24%)
Mutual labels:  fetch, http-client, fetch-api
Gretchen
Making fetch happen in TypeScript.
Stars: ✭ 301 (+785.29%)
Mutual labels:  http-client, fetch, request
miniprogram-network
Redefine the Network API of Wechat MiniProgram (ε°η¨‹εΊη½‘η»œεΊ“)
Stars: ✭ 93 (+173.53%)
Mutual labels:  fetch, promise, request
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (+32.35%)
Mutual labels:  fetch, http-client, request
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (+1138.24%)
Mutual labels:  http-client, fetch, request
P Map
Map over promises concurrently
Stars: ✭ 639 (+1779.41%)
Mutual labels:  async, promise
Urllib
Request HTTP(s) URLs in a complex world
Stars: ✭ 600 (+1664.71%)
Mutual labels:  http-client, request
Fetch
A window.fetch JavaScript polyfill.
Stars: ✭ 25,118 (+73776.47%)
Mutual labels:  promise, fetch
Http Client
Async HTTP/1.1+2 client for PHP based on Amp.
Stars: ✭ 553 (+1526.47%)
Mutual labels:  async, http-client
Awaitkit
The ES8 Async/Await control flow for Swift
Stars: ✭ 709 (+1985.29%)
Mutual labels:  async, promise

@bitty/create-request

License Library minified size Library minified + gzipped size

Apply interceptors to fetch and create a custom request function.

  • ⚑️ Dependency free and pretty small, 0.3KB (minified + gzipped);
  • 🏷 Well type defined with TypeScript (with generics);
  • πŸ“¦ There are ESM, CommonJS and UMD distributions;

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

npm install @bitty/create-request --save

# Use the command below if you're using Yarn.
yarn add @bitty/create-request

Usage

@bitty/create-request is curry function, which applies interceptors to fetch and returns a new request function.

import createRequest from '@bitty/create-request';

type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';

const request = createRequest(window.fetch, {
  onRequest: (method: Method, route: string, data: any = undefined) => ({
    url: 'https://api.example.com' + route,
    body: JSON.stringify(data),
    method,
    headers: { 'Content-Type': 'application/json' }
  }),
  onResponse: (response: Response) => {
    if (response.status === 403)
      throw new Error('Authorization error.');
    return response.json();
  },
  onError: (error:? Error) => {
    sentry.captureException(error);
    return Promise.reject(message);
  }
});

request('POST', '/user', {
  name: 'Vitor'
})
  .then((response) => response.success && alert('User was created!'));

Interceptors

  • onError

    onError?: (reason?: Error) => Promise<never>;
    

    Handle request and response errors.

  • onRequest

    onRequest?: <A extends any[] = [RequestOptions]>(...params: A) => RequestOptions;
    

    Handle request and define request arguments. A generic type defines returned request function arguments type.

    onRequest GIF

  • onRequestError

    onRequestError?: (reason?: Error) => Promise<never>;
    

    Handle request errors. Overwrites onError handling request errors.

  • onResponse

    onResponse?: <R = Response>(response: R) => R | PromiseLike<R>;
    

    Handle response and define the request return. R generic type defines returned request function result type.

    onResponse GIF

  • onResponseError

    onResponseError?: (reason?: Error) => Promise<never>;
    

    Handle response errors. Overwrites onError handling response errors.

Usage on unsupported browsers

Do I support fetch?

Older browsers don't support Fetch API, but you can use unfetch or other polyfill to achieve it.

import fetch from 'unfetch';
import createRequest from '@bitty/create-request';

export default createRequest(fetch, {
  onRequest: (options) => ({
    ...options,
    headers: {
      ...options.headers,
      'Content-Type': 'application/json'
    }
  }),
  onResponse: (response) => response.json()
});

Usage on Node.js

Node environment does not provide global fetch function, but you can use node-fetch to achieve it.

const fetch = require('node-fetch');
const createRequest = require('@bitty/create-request');

module.exports = createRequest(fetch, {
  onResponse (response) {
    return response.json();
  }
});

License

Released under MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].