1.0.9 • Published 10 months ago

the-plug v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Plug

A wrapper for the fetch API that simplifies HTTP requests

NPM Downloads NPM Version


Installation

use npm:

npm i the-plug

or cdn:

<script src="https://cdn.jsdelivr.net/npm/the-plug@1.0.8/plug.js"></script>

Usage:

Example (express):

const express = require('express')
const app = express()

const plug = require('the-plug')

app.get('/', async (req, res)=>{
    const response = await plug.get('https://official-joke-api.appspot.com/random_joke')

    res.json(response)
})

app.listen(3000)

Example (HTML):

<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/the-plug@1.0.8/plug.js"></script>
</head>
<body>
    <h2>Random Joke</h2>
    <p></p>
    <p></p>

    <script>
        async function getJoke() {
            const joke = await plug.get('https://official-joke-api.appspot.com/random_joke');

            document.querySelectorAll('p')[0].innerHTML = joke.setup;
            document.querySelectorAll('p')[1].innerHTML = joke.punchline;
        }

        getJoke();
    </script>
</body>
</html>

Additional Examples:

POST Request

To send data to a server, you can use the post method:

const data = { name: "John Doe", age: 30 };
const response = await plug.post('https://your-api-endpoint.com/users', data, {
    'Content-Type': 'application/json'
});
console.log(response);

PUT Request

To update existing data, use the put method:

const updatedData = { name: "John Doe", age: 31 };
const response = await plug.put('https://your-api-endpoint.com/users/1', updatedData, {
    'Content-Type': 'application/json'
});
console.log(response);

DELETE Request

To delete data from a server, use the delete method:

const response = await plug.delete('https://your-api-endpoint.com/users/1', {
    'Authorization': 'Bearer your-token-here'
});
console.log(response);
1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago