httpFetch
Individual fetch API
wrapper for the browser
(experimental,
reactionary)
overview
The thing, is pretty fast as it doesnt try to connect worlds,
but rather lives in it's own native environment (the browser).
So it will catch up with you faster than you may think.
HTTP request and response routine is super boring
but standard procedure for all kinds of stuff.
For the UI and interactions, the browser's api is used,
so it's almost the same thing you do at the server side.
While some folks having trouble with so called "native" apis,
the best of us are using wrappers (self-made or libs - doesnt matter).
This thing, is kind of a wrapper,
but packed with extra options and routes
which may or may not happen with you
during the HTTP request/response exchange.
It's more advanced that any other tool (which try to keep pace with the NODE's world)
as it uses (currently) experimental features of the browser (like fetch, streams, encryption).
It may be more aggressive at your environment -
only modern syntax, only modern apis (the approach may be same).
As far as this time point vanishes in the past,
the previous statement becomes falsy.
So, the attempt of escaping the doom is futile:
face it one-to-one with the Spider Mastermind.
[![Spider Mastermind](https://raw.githack.com/determin1st/httpFetch/master/tests/logo.jpg)](http://www.nathanandersonart.com/)
[![](https://data.jsdelivr.com/v1/package/npm/http-fetch-json/badge)](https://www.jsdelivr.com/package/npm/http-fetch-json)
Tests
- Fail: check everything
- Cancellation: cancel anything
- Encryption: encrypt everything (FF only)
- Retry: restart anything
- Download: download anything
- Upload: upload anything
- Streams: stream something
- Mix: mix everything
Try
inject into HTML:
# from CDN (stable):
<script src="https://cdn.jsdelivr.net/npm/http-fetch-json@2/httpFetch.js"></script>
# from GIT (lastest)
<script src="http://raw.githack.com/determin1st/httpFetch/master/httpFetch.js"></script>
get the code:
git clone https://github.com/determin1st/httpFetch
npm i http-fetch-json
Syntax
httpFetch(options[, callback(ok, res)])
httpFetch(url[, data[, callback(ok, res)]])
httpFetch(url[, callback(ok, res)])
Parameters
-
options
- object with:
basic
name | type | default | description |
---|
url | string | | reference to the local or remote web resource (auto-prefixed with baseUrl if doesn't contain sheme) |
data | any | | content to be sent as the request body |
native fetch
advanced
-
callback
- optional result handler function
Returns
Promise
(no callback) or AbortController
(callback)
Result
Optimistic style (the default)
async/await
var res = await httpFetch('/resource');
if (res instanceof Error)
{
}
else if (!res)
{
}
else
{
}
promise
httpFetch('/resource')
.then(function(res) {
if (res instanceof Error)
{
}
else if (!res)
{
}
else
{
}
});
callback
httpFetch('/resource', function(ok, res) {
if (ok && res)
{
}
else if (!res)
{
}
else
{
}
});
Optimistic, when notNull
custom instance
var oFetch = httpFetch.create({
notNull: true
});
async/await
var res = await oFetch('/resource');
if (res instanceof Error)
{
}
else
{
}
promise
oFetch('/resource')
.then(function(res) {
if (res instanceof Error)
{
}
else
{
}
});
callback
oFetch('resource', function(ok, res) {
if (ok)
{
}
else
{
}
});
Pessimistic style, when promiseReject
custom instance
var pFetch = httpFetch.create({
promiseReject: true
});
async/await
try
{
var res = await pFetch('/resource');
if (res)
{
}
else
{
}
}
catch (err)
{
}
promise
oFetch('/resource')
.then(function(res) {
if (res)
{
}
else
{
}
})
.catch(function(err)
{
});
Pessimistic, when promiseReject
and notNull
custom instance
var pFetch = httpFetch.create({
notNull: true,
promiseReject: true
});
async/await
try
{
var res = await pFetch('/resource');
}
catch (err)
{
}
promise
oFetch('/resource')
.then(function(res) {
})
.catch(function(err)
{
});
Result types
FetchError
error categories
if (res instanceof Error)
{
switch (res.id)
{
case 0:
console.log(res.message);
console.log(res.response);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
Advanced
httpFetch.create
httpFetch.create(config)
Parameters
Description
Creates a new instance of of httpFetch
Examples
var a = httpFetch.create();
var b = a.create();
if ((a instanceof httpFetch) &&
(b instanceof httpFetch))
{
}
httpFetch.cancel
httpFetch.cancel()
Description
Cancels all running fetches of the instance
httpFetch.form
httpFetch.form(url, data[, callback(ok, res)])
httpFetch.form(options[, callback(ok, res)])
Description
httpFetch operates with JSON content by default.
This shortcut method allows to send a POST
request
with body conforming to one of the form enctypes:
Parameters
Same as httpFetch
Examples
res = httpFetch.form(url, {
param1: 1,
param2: 2,
param3: 3
});
$sum = $_POST['param1'] + $_POST['param2'] + $_POST['param3'];
echo json_encode($sum);
exit;
console.log(await res);
res = await httpFetch.form(url, {
param1: 1,
param2: 2,
param3: 3,
fileInput: document.querySelector('input[type="file"]')
});
console.log(res);
KISS API
Use POST method (Keep It Simple Stupid)
res = await httpFetch(url, {});
res = await httpFetch(url, undefined);
res = await httpFetch(url, null);
res = await httpFetch(url, {
categories: ['one', 'two'],
flag: true
});
res = await httpFetch(url, {
fullDescription: true,
ownerInfo: true
});
res = await httpFetch(url+'?flags=123&names=one,two&isPulluted=true');
res = await httpFetch(url+'?more=params', params);
res = await httpFetch(url+'/more/params', params);
res = await httpFetch(url, Object.assign(params, {more: "params"}));
if (res instanceof Error) {
console.log(res.status);
}
else {
console.log(res.status);
}
Links
https://javascript.info/fetch-api
https://tom.preston-werner.com/2010/08/23/readme-driven-development.html
https://code.tutsplus.com/tutorials/why-youre-a-bad-php-programmer--net-18384
summary {font-size:1.2em;font-weight:bold;color:skyblue;}