❀Napoleon's Pixelated Mugshot

    @bradgarropy/http
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.0Β β€’Β PublicΒ β€’Β Published

    πŸ“» 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

    πŸ’» πŸ“– ⚠️ πŸš‡

    Install

    npm i @bradgarropy/http

    DownloadsWeekly Downloads

    16

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    18.9 kB

    Total Files

    15

    Last publish

    Collaborators

    • bradgarropy