Options
All
  • Public
  • Public/Protected
  • All
Menu

Redux-Routines-Ts

A helper for the REQUEST/SUCCESS/FAILURE action creator pattern found in Redux.

Utilizes deox to provide strong TypeScript inferences.

Helps reduce boilerplate when making asynchronous API calls in Sagas or Thunks.

API Reference

Full Example


Example Usage

// saga.ts
function* fetchFooSaga({ meta }: ReturnType<typeof fetchFoo.trigger>) {
  const { id } = meta;

  try {
    const foo = yield call(api, id);
    yield put(fetchFoo.success(foo, meta));
  } catch (error) {
    yield put(fetchFoo.failure(error, meta));
  }
}

export function* fooSaga() {
  yield takeEvery(fetchFoo.TRIGGER, fetchFooSaga);
}

// Component.tsx
dispatch(fetchFoo.trigger({ id: '5' }))

Inspired By

Index

Type aliases

RoutineCreator

RoutineCreator: function

Type declaration

    • <Payload, Params>(prefix: string): Routine<Payload, Params>
    • Type parameters

      • Payload

      • Params

      Parameters

      • prefix: string

      Returns Routine<Payload, Params>

Variables

Const FAILURE_POSTFIX

FAILURE_POSTFIX: "FAILURE" = "FAILURE"

Const SUCCESS_POSTFIX

SUCCESS_POSTFIX: "SUCCESS" = "SUCCESS"

Const TRIGGER_POSTFIX

TRIGGER_POSTFIX: "TRIGGER" = "TRIGGER"

Functions

Const createRoutine

  • createRoutine<Payload, Params>(typePrefix: string): Routine<Payload, Params>
  • Creates a set of life-cycle actions that are useful for asynchronous actions like fetching data

    const fetchFoo = createRoutine<Foo, { id: string }>('FETCH_FOO')
    const fetchAll = createRoutine<Foo[]>('FETCH_ALL_FOO')

    Type parameters

    • Payload

      the data to be merged into state, usually a domain object from your API

    • Params

      the metadata required to start a routine, for example the ID of an object

    Parameters

    • typePrefix: string

      prefix for action type

    Returns Routine<Payload, Params>

errorReducer

  • errorReducer<T>(state?: DeepImmutableObject<T>, __namedParameters: object): any
  • Listens to all actions on the store and stores error state for every routine

    // store.ts
    import { errorReducer } from 'redux-routines-ts'
    
    const rootReducer = combineReducers({
      // your other reducers...
      error: errorReducer,
    });
    
    const store = createStore(rootReducer, middleware)

    Type parameters

    • T

    Parameters

    • Default value state: DeepImmutableObject<T> = {} as DeepImmutableObject<T>
    • __namedParameters: object
      • payload: any
      • type: any

    Returns any

loadingReducer

  • loadingReducer<T>(state?: DeepImmutableObject<T>, __namedParameters: object): any
  • Listens to all actions on the store and stores loading state for every routine

    // store.ts
    import { loadingReducer } from 'redux-routines-ts'
    
    const rootReducer = combineReducers({
      // your other reducers...
      loading: loadingReducer,
    });
    
    const store = createStore(rootReducer, middleware)

    Type parameters

    • T

    Parameters

    • Default value state: DeepImmutableObject<T> = {} as DeepImmutableObject<T>
    • __namedParameters: object
      • type: any

    Returns any

Const matchPostfix

  • matchPostfix(type: string): [string, string] | null

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc