Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mersocarlin/fetch
fetch wrapper
https://github.com/mersocarlin/fetch
api fetch fetch-api
Last synced: 1 day ago
JSON representation
fetch wrapper
- Host: GitHub
- URL: https://github.com/mersocarlin/fetch
- Owner: mersocarlin
- License: mit
- Created: 2020-05-12T07:33:53.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T04:41:49.000Z (almost 2 years ago)
- Last Synced: 2024-04-24T10:07:09.378Z (8 months ago)
- Topics: api, fetch, fetch-api
- Language: TypeScript
- Homepage:
- Size: 1.18 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @mersocarlin/fetch
`fetch` wrapper on top of `isomorphic-fetch`.
## Motivation
I needed a consistent `fetch` wrapper that always returns the same structure (`body` and `statusCode`) for all API calls throughout my projects.
Basic error handling was also a "_must-have_" and I ended up duplicating the same code in every new project/repository I was working on.
I've also written this code a long time ago but never had the chance to give it the attention it deservs + open source it.
It works seamless with [@mersocarlin/mersocarlin/api-error](https://github.com/mersocarlin/api-error) so I get named API errors for free.
## Install
```bash
yarn add @mersocarlin/fetch
``````bash
npm i @mersocarlin/fetch
```## Usage
```ts
import fetch from '@mersocarlin/fetch'const login = async (username: string, password: string) => {
try {
const { body, statusCode } = await fetch('https://example.com/api/signin', {
body: {
username,
password,
},
method: 'post',
})return body
} catch(error) {
throw error
}
}const fetchPrivateUsers = async () => {
try {
const { body, statusCode } = await fetch('https://example.com/api/users', {
headers: {
'Authorization': 'Bearer TOKEN',
}
})return body
} catch(error) {
throw error
}
}const fetchPublicUsers = async () => {
try {
const { body, statusCode } = await fetch('https://example.com/users')return body
} catch(error) {
throw error
}
}
```