flux-fetch v0.1.1
Flux Fetch
Installation
Install from the repo:
$ npm install --save flux-fetchImport 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 theflux_tokencookie.method(String - default: 'get'): The request's HTTP methodbody(any): The request payload. If thecontent-typeheader is set to something other thanapplication/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 useheaders.get(header). Listing all headers is environment-specific, e.g., in Node you can useheaders.raw()and in the browser you can iterate over the headers withfor...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 git@github.com:fluxio/flux-fetch.gitnpm installnpm run test:watchto run the tests on changes ornpm testto run them once
Publishing a New Version
This assumes that dependencies have been installed with npm install.
- Increment the version in
package.jsonusing Semver npm run prepublishgit add package.json && git commit -m 'Update to version <version>'git tag v<version>git push && git push --tagsnpm publish