Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bradgarropy/http
📻 http request library
https://github.com/bradgarropy/http
ajax eslint fetch http jest node prettier typescript
Last synced: 2 months ago
JSON representation
📻 http request library
- Host: GitHub
- URL: https://github.com/bradgarropy/http
- Owner: bradgarropy
- License: mit
- Created: 2021-07-07T03:06:55.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-05-19T02:38:08.000Z (over 2 years ago)
- Last Synced: 2024-10-05T07:17:46.516Z (3 months ago)
- Topics: ajax, eslint, fetch, http, jest, node, prettier, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@bradgarropy/http
- Size: 85.9 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license
Awesome Lists containing this project
README
# 📻 http
[![version][version-badge]][npm]
[![downloads][downloads-badge]][npm]
[![size][size-badge]][bundlephobia]
[![github actions][github-actions-badge]][github-actions]
[![coverage][codecov-badge]][codecov]
[![typescript][typescript-badge]][typescript]
[![contributing][contributing-badge]][contributing]
[![contributors][contributors-badge]][contributors]
[![discord][discord-badge]][discord]_📻 http request library heavily inspired by [axios][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.
## 📦 Installation
This package is hosted on [npm][npm].
```bash
npm install @bradgarropy/http
```## 🥑 Usage
This library's API is very similar to [axios][axios]. You can issue HTTP requests which are assumed to be JSON by default. It returns a `Promise` with the response data.
```javascript
import {http} from "@bradgarropy/http"// get all posts
const posts = await http.get("https://jsonplaceholder.typicode.com/posts")// get one post
const post = await http.get("https://jsonplaceholder.typicode.com/posts/1")// get posts by user
const userPosts = await http.get("https://jsonplaceholder.typicode.com/posts", {
params: {
userId: 1,
},
})// create a post
const newPost = await http.post("https://jsonplaceholder.typicode.com/posts", {
body: {
userId: 1,
title: "My post title",
body: "This is my post body.",
},
})// update a post
const newPost = await http.patch(
"https://jsonplaceholder.typicode.com/posts/1",
{
body: {
userId: 1,
title: "My new post title",
body: "This is my new post body.",
},
},
)// delete a post
const oldPost = await http.delete(
"https://jsonplaceholder.typicode.com/posts/1",
)
```## 📖 API Reference
Every method assumes that the response coming back from the API is `json`.
The request `body` is converted to a `string` if the Content Type is `json`, or a `FormData` object if the Content Type is `form`. The request `body` is ignored when using the `GET` method.
### `options`
All methods support an `options` object. Here are the set of supported options.
| Name | Required | Default | Example | Description |
| --------- | -------- | ------- | ------------------------------------ | ---------------------------------------------------------------- |
| `headers` | `false` | `{}` | `{"authorization": "Bearer abc123"}` | Headers object, similar to `fetch`. |
| `params` | `false` | `{}` | `{userId: 1}` | Query parameters object. |
| `body` | `false` | `{}` | `{name: "Brad Garropy"}` | Body to send to the API. Define the `Content-Type` using `type`. |
| `type` | `false` | `json` | `form` | Content type of the body. (`json` \| `form`) |### `get(url, options)`
| Name | Required | Default | Example | Description |
| --------- | -------- | ------- | -------------------------------------------- | ---------------------------- |
| `url` | `true` | | `https://jsonplaceholder.typicode.com/posts` | Web address of the API. |
| `options` | `false` | `{}` | `{params: {userId: 1}}` | Request [options](#options). |Perform an HTTP GET request.
```javascript
// 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},
body: {
first: "Brad",
last: "Garropy",
},
type: "json",
})
```### `post(url, options)`
| Name | Required | Default | Example | Description |
| --------- | -------- | ------- | -------------------------------------------- | ---------------------------- |
| `url` | `true` | | `https://jsonplaceholder.typicode.com/posts` | Web address of the API. |
| `options` | `false` | `{}` | `{params: {userId: 1}}` | Request [options](#options). |Perform an HTTP POST request.
```javascript
// 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",
})
```### `patch(url, options)`
| Name | Required | Default | Example | Description |
| --------- | -------- | ------- | -------------------------------------------- | ---------------------------- |
| `url` | `true` | | `https://jsonplaceholder.typicode.com/posts` | Web address of the API. |
| `options` | `false` | `{}` | `{params: {userId: 1}}` | Request [options](#options). |Perform an HTTP PATCH request.
```javascript
// patch without options
http.patch("https://jsonplaceholder.typicode.com/posts")// patch with options
http.patch("https://jsonplaceholder.typicode.com/posts", {
headers: {authorization: "Bearer abc123"},
params: {userId: 1},
body: {
first: "Brad",
last: "Garropy",
},
type: "json",
})
```### `delete(url, options)`
| Name | Required | Default | Example | Description |
| --------- | -------- | ------- | -------------------------------------------- | ---------------------------- |
| `url` | `true` | | `https://jsonplaceholder.typicode.com/posts` | Web address of the API. |
| `options` | `false` | `{}` | `{params: {userId: 1}}` | Request [options](#options). |Perform an HTTP DELETE request.
```javascript
// delete without options
http.delete("https://jsonplaceholder.typicode.com/posts")// delete with options
http.delete("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][issues]
📢 provide feedback with [issues][issues] or on [twitter][twitter]
🙋🏼♂️ use my [ama][ama] or [twitter][twitter] to ask any other questions## ✨ contributors
[codecov]: https://app.codecov.io/gh/bradgarropy/http
[contributing]: https://github.com/bradgarropy/http/blob/master/contributing.md
[contributors]: #-contributors
[npm]: https://www.npmjs.com/package/@bradgarropy/http
[codecov-badge]: https://img.shields.io/codecov/c/github/bradgarropy/http?style=flat-square
[version-badge]: https://img.shields.io/npm/v/@bradgarropy/http.svg?style=flat-square
[downloads-badge]: https://img.shields.io/npm/dt/@bradgarropy/http?style=flat-square
[contributing-badge]: https://img.shields.io/badge/PRs-welcome-success?style=flat-square
[contributors-badge]: https://img.shields.io/github/all-contributors/bradgarropy/http?style=flat-square
[issues]: https://github.com/bradgarropy/http/issues
[twitter]: https://twitter.com/bradgarropy
[ama]: https://bradgarropy.com/ama
[bundlephobia]: https://bundlephobia.com/result?p=@bradgarropy/http
[size-badge]: https://img.shields.io/bundlephobia/minzip/@bradgarropy/http?style=flat-square
[github-actions]: https://github.com/bradgarropy/http/actions
[github-actions-badge]: https://img.shields.io/github/workflow/status/bradgarropy/http/%F0%9F%9A%80%20release?style=flat-square
[typescript]: https://www.typescriptlang.org/dt/search?search=%40bradgarropy%2Fhttp
[typescript-badge]: https://img.shields.io/npm/types/@bradgarropy/http?style=flat-square
[discord]: https://bradgarropy.com/discord
[discord-badge]: https://img.shields.io/discord/748196643140010015?style=flat-square
[axios]: https://github.com/axios/axios