https://github.com/seangenabe/uget
Isomorphic JSON API helper on top of the Fetch API.
https://github.com/seangenabe/uget
fetch-api json
Last synced: 7 months ago
JSON representation
Isomorphic JSON API helper on top of the Fetch API.
- Host: GitHub
- URL: https://github.com/seangenabe/uget
- Owner: seangenabe
- License: mit
- Created: 2016-10-14T06:01:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-07-22T21:42:18.000Z (about 7 years ago)
- Last Synced: 2025-01-31T08:16:11.088Z (8 months ago)
- Topics: fetch-api, json
- Language: JavaScript
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# uget
Isomorphic JSON API helper on top of the Fetch API.
## Usage
```javascript
const uget = require('uget')
```On the browser, the native Fetch API is used. On node, [node-fetch](https://npmjs.com/package/node-fetch) is used. (via [browserify](https://npmjs.com/package/browserify))
### uget(input, init)
Fetches a resource from a URI.
**Parameters** are the same as the native `GlobalFetch.fetch` function, but some defaults useful for JSON API endpoints are set for `init`:
* `method` = `GET`
* `headers` = `{ accept: 'application/json' }`Can be used to distinguish between a JSON API request and a normal (HTML) request.
* `credentials` = `same-origin`
Ensures that cookies are sent with the request.
* `redirect` = `follow`
Follow redirects.
If you pass a plain object or an array to `init.body`, that will be serialized with `JSON.stringify()` and `{ 'content-type': 'application/json' }` will be added to the default `headers`.
**Returns:**
A `Promise` that resolves with a value or rejects with an `Error`:
* If the response is not OK (does not have status 2xx):
1. Parses the response body.
* If the response body is valid JSON, attempts to get the `message` property and incorporates it into the error message.
* If the response body is valid plaintext, gets the first hundred characters and incorporates it into the error message.
2. Throws an error.
* If the response status is 204:
* Returns `undefined`.
* Parses the response body as text
* If the response body is empty, returns `undefined`.
* Parses the response body text as JSON and returns it.If the response is not OK and has a plaintext body, the error object will additionally have the `payload` property containing the response body. Furthermore, if it's valid JSON, the `payloadJson` property will be set containing the JSON object. Thirdly, if a `message` property was found, the `payloadJsonDetail` property will be set containing its value.