@cgnal/redux/actions

Utilities to create and manage flux standard actions.

Source:

Methods

(static) createActionWithSource(type) → {function}

Source:
Since:
  • 0.0.19

Builds an action creator that accepts an Action as the second optional parameter that will be put in the sourceAction property of the metadata.

Example
const payload = [1, 2, 3];
const sourceAction = { type: "SOURCE_ACTION" };
const createSomeAction = createActionWithSource("SOME_ACTION");

createSomeAction(payload, sourceAction) // =>
// {
//     meta: {
//         sourceAction: { type: "SOURCE_ACTION" }
//     },
//     payload: [1, 2, 3],
//     type: "SOME_ACTION"
// }
Parameters:
Name Type Description
type String
Returns:

The action creator: (Any, Action?) => Action

Type
function

(static) createEmptyAction(type) → {function}

Source:
Since:
  • 0.0.12

Builds an action creator to make a flux standard action without any payload or metadata.

Example
const createSomeAction = createEmptyAction("SOME_ACTION");

createSomeAction([1, 2, 3]) // => { type: "SOME_ACTION" }
Parameters:
Name Type Description
type String

The action type

Returns:

The action creator: () => Action

Type
function

(static) createHttpFailureAction(type) → {function}

Source:
Since:
  • 0.0.1

Builds an action creator to make flux standard actions representing HTTP failures.
The action creator expects an Error as first parameter, that will be the action payload, and the action that led to the HTTP failure as the second parameter.
The resulting action will have an additional meta property, which will be an object with a single key originalAction.

Example
const originalAction = { type: "ORIGINAL_ACTION" };
const failure = new Error("some message");
const createErrorAction = createHttpFailureAction("API_CALL_KO");

createErrorAction(failure, originalAction) // =>
// {
//     error: true,
//     meta: {
//         originalAction: { type: "ORIGINAL_ACTION" }
//     },
//     payload: Error, // the "failure" error above
//     type: "API_CALL_KO"
// }

createErrorAction(failure) // =>
// {
//     error: true,
//     meta: {
//         originalAction: undefined
//     },
//     payload: Error, // the "failure" error above
//     type: "API_CALL_KO"
// }
Parameters:
Name Type Description
type String

The action type

Returns:

The action creator: (Error, Action?) => Action

Type
function

(static) createHttpSuccessAction(type) → {function}

Source:
Since:
  • 0.0.7

Builds an action creator to make flux standard actions representing HTTP successes.
The action creator expects a successful HTTP Response and will put its body in the action payload.
The metadata will be an object with the complete Response in the response property, and the original action in the originalAction one.

Example
const originalAction = { type: "ORIGINAL_ACTION" };
const response = {
    body: [1, 2, 3],
    status: 200
};
const createSuccessAction = createHttpSuccessAction("API_CALL_OK");

createSuccessAction(response, originalAction) // =>
// {
//     meta: {
//         originalAction: { type: "ORIGINAL_ACTION" },
//         response: {
//             body: [1, 2, 3],
//             status: 200
//         }
//     },
//     payload: [1, 2, 3],
//     type: "API_CALL_OK"
// }
Parameters:
Name Type Description
type String

The action type

Returns:

The action creator: (Response, Action?) => Action

Type
function

(static) getPayloadAsArray(action) → {Array}

Source:
Since:
  • 0.0.19

Returns the payload of the received Action wrapped in a one element array.

Example
const action = {
    payload: { value: 1 },
    type: "SOME_TYPE"
};

getPayloadAsArray(action) => // [{ value : 1 }]
Parameters:
Name Type Description
action Action
Returns:

A one element array containing the payload.

Type
Array

(static) isHttpUnauthorizedAction(action) → {Boolean}

Source:
Since:
  • 0.0.19

Checks if the received action contains an unauthorized HTTP error (401)

Example
const fake401Error = { status: 401 };
const fake500Error = { status: 500 };
const action1 = {
    error: true,
    payload: fake401Error,
    type: "API_CALL_KO"
};
const action2 = {
    payload: fake401Error,
    type: "API_CALL_KO"
};
const action3 = {
    error: true,
    payload: fake500Error,
    type: "API_CALL_KO"
};

isHttpUnauthorizedAction(action1) // => true
isHttpUnauthorizedAction(action2) // => false
isHttpUnauthorizedAction(action3) // => false
Parameters:
Name Type Description
action Action
Returns:
Type
Boolean