Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow per-resources defaults to be passed in via second arg #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _tahoe = require('tahoe');

var _pluralize = require('pluralize');
Expand All @@ -20,27 +24,28 @@ var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var resourceToActions = function resourceToActions(resourceName, resource) {
var resourceToActions = function resourceToActions(resourceName, resource, defaults) {
var model = new _normalizr.Schema(resourceName);
// model.define(mapValues(resource.model.relationships, relationshipMap))

return (0, _lodash2.default)(resource.endpoints, function (prev, _endpoint) {
prev[_endpoint.name] = (0, _tahoe.createAction)({
prev[_endpoint.name] = (0, _tahoe.createAction)((0, _extends3.default)({
endpoint: function endpoint(opt) {
return (0, _templateUrl2.default)(_endpoint.path, opt);
},
method: _endpoint.method,
model: model,
collection: !_endpoint.instance,
credentials: 'include'
});
}, defaults));
return prev;
}, {});
};

exports.default = function (resources) {
var defaults = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return (0, _lodash2.default)(resources.toJS ? resources.toJS() : resources, function (prev, v, k) {
prev[(0, _pluralize.plural)(k)] = resourceToActions(k, v);
prev[(0, _pluralize.plural)(k)] = resourceToActions(k, v, defaults);
return prev;
}, {});
};
Expand Down
19 changes: 11 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import { Schema } from 'normalizr'
import template from 'template-url'
import reduce from 'lodash.reduce'

const resourceToActions = (resourceName, resource) => {
const resourceToActions = (resourceName, resource, defaults) => {
let model = new Schema(resourceName)
// model.define(mapValues(resource.model.relationships, relationshipMap))

return reduce(resource.endpoints, (prev, endpoint) => {
prev[endpoint.name] = createAction({
endpoint: (opt) => template(endpoint.path, opt),
method: endpoint.method,
model: model,
collection: !endpoint.instance,
credentials: 'include'
...{
endpoint: (opt) => template(endpoint.path, opt),
method: endpoint.method,
model: model,
collection: !endpoint.instance,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

createAction({
  ...defaults,
  someProp: 123,
  anotherProp: {},
  someStuff: 456
})

You spread into existing objects, ordering is wherever you put it in the object so in this case defaults at the beginning

credentials: 'include'
},
...defaults
})
return prev
}, {})
}

export default (resources) =>
export default (resources, defaults = {}) =>
reduce((resources.toJS ? resources.toJS() : resources), (prev, v, k) => {
prev[plural(k)] = resourceToActions(k, v)
prev[plural(k)] = resourceToActions(k, v, defaults)
return prev
}, {})