cf-crud-service-api-builder
Advanced tools
Comparing version 0.1.3 to 0.2.0
module.exports = buildApi | ||
var routes = | ||
{ get: require('./endpoints/get') | ||
, post: require('./endpoints/post') | ||
, put: require('./endpoints/put') | ||
, patch: require('./endpoints/patch') | ||
, 'delete': require('./endpoints/delete') | ||
} | ||
{ get: require('./endpoints/get') | ||
, post: require('./endpoints/post') | ||
, put: require('./endpoints/put') | ||
, patch: require('./endpoints/patch') | ||
, 'delete': require('./endpoints/delete') | ||
} | ||
, EventEmitter = require('events').EventEmitter | ||
function buildApi(service, urlRoot, router, logger, middleware, verbs) { | ||
function Api() { EventEmitter.call(this) } | ||
Api.prototype = Object.create(EventEmitter.prototype) | ||
var api = new Api() | ||
if (!Array.isArray(middleware) && typeof middleware !== 'function') throw new Error('Middleware is not defined') | ||
@@ -20,5 +26,7 @@ | ||
verbs.forEach(function (verb) { | ||
routes[verb](service, urlRoot, router, logger, middleware) | ||
routes[verb](service, urlRoot, router, logger, middleware, api.emit.bind(api)) | ||
}) | ||
return api | ||
} |
module.exports = del | ||
function del(service, urlRoot, router, logger, middleware) { | ||
function del(service, urlRoot, router, logger, middleware, emit) { | ||
@@ -15,2 +15,3 @@ router.delete(urlRoot + '/:id', middleware, function (req, res) { | ||
} else { | ||
emit('delete', req) | ||
res.send(204) | ||
@@ -17,0 +18,0 @@ } |
module.exports = patch | ||
function patch(service, urlRoot, router, logger, middleware) { | ||
function patch(service, urlRoot, router, logger, middleware, emit) { | ||
@@ -12,2 +12,3 @@ router.patch(urlRoot + '/:id', middleware, function (req, res) { | ||
} else { | ||
emit('partialUpdate', req, updatedObject) | ||
res.status(200).json(updatedObject) | ||
@@ -14,0 +15,0 @@ } |
module.exports = post | ||
function post(service, urlRoot, router, logger, middleware) { | ||
function post(service, urlRoot, router, logger, middleware, emit) { | ||
@@ -11,2 +11,3 @@ router.post(urlRoot, middleware, function (req, res) { | ||
} else { | ||
emit('create', req, newObject) | ||
res.status(201).json(newObject) | ||
@@ -13,0 +14,0 @@ } |
@@ -6,3 +6,3 @@ module.exports = put | ||
function put(service, urlRoot, router, logger, middleware) { | ||
function put(service, urlRoot, router, logger, middleware, emit) { | ||
@@ -35,2 +35,3 @@ // Optional :id url param to allow for arrays to be PUT | ||
} else { | ||
emit('update', req, updatedObject) | ||
cb(null, updatedObject) | ||
@@ -37,0 +38,0 @@ } |
@@ -10,18 +10,18 @@ module.exports = createFilterParser | ||
, ignoredTypes = [ Object, Array ] | ||
, type = null | ||
, type = getType(key, parentKey) | ||
if (parentKey) { | ||
type = schema.schema[parentKey].type | ||
} else { | ||
type = schema.schema[key].type | ||
} | ||
// Skip ignored types and Schemata Arrays | ||
if (ignoredTypes.indexOf(type) === -1 && !type.arraySchema) { | ||
if (Array.isArray(value)) { | ||
var newValue = [] | ||
value.forEach(function (item) { | ||
newValue.push(schema.castProperty(type, item)) | ||
if (isMongoOperator(key) && Array.isArray(value)) { | ||
value = value.map(function (item) { | ||
// Recursively cast objects like `{ $in: [1, 2, 3 }` | ||
if (typeof item === 'object' && null !== item) return parseObject(item) | ||
// Do a simple cast if they arent objects | ||
return schema.castProperty(type, item) | ||
}) | ||
value = newValue | ||
} else if (Array.isArray(value)) { | ||
value = value.map(function (item) { | ||
return schema.castProperty(type, item) | ||
}) | ||
} else if (typeof value === 'object' && null !== value) { | ||
@@ -38,3 +38,18 @@ value = parseObject(value, key) | ||
function getType(key, parentKey) { | ||
if (parentKey) { | ||
return schema.schema[parentKey].type | ||
} else if (isMongoOperator(key)) { | ||
return {} | ||
} else { | ||
return schema.schema[key].type | ||
} | ||
} | ||
// Key starts with $ e.g. $or, $and | ||
function isMongoOperator(key) { | ||
return key.match(/^\$/) | ||
} | ||
return parseObject | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Build an HTTP API for a crud-service", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"tags": [], | ||
@@ -35,2 +35,3 @@ "repository": { | ||
"jshint": "2", | ||
"mc-logger": "0.0.0", | ||
"mocha": "1", | ||
@@ -37,0 +38,0 @@ "save": "0.0.20", |
@@ -21,2 +21,23 @@ # cf-crud-service-api-builder | ||
### Events | ||
When using the api builder, you can listen for certain events so that you can add hooks to perform your own actions after a request has been succesful. e.g | ||
```js | ||
var api = crudServiceApiBuilder(service, '/article', router, logger, middleware) | ||
api.on('create', function (req, newArticle) { | ||
// do whatever you like with the req and article object | ||
}) | ||
``` | ||
Supported events are: | ||
* `create` | ||
* `update` | ||
* `partialUpdate` | ||
* `delete` | ||
## Credits | ||
@@ -23,0 +44,0 @@ Built by developers at [Clock](http://clock.co.uk). |
@@ -5,3 +5,3 @@ var createGetEndpoint = require('../../endpoints/get') | ||
, request = require('supertest') | ||
, logger = { debug: noop, info: noop, warn: noop, error: noop } | ||
, logger = require('mc-logger') | ||
, assert = require('assert') | ||
@@ -12,4 +12,2 @@ , async = require('async') | ||
function noop() {} | ||
describe('GET endpoint', function () { | ||
@@ -16,0 +14,0 @@ |
@@ -69,2 +69,21 @@ var schemata = require('schemata') | ||
it('should not throw for keys that start with $', function () { | ||
assert.doesNotThrow(function () { | ||
var params = { $or: [] } | ||
params = filterParser(params) | ||
}, /Cannot read property 'type' of undefined/) | ||
}) | ||
it('should work for keys that start with $', function () { | ||
var params = { $or: [ { string: 1 } ] } | ||
params = filterParser(params) | ||
assert.equal(typeof params.$or[0].string, 'string') | ||
}) | ||
it('should recursively for keys that start with $', function () { | ||
var params = { $or: [ { string: { $in: [1] } }, { string: { $size: 0 } } ] } | ||
params = filterParser(params) | ||
assert.equal(params.$or[0].string.$in[0], '1') | ||
assert.equal(params.$or[1].string.$size, '0') | ||
}) | ||
}) | ||
@@ -71,0 +90,0 @@ |
@@ -7,6 +7,4 @@ module.exports = createService | ||
, validity = require('validity') | ||
, logger = { debug: noop, info: noop, warn: noop, error: noop } | ||
, logger = require('mc-logger') | ||
function noop() {} | ||
function createService() { | ||
@@ -13,0 +11,0 @@ return new Service('thing', save('thing', { logger: logger }), createSchema()) |
25218
18
633
47
11