Skip to content

BlazeInferno64/blazed.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

blazed.js

Blazing-fast, light weight, high-performance, promise-based HTTP client

Npm Package

For the npm package check here or here for the source code

Setup and installation

Include the library in your project:

<script src="https://raw.githubusercontent.com/BlazeInferno64/blazed.js/main/lib/client/blazed.js"></script>

Or you can include it like this too

<script src="https://blazeinferno64.github.io/blazed.js/lib/client/blazed.js"></script>

Or there's another alternative way to load, i.e, using jsDelivr

<script src="https://cdn.jsdelivr.net/gh/blazeinferno64/blazed.js/lib/client/blazed.js"></script>

Info

Using blazed.js you can perform advanced HTTP Requests from your browser directly with custom error handling like network error, timeout,etc. and automatic JSON Parsing

Its based on XMLHttpRequest Library which is already built-in almost all the modern web browsers!

Blazed()

Include this in your script

const blazed = new Blazed();

GET Request

After you have included this to your project Here's how you can perform a GET request:

blazed.get("https://jsonplaceholder.typicode.com/posts/1")
    .then(data => console.log(data)) // No need for data.json() as it automatically parses it as a JSON object!
    .catch(err => console.error(err)); // For error handling

It's similar to the native fetch api which is built-in modern web browsers!

POST Request

To perform POST Requests, you need to have some data for posting. Here's how you can achieve this

blazed.post("https://jsonplaceholder.typicode.com/posts", { title: 'foo', body: 'bar', userId: 1 }) // Popsting with some dummy data
    .then(data => console.log(data)) // Saving the response to the console
    .catch(err => console.error(err)); // Again catching any errors

DELETE Request

Perform DELETE Request as belows

// Perform DELETE request
blazed.delete("https://jsonplaceholder.typicode.com/posts/1")
    .then(response => {
        console.log("DELETE successful:", response);
    })
    .catch(error => {
        console.error("DELETE failed:", error);
    });

PUT Request

To perform PUT Request, you can follow the given example which is similar to POST Requests

const putData = {
    title: 'foo',
    body: 'bar',
    userId: 1
};

// Perform PUT request
blazed.put("https://jsonplaceholder.typicode.com/posts/1", putData)
    .then(response => {
        console.log("PUT successful:", response);
    })
    .catch(error => {
        console.error("PUT failed:", error);
    });

Request Cancellation

You can also cancel a request Here's a simple example

blazed.cancel();

Request Interception

Here's how you can perform request interception

// Add response interceptor
const responseInterceptorId = blazed.addResponseInterceptor(
    response => {
        // Modify response data here if needed
        console.log('Response interceptor fired:', response);
        return response;
    },
    error => {
        console.error('Response interceptor error:', error);
        throw error;
    }
)

Let me show you an advanced example

// Note that I have included blazed() here in this example 

const blazed = new Blazed('https://api.example.com');

// Add request interceptor
const requestInterceptorId = blazed.addRequestInterceptor(
    config => {
        console.log('Request interceptor fired:', config);
        return config;
    },
    error => {
        console.error('Request interceptor error:', error);
        throw error;
    }
);

// Add response interceptor
const responseInterceptorId = blazed.addResponseInterceptor(
    response => {
        console.log('Response interceptor fired:', response);
        return response;
    },
    error => {
        console.error('Response interceptor error:', error);
        throw error;
    }
);

// Example of making a request
blazed.get('/data')
    .then(response => {
        console.log('Successful response:', response);
    })
    .catch(error => {
        console.error('Request failed:', error);
    });

// Remove request interceptor after some time
setTimeout(() => {
    blazed.removeInterceptor('request', requestInterceptorId);
    console.log('Request interceptor removed');
}, 5000);

// Remove response interceptor after some time
setTimeout(() => {
    blazed.removeInterceptor('response', responseInterceptorId);
    console.log('Response interceptor removed');
}, 10000);
  1. Interceptor:
  • Interceptors are added using addRequestInterceptor and addResponseInterceptor
  • They are removed using removeInterceptor
  1. Usage: The example demonstrates adding interceptors, making a request (get in this case), and then removing interceptors after a certain delay using setTimeout.

  2. Error handling:

  • Errors within interceptors are caught and logged appropriately
  • Network errors and timeout handling are also included in the _makeRequest method
  1. Cancellation:
  • Cancellation token is implemented to cancel ongoing requests if needed

Issues/Bugs

Feel free to open up an issue here if any bugs/issues occur!

Thanks for reading :)

Have a great day ahead :D