Skip to content

Commit c9afbc8

Browse files
committed
gen lib
1 parent 7d5e860 commit c9afbc8

File tree

4 files changed

+131
-65
lines changed

4 files changed

+131
-65
lines changed

lib/__tests__/createAPIAction-test.js

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,6 @@ describe('createAPIAction()', function () {
125125
});
126126
});
127127

128-
it('test GET by ID endpoint', function () {
129-
var getItems = (0, _.createAPIAction)(type, 'GET', '/sample');
130-
expect(getItems(9)).to.deep.equal({
131-
type: type,
132-
payload: {},
133-
meta: {
134-
api: true,
135-
method: 'GET',
136-
endpoint: '/sample/9',
137-
types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')]
138-
}
139-
});
140-
});
141-
142128
it('test POST Data endpoint', function () {
143129
var createItems = (0, _.createAPIAction)(type, 'POST', '/sample');
144130
var postData = { name: 'james' };
@@ -185,4 +171,97 @@ describe('createAPIAction()', function () {
185171
});
186172
});
187173
});
174+
175+
describe('Testing Custom Endpoints', function () {
176+
var type = 'TYPE';
177+
it('test GET with Custom Endpoint', function () {
178+
var customEndpoint = function customEndpoint() {
179+
return '/tester/mctesterson';
180+
};
181+
var getItems = (0, _.createAPIAction)(type, 'GET', customEndpoint);
182+
expect(getItems()).to.deep.equal({
183+
type: type,
184+
payload: {},
185+
meta: {
186+
api: true,
187+
method: 'GET',
188+
endpoint: '/tester/mctesterson',
189+
types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')]
190+
}
191+
});
192+
});
193+
194+
it('test Get by ID with Custom Endpoint', function () {
195+
var customEndpoint = function customEndpoint(p) {
196+
return '/tester/' + p + '/mctesterson';
197+
};
198+
var getItems = (0, _.createAPIAction)(type, 'GET', customEndpoint);
199+
expect(getItems(10)).to.deep.equal({
200+
type: type,
201+
payload: 10,
202+
meta: {
203+
api: true,
204+
method: 'GET',
205+
endpoint: '/tester/10/mctesterson',
206+
types: [type.concat('_GET_REQUEST'), type.concat('_GET_SUCCESS'), type.concat('_GET_FAILURE')]
207+
}
208+
});
209+
});
210+
211+
it('test POST with Custom Endpoint', function () {
212+
var customEndpoint = function customEndpoint(params) {
213+
return '/user/' + params.id + '/ronald/' + params.name;
214+
};
215+
var createItem = (0, _.createAPIAction)(type, 'POST', customEndpoint);
216+
var payload = { id: 10, name: 'james' };
217+
expect(createItem(payload)).to.deep.equal({
218+
type: type,
219+
payload: payload,
220+
meta: {
221+
api: true,
222+
method: 'POST',
223+
endpoint: '/user/10/ronald/james',
224+
types: [type.concat('_POST_REQUEST'), type.concat('_POST_SUCCESS'), type.concat('_POST_FAILURE')]
225+
}
226+
});
227+
});
228+
229+
it('test PUT with Custom Endpoint', function () {
230+
var customEndpoint = function customEndpoint(params) {
231+
return '/user/' + params.id;
232+
};
233+
var updateItem = (0, _.createAPIAction)(type, 'PUT', customEndpoint);
234+
var payload = { id: 10, name: 'james' };
235+
expect(updateItem(payload)).to.deep.equal({
236+
type: type,
237+
payload: payload,
238+
meta: {
239+
api: true,
240+
method: 'PUT',
241+
endpoint: '/user/10',
242+
types: [type.concat('_PUT_REQUEST'), type.concat('_PUT_SUCCESS'), type.concat('_PUT_FAILURE')]
243+
}
244+
});
245+
});
246+
247+
it('test DELETE with Custom Endpoint', function () {
248+
var customEndpoint = function customEndpoint(_ref2) {
249+
var id = _ref2.id;
250+
var accountID = _ref2.accountID;
251+
return '/user/' + id + '/account/' + accountID;
252+
};
253+
var deleteItem = (0, _.createAPIAction)(type, 'DELETE', customEndpoint);
254+
var payload = { id: 10, accountID: 25 };
255+
expect(deleteItem(payload)).to.deep.equal({
256+
type: type,
257+
payload: payload,
258+
meta: {
259+
api: true,
260+
method: 'DELETE',
261+
endpoint: '/user/10/account/25',
262+
types: [type.concat('_DELETE_REQUEST'), type.concat('_DELETE_SUCCESS'), type.concat('_DELETE_FAILURE')]
263+
}
264+
});
265+
});
266+
});
188267
});

