Flux Fetch
Installation
Install from the repo:
$ npm install --save flux-fetch
Import the package into your project:
// Using ES6 modules; // Using CommonJS modulesvar fluxFetch = 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:
;; { return
Development
git clone git@github.com: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