1.0.29 • Published 5 years ago

redux-provider v1.0.29

Weekly downloads
118
License
-
Repository
-
Last release
5 years ago

redux-provider

N|Crowdkeep

Redux provider is an immutable store for api request responses built with redux and redux-sagas. Support for React and Angular is built-in.

Features

  • React support
  • Angular support
  • Schema type serializations
  • Immutable store
  • API Tokenization
  • Tokenization
  • Model preloading
  • Request preloading
  • Reusuable redux type responses
  • Request transformation
  • Response transformation
  • Request cancellation
  • Paremeter serialization
  • Currently supports all HTTP request types
  • Authorization
  • Custom adapaters
  • Upload and Download progress statuses
  • Status Validations
  • WebSockets
  • Proxies

Usage

Provider

import Provider from 'redux-provider'

import { MyComponent } from './my-component'

import { Type } from './type' // example Type after this example

// need to update with dynamic importing
export const MyModel = new Provider({
  basePath: apiUrl,
  path: paths.PROVIDER_PATH, // basePath/path
  token: ACCESS_TOKEN,
  type: Type,
})

const myPreloadFunction = (params, enhance) => {
  const type = 'MY_REDUX_TYPE'
  const { someParam } = params

  MyModel.post(
    {
      component: MyComponent, // optional currently for React only
      type: type, // required
    },
    {
      params: {
        someParam: someParam,
        type: type, 
        /* 
          "type" param is not required, 
           but good practice for backend developers to connect what is going on frontend wise.
        */
      },
      transformResponse: enhance,
      /*
        not required, but extra filter on response other than type property filters
      */
    },
  )
}

// Optional preload function for multiple request types
export const preloadMyModel = (params, enhance) => {
  myPreloadFunction(params, enhance)
}

Provider Type

// Required response property types
const MyModel = {
  name: String,
  status: String,
  user: String,
  account: String,
  created_at: String,
  updated_at: String,
  guid: String,
}

// Optional response property types
const MyModelOptions = {
  rank: Number,
  count: Number,
}

export const Type = {
  defaults: MyModel,
  options: MyModelOptions,
}

React

import React from 'react'

import { MyModel, preloadMyModel } from '../../../providers'

class MyComponent extends React.PureComponent {
  constructor(props) { // not required
    super(props)
  }
  componentWillMount() {
    const params = {
      ...
    }
    preloadMyModel(params, (response, type) => {
      const { ... } = response
      if (type === 'SOME_TYPE_ONE') {
        ...
      } else if (type === 'SOME_TYPE_TWO') {
        ...
      } else if (type === 'SOME_TYPE_THREE') {
        ...
      }
      return response
    })
  }
}