A helper which extends fetch with powerful methods for simplifying interaction with RESTful APIs.
npm install @customd/cd-fetch-modelFetch 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.
``javascript
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.
`javascript
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.,
`javascript
import Results from 'api/Results'
Results.getWhere({ foo: 'bar' }).then(() => {
/ do something /
})
`
You can also implement custom methods accessing fetch helper methods directly, as required.
`javascript
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)
}
``