Apply async/await on generated API

This commit is contained in:
Aurélien Georget 2016-11-28 11:34:05 +01:00
parent c726b28395
commit a50f33c5ff
2 changed files with 52 additions and 63 deletions

View File

@ -16,53 +16,42 @@ const _ = require('lodash');
*/
module.exports = scope => {
const newRoutes = {
routes: {}
return {
routes: [{
method: 'GET',
path: '/ ' + scope.humanizeId,
handler: scope.globalID + '.find',
config: {
policies: []
}
}, {
method: 'GET',
path: '/ ' + scope.humanizeId + '/:id',
handler: scope.globalID + '.findOne',
config: {
policies: []
}
}, {
method: 'POST',
path: '/ ' + scope.humanizeId,
handler: scope.globalID + '.create',
config: {
policies: []
}
}, {
method: 'PUT',
path: '/ ' + scope.humanizeId + '/:id',
handler: scope.globalID + '.update',
config: {
policies: []
}
}, {
method: 'DELETE',
path: '/ ' + scope.humanizeId + '/:id',
handler: scope.globalID + '.destroy',
config: {
policies: []
}
}]
};
newRoutes.routes['GET /' + scope.humanizeId] = {
controller: scope.globalID,
action: 'find',
policies: []
};
newRoutes.routes['GET /' + scope.humanizeId + '/:id'] = {
controller: scope.globalID,
action: 'findOne',
policies: []
};
newRoutes.routes['POST /' + scope.humanizeId] = {
controller: scope.globalID,
action: 'create',
policies: []
};
newRoutes.routes['PUT /' + scope.humanizeId + '/:id'] = {
controller: scope.globalID,
action: 'update',
policies: []
};
newRoutes.routes['DELETE /' + scope.humanizeId + '/:id'] = {
controller: scope.globalID,
action: 'destroy',
policies: []
};
if (scope.template && scope.template !== 'mongoose') {
newRoutes.routes['POST /' + scope.humanizeId + '/:id/relationships/:relation'] = {
controller: scope.globalID,
action: 'createRelation',
policies: []
};
newRoutes.routes['DELETE /' + scope.humanizeId + '/:id/relationships/:relation'] = {
controller: scope.globalID,
action: 'destroyRelation',
policies: []
};
}
return newRoutes;
};

View File

@ -11,11 +11,11 @@ module.exports = {
* @return {Object|Array}
*/
find: function * () {
find: async (ctx, next) => {
try {
this.body = yield strapi.services.<%= humanizeId %>.fetchAll(this.query);
ctx.body = await strapi.services.<%= humanizeId %>.fetchAll(ctx.query);
} catch (err) {
this.body = err;
ctx.body = err;
}
},
@ -25,11 +25,11 @@ module.exports = {
* @return {Object|Array}
*/
findOne: function * () {
findOne: async (ctx, next) => {
try {
this.body = yield strapi.services.<%= humanizeId %>.fetch(this.params)
ctx.body = await strapi.services.<%= humanizeId %>.fetch(ctx.params)
} catch (err) {
this.body = err;
ctx.body = err;
}
},
@ -39,11 +39,11 @@ module.exports = {
* @return {Object}
*/
create: function * () {
create: async (ctx, next) => {
try {
this.body = yield strapi.services.<%= humanizeId %>.add(this.request.body);
ctx.body = await strapi.services.<%= humanizeId %>.add(ctx.request.body);
} catch (err) {
this.body = err;
ctx.body = err;
}
},
@ -53,11 +53,11 @@ module.exports = {
* @return {Object}
*/
update: function * () {
update: async (ctx, next) => {
try {
this.body = yield strapi.services.<%= humanizeId %>.edit(this.params, this.request.body) ;
ctx.body = await strapi.services.<%= humanizeId %>.edit(ctx.params, ctx.request.body) ;
} catch (err) {
this.body = err;
ctx.body = err;
}
},
@ -67,11 +67,11 @@ module.exports = {
* @return {Object}
*/
destroy: function * () {
destroy: async (ctx, next) => {
try {
this.body = yield strapi.services.<%= humanizeId %>.remove(this.params);
ctx.body = await strapi.services.<%= humanizeId %>.remove(ctx.params);
} catch (err) {
this.body = err;
ctx.body = err;
}
}
};