# fetch-json-simple

> Configurable fetch wrapper for retrieving JSON

Latest version **0.1.3** (published 2017-10-10) · 0 weekly downloads

## Install

```sh
npm install fetch-json-simple
pnpm add fetch-json-simple
yarn add fetch-json-simple
bun add fetch-json-simple
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2017-10-10 |
| First published | 2017-05-09 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | laggingreflex |

## Links

- npm: https://www.npmjs.com/package/fetch-json-simple
- Repository: https://github.com/laggingreflex/fetch-json-simple
- Homepage: https://github.com/laggingreflex/fetch-json-simple#readme
- Issues: https://github.com/laggingreflex/fetch-json-simple/issues
- npm.io page: https://npm.io/package/fetch-json-simple

## Dependencies (1)

- [merge-options](https://npm.io/package/merge-options.md) ^1.0.0

## Recent versions

- 0.1.3 (latest) — 2017-10-10
- 0.1.2 — 2017-09-28
- 0.1.1 — 2017-05-25
- 0.1.0 — 2017-05-09

## README

# fetch-json-simple

Configurable wrapper around [**`fetch`**][fetch] to retrieve **JSON**

[fetch]: https://developer.mozilla.org/en/docs/Web/API/Fetch_API

Main features:

* Attaches relevant headers for JSON:
  ```js
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }
  ```
* Stringifies request `{ body }` (but can also handle already stringified body)
* Text responses converted to JSON `{ body }`
* Options are **[merge]d** rather than [assign]ed.
* Auto-specifies `{method: 'post'}` if omitted and a body was passed
* Shortcut methods: `fetch.get`, `fetch.post`, `fetch.put`, ...

Additional features:

* Configurable `fetch` - use either native or polyfill
* Configurable `host` - prefixed to paths before sending request
* ... see [#config](#config)

Other similar libraries: [json-fetch], [fetch-json].

[json-fetch]: https://github.com/goodeggs/json-fetch
[fetch-json]: https://github.com/kahwee/fetch-json

## Install

```sh
npm install fetch-json-simple --save
```

## Usage

### Example

```js
fetch('/data-path')
  .then(json => {
    if (json.body) {
      // text body
    } else {
      // json
    }
  }).catch(error => {

  })
```

### API

```js
fetch(path, options)
```

* **`path`** `[string](required)` Path to make request to.

* **`options`** `[object]` Options object containing headers, body etc. `body` can be plain JS object.

  [Merge]d with configured options object (see [#config](#config)) and the following json-related default options object:
  ```js
  defaults = {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
  }
  ```
  ```js
    merge({},
      defaults, // above
      {method: body ? 'post' : 'get'},
      fetch.options, // configured
      opts, // passed as argument
      {body} // after stringifying
    )
  ```

[merge]: https://github.com/schnittstabil/merge-options
[assign]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

#### Shortcut Methods

```js
fetch.⟪method⟫(...)
```

* **`get`** Equivalent of: `fetch(path, {method: 'get'})`
* **`post`** Equivalent of: `fetch(path, {method: 'post'})`
* ... **`put`**, **`patch`**, **`delete`**  〃

#### Config
```js
fetch.⟪config⟫ = ...
```

* **`host`** `[string](default:none)` Use this host to add all paths to before making the fetch request

  ```js
  fetch.host = 'http://server.com'
  ```

* **`options`** `[object](default:none)` Default set of options used in every fetch request. **Merged** with *actual* (json-related) options.

  ```js
  fetch.options = {
    headers: {
      'Cache-Control': 'no-cache'
      // 'Accept': 'application/json',  < will still be present in final request
      'Content-Type': 'json' // < overridden default json-related option

    }
  }
  ```

* **`fetch`** `[function](default:none(uses native))` Underlying `fetch` function to use. Use this to polyfill if needed.

  ```js
  fetch.fetch = require('isomorphic-fetch')
  ```
  You may also use `isomorphic-fetch` to globally polyfill the underlying fetch in which case the above won't be needed.

---
_Source: https://npm.io/package/fetch-json-simple · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
