Skip to content

Commit abf0377

Browse files
committed
Merge pull request #60 from lexich/crud-option
Crud option
2 parents 56268c1 + b0434a7 commit abf0377

11 files changed

+382
-129
lines changed

DOCS.md

+22
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ rest.actions.test.post(1, "admin", {msg: "Hello"}, (err)=> {
295295
rest.actions.test.async();
296296
```
297297

298+
####crud
299+
- @description: autogenerate `helpers` ("get", "post", "put", "delete", "patch") for selected endpoint. Also you can overwrite autogenerate action with `helpers` definitions.
300+
- @type: Boolean
301+
- @default: false
302+
- @example:
303+
```js
304+
{
305+
test: {
306+
url: "/test/:id",
307+
crud: true
308+
}
309+
}
310+
311+
//using
312+
rest.action.test.get({ id: 1})
313+
rest.action.test.post({ id: 1}, { body: "data" }, (err, data)=> {
314+
//code
315+
});
316+
rest.action.test.put({ id: 1}, { body: "data" })
317+
rest.action.test.delete({ id: 1 });
318+
```
319+
298320
### reduxApi object
299321

300322
####use(key, value)

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.8.10",
3+
"version": "0.9.0",
44
"main": "dist/redux-api.min.js",
55
"dependencies": {}
66
}

dist/redux-api.js

+48-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js

+100-100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.8.10",
3+
"version": "0.9.0",
44
"author": {
55
"name": "Efremov Alex",
66
"email": "lexich121@gmail.com",

src/actionFn.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ function extractArgs(args) {
2727
return [pathvars, params, callback];
2828
}
2929

30+
function helperCrudFunction(name) {
31+
return (...args)=> {
32+
const [pathvars, params, cb] = extractArgs(args);
33+
return [pathvars, { ...params, method: name }, cb];
34+
};
35+
}
36+
37+
export const CRUD = reduce(["get", "post", "put", "delete", "patch"],
38+
(memo, name)=> {
39+
memo[name] = helperCrudFunction(name);
40+
return memo;
41+
}, {});
42+
3043
/**
3144
* Constructor for create action
3245
* @param {String} url endpoint's url
@@ -151,7 +164,12 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
151164
};
152165
};
153166

154-
return reduce(meta.helpers, (memo, func, helpername)=> {
167+
let helpers = meta.helpers || [];
168+
if (meta.crud) {
169+
helpers = { ...CRUD, ...helpers };
170+
}
171+
172+
return reduce(helpers, (memo, func, helpername)=> {
155173
if (memo[helpername]) {
156174
throw new Error(
157175
`Helper name: "${helpername}" for endpoint "${name}" has been already reserved`

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export default function reduxApi(config) {
9797
}
9898

9999
const {
100-
url, options, transformer, broadcast,
101-
reducerName, prefetch, postfetch, validation, helpers
100+
url, options, transformer, broadcast, crud,
101+
reducerName, prefetch, postfetch, validation, helpers,
102102
} = opts;
103103

104104
const ACTIONS = {
@@ -117,7 +117,7 @@ export default function reduxApi(config) {
117117
virtual: !!opts.virtual,
118118
actions: memo.actions,
119119
prefetch, postfetch, validation,
120-
helpers, transformer
120+
helpers, transformer, crud
121121
};
122122

123123
memo.actions[key] = actionFn(url, key, options, ACTIONS, meta);

0 commit comments

Comments
 (0)