@eptic/http v1.1.2
HTTP
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)
Name | Required | Default | Example | Description |
---|---|---|---|---|
url | true | https://jsonplaceholder.typicode.com/posts | Web address of the API. | |
options.params | false | {} | {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)
Name | Required | Default | Example | Description |
---|---|---|---|---|
url | true | https://jsonplaceholder.typicode.com/posts | Web address of the API. | |
options.params | false | {} | { userId: 1 } | Query parameters object. |
options.method | false | POST | PUT | The 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