npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-gateway

v1.5.0

Published

API Gateway request lifecycle with redux

Downloads

5

Readme

redux-gateway

API gateway request lifecycle management with redux

import gateway from 'redux-gateway'

This gives you access to the reducer, constants, actions, and selectors available

connectRequest

import { connectRequest } from 'redux-gateway'

connectRequest is a higher-order-component that connects the request object to another component.
You can listen in on request events and dispatch other actions. You can add custom views to different request states, as well as add custom actions to the mount and unmount lifecycle events

Below is the full interface for connectRequest, ordered in the sequence that the actions take place

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'
import notification from 'path/to/notification'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  // onMount
  onMount: (props, dispatch) => {
    dispatch(customAction(props.request))
  },
  clearOnMount: true,

  // request on mount options
  requestOnMount: true,
  requestOnMountParams: (props) => ({name: 'First', last: 'Last'}),
  requestOnMountBody: (props) => ({name: 'First', last: 'Last'}),

  // on unmount
  onUnMount: (props, dispatch) => {
    dispatch(customAction(props.request))
  },
  clearOnUnmount: true,

  // state actions
  onProcessing: (request, dispatch) => {
    dispatch(notification({message: 'In progress'}))
  },
  onProcessing: (request, dispatch) => {
    console.log('onProcessing - ', request)
  },
  onSuccess: (request, dispatch) => {
    console.log('onSuccess - ', request)
  },
  onFailure: (request, dispatch) => {
    console.log('onFailure - ', request.error)
  },
  onComplete: (request, dispatch) => {
    console.log('onComplete - ', request)
  },

  // alternate views
  processingView: LoadingView,
  failureView: FailureView
})(MyComponent)

Examples

Create request / dispatch request

import gateway, { createRequest } from 'redux-gateway'

// create request
gateway.actions.createRequest({route: 'apiGatewayRoute'})
createRequest({route: 'apiGatewayRoute'})

// create GET request with params
gateway.actions.createRequest({route: 'apiGatewayRoute', params: {name: 'value'}})
createRequest({route: 'apiGatewayRoute', params: {name: 'value'}})

// create POST request with body
gateway.actions.createRequest({route: 'apiGatewayRoute', body: {name: 'value'}})
createRequest({route: 'apiGatewayRoute', body: {name: 'value'}})

// with dispatch
dispatch(gateway.actions.createRequest({route: 'apiGatewayRoute'}))
dispatch(createRequest({route: 'apiGatewayRoute'}))

Clear request / dispatch clear request

This resets the request object back to the initial model state

import gateway, { clearRequest } from 'redux-gateway'

// clear request
gateway.actions.clearRequest('apiGatewayRoute')
clearRequest('apiGatewayRoute')

// with dispatch
dispatch(gateway.actions.clearRequest('apiGatewayRoute'))
dispatch(clearRequest('apiGatewayRoute'))

Get request selector

To use and transform the data from the request, use the getRequestByName by name selector

import gateway, { getRequestByName }

// usage
gateway.selectors.getRequestByName(state, 'requestRouteName')
getRequestByName(state, 'requestRouteName')

// With connect from react-redux, you can map the request properties
import { connect } from 'react-redux'
import MyComponent from '/path/to/component'
connect((state) => {
  const req = getRequestByName(state, 'requestRouteName')
  return {
    list: req.response,
    submitting: req.isProcessing
  }
})(MyComponent)

Send request when component mounts

Often times you'll want to get data when the component mounts. Using requestOnMount will fire off the API request on the componentWillMount event.

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  requestOnMount: true,
})(MyComponent)

On mount request with params

For GET requests that require parameters when triggered on mount, use requestOnMountParams

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  requestOnMountParams: (props) => ({name: 'First', last: 'Last'}),
})(MyComponent)

On mount request with body

For POST requests that require parameters when triggered on mount, use requestOnMountBody

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  requestOnMountBody: (props) => ({name: 'First', last: 'Last'})
})(MyComponent)

On mount

If you need to do anything on mount, you have access to the props object and dispatch with the onMount callback. The request object is available with props.request

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  onMount: (props, dispatch) => {
    dispatch(customAction(props.request))
  }
})(MyComponent)

Clear on mount

If you want to reset the request back to it's initial state on mount, you can set the clearOnMount parameter

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  clearOnMount: true
})(MyComponent)

On unmount

If you need to do anything on unmount, you have access to the props object and dispatch with the onUnmount callback. The request object is available with props.request

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  onUnmount: (props, dispatch) => {
    dispatch(customAction(props.request))
  }
})(MyComponent)

Clear on unmount

If you want to reset the request back to it's initial state, you can set the clearOnUnmount parameter

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  clearOnUnmount: true
})(MyComponent)

mapStateToProps / mapDispatchToProps

You can map state and dispatch to props, and transform the request object as needed.

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  requestOnMountParams: (props) => ({name: 'First', last: 'Last'}),
  mapStateToProps: (state, request) => {
    return {
      plans: request.response,
    }
  }
})(MyComponent)

Listen for request events

connectRequest has an interface to listen to events, when the request is processing, is successful, has a failure, or becomes complete. You have access to the request object itself, and dispatch for firing other redux actions

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  onProcessing: (request, dispatch) => {
    console.log('onProcessing - ', request)
  },
  onSuccess: (request, dispatch) => {
    console.log('onSuccess - ', request)
  },
  onComplete: (request, dispatch) => {
    console.log('onComplete - ', request)
  },
  onFailure: (request, dispatch) => {
    console.log('onFailure - ', request.error)
})(MyComponent)

Processing and Failure Views

If you want show a different view for isProcessing and isFailure states, you may use processingView and failureView accordingly

import { connectRequest } from 'redux-gateway'
import MyComponent from 'path/to/MyComponent'
import LoadingView from 'path/to/LoadingView'
import FailureView from 'path/to/FailureView'

const SearchRequestForm = connectRequest({
  route: 'searchPlansRoute',
  processingView: LoadingView,
  failureView: FailureView
})(MyComponent)