# simple-fetch

> Simple and minimal wrapper around fetch API for JSON requests

Latest version **2.2.3** (published 2022-12-16) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.3 |
| Published | 2022-12-16 |
| First published | 2016-01-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Tri Nguyen |
| Maintainers | tridnguyen |
| Keywords | fetch |

## Links

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

## Dependencies (3)

- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.0.6
- [es6-promise](https://npm.io/package/es6-promise.md) ^3.0.2
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 2.2.3 (latest) — 2022-12-16
- 2.2.2 — 2021-05-09
- 2.2.1 — 2020-12-06
- 2.2.0 — 2020-12-06
- 2.1.0 — 2020-08-23
- 2.0.1 — 2020-08-09
- 2.0.0 — 2019-10-08
- 1.5.1 — 2018-08-09
- 1.5.0 — 2016-08-23
- 1.4.2 — 2016-08-17
- 1.4.1 — 2016-08-16
- 1.4.0 — 2016-08-16
- 1.3.0 — 2016-08-04
- 1.2.1 — 2016-03-09
- 1.2.0 — 2016-01-16
- … 3 more at https://npm.io/package/simple-fetch/versions

## README

# simple-fetch
> a simple wrapper around [cross-fetch](https://github.com/lquixada/cross-fetch) to make it easier to work with common [fetch](github/fetch) tasks

This is only meant for more convenient basic JSON requests. Please use the fetch API for more complex use cases.

By default, HTTP response codes other than 2xx will cause the fetch promise handler to throw. To change this behavior, set `opts.only2xx = false`.

## Examples

```js
const simpleFetch = require('simple-fetch');
const { getJson, postJson } = simpleFetch;

getJson('https://myapi.com/events')
  .then(function (events) {
    console.log(events);
  });

const response = await postJson('https://myapi.com/events', {
  name: 'New Event',
  date: 'tomorrow'
}, {
  headers: {
    Authorization: `Bearer ${token}`
  }
});

simpleFetch('patch', 'https://myapi.com/events/1', {
  name: 'Other Event',
  date: 'next Sunday'
}).then(...)
```

If common options, such as headers, need to be passed to all methods, use `createFetch`:

```js
const { createFetch } = require('simple-fetch');
const { getJson, deleteJson } = createFetch({
  headers: {
    Authorization: `Bearer ${token}`
  }
});

await getJson('https://myauthenticatedapi.com/events');
await deleteJson('https://myauthenticatedapi.com/events/2');

```

## API

- `simpleFetch(method, url[, data][, opts])`
- `.getJson(url[, opts])`
- `.postJson(url, data[, opts])`
- `.putJson(url, data[, opts])`
- `.patchJson(url, data[, opts])`
- `.deleteJson(url[, opts])`

### Parameters
- `data` can be an object, array or JSON string
- `opts.only2xx`: if set to `false`, will not throw error even for error codes other than 2xx. Defaults to `true`.
- `opts.skipParsing`: skip parsing of response into JSON, will return the `Response` object directly. Defaults to `false`.

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