Skip to content

Instantly share code, notes, and snippets.

@kristofsajdak
Last active August 29, 2015 14:17
Show Gist options
  • Save kristofsajdak/fd1a34de85c3915484ed to your computer and use it in GitHub Desktop.
Save kristofsajdak/fd1a34de85c3915484ed to your computer and use it in GitHub Desktop.
API stack rework
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
// register routes of interests
// all routes are bootstrapped with default swagger spec and validation
// e.g. categories.get registers a mustbe permission check activity with 'categories.get'
// the Joi schema attributes are used to evaluate body or query params depending on the verb
category.routes.get().route();
category.routes.getById().route();
category.routes.getChangeEventsStreaming().route();
category.routes.delete().route();
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
category.routes.get().route();
category.routes.getById().swagger({summary: 'all the lovely categories by id'}).route();
// only explicit .swagger() declaration needed when an override of standard swagger spec is wanted
category.routes.getChangeEventsStreaming().route();
category.routes.delete().route();
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
var validation = {
get: {query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}}
};
category.routes.get().validate(validation.get).route();
// only explicit .validate() needed when you want to override/augment defaults
category.routes.getById().route();
category.routes.getChangeEventsStreaming().route();
category.routes.delete().route();
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
category.routes.get().route();
category.routes.getById().route();
category.routes.getChangeEventsStreaming().route();
category.routes.delete().authorize(function(req, res, next) {
// lookup identity with req.user['agco-uuid']
// data lookup and execute business rules
if (ok) {
next(req, res);
} else {
throw new JSONAPI_Error({status:403, message: 'not cool enough, sorry :-) '});
}
})
.route();
// overrides default mustbe.authorized('category.delete')
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester, mustbeConfig) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
category.routes.get().route();
category.routes.getById().route();
category.routes.getChangeEventsStreaming().route();
category.routes.delete().before(beforeDelete).route();
// adding validation fn handler
// however this clause accepts an unbounded amount of fn handlers which get triggered before the actual delete fn handler;
function beforeDelete(req, res, next) {
// do some checks with req.
if ('untouchable'===req.body.categories[0].name) {
next(new harvester.JSONAPI_Err({status: 400, detail: 'untouchable category'}));
} else {
next();
}
}
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
var validation = {
get: {query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}}
};
category.routes.get().validate(validation.get).route();
category.routes.getById().swagger({summary: 'all the lovely categories by id'}).route();
category.routes.getChangeEventsStreaming().route();
category.routes.delete()
.authorize(function(req, res, next) {
// lookup identity with req.user['agco-uuid']
// data lookup and execute business rules
if (ok) {
next(req, res);
} else {
throw new JSONAPI_Error({status:403, message: 'not cool enough, sorry :-) '});
}
})
.before(beforeDelete).route();
// overrides default mustbe.authorized('category.delete')
function beforeDelete(req, res, next) {
if ('untouchable'===req.body.categories[0].name) {
next(new harvester.JSONAPI_Err({status: 400, detail: 'untouchable category'}));
} else {
next();
}
}
};
'use strict';
var Joi = require('joi');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
});
// retrieve express from app namespace
var app = harvester.app;
app.get('/categories', category.routes.get().handler());
app.get('/categories/:id', category.routes.getById().handler());
app.get('/categories/changes/stream', category.routes.getChangeEventsStreaming().handler());
app.delete('/categories', category.routes.delete().handler());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment