Simple XHR and navigator.sendBeacon wrapper
Maybe fetch in a later version
No dependencies
npm install paper-plane
paper-plane is an ES module.
However, dist/paperplan.min.js
is not (as browser support for ESM is not widespread), this file will create a PaperPlane
object attached to the window (window.PaperPlane
). This will likely change in the future.
const headers = new Map();
const successCallback = function() {
alert('GET call successful');
};
PaperPlane.get(
'/endpoint',
headers,
successCallback
);
const headers = new Map();
const successCallback = function() {
alert('HEAD call successful');
};
PaperPlane.head(
'/endpoint',
headers,
successCallback
);
Sending data requires constructing a request payload or query string with one of the PaperPlane helper methods. The examples below construct a JSON payload via PaperPlane.makeJsonRequestData()
.
Constructing a payload from FormData is supported via PaperPlane.makeFormDataRequestData()
.
Constructing a payload from a Blob (or File, as a File is a Blob) is supported via PaperPlane.makeBlobRequestData()
.
Constructing a query string (to append to an endpoint) is supported via PaperPlane.makeUrlQueryString()
const headers = new Map();
const successCallback = function() {
alert('POST call successful');
};
PaperPlane.post(
'/endpoint',
PaperPlane.makeJsonRequestData({}, headers),
successCallback
);
const headers = new Map();
const successCallback = function() {
alert('PUT call successful');
};
PaperPlane.put(
'/resource/id',
PaperPlane.makeJsonRequestData({}, headers),
successCallback
);
Send an HTTP beacon request to server.
PaperPlane.postBeacon(
'/analytics/view',
PaperPlane.makeJsonRequestData({}, headers)
);
const headers = new Map();
const successCallback = function() {
alert('DELETE call successful');
};
PaperPlane.delete(
'/resources/id',
PaperPlane.makeJsonRequestData({}, headers),
successCallback
);