Skip to content

Commit e49cc64

Browse files
committed
custom Endpoint #1
1 parent a97bf1a commit e49cc64

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/__tests__/createAPIAction-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,28 @@ describe('createAPIAction()', () => {
212212
});
213213

214214
});
215+
216+
describe('Testing Custom Endpoints', () => {
217+
const type = 'TYPE';
218+
it('test GET with Custom Endpoint', () => {
219+
const customEndpoint = () => {
220+
return '/tester/mctesterson';
221+
};
222+
const getItems = createAPIAction(type, 'GET', customEndpoint);
223+
expect(getItems()).to.deep.equal({
224+
type,
225+
payload: {},
226+
meta: {
227+
api: true,
228+
method: 'GET',
229+
endpoint: '/tester/mctesterson',
230+
types: [
231+
type.concat('_GET_REQUEST'),
232+
type.concat('_GET_SUCCESS'),
233+
type.concat('_GET_FAILURE')
234+
]
235+
}
236+
});
237+
});
238+
});
215239
});

src/createAPIAction.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
* Contains a specific case for GET BY ID because
55
* the verb is the same as a GET ALL
66
**/
7-
function getEndpoint(method, endpoint, itemID) {
7+
function getEndpoint(method, endpoint, params) {
88

9-
if (method === 'GET' && typeof itemID === 'number') {
10-
return `${endpoint}/${itemID}`;
9+
const [firstParam, ...others] = params;
10+
11+
if (typeof endpoint === 'function') {
12+
return endpoint(firstParam);
13+
}
14+
15+
if (method === 'GET' && typeof firstParam === 'number') {
16+
return `${endpoint}/${firstParam}`;
1117
}
1218

1319
switch (method) {
1420
case 'DELETE':
1521
case 'PUT':
16-
return `${endpoint}/${itemID}`;
22+
return `${endpoint}/${firstParam}`;
1723
default:
1824
return endpoint;
1925
}
@@ -36,7 +42,6 @@ function getPayload(method, itemIDorPayload, data) {
3642
case 'POST':
3743
return itemIDorPayload;
3844
case 'PUT':
39-
console.log('got a put', itemIDorPayload, data);
4045
return data;
4146
default:
4247
return {};
@@ -48,7 +53,7 @@ export default function createAPIAction(type, method, endpoint, actionCreator, m
4853

4954
const [firstParam, ...others] = params;
5055

51-
const finalEndpoint = getEndpoint(method, endpoint, firstParam);
56+
const finalEndpoint = getEndpoint(method, endpoint, params);
5257

5358
const action = {
5459
type,

0 commit comments

Comments
 (0)