sifrr-fetch ·

Fetch API and websockets API based small, easy to use, promise based requests library for browsers.
Size
Minified (dist/sifrr.fetch.min.js ) |  |
Minified + Gzipped (dist/sifrr.fetch.min.js ) |  |
How to use
Directly in Browser using standalone distribution
Add script tag in your website.
<script src="https://unpkg.com/@sifrr/fetch@{version}/dist/sifrr.fetch.min.js"></script>
Browser API support needed for
Using npm
Do npm i @sifrr/fetch
or yarn add @sifrr/fetch
or add the package to your package.json
file.
example, put in your frontend js module (compatible with webpack/rollup/etc):
Commonjs
window.Sifrr = window.Sifrr || {};
window.Sifrr.Fetch = require('@sifrr/fetch');
ES modules
import Fetch from '@sifrr/fetch';
import { Fetch, Socket } from '@sifrr/fetch';
With node
global.fetch = require('node-fetch);
const { Fetch } = require('@sifrr/fetch');
// use SFetch.get, post etc,
global.WebSocket = require('isomorphic-ws');
const { Socket } = require('@sifrr/fetch');
Note: You can not use websockets with node
API
HTTP Requests
options are Fetch API options with some extra keys:
- params
json object
key, value pairs will be added to url as ?key=value
- body
json object | string
body to send with post requests
- onProgress
function
if response has content-length, this function will be called with
{
loaded,
total,
percent,
speed,
value,
...
}
- timeout
time in ms
timeout for request
- before
function
this function will be called with { url, options, method }
and should return modified { url, options, method }
which will be used to send requests
- after
function
this function will be called with response
and should return modified response
- use
function
this function will be called with { url, options, method }
and resolve/return with response which will be returned, if this function errors, response will be fetched normally (use case: use it as a middleware for cache)
GET request
you can add query parameters to get request options.
options.query = { key: 'value' };
Sifrr.Fetch.get(url, options)
.then(response => {
})
.catch(e => {
});
PUT request
Sifrr.Fetch.put(url, options).then(response => {
});
POST request
you can add post request body parameters to post request options.
options.body = { key: 'value' };
options.headers = {
'content-type': 'aaplication/json'
};
Sifrr.Fetch.post(url, options).then(response => {
});
DELETE request
Sifrr.Fetch.delete(url, options).then(response => {
});
GET file request
Sifrr.Fetch.file(url, options).then(response => {
});
GRAPHQL request
graphql request is a POST request.
Sifrr.Fetch.graphql(url, {
query: 'graphql query string',
variables: { a: 'b' },
...otherOptions
}).then(response => {
});
Cache as Middleware
const storage = new Sifrr.Storage();
function cacheOrGet(url) {
Sifrr.Fetch.get(url, {
use: url =>
storage.get(url).then(v => (typeof v[url] === 'undefined' ? throw 'Not found' : v[url])),
after: response => {
storage.set(url, response);
return response;
}
});
}
Instance with default options
const fetch = new Sifrr.Fetch(defaultOptions);
fetch.get;
fetch.put;
fetch.post;
fetch.delete;
fetch.graphql;
WebSockets
Automatic connection retries, calls fallback on message sending failure/error
WebSocket fetch
Note: Only works with JSON messages/responses
const socket = new Sifrr.Fetch.Socket(url, protocols, fallback );
socket.send(message [, type]).then(resp => {
});
Graphql query over websocket (compatible with sifrr-server)
socket.graphql({
query: ...,
variables: ...
}).then(data => {
});
Graphql Subscriptions (compatible with graphql-subscription-ws and sifrr-server)
let subscriptionId;
socket.subscribe({ query: `subscription { ... }`, variables: { ... } }, callback).then(id => subscriptionId = id);
socket.unsubscribe(subscriptionId).then(...);
Traditional WebSocket messaging
const socket = new Sifrr.Fetch.Socket(
url,
protocols,
fallback
);
socket.sendRaw(message);
Hooks
socket.onmessage = event => {};
socker.onopen = () => {};
socker.onclose = () => {};
socker.onerror = e => {};
References