xhrify

0.0.4 • Public • Published

xhrify

xhrify is a tool to make HTTP requests, made simple.

  • xhrify returns a Promise so you can code in sync.

Installation

npm:

$ npm install xhrify -s

Or cdn:

<head>
    <script src="https://cdn.jsdelivr.net/npm/xhrify@0.0.3/xhrify.js"></script>
</head>

Usage

You can use xhrify in two different ways:

Instantiation

// -> The basepath inside the constructor is optional, and can be used in case your need to access different APIS from the same host. If you don't want to use it, just leave it blank and pass your full path inside the functions. If you specify the basepath, you only need to pass the remainder of the full path of your API to the function.

    const xhrify = new xhrify("BASE_PATH_URL") 
    xhrify.get("URL").then((response) => {
        console.log(response);
    })
    .catch((err) => {
        console.log(err);
    })

Static

    xhrify.get("URL").then((response) => {
        console.log(response);
    })
    .catch((err) => {
        console.log(err);
    })

Functions

get

    xhrify.get("http://url.com/api/get").then((response) => {
        alert(response)
    })
    .catch((err) => {
        alert('Error!!!')
    })

post

This takes two arguments, the path and the body of the POST request, and optionally a config, containing the Content-type

This returns a string of the response.

    xhrify.post("http://url.com/api/post", {name: "Bob"}, {ContentType: "application/json"}).then((response) => {
        var result = JSON.parse(response) // Response now in JSON format.
    })
    .catch((err) => {
        alert('Error!!!')
    })

put

    xhrify.put("http://url.com/api/put", {name: "Bob"}).then(() => {
        console.log('PUT completed.')
    })
    .catch((err) => {
        alert('Error!!!')
    })

attach

    xhrify.attach("http://url.com/api/attach", {name: "Bob"}).then((response) => {
        console.log('Attach completed.')
    })
    .catch((err) => {
        alert('Error!!!')
    })

delete

    xhrify.delete("http://url.com/api/delete", {name: "Bob"}).then(() => {
        console.log('Delete completed.')
    })
    .catch((err) => {
        alert('Error!!!')
    })

Readme

Keywords

Package Sidebar

Install

npm i xhrify

Weekly Downloads

2

Version

0.0.4

License

ISC

Last publish

Collaborators

  • edisoncury