Socket
Socket
Sign inDemoInstall

@xtreamr/cla-request

Package Overview
Dependencies
15
Maintainers
14
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @xtreamr/cla-request

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


Version published
Maintainers
14
Install size
806 kB
Created

Readme

Source

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
})

FAQs

Last updated on 03 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc