ffetch
Simple thin fetch wrapper. ffetch
means more human **"f"**riendly "fetch".
Example
; // fetch from GET /path/to/api/page/3?q=github&order=idffetch ;
; // create your ffetch instance with configconst ffetch = baseUrl: 'http://your.web.api/v2' headers: 'X-Auth-Token': '123456789ABCDEF0' ; // send JSON payload to PUT http://your.web.api/v2/path/to/apiffetch ;
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; windowPromise = Promise;
Then, use directly:
;; // just a polyfill // call fetch() friendlyffetch ;
Or use your instance with options:
; const ffetch = { /* your custom fetch function */ }; // call fetch() friendlyffetch ;
Working on the Node.js:
;;; const ffetch = fetch: nodeFetch; // call fetch() friendlyffetch ;
API
ffetch.get(url: string [, options: object]): Promise
ffetch.post()
ffetch.put()
ffetch.del()
ffetch.head()
ffetch.opt()
Call fetch()
like human friendly.
ffetch ;
argument | type | |
---|---|---|
url |
string | URL of request. |
options.params |
object | URL parameters. |
options.queries |
object | URL queries. |
options.headers |
object | Request headers. |
options.body |
Request body. If it is an object or an array, It will be a string by JSON.stringify() . |
|
options.timeout |
number | If request exceeded this value, ffetch() throws an error(promisified). |
options.*** |
Some other options. |
new FFetch([options])
Create an instance for fetching.
; const ffetch = baseUrl: 'http://your.web.api/v2' headers: 'X-Auth-Token': '123456789ABCDEF0' timeout: 30000 fetch;
argument | type | |
---|---|---|
options.baseUrl |
string | URL prefix of each request. |
options.headers |
object | Request headers. it will merge to each request. |
options.timeout |
number | the default of options.timeout of such as ffetch.get() . |
options.fetch |
function | Custom request function. default: '(global).fetch' |