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

typescript-fsa-redux-thunk

v2.10.2

Published

TypeScript FSA utilities for redux-thunk

Downloads

28,060

Readme

TypeScript FSA utilities for redux-thunk

npm (tag) npm GitHub last commit (branch) Build Status codecov

NOTE: There's breaking changes from 1.x. Read on to find out more and check the notes at the bottom for more info

Installation

npm install typescript-fsa-redux-thunk redux redux-thunk

API

thunkToAction(ThunkActionCreator): ((Params) => Result)

Another useful cast function that can help when attempting to extract the return value out of your async action creator. If the action is being pre-bound to dispatch, then all we want back is the return value (the action object). Coming soon: an example. TL;DR: pass your async action creator into this before passing it to bindActionCreators or the mapDispatchToProps object (react-redux).

asyncFactory<State>(ActionCreatorFactory): ((type: string, AsyncWorker) => ({ (params?): ThunkActionCreator, async: AsyncActionCreators }))

Factory function to easily create a typescript-fsa redux thunk.

Example

import 'isomorphic-fetch';
import { createStore, applyMiddleware, AnyAction } from 'redux';
import thunkMiddleware, { ThunkMiddleware } from 'redux-thunk';
import { reducerWithInitialState } from 'typescript-fsa-reducers';
import actionCreatorFactory from 'typescript-fsa';
import { asyncFactory } from 'typescript-fsa-redux-thunk';

/** You can optionally use custom Error types */
class CustomError extends Error {}

/** Parameters used for logging in */
interface LoginParams {
  email: string;
  password: string;
}

/** The object that comes back from the server on successful login */
interface UserToken {
  token: string;
}

/** The shape of our Redux store's state */
interface State {
  title: string;
  userToken: UserToken;
  loggingIn?: boolean;
  error?: CustomError;
}

/** The typescript-fsa action creator factory function */
const create = actionCreatorFactory('examples');

/** The typescript-fsa-redux-thunk async action creator factory function */
const createAsync = asyncFactory<State>(create);

/** Normal synchronous action */
const changeTitle = create<string>('Change the title');

/** The asynchronous login action; Error type is optional */
const login = createAsync<LoginParams, UserToken, CustomError>(
  'Login',
  async (params, dispatch) => {
    const url = `https://reqres.in/api/login`;
    const options: RequestInit = {
      method: 'POST',
      body: JSON.stringify(params),
      headers: {
        'Content-Type': 'application/json; charset=utf-8',
      },
    };
    const res = await fetch(url, options);
    if (!res.ok) {
      throw new CustomError(`Error ${res.status}: ${res.statusText}`);
    }

    dispatch(changeTitle('You are logged-in'));

    return res.json();
  },
);

/** An initial value for the application state */
const initial: State = {
  title: 'Please login',
  userToken: {
    token: '',
  },
};

/** Reducer, handling updates to indicate logging-in status/error */
const reducer = reducerWithInitialState(initial)
  .case(changeTitle, (state, title) => ({
    ...state,
    title,
  }))
  .case(login.async.started, (state) => ({
    ...state,
    loggingIn: true,
    error: undefined,
  }))
  .case(login.async.failed, (state, { error }) => ({
    ...state,
    loggingIn: false,
    error,
  }))
  .case(login.async.done, (state, { result: userToken }) => ({
    ...state,
    userToken,
    loggingIn: false,
    error: undefined,
  }));

/** Putting it all together */
(async () => {
  // Declaring the type of the redux-thunk middleware is what makes
  // `store.dispatch` work. ([email protected], [email protected])
  const thunk: ThunkMiddleware<State, AnyAction> = thunkMiddleware;
  const store = createStore(reducer, applyMiddleware(thunk));

  console.log(store.getState().title);

  try {
    // See https://reqres.in/api/users for valid users on this site
    await store.dispatch(
      login({
        email: '[email protected]',
        password: 'cityslicka',
      }),
    );

    const { title, userToken } = store.getState();

    console.log(title, userToken);
  } catch (err) {
    console.log(err);
  }
})();

Note: A change from 1.x is the result type is not always assumed to be a Promise. If you want the result to be a promise, just return one from your worker function; but continue to specify the result as T rather than Promise<T> (same as 1.x).

The API has been simplified. This release is in preparation for a new project that works with react hooks. Coming soon!

react-redux integrated

import { ThunkDispatch, AnyAction } from 'redux-thunk';
import { RootState } from 'to/your/reducers';

declare module 'react-redux' {
  interface DefaultRootState extends RootState {}

  function useDispatch(): ThunkDispatch<RootState, never, AnyAction>;
}

declare module 'typescript-fsa-redux-thunk' {
  interface DefaultRootState extends RootState {}
}