strapi-utils
Advanced tools
Comparing version 3.5.4 to 3.6.0-next.0
'use strict'; | ||
const _ = require('lodash'); | ||
const pluralize = require('pluralize'); | ||
@@ -16,5 +17,2 @@ const SINGLE_TYPE = 'singleType'; | ||
const NON_WRITABLE_ATTRIBUTES = [ID_ATTRIBUTE, CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE]; | ||
const NON_VISIBLE_ATTRIBUTES = [...NON_WRITABLE_ATTRIBUTES, PUBLISHED_AT_ATTRIBUTE]; | ||
const constants = { | ||
@@ -61,5 +59,8 @@ ID_ATTRIBUTE, | ||
return _.uniq( | ||
NON_WRITABLE_ATTRIBUTES.concat(model.primaryKey, getTimestamps(model), nonWritableAttributes) | ||
); | ||
return _.uniq([ | ||
ID_ATTRIBUTE, | ||
model.primaryKey, | ||
...getTimestamps(model), | ||
...nonWritableAttributes, | ||
]); | ||
}; | ||
@@ -71,4 +72,14 @@ | ||
const isWritableAttribute = (model, attributeName) => { | ||
return getWritableAttributes(model).includes(attributeName); | ||
}; | ||
const getNonVisibleAttributes = model => { | ||
return _.uniq([model.primaryKey, ...getTimestamps(model), ...NON_VISIBLE_ATTRIBUTES]); | ||
const nonVisibleAttributes = _.reduce( | ||
model.attributes, | ||
(acc, attr, attrName) => (attr.visible === false ? acc.concat(attrName) : acc), | ||
[] | ||
); | ||
return _.uniq([ID_ATTRIBUTE, model.primaryKey, ...getTimestamps(model), ...nonVisibleAttributes]); | ||
}; | ||
@@ -80,2 +91,6 @@ | ||
const isVisibleAttribute = (model, attributeName) => { | ||
return getVisibleAttributes(model).includes(attributeName); | ||
}; | ||
const hasDraftAndPublish = model => _.get(model, 'options.draftAndPublish', false) === true; | ||
@@ -115,5 +130,85 @@ | ||
const getKind = obj => obj.kind || 'collectionType'; | ||
const pickSchema = model => { | ||
const schema = _.cloneDeep( | ||
_.pick(model, [ | ||
'connection', | ||
'collectionName', | ||
'info', | ||
'options', | ||
'pluginOptions', | ||
'attributes', | ||
]) | ||
); | ||
schema.kind = getKind(model); | ||
return schema; | ||
}; | ||
const createContentType = ( | ||
model, | ||
{ modelName, defaultConnection }, | ||
{ apiName, pluginName } = {} | ||
) => { | ||
if (apiName) { | ||
Object.assign(model, { | ||
uid: `application::${apiName}.${modelName}`, | ||
apiName, | ||
collectionName: model.collectionName || modelName.toLocaleLowerCase(), | ||
globalId: getGlobalId(model, modelName), | ||
}); | ||
} else if (pluginName) { | ||
Object.assign(model, { | ||
uid: `plugins::${pluginName}.${modelName}`, | ||
plugin: pluginName, | ||
collectionName: model.collectionName || `${pluginName}_${modelName}`.toLowerCase(), | ||
globalId: getGlobalId(model, modelName, pluginName), | ||
}); | ||
} else { | ||
Object.assign(model, { | ||
uid: `strapi::${modelName}`, | ||
plugin: 'admin', | ||
globalId: getGlobalId(model, modelName, 'admin'), | ||
}); | ||
} | ||
Object.assign(model, { | ||
__schema__: pickSchema(model), | ||
kind: getKind(model), | ||
modelType: 'contentType', | ||
modelName, | ||
connection: model.connection || defaultConnection, | ||
}); | ||
Object.defineProperty(model, 'privateAttributes', { | ||
get() { | ||
return strapi.getModel(model.uid).privateAttributes; | ||
}, | ||
}); | ||
}; | ||
const getGlobalId = (model, modelName, prefix) => { | ||
let globalId = prefix ? `${prefix}-${modelName}` : modelName; | ||
return model.globalId || _.upperFirst(_.camelCase(globalId)); | ||
}; | ||
const isRelationalAttribute = attribute => | ||
_.has(attribute, 'model') || _.has(attribute, 'collection'); | ||
/** | ||
* Returns a route prefix for a contentType | ||
* @param {object} contentType | ||
* @returns {string} | ||
*/ | ||
const getContentTypeRoutePrefix = contentType => { | ||
return isSingleType(contentType) | ||
? _.kebabCase(contentType.modelName) | ||
: _.kebabCase(pluralize(contentType.modelName)); | ||
}; | ||
module.exports = { | ||
isScalarAttribute, | ||
isMediaAttribute, | ||
isRelationalAttribute, | ||
getPrivateAttributes, | ||
@@ -125,4 +220,6 @@ getTimestampsAttributes, | ||
getWritableAttributes, | ||
isWritableAttribute, | ||
getNonVisibleAttributes, | ||
getVisibleAttributes, | ||
isVisibleAttribute, | ||
hasDraftAndPublish, | ||
@@ -133,2 +230,5 @@ isDraft, | ||
isKind, | ||
createContentType, | ||
getGlobalId, | ||
getContentTypeRoutePrefix, | ||
}; |
@@ -36,2 +36,5 @@ 'use strict'; | ||
const relations = require('./relations'); | ||
const setCreatorFields = require('./set-creator-fields'); | ||
const hooks = require('./hooks'); | ||
const providerFactory = require('./provider-factory'); | ||
@@ -69,2 +72,5 @@ module.exports = { | ||
relations, | ||
setCreatorFields, | ||
hooks, | ||
providerFactory, | ||
}; |
@@ -397,2 +397,3 @@ 'use strict'; | ||
filter: details.filter, | ||
populate: association.populate, | ||
}; | ||
@@ -425,2 +426,3 @@ | ||
filter: details.filter, | ||
populate: association.populate, | ||
}); | ||
@@ -478,2 +480,3 @@ return; | ||
filter: association.filter, | ||
populate: association.populate, | ||
}); | ||
@@ -480,0 +483,0 @@ } catch (e) { |
'use strict'; | ||
const _ = require('lodash'); | ||
const { constants, isPrivateAttribute } = require('./content-types'); | ||
const { | ||
ID_ATTRIBUTE, | ||
PUBLISHED_AT_ATTRIBUTE, | ||
CREATED_BY_ATTRIBUTE, | ||
UPDATED_BY_ATTRIBUTE, | ||
} = constants; | ||
constants, | ||
isPrivateAttribute, | ||
getNonWritableAttributes, | ||
getNonVisibleAttributes, | ||
getWritableAttributes, | ||
} = require('./content-types'); | ||
const { ID_ATTRIBUTE } = constants; | ||
const sanitizeEntity = (dataSource, options) => { | ||
@@ -127,3 +128,9 @@ const { model, withPrivate = false, isOutput = true, includeFields = null } = options; | ||
const { options, primaryKey } = model; | ||
const nonWritableAttributes = getNonWritableAttributes(model); | ||
const nonVisibleAttributes = getNonVisibleAttributes(model); | ||
const writableAttributes = getWritableAttributes(model); | ||
const nonVisibleWritableAttributes = _.intersection(writableAttributes, nonVisibleAttributes); | ||
const timestamps = options.timestamps || []; | ||
@@ -139,7 +146,6 @@ | ||
COMPONENT_FIELDS, | ||
CREATED_BY_ATTRIBUTE, | ||
UPDATED_BY_ATTRIBUTE, | ||
PUBLISHED_AT_ATTRIBUTE, | ||
...nonWritableAttributes, | ||
...nonVisibleAttributes, | ||
] | ||
: [primaryKey, STATIC_FIELDS, COMPONENT_FIELDS]) | ||
: [primaryKey, STATIC_FIELDS, COMPONENT_FIELDS, ...nonVisibleWritableAttributes]) | ||
); | ||
@@ -146,0 +152,0 @@ }; |
{ | ||
"name": "strapi-utils", | ||
"version": "3.5.4", | ||
"version": "3.6.0-next.0", | ||
"description": "Shared utilities for the Strapi packages", | ||
@@ -48,3 +48,3 @@ "homepage": "https://strapi.io", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"gitHead": "3065d72a4b9ef04a94bb5f3f62683cf59712e69b" | ||
"gitHead": "fd18d2a67aec0bd2c76a375297ac30d4c3c0dfb7" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
67473
26
1997
0
1