1.1.2 ā€¢ Published 3 years ago

@eptic/http v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

HTTP

version downloads size github actions coverage typescript contributing CodeFactor

http request library heavily inspired by axios and bradgarropy/http

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.

šŸ“¦ Installation

This package is hosted on npm.

npm install @eptic/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,
    },
})

// update a post or create a new one
const updatePost = await post(
    "https://jsonplaceholder.typicode.com/posts/101",
    {
        body: {
            title: "My post title",
            body: "This is my post body.",
            userId: 1,
        },
    },
)

šŸ“– API Reference

get(url, options)

NameRequiredDefaultExampleDescription
urltruehttps://jsonplaceholder.typicode.com/postsWeb address of the API.
options.paramsfalse{}{userId: 1}Query parameters object.
options...false{ headers: { 'content-type': 'application/json' } }{ body: {first: "Brad", last: "Garropy"} }The default fetch options with application/json as a default header

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", {
    params: {userId: 1},
})

post(url, options)

NameRequiredDefaultExampleDescription
urltruehttps://jsonplaceholder.typicode.com/postsWeb address of the API.
options.paramsfalse{}{ userId: 1 }Query parameters object.
options.methodfalsePOSTPUTThe method of the request.
options...false{ headers: { 'content-type': 'application/json' } }{ body: {first: "Brad", last: "Garropy"} }The default fetch options with application/json as a default header

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 can be used for POST | PUT | PATCH | DELETE

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

// post with options
post("https://jsonplaceholder.typicode.com/posts", {
    params: {userId: 1},
    body: {
        first: "Test",
        last: "User",
    },
})

// put request
post("https://jsonplaceholder.typicode.com/posts/1", {
    method: "PUT"
    params: {userId: 1},
    body: {
        first: "Test",
        last: "User",
    },
})

ā” Questions

šŸ› report bugs by filing issues

āœØ contributors

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago