REST-API caller using fetch
Call any REST-API in the browser.
How to use
Install
You can install this package via NPM
npm install --save fetch-rest-connect
Using yarn
yarn add fetch-rest-connect
Usage
All methods used provided by the package are asynchronous.
const CONTENT_ENDPOINT = 'content' const fetcher = apiUrl: `/api` endpoints: CONTENT_ENDPOINT: '/content/' options: // optional credentials: 'include' const getContent = async { try const data = await fetcher return data catch error throw error }
Your API endpoints can also hold any kind of variable:
const endpoints = CONTENT_ENDPOINT: '/article/:articleId/content/'
When calling the fetcher you can now pass uriParams
to fill the vars in the
route.
const articleId = `foo`;const uriParams = articleId;const data = await fetcher
Methods
All methods build upon the main method fetch
:
fetch
, Fetch entry pointaction: string
, Action or endpoint you want to call, requiredid: String
, If you want to call a specific ID of an endpoint, you can use this fielduriParams: Object
, If the endpoint has variables in it you can use this object to fill themadditionalQueryParams: object
, Pass get parameters for the requestdata: Object
, Data you want to send with the requestmethod: ENUM('GET' | 'PUT' | 'POST' | 'DELETE') = 'GET'
, Specific method you want for your callheaders: Object
, additional headers for request
Params are the same as above
get(action, id, uriParams, additionalQueryParams, headers)
getAll(action, uriParams, additionalQueryParams, headers)
create(action, data, uriParams, additionalQueryParams, headers)
update(action, id, data, uriParams, additionalQueryParams, headers)
upsert(action, id, data, uriParams, additionalQueryParams, headers)
delete(action, id, uriParams, additionalQueryParams, headers)
License
This library is licensed under MIT