Skip to content

Commit

Permalink
Merge pull request #60 from lexich/crud-option
Browse files Browse the repository at this point in the history
Crud option
  • Loading branch information
lexich committed Mar 17, 2016
2 parents 56268c1 + b0434a7 commit abf0377
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 129 deletions.
22 changes: 22 additions & 0 deletions DOCS.md
Expand Up @@ -295,6 +295,28 @@ rest.actions.test.post(1, "admin", {msg: "Hello"}, (err)=> {
rest.actions.test.async();
```

####crud
- @description: autogenerate `helpers` ("get", "post", "put", "delete", "patch") for selected endpoint. Also you can overwrite autogenerate action with `helpers` definitions.
- @type: Boolean
- @default: false
- @example:
```js
{
test: {
url: "/test/:id",
crud: true
}
}

//using
rest.action.test.get({ id: 1})
rest.action.test.post({ id: 1}, { body: "data" }, (err, data)=> {
//code
});
rest.action.test.put({ id: 1}, { body: "data" })
rest.action.test.delete({ id: 1 });
```

### reduxApi object

####use(key, value)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.8.10",
"version": "0.9.0",
"main": "dist/redux-api.min.js",
"dependencies": {}
}
67 changes: 48 additions & 19 deletions dist/redux-api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/redux-api.js.map

Large diffs are not rendered by default.

200 changes: 100 additions & 100 deletions dist/redux-api.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/redux-api.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.8.10",
"version": "0.9.0",
"author": {
"name": "Efremov Alex",
"email": "lexich121@gmail.com",
Expand Down
20 changes: 19 additions & 1 deletion src/actionFn.js
Expand Up @@ -27,6 +27,19 @@ function extractArgs(args) {
return [pathvars, params, callback];
}

function helperCrudFunction(name) {
return (...args)=> {
const [pathvars, params, cb] = extractArgs(args);
return [pathvars, { ...params, method: name }, cb];
};
}

export const CRUD = reduce(["get", "post", "put", "delete", "patch"],
(memo, name)=> {
memo[name] = helperCrudFunction(name);
return memo;
}, {});

/**
* Constructor for create action
* @param {String} url endpoint's url
Expand Down Expand Up @@ -151,7 +164,12 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
};
};

return reduce(meta.helpers, (memo, func, helpername)=> {
let helpers = meta.helpers || [];
if (meta.crud) {
helpers = { ...CRUD, ...helpers };
}

return reduce(helpers, (memo, func, helpername)=> {
if (memo[helpername]) {
throw new Error(
`Helper name: "${helpername}" for endpoint "${name}" has been already reserved`
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Expand Up @@ -97,8 +97,8 @@ export default function reduxApi(config) {
}

const {
url, options, transformer, broadcast,
reducerName, prefetch, postfetch, validation, helpers
url, options, transformer, broadcast, crud,
reducerName, prefetch, postfetch, validation, helpers,
} = opts;

const ACTIONS = {
Expand All @@ -117,7 +117,7 @@ export default function reduxApi(config) {
virtual: !!opts.virtual,
actions: memo.actions,
prefetch, postfetch, validation,
helpers, transformer
helpers, transformer, crud
};

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

0 comments on commit abf0377

Please sign in to comment.