mirror of
https://github.com/strapi/strapi.git
synced 2025-07-13 03:51:54 +00:00
113 lines
2.9 KiB
JavaScript
113 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Module dependencies
|
|
*/
|
|
|
|
// Public node modules.
|
|
const _ = require('lodash');
|
|
const utilsBookShelf = require('./bookshelf');
|
|
const utilsWaterline = require('./waterline');
|
|
|
|
/**
|
|
* JSON API utils
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
* Verify ressource object
|
|
*/
|
|
|
|
isRessourceObject: function (object) {
|
|
return _.isObject(object) && object.hasOwnProperty('id') && object.hasOwnProperty('type');
|
|
},
|
|
|
|
/**
|
|
* Verify if the route exists
|
|
*/
|
|
|
|
isRoute: function (link) {
|
|
return strapi.config.routes.hasOwnProperty(link);
|
|
},
|
|
|
|
/**
|
|
* Find data object
|
|
*/
|
|
|
|
getObject: function (matchedRoute) {
|
|
// TODO:
|
|
// - Improve way to detect collection/ressource/relationships/related
|
|
switch (_.size(matchedRoute.regexp.keys)) {
|
|
case 0:
|
|
return 'collection';
|
|
case 1:
|
|
return 'ressource';
|
|
case 2:
|
|
return (matchedRoute.path.indexOf('relationships') !== -1) ? 'relationships' : 'related';
|
|
default:
|
|
return 'collection';
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Find data type
|
|
*/
|
|
|
|
getType: function (ctx, supposedType) {
|
|
// Relationships or related ressource
|
|
if (strapi.models.hasOwnProperty(supposedType.toLowerCase()) && ctx.params.hasOwnProperty('relation') && ctx.method === 'GET') {
|
|
return _.first(_.reject(_.map(strapi.models[supposedType.toLowerCase()].associations, function (relation) {
|
|
return (ctx.params.hasOwnProperty('relation') && ctx.params.relation === relation.alias) ? relation.model || relation.collection : undefined;
|
|
}), _.isUndefined));
|
|
} else if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
|
return supposedType.toLowerCase();
|
|
} else if (!strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
|
// Deep search based on the relation alias
|
|
const tryFindType = _.reject(_.flattenDeep(_.map(strapi.models, function (model) {
|
|
return _.map(model.associations, function (relation) {
|
|
return (supposedType.toLowerCase() === relation.alias) ? relation.model || relation.collection : undefined;
|
|
});
|
|
})), _.isUndefined);
|
|
|
|
if (!_.isUndefined(tryFindType)) {
|
|
return _.first(tryFindType);
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
},
|
|
|
|
/**
|
|
* Find primary key
|
|
*/
|
|
|
|
getPK: function (type) {
|
|
if (!strapi.models.hasOwnProperty(type)) {
|
|
return null;
|
|
}
|
|
|
|
switch (strapi.models[type].orm.toLowerCase()) {
|
|
case 'bookshelf':
|
|
return utilsBookShelf.getPK(type);
|
|
case 'waterline':
|
|
return utilsWaterline.getPK(type);
|
|
default:
|
|
return null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Find router object for matched route
|
|
*/
|
|
|
|
matchedRoute: function (ctx) {
|
|
return _.find(strapi.router.stack, function (stack) {
|
|
if (new RegExp(stack.regexp).test(ctx.request.url) && _.includes(stack.methods, ctx.request.method.toUpperCase())) {
|
|
return stack;
|
|
}
|
|
});
|
|
}
|
|
|
|
};
|