Skip to content

Commit 3cd1d8c

Browse files
committed
POST custom endpoints
1 parent 4e85471 commit 3cd1d8c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/__tests__/createAPIAction-test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('createAPIAction()', () => {
226226
const getItems = createAPIAction(type, 'GET', customEndpoint);
227227
expect(getItems(10)).to.deep.equal({
228228
type,
229-
payload: {},
229+
payload: 10,
230230
meta: {
231231
api: true,
232232
method: 'GET',
@@ -262,5 +262,27 @@ describe('createAPIAction()', () => {
262262
});
263263
});
264264

265+
it('test PUT with Custom Endpoint', () => {
266+
const customEndpoint = (params) => {
267+
return `/user/${params.id}`;
268+
};
269+
const updateItem = createAPIAction(type, 'PUT', customEndpoint);
270+
const payload = { id: 10, name: 'james' };
271+
expect(updateItem(payload)).to.deep.equal({
272+
type,
273+
payload: payload,
274+
meta: {
275+
api: true,
276+
method: 'PUT',
277+
endpoint: '/user/10',
278+
types: [
279+
type.concat('_PUT_REQUEST'),
280+
type.concat('_PUT_SUCCESS'),
281+
type.concat('_PUT_FAILURE')
282+
]
283+
}
284+
});
285+
});
286+
265287
});
266288
});

src/createAPIAction.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ function getEndpoint(method, endpoint, params) {
2929
*
3030
* In an error case, we just return the error.
3131
**/
32-
function getPayload(method, params) {
32+
function getPayload(method, endpoint, params) {
3333

3434
const [firstParam, ...others] = params;
3535

3636
if (firstParam instanceof Error) {
3737
return firstParam;
3838
}
3939

40+
if (typeof endpoint === 'function') {
41+
return firstParam || {};
42+
}
43+
4044
switch (method) {
4145
case 'POST':
4246
return firstParam;
@@ -56,7 +60,7 @@ export default function createAPIAction(type, method, endpoint, actionCreator, m
5660

5761
const action = {
5862
type,
59-
payload: getPayload(method, params)
63+
payload: getPayload(method, endpoint, params)
6064
};
6165

6266
if (action.payload instanceof Error) {

0 commit comments

Comments
 (0)