https://github.com/zacanger/fetchyeah
Miniscule JSON fetch wrapper library.
https://github.com/zacanger/fetchyeah
browser fetch json node request xhr
Last synced: about 1 year ago
JSON representation
Miniscule JSON fetch wrapper library.
- Host: GitHub
- URL: https://github.com/zacanger/fetchyeah
- Owner: zacanger
- License: mit
- Created: 2019-05-03T16:17:09.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-23T17:21:31.000Z (about 2 years ago)
- Last Synced: 2024-04-23T19:23:03.703Z (about 2 years ago)
- Topics: browser, fetch, json, node, request, xhr
- Language: TypeScript
- Homepage: http://npm.im/fetchyeah
- Size: 2.46 MB
- Stars: 3
- Watchers: 16
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# fetchyeah
Tiny JSON fetch wrapper library. ~1.7kb gzipped.
[](https://npm.im/fetchyeah) [](https://ko-fi.com/zacanger)
----
`fetchyeah` is a small fetch wrapper library that always parses JSON and returns
JS. Smaller than Axios, Request, R2, and the `whatwg-fetch` polyfill itself.
# Installation
`npm i fetchyeah`
# Usage
```javascript
import { get } from 'fetchyeah'
get('/foo')
```
## Methods
* `del`
* `get`
* `patch`
* `post`
* `put`
This only provides functions for these common HTTP methods, but you can easily add
your own. Check out the source for notes on how to use `sendJson` directly.
The return value is always a simple response of type
```typescript
type SimpleResponse = {
ok: boolean
status: number
headers: Headers
body: T
}
```
## Examples
Node:
```javascript
require('isomorphic-fetch') // brings in fetch for Node
import * as f from 'fetchyeah'
// some koa route
router.get('/foo/:id', async (ctx) => {
try {
const thing = await f.get(`/some-service/${id}`)
ctx.type = 'application/json'
ctx.body = thing
} catch (e) {
someLogger.error(e)
ctx.status = 500
ctx.body = e
}
})
```
Browser:
```javascript
import * as React from 'react'
import { post } from 'fetchyeah'
class Foo extends React.Component {
state = { things: null }
submitThings = () => {
post('/stuff', { body: this.state.things })
.then((res) => {
if (res) {
alert(res)
}
})
.catch((err) => {
someErrorHandler(err)
})
}
setThings = (e) => {
this.setState({ things: e.target.value })
}
render () {
return (
Send the things!
)
}
}
```
Adding headers:
```javascript
import { post } from 'fetchyeah'
post('/foo', {
body: someObject,
headers: {
'x-foo-bar': 'baz',
}
})
```
## Environment
This library assumes `fetch` is available. You may need to polyfill it!
[LICENSE](./LICENSE.md)