📝 Codepen ・
🔬 Observable ・
📦 Npm
Ira Fetch: Vanilla JS Fetch API wrapper with goodies 🍒
Ira is a window.fetch API wrapper with some extra stuff. Debug logs, persistent settings and custom currying to request functions with a set of options.
This little wrapper tries to function using current JS Engine features, no babel or typescript used. It's plain vanilla Javascript.
Npm Install
npm install --save irajs
Yarn Install
yarn add irajs
CDN Load
<script src="https://d3portillo.github.io/ira/src/index.min.js"></script>
Usage
import ira from "irajs"
const ira = require("irajs")
ira.get("/stuff")
Playground
Observable playground, live examples with the power of reactivity.
Recommended as start point
https://observablehq.com/@d3portillo/ira-fetch-wrapper
Examples
GET Method
ira.get(`https://postman-echo.com/get?foo1=bar1&foo2=bar2`).then(({ data }) => {
console.log(data.json, data.text, data.blob)
})
ira.config({
headers: {
"Content-type": "application/json",
},
})
Parse blob to base64
const blob = new Blob(["something"])
ira.blobToBase64(blob).then((base64) => console.log(base64))
Base URL
const request = ira.extend({
baseURL: "https://yourendpoint.com/dev/branch",
})
request.get("/binary")
Extend Ira fork
A custom settings fork of main Ira function that's gapped to provided - config
const request = ira.extend({
headers: {
"x-api-key": "somsaltedencryptedawesomekey",
},
debug: true ,
parseBlob: false ,
})
request
.get("https://something")
.then(({ data: { blob } }) => console.info(null == blob))
Fetching with params
ira.get("https://anendpoint", {
params: {
token: 222,
"another-token": 354,
},
})
The Ira Instance
RESPONSE = {
data: { json: Object, text: String, blob: ?Blob }
ok: Boolean,
status: Number,
statusText: String,
statusCode: status<Number>,
error: ?Error
}
ON_REQUEST_PROPS = {
headers: {},
body: ?String,
debug: ?Boolean,
parseBlob: ?Boolean,
...`https://developer.mozilla.org/en-US/docs/Web/API/Request`
}
GLOBAL_SETTINGS = {
headers: {},
debug: Boolean,
parseBlob: Boolean,
baseURL: ?String,
}
HTTP_METHODS = {
get: Function,
put: Function,
post: Function,
head: Function,
delete: Function,
connect: Function,
options: Function,
trace: Function,
}
ira = {
...IRA_HTTP_METHODS,
default(): IRA_HTTP_METHODS.get,
_settings: Object,
config: Function,
extend: Function() => ira ,
blobToBase64: Function
}
Ira will return a void response if an error ocurred and status of 500.
Ira stands for: Go-to
in spanish Ir-a
. Can also mean rage or anger, That's all the feelings you have while handling HTTP stuff : )