Comparing version 0.0.1 to 0.0.2
284
dist/duxy.js
@@ -89,2 +89,4 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _invariant = __webpack_require__(10); | ||
@@ -110,9 +112,9 @@ | ||
var Collection = function (_Runnable) { | ||
_inherits(Collection, _Runnable); | ||
var Namespace = function (_Runnable) { | ||
_inherits(Namespace, _Runnable); | ||
function Collection(parent, name, options, fn) { | ||
_classCallCheck(this, Collection); | ||
function Namespace(parent, name, options, fn) { | ||
_classCallCheck(this, Namespace); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Collection).call(this)); | ||
var _this = _possibleConstructorReturn(this, (Namespace.__proto__ || Object.getPrototypeOf(Namespace)).call(this)); | ||
@@ -124,2 +126,3 @@ (0, _invariant2.default)(parent, '`parent` is required'); | ||
_this.name = name; | ||
_this.finders = []; | ||
@@ -140,3 +143,3 @@ if (typeof fn === 'function') { | ||
if (parent instanceof Collection) { | ||
if (parent instanceof Namespace) { | ||
_this.path = (0, _urlJoin2.default)(parent.path, parent.key(), path); | ||
@@ -151,6 +154,13 @@ } else if (path.indexOf('http') === 0) { | ||
return Collection; | ||
_createClass(Namespace, [{ | ||
key: 'key', | ||
value: function key() { | ||
return ''; | ||
} | ||
}]); | ||
return Namespace; | ||
}(_Runnable3.default); | ||
exports.default = Collection; | ||
exports.default = Namespace; | ||
@@ -203,3 +213,3 @@ /***/ }, | ||
value: function draw() { | ||
var tree = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var tree = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -283,8 +293,6 @@ return this.runnables.reduce(function (tree, runnable) { | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _Namespace2 = __webpack_require__(1); | ||
var _Collection2 = __webpack_require__(1); | ||
var _Namespace3 = _interopRequireDefault(_Namespace2); | ||
var _Collection3 = _interopRequireDefault(_Collection2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -298,26 +306,19 @@ | ||
var Namespace = function (_Collection) { | ||
_inherits(Namespace, _Collection); | ||
var Resource = function (_Namespace) { | ||
_inherits(Resource, _Namespace); | ||
function Namespace(parent, name, options, fn) { | ||
_classCallCheck(this, Namespace); | ||
function Resource(parent, name, options, fn) { | ||
_classCallCheck(this, Resource); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Namespace).call(this, parent, name, options, fn)); | ||
var _this = _possibleConstructorReturn(this, (Resource.__proto__ || Object.getPrototypeOf(Resource)).call(this, parent, name, options, fn)); | ||
_this.finders = []; | ||
_this.finders = [{ name: 'findOne', method: 'GET', path: null }, { name: 'create', method: 'POST', path: null }, { name: 'update', method: 'PUT', path: null }, { name: 'delete', method: 'DELETE', path: null }]; | ||
return _this; | ||
} | ||
_createClass(Namespace, [{ | ||
key: 'key', | ||
value: function key() { | ||
return ''; | ||
} | ||
}]); | ||
return Resource; | ||
}(_Namespace3.default); | ||
return Namespace; | ||
}(_Collection3.default); | ||
exports.default = Resource; | ||
exports.default = Namespace; | ||
/***/ }, | ||
@@ -339,5 +340,5 @@ /* 5 */ | ||
var _Collection2 = __webpack_require__(1); | ||
var _Namespace2 = __webpack_require__(1); | ||
var _Collection3 = _interopRequireDefault(_Collection2); | ||
var _Namespace3 = _interopRequireDefault(_Namespace2); | ||
@@ -352,9 +353,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Resource = function (_Collection) { | ||
_inherits(Resource, _Collection); | ||
var Resources = function (_Namespace) { | ||
_inherits(Resources, _Namespace); | ||
function Resource(parent, name, options, fn) { | ||
_classCallCheck(this, Resource); | ||
function Resources(parent, name, options, fn) { | ||
_classCallCheck(this, Resources); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Resource).call(this, parent, name, options, fn)); | ||
var _this = _possibleConstructorReturn(this, (Resources.__proto__ || Object.getPrototypeOf(Resources)).call(this, parent, name, options, fn)); | ||
@@ -365,3 +366,3 @@ _this.finders = [{ name: 'findOne', method: 'GET', path: '{id}' }, { name: 'findAll', method: 'GET', path: null }, { name: 'create', method: 'POST', path: null }, { name: 'update', method: 'PUT', path: '{id}' }, { name: 'delete', method: 'DELETE', path: '{id}' }]; | ||
_createClass(Resource, [{ | ||
_createClass(Resources, [{ | ||
key: 'key', | ||
@@ -373,6 +374,6 @@ value: function key() { | ||
return Resource; | ||
}(_Collection3.default); | ||
return Resources; | ||
}(_Namespace3.default); | ||
exports.default = Resource; | ||
exports.default = Resources; | ||
@@ -411,3 +412,3 @@ /***/ }, | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Root).call(this)); | ||
var _this = _possibleConstructorReturn(this, (Root.__proto__ || Object.getPrototypeOf(Root)).call(this)); | ||
@@ -430,3 +431,3 @@ _this.dsl = dsl; | ||
var tree = this.create(); | ||
return _get(Object.getPrototypeOf(Root.prototype), 'draw', this).call(this, tree); | ||
return _get(Root.prototype.__proto__ || Object.getPrototypeOf(Root.prototype), 'draw', this).call(this, tree); | ||
} | ||
@@ -452,8 +453,12 @@ }]); | ||
var _Namespace = __webpack_require__(4); | ||
var _Namespace = __webpack_require__(1); | ||
var _Namespace2 = _interopRequireDefault(_Namespace); | ||
var _Resource = __webpack_require__(5); | ||
var _Resources = __webpack_require__(5); | ||
var _Resources2 = _interopRequireDefault(_Resources); | ||
var _Resource = __webpack_require__(4); | ||
var _Resource2 = _interopRequireDefault(_Resource); | ||
@@ -469,33 +474,34 @@ | ||
var Duxy = function () { | ||
function Duxy() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var Runner = function () { | ||
function Runner() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, Duxy); | ||
_classCallCheck(this, Runner); | ||
this.dsl = { | ||
resources: this.resources.bind(this), | ||
resource: this.resource.bind(this), | ||
namespace: this.namespace.bind(this), | ||
get: this.get.bind(this), | ||
post: this.post.bind(this), | ||
put: this.put.bind(this), | ||
patch: this.patch.bind(this), | ||
del: this.del.bind(this), | ||
map: this.map.bind(this) | ||
}; | ||
this.options = options; | ||
this.root = new _Root2.default(this, options.http); | ||
this.root = new _Root2.default(this.dsl, options.http); | ||
this.stack = [this.root]; | ||
// NOTE(vesln): DSL methods | ||
this.resource = this.resource.bind(this); | ||
this.namespace = this.namespace.bind(this); | ||
this.get = this.get.bind(this); | ||
this.post = this.post.bind(this); | ||
this.put = this.put.bind(this); | ||
this.patch = this.patch.bind(this); | ||
this.del = this.del.bind(this); | ||
this.map = this.map.bind(this); | ||
} | ||
_createClass(Duxy, [{ | ||
_createClass(Runner, [{ | ||
key: 'resources', | ||
value: function resources(name, options, fn) { | ||
this.addRunnable(new _Resources2.default(this.current, name, options, fn)); | ||
} | ||
}, { | ||
key: 'resource', | ||
value: function resource(name, options, fn) { | ||
var resource = new _Resource2.default(this.current, name, options, fn); | ||
this.current.runnables.push(resource); | ||
this.stack.push(resource); | ||
resource.run(); | ||
this.stack.pop(); | ||
this.addRunnable(new _Resource2.default(this.current, name, options, fn)); | ||
} | ||
@@ -505,33 +511,28 @@ }, { | ||
value: function namespace(name, options, fn) { | ||
var namespace = new _Namespace2.default(this.current, name, options, fn); | ||
this.current.runnables.push(namespace); | ||
this.stack.push(namespace); | ||
namespace.run(); | ||
this.stack.pop(); | ||
this.addRunnable(new _Namespace2.default(this.current, name, options, fn)); | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get(name) { | ||
this.member(name, 'GET'); | ||
value: function get(name, options) { | ||
this.member(name, options, 'GET'); | ||
} | ||
}, { | ||
key: 'post', | ||
value: function post(name) { | ||
this.member(name, 'POST'); | ||
value: function post(name, options) { | ||
this.member(name, options, 'POST'); | ||
} | ||
}, { | ||
key: 'put', | ||
value: function put(name) { | ||
this.member(name, 'PUT'); | ||
value: function put(name, options) { | ||
this.member(name, options, 'PUT'); | ||
} | ||
}, { | ||
key: 'patch', | ||
value: function patch(name) { | ||
this.member(name, 'PATCH'); | ||
value: function patch(name, options) { | ||
this.member(name, options, 'PATCH'); | ||
} | ||
}, { | ||
key: 'del', | ||
value: function del(name) { | ||
this.member(name, 'DELETE'); | ||
value: function del(name, options) { | ||
this.member(name, options, 'DELETE'); | ||
} | ||
@@ -551,6 +552,16 @@ }, { | ||
key: 'member', | ||
value: function member(name, method) { | ||
this.current.members.push({ name: name, path: name, method: method, force: true }); | ||
value: function member(name, options, method) { | ||
var path = options && options.path || name; | ||
this.current.members.push({ name: name, path: path, method: method, force: true }); | ||
} | ||
}, { | ||
key: 'addRunnable', | ||
value: function addRunnable(collection) { | ||
this.current.runnables.push(collection); | ||
this.stack.push(collection); | ||
collection.run(); | ||
this.stack.pop(); | ||
} | ||
}, { | ||
key: 'current', | ||
@@ -562,6 +573,6 @@ get: function get() { | ||
return Duxy; | ||
return Runner; | ||
}(); | ||
exports.default = Duxy; | ||
exports.default = Runner; | ||
@@ -603,3 +614,3 @@ /***/ }, | ||
// NOTE(vesln): check if this endpoint is allowed | ||
if (!force && allowed && ! ~allowed.indexOf(name)) { | ||
if (!force && allowed && !~allowed.indexOf(name)) { | ||
throw new Error('`' + name + '()` is not allowed on this resource'); | ||
@@ -1783,4 +1794,90 @@ } | ||
// shim for using process in browser | ||
var process = module.exports = {}; | ||
var process = module.exports = {}; | ||
// cached from whatever global is present so that test runners that stub it | ||
// don't break things. But we need to wrap it in a try catch in case it is | ||
// wrapped in strict mode code which doesn't define any globals. It's inside a | ||
// function because try/catches deoptimize in certain engines. | ||
var cachedSetTimeout; | ||
var cachedClearTimeout; | ||
function defaultSetTimout() { | ||
throw new Error('setTimeout has not been defined'); | ||
} | ||
function defaultClearTimeout () { | ||
throw new Error('clearTimeout has not been defined'); | ||
} | ||
(function () { | ||
try { | ||
if (typeof setTimeout === 'function') { | ||
cachedSetTimeout = setTimeout; | ||
} else { | ||
cachedSetTimeout = defaultSetTimout; | ||
} | ||
} catch (e) { | ||
cachedSetTimeout = defaultSetTimout; | ||
} | ||
try { | ||
if (typeof clearTimeout === 'function') { | ||
cachedClearTimeout = clearTimeout; | ||
} else { | ||
cachedClearTimeout = defaultClearTimeout; | ||
} | ||
} catch (e) { | ||
cachedClearTimeout = defaultClearTimeout; | ||
} | ||
} ()) | ||
function runTimeout(fun) { | ||
if (cachedSetTimeout === setTimeout) { | ||
//normal enviroments in sane situations | ||
return setTimeout(fun, 0); | ||
} | ||
// if setTimeout wasn't available but was latter defined | ||
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { | ||
cachedSetTimeout = setTimeout; | ||
return setTimeout(fun, 0); | ||
} | ||
try { | ||
// when when somebody has screwed with setTimeout but no I.E. maddness | ||
return cachedSetTimeout(fun, 0); | ||
} catch(e){ | ||
try { | ||
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | ||
return cachedSetTimeout.call(null, fun, 0); | ||
} catch(e){ | ||
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error | ||
return cachedSetTimeout.call(this, fun, 0); | ||
} | ||
} | ||
} | ||
function runClearTimeout(marker) { | ||
if (cachedClearTimeout === clearTimeout) { | ||
//normal enviroments in sane situations | ||
return clearTimeout(marker); | ||
} | ||
// if clearTimeout wasn't available but was latter defined | ||
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { | ||
cachedClearTimeout = clearTimeout; | ||
return clearTimeout(marker); | ||
} | ||
try { | ||
// when when somebody has screwed with setTimeout but no I.E. maddness | ||
return cachedClearTimeout(marker); | ||
} catch (e){ | ||
try { | ||
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | ||
return cachedClearTimeout.call(null, marker); | ||
} catch (e){ | ||
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. | ||
// Some versions of I.E. have different rules for clearTimeout vs setTimeout | ||
return cachedClearTimeout.call(this, marker); | ||
} | ||
} | ||
} | ||
var queue = []; | ||
@@ -1792,2 +1889,5 @@ var draining = false; | ||
function cleanUpNextTick() { | ||
if (!draining || !currentQueue) { | ||
return; | ||
} | ||
draining = false; | ||
@@ -1808,3 +1908,3 @@ if (currentQueue.length) { | ||
} | ||
var timeout = setTimeout(cleanUpNextTick); | ||
var timeout = runTimeout(cleanUpNextTick); | ||
draining = true; | ||
@@ -1826,3 +1926,3 @@ | ||
draining = false; | ||
clearTimeout(timeout); | ||
runClearTimeout(timeout); | ||
} | ||
@@ -1839,3 +1939,3 @@ | ||
if (queue.length === 1 && !draining) { | ||
setTimeout(drainQueue, 0); | ||
runTimeout(drainQueue); | ||
} | ||
@@ -1842,0 +1942,0 @@ }; |
@@ -32,3 +32,3 @@ 'use strict'; | ||
// NOTE(vesln): check if this endpoint is allowed | ||
if (!force && allowed && ! ~allowed.indexOf(name)) { | ||
if (!force && allowed && !~allowed.indexOf(name)) { | ||
throw new Error('`' + name + '()` is not allowed on this resource'); | ||
@@ -35,0 +35,0 @@ } |
@@ -9,6 +9,14 @@ 'use strict'; | ||
var _Collection2 = require('./Collection'); | ||
var _invariant = require('invariant'); | ||
var _Collection3 = _interopRequireDefault(_Collection2); | ||
var _invariant2 = _interopRequireDefault(_invariant); | ||
var _urlJoin = require('url-join'); | ||
var _urlJoin2 = _interopRequireDefault(_urlJoin); | ||
var _Runnable2 = require('./Runnable'); | ||
var _Runnable3 = _interopRequireDefault(_Runnable2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -22,4 +30,4 @@ | ||
var Namespace = function (_Collection) { | ||
_inherits(Namespace, _Collection); | ||
var Namespace = function (_Runnable) { | ||
_inherits(Namespace, _Runnable); | ||
@@ -29,5 +37,32 @@ function Namespace(parent, name, options, fn) { | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Namespace).call(this, parent, name, options, fn)); | ||
var _this = _possibleConstructorReturn(this, (Namespace.__proto__ || Object.getPrototypeOf(Namespace)).call(this)); | ||
(0, _invariant2.default)(parent, '`parent` is required'); | ||
(0, _invariant2.default)(name, 'You must specify a name for the given resource or namespace'); | ||
_this.parent = parent; | ||
_this.name = name; | ||
_this.finders = []; | ||
if (typeof fn === 'function') { | ||
_this.fn = fn; | ||
_this.options = options; | ||
} else if (typeof options === 'function') { | ||
_this.options = {}; | ||
_this.fn = options; | ||
} else if (!options) { | ||
_this.options = {}; | ||
} else { | ||
_this.options = options; | ||
} | ||
var path = _this.options.path || _this.name; | ||
if (parent instanceof Namespace) { | ||
_this.path = (0, _urlJoin2.default)(parent.path, parent.key(), path); | ||
} else if (path.indexOf('http') === 0) { | ||
_this.path = path; | ||
} else { | ||
_this.path = (0, _urlJoin2.default)(parent.path, path); | ||
} | ||
return _this; | ||
@@ -44,4 +79,4 @@ } | ||
return Namespace; | ||
}(_Collection3.default); | ||
}(_Runnable3.default); | ||
exports.default = Namespace; |
@@ -7,12 +7,6 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _Namespace2 = require('./Namespace'); | ||
var _inflection = require('inflection'); | ||
var _Namespace3 = _interopRequireDefault(_Namespace2); | ||
var _inflection2 = _interopRequireDefault(_inflection); | ||
var _Collection2 = require('./Collection'); | ||
var _Collection3 = _interopRequireDefault(_Collection2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -26,4 +20,4 @@ | ||
var Resource = function (_Collection) { | ||
_inherits(Resource, _Collection); | ||
var Resource = function (_Namespace) { | ||
_inherits(Resource, _Namespace); | ||
@@ -33,18 +27,11 @@ function Resource(parent, name, options, fn) { | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Resource).call(this, parent, name, options, fn)); | ||
var _this = _possibleConstructorReturn(this, (Resource.__proto__ || Object.getPrototypeOf(Resource)).call(this, parent, name, options, fn)); | ||
_this.finders = [{ name: 'findOne', method: 'GET', path: '{id}' }, { name: 'findAll', method: 'GET', path: null }, { name: 'create', method: 'POST', path: null }, { name: 'update', method: 'PUT', path: '{id}' }, { name: 'delete', method: 'DELETE', path: '{id}' }]; | ||
_this.finders = [{ name: 'findOne', method: 'GET', path: null }, { name: 'create', method: 'POST', path: null }, { name: 'update', method: 'PUT', path: null }, { name: 'delete', method: 'DELETE', path: null }]; | ||
return _this; | ||
} | ||
_createClass(Resource, [{ | ||
key: 'key', | ||
value: function key() { | ||
return '{' + (this.options.key || _inflection2.default.singularize(this.name) + 'Id') + '}'; | ||
} | ||
}]); | ||
return Resource; | ||
}(_Collection3.default); | ||
}(_Namespace3.default); | ||
exports.default = Resource; |
@@ -29,3 +29,3 @@ 'use strict'; | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Root).call(this)); | ||
var _this = _possibleConstructorReturn(this, (Root.__proto__ || Object.getPrototypeOf(Root)).call(this)); | ||
@@ -48,3 +48,3 @@ _this.dsl = dsl; | ||
var tree = this.create(); | ||
return _get(Object.getPrototypeOf(Root.prototype), 'draw', this).call(this, tree); | ||
return _get(Root.prototype.__proto__ || Object.getPrototypeOf(Root.prototype), 'draw', this).call(this, tree); | ||
} | ||
@@ -51,0 +51,0 @@ }]); |
@@ -43,3 +43,3 @@ 'use strict'; | ||
value: function draw() { | ||
var tree = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var tree = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -46,0 +46,0 @@ return this.runnables.reduce(function (tree, runnable) { |
@@ -13,2 +13,6 @@ 'use strict'; | ||
var _Resources = require('./Resources'); | ||
var _Resources2 = _interopRequireDefault(_Resources); | ||
var _Resource = require('./Resource'); | ||
@@ -26,33 +30,34 @@ | ||
var Duxy = function () { | ||
function Duxy() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var Runner = function () { | ||
function Runner() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, Duxy); | ||
_classCallCheck(this, Runner); | ||
this.dsl = { | ||
resources: this.resources.bind(this), | ||
resource: this.resource.bind(this), | ||
namespace: this.namespace.bind(this), | ||
get: this.get.bind(this), | ||
post: this.post.bind(this), | ||
put: this.put.bind(this), | ||
patch: this.patch.bind(this), | ||
del: this.del.bind(this), | ||
map: this.map.bind(this) | ||
}; | ||
this.options = options; | ||
this.root = new _Root2.default(this, options.http); | ||
this.root = new _Root2.default(this.dsl, options.http); | ||
this.stack = [this.root]; | ||
// NOTE(vesln): DSL methods | ||
this.resource = this.resource.bind(this); | ||
this.namespace = this.namespace.bind(this); | ||
this.get = this.get.bind(this); | ||
this.post = this.post.bind(this); | ||
this.put = this.put.bind(this); | ||
this.patch = this.patch.bind(this); | ||
this.del = this.del.bind(this); | ||
this.map = this.map.bind(this); | ||
} | ||
_createClass(Duxy, [{ | ||
_createClass(Runner, [{ | ||
key: 'resources', | ||
value: function resources(name, options, fn) { | ||
this.addRunnable(new _Resources2.default(this.current, name, options, fn)); | ||
} | ||
}, { | ||
key: 'resource', | ||
value: function resource(name, options, fn) { | ||
var resource = new _Resource2.default(this.current, name, options, fn); | ||
this.current.runnables.push(resource); | ||
this.stack.push(resource); | ||
resource.run(); | ||
this.stack.pop(); | ||
this.addRunnable(new _Resource2.default(this.current, name, options, fn)); | ||
} | ||
@@ -62,33 +67,28 @@ }, { | ||
value: function namespace(name, options, fn) { | ||
var namespace = new _Namespace2.default(this.current, name, options, fn); | ||
this.current.runnables.push(namespace); | ||
this.stack.push(namespace); | ||
namespace.run(); | ||
this.stack.pop(); | ||
this.addRunnable(new _Namespace2.default(this.current, name, options, fn)); | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get(name) { | ||
this.member(name, 'GET'); | ||
value: function get(name, options) { | ||
this.member(name, options, 'GET'); | ||
} | ||
}, { | ||
key: 'post', | ||
value: function post(name) { | ||
this.member(name, 'POST'); | ||
value: function post(name, options) { | ||
this.member(name, options, 'POST'); | ||
} | ||
}, { | ||
key: 'put', | ||
value: function put(name) { | ||
this.member(name, 'PUT'); | ||
value: function put(name, options) { | ||
this.member(name, options, 'PUT'); | ||
} | ||
}, { | ||
key: 'patch', | ||
value: function patch(name) { | ||
this.member(name, 'PATCH'); | ||
value: function patch(name, options) { | ||
this.member(name, options, 'PATCH'); | ||
} | ||
}, { | ||
key: 'del', | ||
value: function del(name) { | ||
this.member(name, 'DELETE'); | ||
value: function del(name, options) { | ||
this.member(name, options, 'DELETE'); | ||
} | ||
@@ -108,6 +108,16 @@ }, { | ||
key: 'member', | ||
value: function member(name, method) { | ||
this.current.members.push({ name: name, path: name, method: method, force: true }); | ||
value: function member(name, options, method) { | ||
var path = options && options.path || name; | ||
this.current.members.push({ name: name, path: path, method: method, force: true }); | ||
} | ||
}, { | ||
key: 'addRunnable', | ||
value: function addRunnable(collection) { | ||
this.current.runnables.push(collection); | ||
this.stack.push(collection); | ||
collection.run(); | ||
this.stack.pop(); | ||
} | ||
}, { | ||
key: 'current', | ||
@@ -119,5 +129,5 @@ get: function get() { | ||
return Duxy; | ||
return Runner; | ||
}(); | ||
exports.default = Duxy; | ||
exports.default = Runner; |
{ | ||
"name": "duxy", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "RESTful resources", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,7 +0,38 @@ | ||
import Collection from './Collection' | ||
import invariant from 'invariant' | ||
import joinUrl from 'url-join' | ||
export default class Namespace extends Collection { | ||
import Runnable from './Runnable' | ||
export default class Namespace extends Runnable { | ||
constructor (parent, name, options, fn) { | ||
super(parent, name, options, fn) | ||
super() | ||
invariant(parent, '`parent` is required') | ||
invariant(name, 'You must specify a name for the given resource or namespace') | ||
this.parent = parent | ||
this.name = name | ||
this.finders = [] | ||
if (typeof fn === 'function') { | ||
this.fn = fn | ||
this.options = options | ||
} else if (typeof options === 'function') { | ||
this.options = {} | ||
this.fn = options | ||
} else if (!options) { | ||
this.options = {} | ||
} else { | ||
this.options = options | ||
} | ||
const path = this.options.path || this.name | ||
if (parent instanceof Namespace) { | ||
this.path = joinUrl(parent.path, parent.key(), path) | ||
} else if (path.indexOf('http') === 0) { | ||
this.path = path | ||
} else { | ||
this.path = joinUrl(parent.path, path) | ||
} | ||
} | ||
@@ -8,0 +39,0 @@ |
@@ -1,6 +0,4 @@ | ||
import inflection from 'inflection' | ||
import Namespace from './Namespace' | ||
import Collection from './Collection' | ||
export default class Resource extends Collection { | ||
export default class Resource extends Namespace { | ||
constructor (parent, name, options, fn) { | ||
@@ -10,13 +8,8 @@ super(parent, name, options, fn) | ||
this.finders = [ | ||
{ name: 'findOne', method: 'GET', path: '{id}' }, | ||
{ name: 'findAll', method: 'GET', path: null }, | ||
{ name: 'findOne', method: 'GET', path: null }, | ||
{ name: 'create', method: 'POST', path: null }, | ||
{ name: 'update', method: 'PUT', path: '{id}' }, | ||
{ name: 'delete', method: 'DELETE', path: '{id}' } | ||
{ name: 'update', method: 'PUT', path: null }, | ||
{ name: 'delete', method: 'DELETE', path: null } | ||
] | ||
} | ||
key () { | ||
return '{' + (this.options.key || `${inflection.singularize(this.name)}Id`) + '}' | ||
} | ||
} |
import Namespace from './Namespace' | ||
import Resources from './Resources' | ||
import Resource from './Resource' | ||
import Root from './Root' | ||
export default class Duxy { | ||
export default class Runner { | ||
constructor (options = {}) { | ||
this.dsl = { | ||
resources: this.resources.bind(this), | ||
resource: this.resource.bind(this), | ||
namespace: this.namespace.bind(this), | ||
get: this.get.bind(this), | ||
post: this.post.bind(this), | ||
put: this.put.bind(this), | ||
patch: this.patch.bind(this), | ||
del: this.del.bind(this), | ||
map: this.map.bind(this) | ||
} | ||
this.options = options | ||
this.root = new Root(this, options.http) | ||
this.root = new Root(this.dsl, options.http) | ||
this.stack = [this.root] | ||
} | ||
// NOTE(vesln): DSL methods | ||
this.resource = this.resource.bind(this) | ||
this.namespace = this.namespace.bind(this) | ||
this.get = this.get.bind(this) | ||
this.post = this.post.bind(this) | ||
this.put = this.put.bind(this) | ||
this.patch = this.patch.bind(this) | ||
this.del = this.del.bind(this) | ||
this.map = this.map.bind(this) | ||
resources (name, options, fn) { | ||
this.addRunnable(new Resources(this.current, name, options, fn)) | ||
} | ||
resource (name, options, fn) { | ||
const resource = new Resource(this.current, name, options, fn) | ||
this.current.runnables.push(resource) | ||
this.stack.push(resource) | ||
resource.run() | ||
this.stack.pop() | ||
this.addRunnable(new Resource(this.current, name, options, fn)) | ||
} | ||
namespace (name, options, fn) { | ||
const namespace = new Namespace(this.current, name, options, fn) | ||
this.current.runnables.push(namespace) | ||
this.stack.push(namespace) | ||
namespace.run() | ||
this.stack.pop() | ||
this.addRunnable(new Namespace(this.current, name, options, fn)) | ||
} | ||
get (name) { | ||
this.member(name, 'GET') | ||
get (name, options) { | ||
this.member(name, options, 'GET') | ||
} | ||
post (name) { | ||
this.member(name, 'POST') | ||
post (name, options) { | ||
this.member(name, options, 'POST') | ||
} | ||
put (name) { | ||
this.member(name, 'PUT') | ||
put (name, options) { | ||
this.member(name, options, 'PUT') | ||
} | ||
patch (name) { | ||
this.member(name, 'PATCH') | ||
patch (name, options) { | ||
this.member(name, options, 'PATCH') | ||
} | ||
del (name) { | ||
this.member(name, 'DELETE') | ||
del (name, options) { | ||
this.member(name, options, 'DELETE') | ||
} | ||
@@ -74,5 +70,14 @@ | ||
member (name, method) { | ||
this.current.members.push({ name, path: name, method, force: true }) | ||
member (name, options, method) { | ||
const path = options && options.path || name | ||
this.current.members.push({ name, path, method, force: true }) | ||
} | ||
addRunnable (collection) { | ||
this.current.runnables.push(collection) | ||
this.stack.push(collection) | ||
collection.run() | ||
this.stack.pop() | ||
} | ||
} |
@@ -6,5 +6,5 @@ import { expect } from './helper' | ||
it('can register top level resources', () => { | ||
const client = createClient(({ resource }) => { | ||
resource('users') | ||
resource('posts') | ||
const client = createClient(({ resources }) => { | ||
resources('users') | ||
resources('posts') | ||
}) | ||
@@ -17,5 +17,5 @@ | ||
it('can register nested resources', () => { | ||
const client = createClient(({ resource }) => { | ||
resource('users', () => { | ||
resource('posts') | ||
const client = createClient(({ resources }) => { | ||
resources('users', () => { | ||
resources('posts') | ||
}) | ||
@@ -29,6 +29,6 @@ }) | ||
it('can register a namespace', () => { | ||
const client = createClient(({ resource, namespace }) => { | ||
const client = createClient(({ resources, namespace }) => { | ||
namespace('api', () => { | ||
resource('users', () => { | ||
resource('posts') | ||
resources('users', () => { | ||
resources('posts') | ||
}) | ||
@@ -43,4 +43,4 @@ }) | ||
it('can register members', () => { | ||
const client = createClient(({ resource, get }) => { | ||
resource('users', () => { | ||
const client = createClient(({ resources, get }) => { | ||
resources('users', () => { | ||
get('latest') | ||
@@ -47,0 +47,0 @@ }) |
@@ -9,5 +9,5 @@ import { expect, spy } from './helper' | ||
const client = createClient({ http }, ({ namespace, resource }) => { | ||
const client = createClient({ http }, ({ namespace, resources }) => { | ||
namespace('posts', { http: localHttp }, () => { | ||
resource('users') | ||
resources('users') | ||
}) | ||
@@ -14,0 +14,0 @@ }) |
@@ -8,4 +8,4 @@ import { expect, spy } from './helper' | ||
const client = createClient({ http }, ({ resource, get }) => { | ||
resource('posts', { only: ['findOne'] }, () => { | ||
const client = createClient({ http }, ({ resources, get }) => { | ||
resources('posts', { only: ['findOne'] }, () => { | ||
get('latest') | ||
@@ -25,2 +25,21 @@ }) | ||
it('supports path option', () => { | ||
const http = spy() | ||
const client = createClient({ http }, ({ resources, get }) => { | ||
resources('posts', { only: ['findOne'] }, () => { | ||
get('edit', { path: '{id}/edit' }) | ||
}) | ||
}) | ||
client.posts.edit({ id: 1, foo: true }) | ||
expect(http).to.have.been.called.with({ | ||
method: 'GET', | ||
url: '/posts/1/edit', | ||
body: undefined, | ||
query: { id: 1, foo: true } | ||
}) | ||
}) | ||
describe('readers', () => { | ||
@@ -30,4 +49,4 @@ it('call the the http adapter with correct params', () => { | ||
const client = createClient({ http }, ({ resource, get }) => { | ||
resource('posts', () => { | ||
const client = createClient({ http }, ({ resources, get }) => { | ||
resources('posts', () => { | ||
get('latest') | ||
@@ -52,4 +71,4 @@ }) | ||
const client = createClient({ http }, ({ resource, post }) => { | ||
resource('posts', () => { | ||
const client = createClient({ http }, ({ resources, post }) => { | ||
resources('posts', () => { | ||
post('latest') | ||
@@ -56,0 +75,0 @@ }) |
@@ -6,4 +6,4 @@ import { expect } from './helper' | ||
it('handles unspecified paths', () => { | ||
const client = createClient(({ resource }) => { | ||
resource('users') | ||
const client = createClient(({ resources }) => { | ||
resources('users') | ||
}) | ||
@@ -15,4 +15,4 @@ | ||
it('handles named paths', () => { | ||
const client = createClient(({ resource }) => { | ||
resource('users', { path: 'people' }) | ||
const client = createClient(({ resources }) => { | ||
resources('users', { path: 'people' }) | ||
}) | ||
@@ -24,6 +24,6 @@ | ||
it('handles nested paths', () => { | ||
const client = createClient(({ resource, namespace }) => { | ||
const client = createClient(({ resources, namespace }) => { | ||
namespace('api', () => { | ||
resource('users', { path: 'people' }, () => { | ||
resource('posts') | ||
resources('users', { path: 'people' }, () => { | ||
resources('posts') | ||
}) | ||
@@ -37,5 +37,5 @@ }) | ||
it('handles full paths', () => { | ||
const client = createClient(({ resource, namespace }) => { | ||
const client = createClient(({ resources, namespace }) => { | ||
namespace('example', { path: 'http://example.com/' }, () => { | ||
resource('users') | ||
resources('users') | ||
}) | ||
@@ -42,0 +42,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102577
31
2638
1