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

    1.0.1Β β€’Β 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.

    next link

    πŸ“¦ 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.

    // get all posts
    const posts = await get("https://jsonplaceholder.typicode.com/posts")
    
    // get posts by user
    const posts = await get("https://jsonplaceholder.typicode.com/posts", {
        params: {
            userId: 1,
        },
    })
    
    // get one post
    const post = await get("https://jsonplaceholder.typicode.com/posts/1")
    
    // create a post
    const newPost = await 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 {} {"content-type": "application/json"} 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
    get("https://jsonplaceholder.typicode.com/posts")
    
    // get with  options
    get("https://jsonplaceholder.typicode.com/posts", {
        headers: {"content-type": "application/json"},
        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 {} {"content-type": "application/json"} Headers object, similar to fetch.
    options.params false {} {userId: 1} Query parameters object.
    options.body false {} {first: "Brad", last: "Garropy"} JSON body to send to the API.

    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
    post("https://jsonplaceholder.typicode.com/posts")
    
    // post with options
    post("https://jsonplaceholder.typicode.com/posts", {
        headers: {"content-type": "application/json"},
        params: {userId: 1},
        body: {
            first: "Brad",
            last: "Garropy",
        },
    })

    ❔ 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

    6

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    14.8 kB

    Total Files

    12

    Last publish

    Collaborators

    • avatar