@xtreamr/cla-request

1.0.5 • Public • Published

Install

yarn add @xtreamr/cla-request 
//or
npm install @xtreamr/cla-request --save-dev

Use:

import Request from '@xtreamr/cla-request'

export const request = new Request()

request.init({ apiUrl: 'www.someapiurl.com' })
request.setBearer({ bearer: 'auth-token' }) // set as authorization header

// - Path: path to build the final end point with apiUrl. If path is: /product/1 -> www.someapiurl.com/product/1
// - Options: same options as vanilla fetch: https://developer.mozilla.org/es/docs/Web/API/Fetch_API
// with some changes. Body should be an object and 'application/json' is used as default content type header. If you prefer use a formData pass a formData key that will be set as body internaly (you can use both at same time: body and formData)
// Add two new options to cache responses: cache and refreshCache (to force refresh cache data).
request.fetch(path, options)

To mock data into development you could configure the request with two paremeters into constructor:

const mockData = {
  // use query, parameters and optionsFetch to mock the api
  // use async functions to not include this code into the final bundle
  '/product/{id}': async ({ 
    query, // query parameters 
    parameters, // path parameters (id in this case), show path '/product/{id}' into mockOptions
    optionsFetch // fetch options { method, mode, headers, body...} https://developer.mozilla.org/es/docs/Web/API/Fetch_API/Using_Fetch
  }) => {
    return {
      id: 1,
      name: 'tv'
    }
  },
  '/products': async () => {
    return [
      {
        id: 1,
        name: 'tv'
      },
      {
        id: 2,
        name: 'computer'
      }
    ]
  },
}

const mockOptions = {
    disabled: false, // if is disable the rest of keys haven't any effect
    delay: 10, // delay of response
    all: false, // all path mock = true
    paths: { // to enable or disable by path
      '/product/{id}': true, // enabled
      '/products': false, // disabled
    },
  },

export const request = new Request({
  mockData, 
  mockOptions
})

Dependencies (1)

Dev Dependencies (13)

Package Sidebar

Install

npm i @xtreamr/cla-request

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

233 kB

Total Files

10

Last publish

Collaborators

  • dcantelar
  • mancas
  • pacorampas
  • xtreamr-ci
  • mirframai
  • frsela
  • guillaumeparis2000
  • mardito