All Projects → zellwk → Zl Fetch

zellwk / Zl Fetch

Licence: mit
A library that makes the Fetch API a breeze

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Zl Fetch

axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (-77.96%)
Mutual labels:  fetch, ajax, fetch-api
Unfetch
🐕 Bare minimum 500b fetch polyfill.
Stars: ✭ 5,239 (+2716.67%)
Mutual labels:  ajax, fetch
Fetch Suspense
A React hook compatible with React 16.6's Suspense component.
Stars: ✭ 479 (+157.53%)
Mutual labels:  fetch, fetch-api
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-81.72%)
Mutual labels:  fetch, fetch-api
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (+126.34%)
Mutual labels:  fetch, browser
Fetch Examples
A repository of Fetch examples. See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for the corresponding documentation.
Stars: ✭ 431 (+131.72%)
Mutual labels:  fetch, fetch-api
Xhr.js
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
Stars: ✭ 12 (-93.55%)
Mutual labels:  ajax, fetch
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (-75.81%)
Mutual labels:  fetch, ajax
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+458.06%)
Mutual labels:  fetch, fetch-api
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+471.51%)
Mutual labels:  fetch, fetch-api
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (-54.3%)
Mutual labels:  fetch, fetch-api
Redux Requests
Declarative AJAX requests and automatic network state management for single-page applications
Stars: ✭ 330 (+77.42%)
Mutual labels:  ajax, fetch
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (+53.23%)
Mutual labels:  fetch, fetch-api
Kkjsbridge
一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
Stars: ✭ 462 (+148.39%)
Mutual labels:  ajax, fetch
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+44.09%)
Mutual labels:  ajax, fetch
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+3758.06%)
Mutual labels:  fetch, fetch-api
Fetch Plus
🐕 Fetch+ is a convenient Fetch API replacement with first-class middleware support.
Stars: ✭ 116 (-37.63%)
Mutual labels:  ajax, fetch
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (-88.71%)
Mutual labels:  fetch, fetch-api
re-frame-fetch-fx
js/fetch Effect Handler for re-frame
Stars: ✭ 24 (-87.1%)
Mutual labels:  fetch, fetch-api
Wretch Middlewares
Collection of middlewares for the Wretch library. 🎁
Stars: ✭ 42 (-77.42%)
Mutual labels:  ajax, fetch

zlFetch

Features

  1. zlFetch helps you create your request. It helps you:
    1. Create query parameters for GET requests
    2. Do Basic and Bearer-type authorization
    3. Formats body for JSON or x-www-form-urlencoded
  2. zlFetch transforms the response:
    1. It lets you use your responses in the first then method.
    2. It directs 400 and 500 errors into catch.

Download/install

You can install zlFetch through npm.

# Installing through npm
npm install zl-fetch --save

If you want to use zlFetch in your browser, download dist/index.min.js or use the CDN link below:

<script src="https://cdn.jsdelivr.net/npm/zl-fetch"></script>

Quick Start

Basic usage (in Browser)

// Basic usage
zlFetch("http://your-website.com")
  .then(response => console.log(response))
  .catch(error => console.log(error));

You can use import zlFetch the ES6 way if you wish to:

// ES6 imports
import zlFetch from "zl-fetch";
zlFetch("http://your-website.com")
  .then(response => console.log(response))
  .catch(error => console.log(error));

Basic usage (in Node)

You use it the same way you expect to with browsers!

const zlFetch = require("zl-fetch");
zlFetch("http://your-website.com")
  .then(response => console.log(response))
  .catch(error => console.log(error));

Response and Error objects

zlFetch changes the response and error objects. In zlFetch, response and error objects both include these five properties:

  1. headers: response headers
  2. body: response body
  3. status: response status
  4. statusText: response status text
  5. response: original response from Fetch
zlFetch("http://your-website.com")
  .then(response => {
    const headers = response.headers;
    const body = response.body;
  })
  .catch(error => {
    const headers = error.headers;
    const body = error.body;
    const status = error.status;
  });

GET requests

To send a GET request, enter the endpoint as the first argument.

// With zlFetch
zlFetch("http://your-website.com");

// With fetch api
fetch("http://your-website.com");

zlFetch formats and encodes query parameters for you if you provide a queries option.

zlFetch('http://your-website.com', {
  queries: {
    param1: 'value1',
    param2: 'to encode'
  }
})

// With fetch API
fetch('http://your-website.com?param1=value1&param2=to%20encode')

POST requests

Set method to post to send a post request. zlFetch will set Content-Type will be set to application/json for you. It will also convert your body to a JSON string automatically.

// with zlFetch
zlFetch("http://your-website.com", {
  method: "post",
  body: {
    key: "value"
  }
});

// Resultant fetch api
fetch("http://your-website.com", {
  method: "post",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    key: "value"
  })
});

// Setting other content type
zlFetch("http://your-website.com", {
  method: "post",
  headers: { "Content-Type": "application/x-www-form-urlencoded" }
});

Other Content-Types

You may choose to overwrite Content-Type yourself. To do so, pass headers and Content-Type property.

fetch("http://your-website.com", {
  method: "post",
  headers: { "Content-Type": "Another Content Type" },
  body: {
    key: "value"
  )
});

If Content-Type is set to application/x-www-form-urlencoded, zlFetch will format body to be valid for x-www-form-urlencoded.

zlFetch("http://your-website.com", {
  method: "post",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: {
    key: "value",
    web: "https://google.com"
  }
});

// Resultant fetch api
fetch("http://your-website.com", {
  method: "post",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: "key=value&web=https%3A%2F%2Fgoogle.com"
});

Authentication/Authorization

zlFetch adds Authorization headers for you if you include an auth property.

zlFetch("http://your-website.com", {
  auth: /* Your credentials */
})

Basic Authentication

To perform basic authentication, pass username and password into auth.

zlFetch("http://your-website.com", {
  auth: {
    username: 'your-username'
    password: 'your-password'
  }
})

// Resultant fetch api
fetch("http://your-website.com", {
  headers: { Authorization: `Basic ${btoa("yourusername:12345678")}` }
});

Token/Bearer Authentication

To perform token-based authentication, pass your token into auth.

zlFetch("http://your-website.com", {
  auth: "token12345"
});

// Resultant fetch api
fetch("http://your-website.com", {
  headers: { Authorization: `Bearer token12345` }
});

Shorhand methods

From v3.0 onwards, zlFetch supports method shorthands.

Supported shorthand methods include:

  1. get
  2. post
  3. put
  4. patch
  5. delete
// These two are the same
zlFetch.post('http://your-website.com')
zlFetch('http://your-website.com', { method: 'post' })

Response Types

zlFetch supports only json and text response types for now. If you run into an error with a response type, file an issue and I'll support it asap. (This might take time though!)

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