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 => { module.exports = scope => {
const newRoutes = { return {
routes: {} routes: [{
}; method: 'GET',
path: '/ ' + scope.humanizeId,
newRoutes.routes['GET /' + scope.humanizeId] = { handler: scope.globalID + '.find',
controller: scope.globalID, config: {
action: 'find',
policies: [] 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; 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: []
}
}]
};
}; };

View File

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