Add options, pluginOptions and info as dynamic types for schemas

This commit is contained in:
Convly 2022-06-03 16:20:38 +02:00
parent a8f9a05873
commit f99f312b1f
6 changed files with 82 additions and 21 deletions

View File

@ -11,9 +11,13 @@ const generateAttributesDefinition = (attributes, uid) => {
attributesDefinitions.push([attributeName, type]);
}
return attributesDefinitions
const formattedDefinitions = attributesDefinitions
.map(([name, attributeType]) => ` ${name}: ${attributeType};`)
.join('\n');
return ` attributes: {
${formattedDefinitions}
}`;
};
const getAttributeType = (attribute, uid) => {

View File

@ -1,20 +1,28 @@
'use strict';
const fp = require('lodash/fp');
const { generateAttributesDefinition } = require('./attributes');
const { addImport } = require('./imports');
const { mapKeyValuesToType } = require('./utils');
const generateComponentDefinition = (uid, schema, type) => {
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 definitions = [componentInfo, componentOptions, componentPluginOptions, componentAttributes]
.filter(def => !fp.isNil(def))
.join('\n');
return `
interface ${type} extends ComponentSchema {
attributes: {
${componentAttributes}
}
}
`;
${definitions}
}`;
};
module.exports = {

View File

@ -4,6 +4,7 @@ const fp = require('lodash/fp');
const { generateAttributesDefinition } = require('./attributes');
const { addImport } = require('./imports');
const { mapKeyValuesToType } = require('./utils');
const generateContentTypeDefinition = (uid, schema, type) => {
const { kind } = schema;
@ -11,15 +12,25 @@ const generateContentTypeDefinition = (uid, schema, type) => {
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 definitions = [
contentTypeInfo,
contentTypeOptions,
contentTypePluginOptions,
contentTypeAttributes,
]
.filter(def => !fp.isNil(def))
.join('\n');
return `
interface ${type} extends ${baseInterface} {
attributes: {
${contentTypeAttributes}
}
}
`;
${definitions}
}`;
};
module.exports = { generateContentTypeDefinition };

View File

@ -30,7 +30,7 @@ module.exports = async function({ outDir, file }) {
const fullDefinition = [
imports,
definitions.map(fp.get('definition')).join(''),
definitions.map(fp.get('definition')).join('\n'),
globalDefinition,
].join('');

View File

@ -9,7 +9,45 @@ const logWarning = message => {
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 = {
logWarning,
getSchemaTypeName,
mapKeyValuesToType,
};

View File

@ -58,12 +58,12 @@ export interface SchemaInfo {
/**
* Singular form of the content type name
*/
singularName: string;
singularName?: string;
/**
* Plural form of the collection type name
*/
pluralName: string;
pluralName?: string;
/**
* Description of the model
@ -85,7 +85,7 @@ export interface SchemaAttributes extends StringRecord<Attribute> {}
* Structure containing every core schema options and their associated value
*/
export interface SchemaOptions {
draftAndPublish: boolean;
draftAndPublish?: boolean;
populateCreatorFields?: boolean;
comment?: string;
}