mirror of
https://github.com/strapi/strapi.git
synced 2025-08-07 16:29:18 +00:00
Add options, pluginOptions and info as dynamic types for schemas
This commit is contained in:
parent
a8f9a05873
commit
f99f312b1f
@ -11,9 +11,13 @@ const generateAttributesDefinition = (attributes, uid) => {
|
|||||||
attributesDefinitions.push([attributeName, type]);
|
attributesDefinitions.push([attributeName, type]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return attributesDefinitions
|
const formattedDefinitions = attributesDefinitions
|
||||||
.map(([name, attributeType]) => ` ${name}: ${attributeType};`)
|
.map(([name, attributeType]) => ` ${name}: ${attributeType};`)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
|
|
||||||
|
return ` attributes: {
|
||||||
|
${formattedDefinitions}
|
||||||
|
}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAttributeType = (attribute, uid) => {
|
const getAttributeType = (attribute, uid) => {
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const fp = require('lodash/fp');
|
||||||
|
|
||||||
const { generateAttributesDefinition } = require('./attributes');
|
const { generateAttributesDefinition } = require('./attributes');
|
||||||
const { addImport } = require('./imports');
|
const { addImport } = require('./imports');
|
||||||
|
const { mapKeyValuesToType } = require('./utils');
|
||||||
|
|
||||||
const generateComponentDefinition = (uid, schema, type) => {
|
const generateComponentDefinition = (uid, schema, type) => {
|
||||||
addImport('ComponentSchema');
|
addImport('ComponentSchema');
|
||||||
|
|
||||||
|
const componentInfo = mapKeyValuesToType(schema.info, 'info', 2);
|
||||||
|
const componentOptions = mapKeyValuesToType(schema.options, 'options', 2);
|
||||||
|
const componentPluginOptions = mapKeyValuesToType(schema.pluginOptions, 'pluginOptions', 2);
|
||||||
|
|
||||||
const componentAttributes = generateAttributesDefinition(schema.attributes, uid);
|
const componentAttributes = generateAttributesDefinition(schema.attributes, uid);
|
||||||
|
|
||||||
|
const definitions = [componentInfo, componentOptions, componentPluginOptions, componentAttributes]
|
||||||
|
.filter(def => !fp.isNil(def))
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
return `
|
return `
|
||||||
interface ${type} extends ComponentSchema {
|
interface ${type} extends ComponentSchema {
|
||||||
attributes: {
|
${definitions}
|
||||||
${componentAttributes}
|
}`;
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -4,6 +4,7 @@ const fp = require('lodash/fp');
|
|||||||
|
|
||||||
const { generateAttributesDefinition } = require('./attributes');
|
const { generateAttributesDefinition } = require('./attributes');
|
||||||
const { addImport } = require('./imports');
|
const { addImport } = require('./imports');
|
||||||
|
const { mapKeyValuesToType } = require('./utils');
|
||||||
|
|
||||||
const generateContentTypeDefinition = (uid, schema, type) => {
|
const generateContentTypeDefinition = (uid, schema, type) => {
|
||||||
const { kind } = schema;
|
const { kind } = schema;
|
||||||
@ -11,15 +12,25 @@ const generateContentTypeDefinition = (uid, schema, type) => {
|
|||||||
|
|
||||||
addImport(baseInterface);
|
addImport(baseInterface);
|
||||||
|
|
||||||
|
const contentTypeInfo = mapKeyValuesToType(schema.info, 'info', 2);
|
||||||
|
const contentTypeOptions = mapKeyValuesToType(schema.options, 'options', 2);
|
||||||
|
const contentTypePluginOptions = mapKeyValuesToType(schema.pluginOptions, 'pluginOptions', 2);
|
||||||
|
|
||||||
const contentTypeAttributes = generateAttributesDefinition(schema.attributes, uid);
|
const contentTypeAttributes = generateAttributesDefinition(schema.attributes, uid);
|
||||||
|
|
||||||
|
const definitions = [
|
||||||
|
contentTypeInfo,
|
||||||
|
contentTypeOptions,
|
||||||
|
contentTypePluginOptions,
|
||||||
|
contentTypeAttributes,
|
||||||
|
]
|
||||||
|
.filter(def => !fp.isNil(def))
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
return `
|
return `
|
||||||
interface ${type} extends ${baseInterface} {
|
interface ${type} extends ${baseInterface} {
|
||||||
attributes: {
|
${definitions}
|
||||||
${contentTypeAttributes}
|
}`;
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { generateContentTypeDefinition };
|
module.exports = { generateContentTypeDefinition };
|
||||||
|
@ -30,7 +30,7 @@ module.exports = async function({ outDir, file }) {
|
|||||||
|
|
||||||
const fullDefinition = [
|
const fullDefinition = [
|
||||||
imports,
|
imports,
|
||||||
definitions.map(fp.get('definition')).join(''),
|
definitions.map(fp.get('definition')).join('\n'),
|
||||||
globalDefinition,
|
globalDefinition,
|
||||||
].join('');
|
].join('');
|
||||||
|
|
||||||
|
@ -9,7 +9,45 @@ const logWarning = message => {
|
|||||||
|
|
||||||
const getSchemaTypeName = fp.flow(fp.replace(/(:.)/, ' '), fp.camelCase, fp.upperFirst);
|
const getSchemaTypeName = fp.flow(fp.replace(/(:.)/, ' '), fp.camelCase, fp.upperFirst);
|
||||||
|
|
||||||
|
const mapKeyValuesToType = (object, typeName, indent = 0) => {
|
||||||
|
if (!object || fp.isEmpty(object)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedTypeName = typeName.includes('-') ? `'${typeName}'` : typeName;
|
||||||
|
|
||||||
|
const properties = Object.entries(object)
|
||||||
|
.reduce((acc, [key, value]) => {
|
||||||
|
const offset = ' '.repeat(indent + 2);
|
||||||
|
const formattedKey = key.includes('-') ? `'${key}'` : key;
|
||||||
|
|
||||||
|
// Common values
|
||||||
|
let newValue = value;
|
||||||
|
|
||||||
|
// Object values
|
||||||
|
if (fp.isObject(value)) {
|
||||||
|
return `${acc}
|
||||||
|
${mapKeyValuesToType(value, key, indent + 2)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// String values
|
||||||
|
else if (fp.isString(value)) {
|
||||||
|
newValue = `'${value}'`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${acc}
|
||||||
|
${offset}${formattedKey}: ${newValue}`;
|
||||||
|
}, '')
|
||||||
|
// Removing leading \n (when acc is an empty string)
|
||||||
|
.slice(1);
|
||||||
|
|
||||||
|
return `${' '.repeat(indent)}${formattedTypeName}: {
|
||||||
|
${properties}
|
||||||
|
${' '.repeat(indent)}};`;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
logWarning,
|
logWarning,
|
||||||
getSchemaTypeName,
|
getSchemaTypeName,
|
||||||
|
mapKeyValuesToType,
|
||||||
};
|
};
|
||||||
|
@ -58,12 +58,12 @@ export interface SchemaInfo {
|
|||||||
/**
|
/**
|
||||||
* Singular form of the content type name
|
* Singular form of the content type name
|
||||||
*/
|
*/
|
||||||
singularName: string;
|
singularName?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plural form of the collection type name
|
* Plural form of the collection type name
|
||||||
*/
|
*/
|
||||||
pluralName: string;
|
pluralName?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of the model
|
* Description of the model
|
||||||
@ -85,7 +85,7 @@ export interface SchemaAttributes extends StringRecord<Attribute> {}
|
|||||||
* Structure containing every core schema options and their associated value
|
* Structure containing every core schema options and their associated value
|
||||||
*/
|
*/
|
||||||
export interface SchemaOptions {
|
export interface SchemaOptions {
|
||||||
draftAndPublish: boolean;
|
draftAndPublish?: boolean;
|
||||||
populateCreatorFields?: boolean;
|
populateCreatorFields?: boolean;
|
||||||
comment?: string;
|
comment?: string;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user