Skip to content

Proxy-like function to run a XMLHttpRequest using the fetch syntax

Notifications You must be signed in to change notification settings

boerredus/_fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

_fetch

This is a proxy function. It uses the same simple-to-read syntax as the fetch API but is the good ol' XMLHttpRequest under the hood.
This was an attempt to help my friend @ericklaus16 on the mobile version of AlfaChat.

I hope it helps!

Syntax

As previously mentioned, it follows the same syntax as the fetch JavaScript API.

Request example:

_fetch("https://google.com", {
    mode: "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    body: null
});

It returns a promise which is resolved as soon as the XMLHttpRequest response loads. Response example:

_fetch().then(res => {
    console.log(this);  // XMLHttpRequest object
    console.log(res);   // Response from the server
});

Note #1: there's no way of getting the original's fetch API responses (e.g Response.ok or Response.status) since it isn't a fetch call. However, it is possible to access the XMLHttpRequest object properties through the this keyword.

Note #2: one can include Headers on the options parameter (the second one) as when using the fetch API.

About

Proxy-like function to run a XMLHttpRequest using the fetch syntax

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published