All Projects → starlight36 → fetch-http-client

starlight36 / fetch-http-client

Licence: MIT license
A http client wrapper for fetch api with middleware support.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to fetch-http-client

Rump
REST client for Java that allows for easy configuration and default values. Allows for quick request construction and a huge range of modifications by using response/request interceptors, adjusting default values related to HTTP requests and creating custom instances for when you need multiple API connection setups.
Stars: ✭ 55 (+30.95%)
Mutual labels:  asynchronous, rest-client
asynctools
Various asynchronous tools for Nim language
Stars: ✭ 88 (+109.52%)
Mutual labels:  asynchronous
rails async migrations
Asynchronous support for ActiveRecord::Migration
Stars: ✭ 56 (+33.33%)
Mutual labels:  asynchronous
ambi
Ambi lets you execute any function ambidextrously; providing you the ability to execute any function (be it synchronous, asynchronous, returns, callbacks, promises) as if it returned a promise.
Stars: ✭ 13 (-69.05%)
Mutual labels:  asynchronous
stencil-fetch
Fetch API implementation with Stenciljs
Stars: ✭ 18 (-57.14%)
Mutual labels:  fetch-api
Promise
Asynchronous Programming with Promises
Stars: ✭ 15 (-64.29%)
Mutual labels:  asynchronous
apiron
🍳 apiron is a Python package that helps you cook a tasty client for RESTful APIs. Just don't wash it with SOAP.
Stars: ✭ 106 (+152.38%)
Mutual labels:  rest-client
cheap-watch
If it works, why use something else? // Mirror of https://git.chor.date/Conduitry/cheap-watch
Stars: ✭ 64 (+52.38%)
Mutual labels:  asynchronous
node-fetch-har
Generate HAR entries for requests made with node-fetch
Stars: ✭ 23 (-45.24%)
Mutual labels:  fetch-api
homeberry
HomeBerry is an Android remote control app for your Raspberry PI
Stars: ✭ 31 (-26.19%)
Mutual labels:  rest-client
arv
ARV: Asynchronous RISC-V Go High-level Functional Model
Stars: ✭ 18 (-57.14%)
Mutual labels:  asynchronous
twjitm-core
采用Netty信息加载实现长连接实时通讯系统,客户端可以值任何场景,支持实时http通讯、webSocket通讯、tcp协议通讯、和udp协议通讯、广播协议等 通过http协议,rpc协议。 采用自定义网络数据包结构, 实现自定义网络栈。
Stars: ✭ 98 (+133.33%)
Mutual labels:  asynchronous
asynchronous
A D port of Python's asyncio library
Stars: ✭ 35 (-16.67%)
Mutual labels:  asynchronous
dynamic-home-example-flutter
How to use dynamic home page in Flutter?
Stars: ✭ 21 (-50%)
Mutual labels:  asynchronous
buckshot
A fast and capable Minecraft name sniper.
Stars: ✭ 21 (-50%)
Mutual labels:  asynchronous
future.callr
🚀 R package future.callr: A Future API for Parallel Processing using 'callr'
Stars: ✭ 52 (+23.81%)
Mutual labels:  asynchronous
fusionauth-python-client
FusionAuth Python Client
Stars: ✭ 16 (-61.9%)
Mutual labels:  rest-client
asynchronous
[READ ONLY] Asynchronous messaging using SimpleBus. Read the full documentation here:
Stars: ✭ 24 (-42.86%)
Mutual labels:  asynchronous
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (-26.19%)
Mutual labels:  asynchronous
ip scan
Scan a list of IPs quickly using multithreading
Stars: ✭ 13 (-69.05%)
Mutual labels:  asynchronous

Fetch Http Client

GitHub license npm version Build Status Coverage Status

A http client wrapper for Fetch API with middleware support.

Introduction

Fetch API is a elegant way to access HTTP resources. I used it in my React/ReactNative project as the default network layer. But it still has some inconvenience to use. For example, every request should carry the access token in HTTP request headers, ervery request error should be logged to console etc.

If Fetch API support middleware, everything can be elegantly fixed. Both fetch-plus and http-client provided the middleware support, but if you need some asynchronous pre-request opreation, they could not suppport elegantly.

So this project is another choice to use Fetch API with middleware support, it's quite simple and powerful.

Installation

npm install fetch-http-client --save

Usage

Import

import FetchHttpClient, { json } from 'fetch-http-client';

Quick start

// Create a new client object.
const client = new FetchHttpClient('http://api.example.com/endpoint');

// Add access token
client.addMiddleware(request => {
  request.options.headers['X-Access-Token'] = 'secret';
});

// Add json support
client.addMiddleware(json());

// Add Logging
client.addMiddleware(request => response => {
  console.log(request, response);
});

// Fire request.
client.get('test').then(response => console.log(response.jsonData));

// Path variables support.
client.get('users/{id}', { uriParams: { id: 1 } }).then(response => console.log(response.jsonData));

Asynchronous pre-request middleware

if your access token is stored in a asynchronous storage, it should be fetch before every request, you can use such kind of middleware:

// Add access token asynchronously
client.addMiddleware(request => {
  return AsynchronousStorage.fetch('accessToken').then(token => {
    request.options.headers['X-Access-Token'] = token;
    return request;
  });
});

That means your middleware could return a Promise object and the real request opreate will be issued after the asynchronous method finished.

NEVER forget returning the request object after you handled the result!

API

FetchHttpClient

new FetchHttpClient(baseUrl:string);

fetch

fetch method can been used the same as Fetch API.

instance.fetch(uri:string[, options: object])

request

Convenience way to issue a request with specific verb.

instance.request(uri:string, method:string[, options: object])

get

Convenience way to issue a GET request.

instance.get(uri:string[, options: object])

post

Convenience way to issue a POST request.

instance.post(uri:string[, options: object])

put

Convenience way to issue a PUT request.

instance.put(uri:string[, options: object])

delete

Convenience way to issue a DELETE request.

instance.delete(uri:string[, options: object])

patch

Convenience way to issue a PATCH request.

instance.patch(uri:string[, options: object])

Build-in middlewares

query

This middleware could add the ability to append object value to query string:

// Add query middleware
client.addMiddleware(query());

// Request
client.get('test', {
  query: {
    foo: 'FOO',
    bar: 'BAR',
  },
});

It will request to http://api.example.com/endpoint/test?foo=FOO&bar=BAR.

form

Like query, this could be used to handle post form values.

// Add form middleware
client.addMiddleware(form());

// Request
client.post('test', {
  form: {
    foo: 'FOO',
    bar: 'BAR',
  },
});

header

A convenience middleware to add headers to request.

// Add header middleware
client.addMiddleware(header({
  'X-Request-By': 'FetchHttpClient',
}));

userAgent

A convenience middleware to set User-Agent to headers.

// Add header middleware
client.addMiddleware(userAgent({
  'Client': '1.1',
}));

json

Convert object to request and parse from response.

// Add json middleware
client.addMiddleware(json());

// Request
client.post('add', {
  json: {
    foo: 'FOO',
  },
}).then(response => {
  console.log(response.jsonData);
});

timeout

Set timeout options to fetch.

// global timeout option
client.addMiddleware(timeout(1000));

// Request timeout option priority global timeout option
client.get('test', {
  timeout: 2000, // If you not add timeout middleware, it will not take effect
});

credentials

Set credentials options to fetch. If you want to automatically send cookies for the current domain, use this middleware and config it as same-origin.

// Add credentials middleware
client.addMiddleware(credentials('same-origin'));

Feedback

If you have any questions, use Issues.

Sina Weibo: @starlight36

License

MIT Licence.

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].