Skip to content

Commit be48df5

Browse files
committed
Add action creator for RSAA action
This gives cleaner boundary for RSAA actions, gives clean API and allows to add validation to action creation process. And this allows for nicer and cleaner typings for Typescript.
1 parent 6e782cd commit be48df5

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import RSAA from './RSAA';
3333
import { isRSAA, validateRSAA, isValidRSAA } from './validation';
3434
import { InvalidRSAA, InternalError, RequestError, ApiError } from './errors';
35-
import { getJSON } from './util';
35+
import { createAction, getJSON } from './util';
3636
import { apiMiddleware, createMiddleware } from './middleware';
3737

3838
export {
@@ -45,6 +45,7 @@ export {
4545
RequestError,
4646
ApiError,
4747
getJSON,
48+
createAction,
4849
createMiddleware,
4950
apiMiddleware
5051
};

src/util.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InternalError, ApiError } from './errors';
2+
import RSAA from './RSAA';
23

34
/**
45
* Extract JSON body from a server response
@@ -93,4 +94,16 @@ async function actionWith(descriptor, args = []) {
9394
return descriptor;
9495
}
9596

96-
export { getJSON, normalizeTypeDescriptors, actionWith };
97+
/**
98+
* Create RSAA action
99+
*
100+
* @function createAction
101+
* @access public
102+
* @param {object} clientCall - The options for the RSAA action
103+
* @returns {object} RSAA Action
104+
*/
105+
function createAction(clientCall) {
106+
return { [RSAA]: clientCall };
107+
}
108+
109+
export { getJSON, normalizeTypeDescriptors, actionWith, createAction };

src/util.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { RSAA, getJSON } from './index.js';
33

44
// Private package import
5-
import { normalizeTypeDescriptors, actionWith } from './util';
5+
import { normalizeTypeDescriptors, actionWith, createAction } from './util';
66

77
describe('#normalizeTypeDescriptors', () => {
88
it('handles string types', () => {
@@ -167,3 +167,10 @@ describe('#getJSON', () => {
167167
expect(result).toBeUndefined();
168168
});
169169
});
170+
171+
describe('#createAction', () => {
172+
it('returns valid RSAA action', () => {
173+
const apiCall = {};
174+
expect(createAction(apiCall)).toHaveProperty(RSAA, apiCall);
175+
});
176+
});

0 commit comments

Comments
 (0)