hw-api-fetch

1.0.12 • Public • Published

hw-api-fetch

NPM JavaScript Style Guide

Install

npm install --save hw-api-fetch
or
yarn add hw-api-fetch

Usage

First you have to pass the properties of your api to the HWApiFetch component:

import HWApiFetch from 'hw-api-fetch'
 
const properties = {
  host: 'https://www.yourapi.com.br/api/',
  cookiesToHeader: [{ 
    key: 'authId',
    cookie: 'authentication'
  },
  'authorization'],
  beforeReturn: [
    res => {return {...res, test: true}}
  ],
  log: true,
  hwResponse: true
}
 
HWApiFetch.init(properties)
 

The properties are:

Key Type Description
host string The host of your api.
cookiesToHeader array Array of objects with the format {key, cookie}. If is passed a string it will be presumed that the key and the cookie are equals. If these cookies exists in the browser, it will be send with all your requests.
beforeReturn array Array of functions to be applied before the data returns.
log boolean Log api calls to the console. Default false.
hwResponse boolean Data comes with hwResponse format. Default false.
noContentType boolean Remove default content-type sended in the header. Default false.
fetchProperties object Add properties to fetch options.

Then you can make the calls to your api:

import HWApiFetch from 'hw-api-fetch'
 
HWApiFetch.get('getUsers', {limit: 10}).then( response => ... )
HWApiFetch.post('newUser', {user: 'John', password: 'ILikeWaffles'}).then( response => ... )
HWApiFetch.put('editUser', {name: 'Not John', password: 'NewPassword'}).then( response => ... )
HWApiFetch.patch('editUser', {name: 'Not John'}).then( response => ... )
HWApiFetch.delete('deleteUser', {name: 'John'}).then( response => ... )

get, post, put, patch, delete params are:

Param Type Description
path string The path of your call.
data object The data that you want to send.

License

MIT © daniellrs

Readme

Keywords

none

Package Sidebar

Install

npm i hw-api-fetch

Weekly Downloads

0

Version

1.0.12

License

MIT

Unpacked Size

374 kB

Total Files

8

Last publish

Collaborators

  • daniellrs