Frisbee

tldr; Stripe-inspired API wrapper around ES6/ES7's fetch() method for making simple HTTP requests (alternative to superagent, request, axios).
If you're using node-fetch
, you need node-fetch@v1.5.3
to use form-data
with files properly (due to https://github.com/bitinn/node-fetch/issues/102)
Index
React Native Usage
- Install the required package (note that
react-native
provides us with a fetch
implementation):
npm install --save frisbee
Browser and Server-Side Usage
-
Install the required packages:
-
Require it, set default options, and make some requests:
import es6promise from 'es6-promise';
es6promise.polyfill();
import 'isomorphic-fetch';
import Frisbee from 'frisbee';
const api = new Frisbee({
baseURI: 'https://api.startup.com',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
makeRequests();
async function makeRequests() {
try {
let res = await api.post('/v1/login');
console.log('response', res.body);
if (res.err) throw res.err;
api.auth(res.body.api_token);
res = await api.post('/v1/messages', { body: 'Hello' });
console.log('response', res.body);
if (res.err) throw res.err;
res = await api.get('/v1/messages', {
body: {
limit: 10,
page: 2
}
});
if (res.err) throw res.err;
res = api.post('/v1/logout');
console.log('response', res.body);
if (res.err) throw res.err;
api.auth();
} catch (err) {
throw err;
}
}
API
import Frisbee from 'frisbee';
Frisbee
is a function that optionally accepts an argument options
, which is an object full of options for constructing your API instance.
Upon being invoked, Frisbee
returns an object with the following chainable methods:
-
api.auth(creds)
- helper function that sets BasicAuth headers, and it accepts user
and pass
arguments
- You can pass
creds
user and pass as an array, arguments, or string: ([user, pass])
, (user, pass)
, or ("user:pass")
, so you shouldn't have any problems!
- If you don't pass both
user
and pass
arguments, then it removes any previously set BasicAuth headers from prior auth()
calls
- If you pass only a
user
, then it will set pass
to an empty string ''
)
- If you pass
:
then it will assume you are trying to set BasicAuth headers using your own user:pass
string
- If you pass more than two keys, then it will throw an error (since BasicAuth only consists of
user
and pass
anyways)
-
api.jwt(token)
- helper function that sets a JWT Bearer header. It accepts the jwt_token
as a single string argument
-
All exposed HTTP methods return a Promise, and they require a path
string, and accept an optional options
object:
- Accepted method arguments:
path
required - the path for the HTTP request (e.g. /v1/login
, will be prefixed with the value of baseURI
mentioned earlier)
options
optional - an object containing options, such as header values, a request body, form data, or a querystring to send along with the request, here are a few examples:
-
To set a custom header value of X-Reply-To
on a POST
request:
const res = await api.post('/messages', {
headers: {
'X-Reply-To': '7s9inuna748y4l1azchi'
}
});
- List of available HTTP methods:
api.get(path, options)
- GET
api.head(path, options)
- HEAD (does not currently work - see tests)
api.post(path, options)
- POST
api.put(path, options)
- PUT
api.del(path, options)
- DELETE
api.options(path, options)
- OPTIONS (does not currently work - see tests)
api.patch(path, options)
- PATCH
-
Note that you can chain the auth
method and an HTTP method together:
const res = await api.auth('foo:bar').get('/');
Frequently Asked Questions
Does this support callbacks, promises, or both?
As of version 1.0.0
we have dropped support for callbacks, it now only supports Promises.
What is the fetch
method?
It is a new method introduced into the ES6 specification. You can read more about it here https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch.
Does the Browser or Node.js support fetch
yet?
Yes, a lot of browsers are now supporting it! See this reference for more information http://caniuse.com/#feat=fetch.
If my engine does not support fetch
yet, is there a polyfill?
Yes you can use the fetch
method (polyfill) from whatwg-fetch or node-fetch.
By default, React Native already has a built-in fetch
out of the box!
Can I make fetch
support older browsers?
Yes, but you'll need a promise polyfill for older browsers.
What is this project about?
Use this package as a universal API wrapper for integrating your API in your client-side or server-side projects.
It's a better working alternative (and with less headaches; at least for me) – for talking to your API – than superagent and the default fetch Network method provide.
Use it for projects in Node, React, Angular, React Native, ...
It supports and is tested for both client-side usage (e.g. with Bower, Browserify, or Webpack, with whatwg-fetch
) and also server-side (with node-fetch
).
Why not just use superagent
or fetch
?
See Background for more information.
Want to build an API back-end with Node.js?
See CrocodileJS as a great starting point, and read this article about building Node.js API's with authentication.
Need help or want to request a feature?
File an issue on GitHub and we'll try our best help you out.
Why don't you have an ES5 compiled version readily available in this git repo?
As this module relies on ES6 fetch, there is currently no backwards compatibility for ES5
Tests
This package is tested to work with whatwg-fetch
and node-fetch
.
This means that it is compatible for both client-side and server-side usage.
Development
- Fork/clone this repository
- Run
npm install
- Run
npm run watch
to watch the src
directory for changes
- Make changes in
src
directory
- Write unit tests in
/test/
if you add more stuff
- Run
npm test
when you're done
- Submit a pull request
Background
The docs suggest that you use superagent
with React Native, but in our experience it did not work properly, therefore we went with the next best solution, the Github fetch
API polyfill included with React Native. After having several issues trying to use fetch
and writing our own API wrapper for a project with it (and running into roadblocks along the way) – we decided to publish this.
Here were the issues we discovered/filed related to this:
We know that solutions like superagent
exist, but they don't seem to work well with React Native (which was our use case for this package).
In addition, the authors of the spec for ES6's fetch support throwing errors instead of catching them and bubbling them up to the callback/promise (for example, with Frisbee any HTTP or API errors are found in the res.err
object).
Therefore we created frisbee
to serve as our API glue, and hopefully it'll serve as yours too.
Contributors
Credits
License
MIT