json-fetch-helpers
v1.0.1
Published
Functions to make JSON APIs easier to use.
Downloads
2
Readme
json-fetch-helpers
Overview
Fetch helper library for JSON APIs.
Quick start
npm install --save json-fetch-helpers
import {
processStatus,
callResource,
getResource,
postResource,
putResource,
deleteResource,
getText,
postText,
putText,
deleteText,
getJSON,
postJSON,
putJSON,
deleteJSON
from 'json-fetch-helpers'
Short example
var configureHelpers = require('json-fetch-helpers').default
let helpers = configureHelpers('http://api.websitehq.com/v1/', {
mode: 'cors',
})
getJSON( '/buckets' )
.then( d => console.log('d', d))
.catch( e => console.log('e', e))
postJSON( '/f/abc123', { foo: 'bar' } )
.then( d => console.log('d', d))
.catch( e => console.log('e', e))
Sample outcomes
---
INPUT:
---
getText("/buckets")
---
OUTPUT:
---
fetch http://api.websitehq.com/v1//buckets {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"mode": "cors",
"method": "GET"
}
---
RESPONSE:
---
text
---
INPUT:
---
getJSON("/buckets")
---
OUTPUT:
---
fetch http://api.websitehq.com/v1//buckets {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"mode": "cors",
"method": "GET"
}
---
RESPONSE:
---
json
---
INPUT:
---
getJSON("/buckets")
---
OUTPUT:
---
fetch http://api.websitehq.com/v1//buckets {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"mode": "cors",
"method": "PUT",
"body": "{\"foo\":\"bar\"}"
}
---
RESPONSE:
---
json
Polyfills
This library depends on a modern JavaScript runtime. Load a polyfill like in core-js or babel-polyfill to support old browsers.
Install required polyfills with core-js:
require('core-js/fn/promise');
require('core-js/fn/object/assign');
Sending Bearer Token
If you want to send the Authorization
header:
Authorization: Bearer {token}
Then set token
in the localStorage object.
localStorage.setItem( 'token', 'my-token' )
Final thought
Documentation is lacking. If you want to use the library then the only option is to read the code.