# very-simple-fetch

> Utility for easy use of the Fetch API

Latest version **0.2.0** (published 2021-09-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-09-04 |
| First published | 2021-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Igor Agapov |
| Maintainers | igor_agapov |
| Keywords | fetch, fetch api, utility, util, helper, http, https, xhr, xmlhttprequest, axios, ajax |

## Links

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

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2021-09-04
- 0.1.9 — 2021-08-26
- 0.1.8 — 2021-08-20
- 0.1.7 — 2021-08-05
- 0.1.6 — 2021-08-05
- 0.1.5 — 2021-08-04
- 0.1.4 — 2021-08-03
- 0.1.3 — 2021-08-03
- 0.1.2 — 2021-08-03
- 0.1.1 — 2021-08-02
- 0.1.0 — 2021-08-01
- 0.0.9 — 2021-08-01
- 0.0.8 — 2021-07-30
- 0.0.7 — 2021-07-30
- 0.0.6 — 2021-07-30
- … 5 more at https://npm.io/package/very-simple-fetch/versions

## README

# Simple Fetch 🌐

### Utility for easy use of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a>

- Returns final (parsed to JSON, text or raw) result, including custom errors
- Uses local cache to store and retrieve results of GET-requests
- Accepts `options.params` object which converted to encoded search parameters
- Can be cancelled (for example, if the request takes too long)
- Contains `baseUrl` and `authToken` setters
- Tested in Chrome, Firefox & Safari
- Tested with React & Vue

## Installation

```bash
yarn add very-simple-fetch
# or
npm i very-simple-fetch
```

## Usage

```js
import simpleFetch from 'very-simple-fetch'

const getTodos = async () => {
  const { data: todos, info } = await simpleFetch.get(
    'https://jsonplaceholder.typicode.com/todos',
    {
      params: {
        _limit: 10
      }
    }
  )
  console.table(todos)
  console.log(JSON.stringify(info, null, 2))

  const { error } = await simpleFetch('https://wrong.com')
  console.error(error)
}
getTodos()
```

[Full example on CodeSanbox](https://codesandbox.io/s/simple-fetch-1o2k)

## Signature of the main function

```js
simpleFetch(options: string | object)
// alias for simpleFetch.get()
```

## Signatures of helper functions

```js
// GET
simpleFetch.get(url: string, options: object)
// or if you set base URL
simpleFetch.get(options: object)

// POST
simpleFetch.post(url: string, body: any, options: object)
// with baseUrl
simpleFetch.post(body: any, options: object)

// PUT
simpleFetch.update(url: string, body: any, options: object)
// with baseUrl
simpleFetch.update(body: any, options: object)

// DELETE
simpleFetch.remove(url: string, options: object)
// with baseUrl
simpleFetch.remove(options: object)
```

## Setting base URL

```js
simpleFetch.baseUrl = 'https://example.com'
```

## Setting auth token

```js
simpleFetch.authToken = token
/*
simpleFetch.headers = {
  // ...
  Authorization: 'Bearer [token]'
}
*/
```

## Cancellation of the request

```js
// current (last sended) request will be cancelled
simpleFetch.cancel()

// send request
simpleFetch(url)
// retrieve id of this request
const { currentRequestId } = simpleFetch
// cancel this request
simpleFetch.cancel(currentRequestId)
```

## Options

- <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#parameters" target="_blank">common</a>
- custom:
  - customCache: boolean - if `true`, result of the GET-request will be stored in the local cache  - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map">Map</a>. Result of the same request will be retrieved from this cache until `customCache` is set to `false`. Default value is `true`
  - log: boolean - if `true`, options, cache and result will be output to DevTools concole. Default value is `false`
  - params?: object - this object is converted to encoded search parameters that are appended to the URL:
    - key: string
    - value: string
  - handlers?: object:
    - onSuccess: function
    - onError: function
    - onAbort: function

## Default options

```js
{
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  },
  referrerPolicy: 'no-referrer',
  customCache: true,
  log: false,
  signal: new window.AbortController().signal
}
```

## Response

- data: any | null - result of the request or `null` if there war an error
- error: null | any - `null` or custom error
- info: object:
  - headers: object
  - status: number,
  - statusText: string
  - url: string

Utility trying to send request with any arguments and return something even if an exception was thrown.

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