@bradgarropy/http

📻 http request library

Usage no npm install needed!

<script type="module">
  import bradgarropyHttp from 'https://cdn.skypack.dev/@bradgarropy/http';
</script>

README

📻 http

version downloads size github actions coverage typescript contributing contributors discord

📻 http request library heavily inspired by axios.

Most of the time, fetch is used to interact with a JSON API. This library is a thin wrapper around fetch that converts the request and response body to JSON by default.

http

📦 Installation

This package is hosted on npm.

npm install @bradgarropy/http

🥑 Usage

This library's API is very similar to axios. You can issue HTTP requests which are assumed to be JSON by default. It returns a Promise with the response data.

import http from "@bradgarropy/http"

// get all posts
const posts = await http.get("https://jsonplaceholder.typicode.com/posts")

// get posts by user
const posts = await http.get("https://jsonplaceholder.typicode.com/posts", {
    params: {
        userId: 1,
    },
})

// get one post
const post = await http.get("https://jsonplaceholder.typicode.com/posts/1")

// create a post
const newPost = await http.post("https://jsonplaceholder.typicode.com/posts", {
    body: {
        title: "My post title",
        body: "This is my post body.",
        userId: 1,
    },
})

📖 API Reference

get(url, options)

Name Required Default Example Description
url true https://jsonplaceholder.typicode.com/posts Web address of the API.
options.headers false {} {"authorization": "Bearer abc123"} Headers object, similar to fetch.
options.params false {} {userId: 1} Query parameters object.

Perform an HTTP GET request. The response is automatically converted to JSON.

// get without options
http.get("https://jsonplaceholder.typicode.com/posts")

// get with  options
http.get("https://jsonplaceholder.typicode.com/posts", {
    headers: {authorization: "Bearer abc123"},
    params: {userId: 1},
})

post(url, options)

Name Required Default Example Description
url true https://jsonplaceholder.typicode.com/posts Web address of the API.
options.headers false {} {"authorization": "Bearer abc123"} Headers object, similar to fetch.
options.params false {} {userId: 1} Query parameters object.
options.body false {} {first: "Brad", last: "Garropy"} Body to send to the API. Define the Content-Type using options.type.
options.type false json form Content type of the body. (json | form)

Perform an HTTP POST request. If a body is supplied, it's automatically converted to a string before being sent in the request. The response is automatically converted to JSON.

// post without options
http.post("https://jsonplaceholder.typicode.com/posts")

// post with options
http.post("https://jsonplaceholder.typicode.com/posts", {
    headers: {authorization: "Bearer abc123"},
    params: {userId: 1},
    body: {
        first: "Brad",
        last: "Garropy",
    },
    type: "json",
})

❔ Questions

🐛 report bugs by filing issues
📢 provide feedback with issues or on twitter
🙋🏼‍♂️ use my ama or twitter to ask any other questions

✨ contributors


Brad Garropy

💻 📖 ⚠️ 🚇