1.1.0 • Published 8 years ago

fetch-x v1.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

fetch-x

Build Status

A wrapper around the new fetch API

Install

$ npm install fetch-x

Usage

import fetchX from 'fetch-x';

GET request

fetchX.get('/xxx?query=12345')
.then(json => console.log(json))
.catch(error => console.error(error));

POST request

the default Content-Type is application/x-www-form-urlencoded

fetchX.post('/xxx', {
  param1: 'jack',
  param2: 'pony'
})
.then(json => console.log(json))
.catch(error => console.error(error));

Methods

the default credentials option is include)

fetchX.request(url[, options]

fetchX.get(url[, data[, options]])

fetchX.post(url[, data[, options]])

fetchX.delete(url[, options])

fetchX.head(url[, options])

fetchX.put(url[, data[, options]])

fetchX.patch(url[, data[, options]])  

fetchX.create([options])

const myfetch = fetchX.create({
  headers: {
    'Authorization': 'Bearer ' + getAPIToken(),
    'X-My-Custom-Header': 'CustomHeader'
  }
});

myfetch.get(url)
.then(res => res.json())
.then(json => console.log(json))
.catch(error => console.error(error));

Middleware

add middleware to intercept response

fetchX.applyMiddleware({
  response: [res => res.json(), json => {
    if (json.code === 'xxx') {
      // do something...
    } else if (...) {
      // do something...
    } else {
      // do something...
      return json;
    }
  }],
  request: request => {
    request.url = xxx + request.url;
    return request;
  }
})
1.1.0

8 years ago

1.0.0

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.1.4

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago