Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redux-api-middleware-actions

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-api-middleware-actions

Strongly typed action creator functions for redux-api-middleware and similar

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

redux-api-middleware-actions

Action creators for https://github.com/agraboso/redux-api-middleware or the like.

This library is optimised for redux projects using TypeScript.

It is inspired heavily by https://github.com/vgmr/redux-helper.

Installation

npm i redux-api-middleware-actions --save

or

yarn add redux-api-middleware-actions

Usage

Assuming you have already set up your api middleware (in this example https://github.com/agraboso/redux-api-middleware

In action creators

import { createApiAction } from 'redux-typed-async-actions';
import { CALL_API } from 'redux-api-middleware';

export const fetchClients = createApiAction<{}, IClientModel[]>('CLIENTS_FETCH', {
  CALL_API,
  method: 'GET',
  endpoint: `/api/clients`,
});

The CALL_API is the name of the signature property or symbol used for your API middleware.

export const createClient = createApiAction<IClientModel, {}>('CLIENT_CREATE', {
  CALL_API,
  method: 'POST',
  endpoint: `/api/clients`,
});

The two generic parameters are the payload and the response types.

The payload is the parameter passed to the function e.g.

dispatch(createClient({
  name: 'Bob the Builder',
}));

You can also pass a function instead of an object as the second parameter. This function can take the payload as an argument.

export const fetchClients = createApiAction<{ name: string }, IClientModel[]>('FETCH_CLIENTS', ({name}) => ({
  CALL_API,
  method: 'POST',
  endpoint: `/api/clients?name=${name}`,
}));

In Reducers

You can then use action creators themselves to match actions and strongly type the action.payload (on success it is the response from the api, of type TResponse you defined in the action creator).

 if (actions.fetchClients.matchesSuccess(action)) {
    return {
      ...state,
      list: action.payload,
    };
  } else if (actions.fetchClients.matchesPending(action)) {
    ...
  } else if (actions.fetchClients.matchesFailure(action)) {
    ...
  }

Keywords

FAQs

Package last updated on 07 Jul 2017

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc