Skip to content

Commit

Permalink
Added create-custom-action creator factory to public API (#113)
Browse files Browse the repository at this point in the history
Resolved #111 
Resolved #52 

Numerous improvements of API reference docs
  • Loading branch information
piotrwitek authored Feb 15, 2019
1 parent f41c08c commit e12cf99
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 310 deletions.
530 changes: 245 additions & 285 deletions README.md

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/create-async-action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StringType, Box, FsaMapBuilder, FsaBuilder } from './types';
import { createActionWithType } from './create-action-with-type';
import { createCustomAction } from './create-custom-action';
import { validateActionType } from './utils/utils';

export interface CreateAsyncAction<
Expand Down Expand Up @@ -68,15 +68,15 @@ export function createAsyncAction<
P3
> {
return {
request: createActionWithType(requestType, type => (payload?: P1) => ({
request: createCustomAction(requestType, type => (payload?: P1) => ({
type: requestType,
payload,
})) as FsaBuilder<T1, Box<P1>>,
success: createActionWithType(successType, type => (payload?: P2) => ({
success: createCustomAction(successType, type => (payload?: P2) => ({
type: successType,
payload,
})) as FsaBuilder<T2, Box<P2>>,
failure: createActionWithType(failureType, type => (payload?: P3) => ({
failure: createCustomAction(failureType, type => (payload?: P3) => ({
type: failureType,
payload,
})) as FsaBuilder<T3, Box<P3>>,
Expand All @@ -89,15 +89,15 @@ export function createAsyncAction<
// failureMapper: (a?: A3) => P3
// ): AsyncActionWithMappers<T1, T2, T3, A1, P1, A2, P2, A3, P3> {
// return {
// request: createActionWithType(requestType, type => (payload?: A1) => ({
// request: createCustomAction(requestType, type => (payload?: A1) => ({
// type,
// payload: requestMapper != null ? requestMapper(payload) : undefined,
// })) as MapBuilder<T1, B<A1>, B<P1>>,
// success: createActionWithType(successType, type => (payload?: A2) => ({
// success: createCustomAction(successType, type => (payload?: A2) => ({
// type,
// payload: successMapper != null ? successMapper(payload) : undefined,
// })) as MapBuilder<T2, B<A2>, B<P2>>,
// failure: createActionWithType(failureType, type => (payload?: A3) => ({
// failure: createCustomAction(failureType, type => (payload?: A3) => ({
// type,
// payload: failureMapper != null ? failureMapper(payload) : undefined,
// })) as MapBuilder<T3, B<A3>, B<P3>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as Types from './types';
import { createActionWithType } from './create-action-with-type';
import { createCustomAction } from './create-custom-action';

describe('createActionWithType', () => {
describe('createCustomAction', () => {
it('with type only using symbol', () => {
const INCREMENT = Symbol(1);
const increment = createActionWithType(INCREMENT, type => () => ({ type }));
const increment = createCustomAction(INCREMENT, type => () => ({ type }));
const actual = increment();
// @dts-jest:pass:snap -> { type: unique symbol; }
actual;
expect(actual).toEqual({ type: INCREMENT });
});

it('with type only', () => {
const increment = createActionWithType('WITH_TYPE_ONLY');
const increment = createCustomAction('WITH_TYPE_ONLY');
const actual: {
type: 'WITH_TYPE_ONLY';
} = increment();
expect(actual).toEqual({ type: 'WITH_TYPE_ONLY' });
});

it('with payload', () => {
const add = createActionWithType('WITH_PAYLOAD', type => {
const add = createCustomAction('WITH_PAYLOAD', type => {
return (amount: number) => ({ type, payload: amount });
});
const actual: {
Expand All @@ -31,7 +31,7 @@ describe('createActionWithType', () => {
});

it('with optional payload', () => {
const create = createActionWithType('WITH_OPTIONAL_PAYLOAD', type => {
const create = createCustomAction('WITH_OPTIONAL_PAYLOAD', type => {
return (id?: number) => ({ type, payload: id });
});
const actual1: {
Expand All @@ -50,7 +50,7 @@ describe('createActionWithType', () => {
});

it('with payload and meta', () => {
const showNotification = createActionWithType(
const showNotification = createCustomAction(
'SHOW_NOTIFICATION',
type => (message: string, scope: string) => ({
type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as Types from './types';
import { createActionWithType } from './create-action-with-type';
import { createCustomAction } from './create-custom-action';

describe('createActionWithType', () => {
describe('createCustomAction', () => {
it('with type only using symbol', () => {
const INCREMENT = Symbol(1);
const increment = createActionWithType(INCREMENT, type => () => ({ type }));
const increment = createCustomAction(INCREMENT, type => () => ({ type }));
const actual = increment();
// @dts-jest:pass:snap
actual;
expect(actual).toEqual({ type: INCREMENT });
});

it('with type only', () => {
const increment = createActionWithType('WITH_TYPE_ONLY');
const increment = createCustomAction('WITH_TYPE_ONLY');
const actual: {
type: 'WITH_TYPE_ONLY';
} = increment();
expect(actual).toEqual({ type: 'WITH_TYPE_ONLY' });
});

it('with payload', () => {
const add = createActionWithType('WITH_PAYLOAD', type => {
const add = createCustomAction('WITH_PAYLOAD', type => {
return (amount: number) => ({ type, payload: amount });
});
const actual: {
Expand All @@ -31,7 +31,7 @@ describe('createActionWithType', () => {
});

it('with optional payload', () => {
const create = createActionWithType('WITH_OPTIONAL_PAYLOAD', type => {
const create = createCustomAction('WITH_OPTIONAL_PAYLOAD', type => {
return (id?: number) => ({ type, payload: id });
});
const actual1: {
Expand All @@ -50,7 +50,7 @@ describe('createActionWithType', () => {
});

it('with payload and meta', () => {
const showNotification = createActionWithType(
const showNotification = createCustomAction(
'SHOW_NOTIFICATION',
type => (message: string, scope: string) => ({
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionCreator, StringOrSymbol } from './types';
/**
* @description create custom action-creator using constructor function with injected type argument
*/
export function createActionWithType<
export function createCustomAction<
T extends StringOrSymbol,
AC extends ActionCreator<T> = () => { type: T }
>(type: T, actionCreatorHandler?: (type: T) => AC): AC {
Expand Down
6 changes: 3 additions & 3 deletions src/create-standard-action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StringType, Box, FsaBuilder, FsaMapBuilder } from './types';
import { createActionWithType } from './create-action-with-type';
import { createCustomAction } from './create-custom-action';
import { validateActionType } from './utils/utils';

export interface CreateStandardAction<T extends StringType> {
Expand All @@ -18,7 +18,7 @@ export function createStandardAction<T extends StringType>(
validateActionType(actionType);

function constructor<P, M = void>(): FsaBuilder<T, Box<P>, Box<M>> {
return createActionWithType(actionType, type => (payload: P, meta: M) => ({
return createCustomAction(actionType, type => (payload: P, meta: M) => ({
type,
payload,
meta,
Expand All @@ -28,7 +28,7 @@ export function createStandardAction<T extends StringType>(
function map<R, P, M>(
fn: (payload: P, meta: M) => R
): FsaMapBuilder<T, Box<R>, Box<P>, Box<M>> {
return createActionWithType(actionType, type => (payload: P, meta: M) =>
return createCustomAction(actionType, type => (payload: P, meta: M) =>
Object.assign(fn(payload, meta), { type })
) as FsaMapBuilder<T, Box<R>, Box<P>, Box<M>>;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
export { action } from './action';
export { createAction } from './create-action';
export { createStandardAction } from './create-standard-action';
export { createCustomAction } from './create-custom-action';
export { createAsyncAction } from './create-async-action';
// guards
export { ActionType, StateType } from './types';
Expand Down

0 comments on commit e12cf99

Please sign in to comment.