flux-fetch
v0.1.1
Published
A wrapper for fetch that makes it easier to send requests to Flux
Downloads
6
Readme
Flux Fetch
Installation
Install from the repo:
$ npm install --save flux-fetch
Import the package into your project:
// Using ES6 modules
import fluxFetch from 'flux-fetch';
// Using CommonJS modules
var fluxFetch = require('flux-fetch').default;
NOTE: This will place fetch
, Request
, Response
, and Headers
in your global context
if they are not already there, e.g., if you are in Node.
Usage
fluxFetch(path, options)
Arguments
path
(String): The request URLoptions
(Object ={ fluxToken: String, ...others })
: The custom options that you ultimately want to get sent tofetch
, in addition to the defaults set byfluxFetch
. For example:
fluxToken
(String - required): The user's Flux CSRF token. Most likely, this is available
as the flux_token
cookie.
method
(String - default: 'get'): The request's HTTP method
body
(any): The request payload. If the content-type
header is set to something other than
application/json
, this will be provided as is. Otherwise, it will be JSON-stringified.
GET requests will fail if passed a body.
headers
(Object): A Headers
object. To access a specific header, you can use headers.get(header)
. Listing all headers is
environment-specific, e.g., in Node you can use headers.raw()
and in the browser you can
iterate over the headers with for...of
.
Returns
(Promise --> Object: { status: Number, statusText: String, headers: Object, body?: any })
:
A promise that resolves to the response. If the response fails (i.e., the status is < 200 or
= 300), it will reject with an error that has the same properties.
Suggested Use
You may want to make another wrapper on top of fluxFetch
that is custom to your app's particular
use case. This is particularly useful so that you don't always need to explicitly pass in the
current user's Flux token, as well as for common error handling.
For example:
import cookie from 'js-cookie';
import fluxFetch from 'flux-fetch';
function request(url, options) {
return fluxFetch(url, Object.assign({}, options, { fluxToken: cookie.get('flux_token') })
.catch(handleRequestError);
}
function handleRequestError(error) {
// e.g., check if the user is still logged in and send them back to lgoin if they're not
}
Development
git clone [email protected]:fluxio/flux-fetch.git
npm install
npm run test:watch
to run the tests on changes ornpm test
to run them once
Publishing a New Version
This assumes that dependencies have been installed with npm install
.
- Increment the version in
package.json
using Semver npm run prepublish
git add package.json && git commit -m 'Update to version <version>'
git tag v<version>
git push && git push --tags
npm publish