3.3.0 • Published 2 years ago

@tg-resources/fetch v3.3.0

Weekly downloads
1,028
License
MIT
Repository
github
Last release
2 years ago

tg-resources Fetch backend

NPM version

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

npm i tg-resources @tg-resources/fetch

Or using Yarn

yarn add tg-resources @tg-resources/fetch

Does it work on react native?

YES

Basic Usage

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);

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.

This package is just Resource implementation using fetch. For additional configuration, see Configuration.

License

MIT © Thorgate

3.4.0-alpha.0

2 years ago

3.4.0-alpha.1

2 years ago

3.3.0

2 years ago

3.2.0

3 years ago

3.1.4

4 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.1.0-beta.1

5 years ago

3.1.0-beta.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

6 years ago