lightpath
v0.0.7
Published
HTTP request wrapper for the browser
Downloads
8
Maintainers
Readme
LightPath
Another Browser based HTTP request wrapper.
Why?
- Promise Based
- Supports Async Await
- Less opinonated than axios
- Easy Error handling
Install
<script src="https://cdn.jsdelivr.net/gh/jakeoverall/lightpath/dist/ligthpath.browser.min.js"></script>
> npm i lightpath
Usage
import LightPath from "lightpath";
// Create Instances
let api = new LightPath({
baseURL: "http://localhost:5000",
timeout: 8000,
headers: { authorization: "Bearer token" },
query: "?api_key=1234567"
});
async function testGetInstance(url) {
try {
await api.get(url);
} catch (e) {
console.log(e);
}
}
// Or use directly
async function testGet(url) {
try {
await LightPath.get(url);
} catch (e) {
console.log(e);
}
}
Error Types
class RequestError extends Error {
constructor(msg, response, body = {}) {
super(msg);
this.response = response;
this.status = response.status;
this.body = body;
}
}
class RequestTimeout extends Error {
constructor(msg, url) {
super(msg);
this.url = url;
}
}
Configure Instance Options
let instance = new LightPath({
baseURL = "",
headers = {},
query = "",
timeout = 8000,
onRequest = (url, options) => {},
onComplete = data => {},
onError = err => {}
})