# @tg-resources/fetch

> Fetch backend for `tg-resources`

Latest version **4.0.0** (published 2024-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tg-resources/fetch
pnpm add @tg-resources/fetch
yarn add @tg-resources/fetch
bun add @tg-resources/fetch
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2024-11-20 |
| First published | 2018-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 84.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Thorgate <hi@thorgate.eu> |
| Maintainers | thorgate-main, jyrno42, metsavaht |
| Keywords | api, request, http, ajax, fetch |

## Links

- npm: https://www.npmjs.com/package/@tg-resources/fetch
- Repository: https://github.com/thorgate/tg-resources
- Homepage: https://github.com/thorgate/tg-resources/tree/master/packages/fetch#readme
- Issues: https://github.com/thorgate/tg-resources/issues
- npm.io page: https://npm.io/package/@tg-resources/fetch

## Dependencies (3)

- [qs](https://npm.io/package/qs.md) ^6.11.0
- [@tg-resources/is](https://npm.io/package/@tg-resources/is.md) ^4.0.0
- [@tg-resources/core](https://npm.io/package/@tg-resources/core.md) ^4.0.0

## 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

- 4.0.0 (latest) — 2024-11-20
- 3.4.0-alpha.1 (next) — 2022-08-15
- 3.4.0-alpha.0 — 2022-08-15
- 3.3.0 — 2021-12-03
- 3.2.0 — 2021-04-17
- 3.1.4 — 2020-02-10
- 3.1.3 — 2019-10-16
- 3.1.2 — 2019-03-04
- 3.1.1 — 2019-02-12
- 3.1.0 — 2019-01-31
- 3.1.0-beta.1 — 2019-01-11
- 3.1.0-beta.0 — 2018-12-31
- 3.0.1 — 2018-11-30
- 3.0.0 — 2018-11-30
- 3.0.0-alpha.1 — 2018-11-26
- … 1 more at https://npm.io/package/@tg-resources/fetch/versions

## README

# tg-resources Fetch backend

[![NPM version][npm-image]][npm-url]

**Notice:** This package expects `fetch`, `Request`, `Response` and `FormData` to be available in global scope, e.g in `window` for Browser or in `global` for Node.js

## Installing

Using NPM

```sh
npm i tg-resources @tg-resources/fetch
```

Or using Yarn

```sh
yarn add tg-resources @tg-resources/fetch
```

### Does it work on react native?

**YES**

## Basic Usage

```js
import { Router } from 'tg-resources';
import { FetchResource } from '@tg-resources/fetch';

const onLoad = (result) => console.log(result);
const onError = (result) => console.error(result);

const api = new Router(
    {
        cats: new FetchResource('/cats'),
        cat: new FetchResource('/cats/${pk}'),
    },
    {
        apiRoot: '/api/v1', // Set api root
    }
);

// Do a get request to /api/v1/cats?gender=M
api.cats.fetch(null, { gender: 'M' }).then(onLoad, onError);

// Do a head request to /api/v1/cats?gender=M
api.cats.head(null, { gender: 'M' }).then(onLoad, onError);

// Do a post request to /api/v1/cats with data: {name: 'Twinky', gender: 'M'}
api.cats.post(null, { name: 'Twinky', gender: 'M' }).then(onLoad, onError);

// Do a patch request to /api/v1/cats/1 with data: {name: 'Tinkelberg'}
api.cat.patch({ pk: 1 }, { name: 'Tinkelberg' }).then(onLoad, onError);

// Do a put request to /api/v1/cats with data: {pk: 1, name: 'Twinky'}
api.cats
    .put(null, { pk: 1, name: 'Twinky', gender: 'M' })
    .then(onLoad, onError);

// Do a delete request to /api/v1/cats/1 with data: {'free':'yes'}
api.cat.del({ pk: 1 }, { free: 'yes' }).then(onLoad, onError);
```

## <a name="configuration"></a>Configuration

This package adds extra configuration methods for `Router` and `FetchResource`.

-   `querySerializeOptions` _(Object|undefined)_: **Advanced usage:** Options to configure query-string serialization. See [qs.stringify](https://github.com/ljharb/qs#stringifying).

This package is just Resource implementation using `fetch`.
For additional configuration, see [Configuration](https://github.com/thorgate/tg-resources/tree/master/README.md#configuration).

## License

MIT © [Thorgate](http://github.com/thorgate)

[npm-url]: https://npmjs.org/package/@tg-resources/fetch
[npm-image]: https://img.shields.io/npm/v/@tg-resources/fetch.svg?style=flat-square

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