0.1.1 • Published 8 years ago

ffetch v0.1.1

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

ffetch

Simple thin fetch wrapper. ffetch means more human "f"riendly "fetch".

npm version Circle CI Circle CI

Example

import ffetch from 'ffetch';

// fetch from GET /path/to/api/page/3?q=github&order=id
ffetch.get('/path/to/api/page/:page', {
  params: { page: 3 },
  queries: { q: 'github', order: 'id' },
})
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));
import { FFetch } from 'ffetch';

// create your ffetch instance with config
const ffetch = new FFetch({
  baseUrl: 'http://your.web.api/v2',
  headers: {
    'X-Auth-Token': '123456789ABCDEF0',
  },
});

// send JSON payload to PUT http://your.web.api/v2/path/to/api
ffetch.put('/path/to/api', {
  body: {
    title: 'json payload',
    text: 'it will be stringified',
  },
})
  .then(res => console.log(res));

Requirement

  • global.Promise()

ffetch works on both of the Browser and the Node.js but It needs Promise API.

Usage

Working on the Browser:

// Promise() polyfill
import { Promise } from 'es6-promise';

window.Promise = Promise;

Then, use directly:

import ffetch from 'ffetch';
import fetch from 'whatwg-fetch';  // just a polyfill

// call fetch() friendly
ffetch.get(/* ... */)
  .then(res => /* ... */)
  .catch(err => /* ... */);

Or use your instance with options:

import { FFetch } from 'ffetch';

const ffetch = new FFetch({
  fetch: () => { /* your custom fetch function */ },
});

// call fetch() friendly
ffetch.get(/* ... */)
  .then(res => /* ... */)
  .catch(err => /* ... */);

Working on the Node.js:

import { Promise } from 'es6-promise';
import nodeFetch from 'node-fetch';
import { FFetch } from 'ffetch';

const ffetch = new FFetch({
  fetch: nodeFetch,
});

// call fetch() friendly
ffetch.get(/* ... */)
  .then(res => /* ... */)
  .catch(err => /* ... */);

API

ffetch.get(url: string , options: object): Promise

ffetch.post()

ffetch.put()

ffetch.del()

ffetch.head()

ffetch.opt()

Call fetch() like human friendly.

ffetch.get('/path/to/api/page/:page', {
  params: { page: 3 },
  queries: { q: 'github', order: 'id' },
})
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));
argumenttype
urlstringURL of request.
options.paramsobjectURL parameters.
options.queriesobjectURL queries.
options.headersobjectRequest headers.
options.bodyRequest body. If it is an object or an array, It will be a string by JSON.stringify().
options.timeoutnumberIf request exceeded this value, ffetch() throws an error(promisified).
options.***Some other options.

new FFetch(options)

Create an instance for fetching.

import fetch from 'node-fetch';

const ffetch = new FFetch({
  baseUrl: 'http://your.web.api/v2',
  headers: {
    'X-Auth-Token': '123456789ABCDEF0',
  },
  timeout: 30000,
  fetch,
});
argumenttype
options.baseUrlstringURL prefix of each request.
options.headersobjectRequest headers. it will merge to each request.
options.timeoutnumberthe default of options.timeout of such as ffetch.get().
options.fetchfunctionCustom request function. default: '(global).fetch'
0.1.1

8 years ago

0.1.0

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

9 years ago