lib/createAPIAction.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.default = createAPIAction;
77

8+
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
9+
810
/**
911
* Returns the endpoint based on HTTP Verb
1012
* Contains a specific case for GET BY ID because
1113
* the verb is the same as a GET ALL
1214
**/
13-
function getEndpoint(method, endpoint, itemID) {
15+
function getEndpoint(method, endpoint, params) {
16+
var _params = _toArray(params);
17+
18+
var firstParam = _params[0];
19+
20+
var others = _params.slice(1);
1421

15-
if (method === 'GET' && typeof itemID === 'number') {
16-
return endpoint + '/' + itemID;
22+
if (typeof endpoint === 'function') {
23+
return endpoint(firstParam);
1724
}
1825

1926
switch (method) {
2027
case 'DELETE':
2128
case 'PUT':
22-
return endpoint + '/' + itemID;
29+
return endpoint + '/' + firstParam;
2330
default:
2431
return endpoint;
2532
}
@@ -33,38 +40,52 @@ function getEndpoint(method, endpoint, itemID) {
3340
*
3441
* In an error case, we just return the error.
3542
**/
36-
function getPayload(method, itemIDorPayload, data) {
37-
if (itemIDorPayload instanceof Error) {
38-
return itemIDorPayload;
43+
function getPayload(method, endpoint, params) {
44+
var _params2 = _toArray(params);
45+
46+
var firstParam = _params2[0];
47+
48+
var others = _params2.slice(1);
49+
50+
if (firstParam instanceof Error) {
51+
return firstParam;
52+
}
53+
54+
if (typeof endpoint === 'function') {
55+
return firstParam || {};
3956
}
4057

4158
switch (method) {
4259
case 'POST':
43-
return itemIDorPayload;
60+
return firstParam;
4461
case 'PUT':
45-
return data;
62+
return others[0];
4663
default:
4764
return {};
4865
}
4966
}
5067

5168
function createAPIAction(type, method, endpoint, actionCreator, metaCreator) {
52-
return function (param1) {
53-
for (var _len = arguments.length, params = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
54-
params[_key - 1] = arguments[_key];
69+
return function () {
70+
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
71+
params[_key] = arguments[_key];
5572
}
5673

57-
var finalEndpoint = getEndpoint(method, endpoint, param1);
74+
var firstParam = params[0];
75+
var others = params.slice(1);
76+
77+
78+
var finalEndpoint = getEndpoint(method, endpoint, params);
5879

5980
var action = {
6081
type: type,
61-
payload: getPayload.apply(undefined, [method, param1].concat(params))
82+
payload: getPayload(method, endpoint, params)
6283
};
6384

6485
if (action.payload instanceof Error) {
6586
// Handle FSA errors where the payload is an Error object. Set error.
6687
action.error = true;
67-
action.payload = param1;
88+
action.payload = firstParam;
6889
}
6990

7091
if (typeof metaCreator === 'function') {

lib/index.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,12 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.handleActions = exports.handleAction = exports.createAPIAction = exports.createAction = undefined;
7-
8-
var _createAction = require('./createAction');
9-
10-
var _createAction2 = _interopRequireDefault(_createAction);
6+
exports.createAPIAction = undefined;
117

128
var _createAPIAction = require('./createAPIAction');
139

1410
var _createAPIAction2 = _interopRequireDefault(_createAPIAction);
1511

16-
var _handleAction = require('./handleAction');
17-
18-
var _handleAction2 = _interopRequireDefault(_handleAction);
19-
20-
var _handleActions = require('./handleActions');
21-
22-
var _handleActions2 = _interopRequireDefault(_handleActions);
23-
2412
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2513

26-
exports.createAction = _createAction2.default;
27-
exports.createAPIAction = _createAPIAction2.default;
28-
exports.handleAction = _handleAction2.default;
29-
exports.handleActions = _handleActions2.default;
14+
exports.createAPIAction = _createAPIAction2.default;

lib/ownKeys.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)