Skip to content

Instantly share code, notes, and snippets.

@kristofsajdak
Forked from ssebro/a1_simple.js
Last active August 29, 2015 14:21
Show Gist options
  • Save kristofsajdak/73aadf4393c5526f3705 to your computer and use it in GitHub Desktop.
Save kristofsajdak/73aadf4393c5526f3705 to your computer and use it in GitHub Desktop.
'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 all routes :
// GET /categories, GET /categories/:id, GET /categories/changes/stream, POST /categories,
// PUT /categories/:id, DELETE /categories/:id,
// all of these are bootstrapped with the default authorization function, swagger spec and validation
// the Joi schema attributes are used to evaluate body or query params depending on the verb
.register();
};
'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'
}
})
// only explicit .swagger() declaration needed when an override of standard swagger spec is wanted
.getById().swagger({summary: 'all the lovely categories by id'})
.register();
};
'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'
}
})
.get().validate({query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}})
.register();
};
'use strict';
var Joi = require('joi');
var Promise = require('bluebird');
module.exports = function (harvester) {
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
})
.get().authorize(false)
.getById().authorize(false)
.delete().before(function(req) {
var resource = this;
return dynamicAuthorizeDelete(req).then(function() {
return resource;
});
})
.register();
function dynamicAuthorizeDelete(req) {
var _category;
return Promise.resolve()
.then(function(){
harvester.adapter.find('category',req.params.id)
})
.then(function(category){
_category = category;
// lookup identity with whoamIfunction
return $http.get('/whoami') //header should have authentication
})
.then(function(resp) {
if (resp.dealerUser && dealerUser.id==_category.links.dealerUser){
return true;
}else{
throw new JSONAPI_Error({403, 'something went wrong'}))
}
})
}
}
};
'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'
}
})
.get().authorize(false).validate({query: {myAwesomeParam: Joi.string().required().description('My awesome parameter')}})
.getById().authorize(false).swagger({summary: 'all the lovely categories by id'})
.delete().before(function(req) {
var resource = this;
return dynamicAuthorizeDelete(req).then(function() {
return resource;
});
})
function dynamicAuthorizeDelete(req) {
var _category;
return Promise.resolve()
.then(function(){
harvester.adapter.find('category',req.params.id)
})
.then(function(category){
_category = category;
// lookup identity with whoamIfunction
return $http.get('/whoami') //header should have authentication
})
.then(function(resp) {
if (resp.dealerUser && dealerUser.id==_category.links.dealerUser){
return true;
}else{
throw new JSONAPI_Error({403, 'something went wrong'}))
}
})
}
}
};
'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.get().handler());
app.get('/categories/:id', category.getById().handler());
app.get('/categories/changes/stream', category.getChangeEventsStreaming().handler());
app.delete('/categories', category.delete().handler());
};
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
})
.immutable() // only POST and GETs are allowed
.register();
var category = harvester
.resource('categories', {
name: Joi.string().required().description('a name'),
links: {
brand: 'brands'
}
})
.readonly() // only GETs are allowed
.register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment