Skip to content

Commit

Permalink
create proper action
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jun 16, 2021
1 parent 43f522e commit f083f84
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_ACTION_PREFIX = '@@saga-query';
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './api';
export * from './types';
export * from './fetch';
export * from './middleware';
export * from './constants';
16 changes: 3 additions & 13 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ApiCtx,
Next,
} from './types';
import { isObject } from './util';
import { isObject, createAction } from './util';

export function* queryCtx<Ctx extends ApiCtx = ApiCtx>(ctx: Ctx, next: Next) {
if (!ctx.request) ctx.request = { url: '', method: 'GET' };
Expand Down Expand Up @@ -105,18 +105,8 @@ interface UndoCtx<R = any, P = any> extends ApiCtx {
undoable: boolean;
}

export const doIt = () => {
const type = '@@saga-query/DO_IT';
const action = { type };
action.toString = () => type;
return action;
};
export const undo = () => {
const type = '@@saga-query/UNDO';
const action = { type };
action.toString = () => type;
return action;
};
export const doIt = createAction('DO_IT');
export const undo = createAction('UNDO');
export function undoer<Ctx extends UndoCtx = UndoCtx>(
doItType = `${doIt}`,
undoType = `${undo}`,
Expand Down
2 changes: 1 addition & 1 deletion src/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CreateActionWithPayload,
PipeCtx,
} from './types';
import { API_ACTION_PREFIX } from './constants';

export function compose<Ctx = any>(middleware: Middleware<Ctx>[]) {
if (!Array.isArray(middleware)) {
Expand Down Expand Up @@ -75,7 +76,6 @@ export const defaultOnError = (err: Error) => {
throw err;
};

export const API_ACTION_PREFIX = '@@saga-query';
export function createPipe<Ctx extends PipeCtx = PipeCtx<any>>({
onError = defaultOnError,
}: {
Expand Down
13 changes: 13 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import test from 'ava';
import { createAction } from './util';
import { API_ACTION_PREFIX } from './constants';

test('createAction - should return action type when stringified', (t) => {
const undo = createAction('UNDO');
t.assert(`${API_ACTION_PREFIX}/UNDO` === `${undo}`);
});

test('createAction - return object with type', (t) => {
const undo = createAction('UNDO');
t.deepEqual(undo(), { type: `${API_ACTION_PREFIX}/UNDO` });
});
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import { API_ACTION_PREFIX } from './constants';
export const isFn = (fn?: any) => fn && typeof fn === 'function';
export const isObject = (obj?: any) => typeof obj === 'object' && obj !== null;
export const createAction = (curType: string) => {
if (!curType) throw new Error('createAction requires non-empty string');
const type = `${API_ACTION_PREFIX}/${curType}`;
const action = () => ({ type });
action.toString = () => type;
return action;
};

0 comments on commit f083f84

Please sign in to comment.