@customd/cd-fetch-model

0.6.9 • Public • Published

Fetch Wrapper

This library provides a set of simple, easy to use wrappers around native fetch. It also provides a convenient Model/API interface which allows you to avoid boilerplate communication code for simple/standardised API endpoints.

Note this is not a polyfill or fetch replacement — it's a set of convenience methods to avoid repetitive configs and parsing.

Installation

Install with Yarn

yarn add cd-fetch-model

Or with NPM

npm install --save cd-fetch-model

Please Note: You'll need to be registered to https://npm.customd.com to access this module. To do this, run;

npm config set registry https://npm.customd.com

Basic Usage

The wrappers main function is to provide you with a Model class that simplifies connections to a Zon RESTful API.

import FetchApi from 'cd-fetch-model'

class ResultsModel extends FetchApi {

    const api_url = Site.api_url + "results";
}

export default ResultsModel

You may find situations where it's useful to overload the default methods or provide defaults.

class ResultsModel extends FetchApi {

    const api_url = Site.api_url + "results";

    /**
     * Overload arguments with a set of defaults.
     */
    getWhere( where, sort = '-created', limit = 25, offset = 0) {

        super.getWhere(where, sort, limit, offset)
    }
}

These classes can then be imported into your action creators and used, E.g.,

import Results from 'api/Results'

Results.getWhere({ foo: 'bar' }).then(() => {

    /** do something **/
})

Direct acces to helper methods

You can also implement custom methods accessing fetch helper methods directly, as required.

import { fetchGet, fetchParams } from 'cd-fetch-model'


export default const getWhere = ( where, sort = '-created', limit = 25, offset = 0 ) => {

    const params = fetchParams({
        ...where,
        sort,
        limit,
        offset
    })

    return fetchGet('https://www.example.com/api/v1/results?'+params)
}

Readme

Keywords

none

Package Sidebar

Install

npm i @customd/cd-fetch-model

Weekly Downloads

1

Version

0.6.9

License

MIT

Unpacked Size

33.7 kB

Total Files

13

Last publish

Collaborators

  • custom-d