@strapi/typescript-utils 5.61.0 → 5.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Orders strings like `localeCompare` on an English machine, whatever the locale of the machine.
3
+ * Without an explicit locale, `localeCompare` uses the machine locale (in Danish, `aa` sorts after
4
+ * `z`), so two machines could generate the same types in a different order.
5
+ */
6
+ export declare const compareEnglish: (a: string, b: string) => number;
7
+ //# sourceMappingURL=compare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../../../src/generators/common/compare.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,WAA6B,CAAC"}
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Orders strings like `localeCompare` on an English machine, whatever the locale of the machine.
5
+ * Without an explicit locale, `localeCompare` uses the machine locale (in Danish, `aa` sorts after
6
+ * `z`), so two machines could generate the same types in a different order.
7
+ */ const compareEnglish = (a, b)=>a.localeCompare(b, 'en');
8
+
9
+ exports.compareEnglish = compareEnglish;
10
+ //# sourceMappingURL=compare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.js","sources":["../../../src/generators/common/compare.ts"],"sourcesContent":["/**\n * Orders strings like `localeCompare` on an English machine, whatever the locale of the machine.\n * Without an explicit locale, `localeCompare` uses the machine locale (in Danish, `aa` sorts after\n * `z`), so two machines could generate the same types in a different order.\n */\nexport const compareEnglish = (a: string, b: string) => a.localeCompare(b, 'en');\n"],"names":["compareEnglish","a","b","localeCompare"],"mappings":";;AAAA;;;;IAKO,MAAMA,cAAAA,GAAiB,CAACC,CAAAA,EAAWC,IAAcD,CAAAA,CAAEE,aAAa,CAACD,CAAAA,EAAG,IAAA;;;;"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Orders strings like `localeCompare` on an English machine, whatever the locale of the machine.
3
+ * Without an explicit locale, `localeCompare` uses the machine locale (in Danish, `aa` sorts after
4
+ * `z`), so two machines could generate the same types in a different order.
5
+ */ const compareEnglish = (a, b)=>a.localeCompare(b, 'en');
6
+
7
+ export { compareEnglish };
8
+ //# sourceMappingURL=compare.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.mjs","sources":["../../../src/generators/common/compare.ts"],"sourcesContent":["/**\n * Orders strings like `localeCompare` on an English machine, whatever the locale of the machine.\n * Without an explicit locale, `localeCompare` uses the machine locale (in Danish, `aa` sorts after\n * `z`), so two machines could generate the same types in a different order.\n */\nexport const compareEnglish = (a: string, b: string) => a.localeCompare(b, 'en');\n"],"names":["compareEnglish","a","b","localeCompare"],"mappings":"AAAA;;;;IAKO,MAAMA,cAAAA,GAAiB,CAACC,CAAAA,EAAWC,IAAcD,CAAAA,CAAEE,aAAa,CAACD,CAAAA,EAAG,IAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../src/generators/common/models/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AASjC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAmCtC;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,MAAM,KAAG,EAAE,CAAC,oBAqC5D,CAAC"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../src/generators/common/models/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AASjC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAoCtC;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,MAAM,KAAG,EAAE,CAAC,oBAqC5D,CAAC"}
@@ -5,6 +5,7 @@ var _ = require('lodash');
5
5
  var utils = require('./utils.js');
6
6
  var attributes = require('./attributes.js');
7
7
  var imports = require('../imports.js');
8
+ var compare = require('../compare.js');
8
9
 
9
10
  function _interopNamespace(e) {
10
11
  if (e && e.__esModule) return e;
@@ -31,7 +32,7 @@ const { factory } = ts__namespace;
31
32
  * Generate a property signature for the schema's `attributes` field
32
33
  */ const generateAttributePropertySignature = (schema)=>{
33
34
  const { attributes: attributes$1 } = schema;
34
- const properties = Object.entries(attributes$1).sort((a, b)=>a[0].localeCompare(b[0])).map(([attributeName, attribute])=>{
35
+ const properties = Object.entries(attributes$1).sort((a, b)=>compare.compareEnglish(a[0], b[0])).map(([attributeName, attribute])=>{
35
36
  return attributes.attributeToPropertySignature(schema, attributeName, attribute);
36
37
  });
37
38
  return factory.createPropertySignature(undefined, factory.createIdentifier('attributes'), undefined, factory.createTypeLiteralNode(properties));
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sources":["../../../../src/generators/common/models/schema.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { isEmpty } from 'lodash';\n\nimport {\n getSchemaExtendsTypeName,\n getSchemaInterfaceName,\n toTypeLiteral,\n NAMESPACES,\n} from './utils';\nimport type { Schema } from './utils';\nimport { attributeToPropertySignature } from './attributes';\nimport { addImport } from '../imports';\n\nconst { factory } = ts;\n\n/**\n * Generate a property signature for the schema's `attributes` field\n */\nconst generateAttributePropertySignature = (schema: Schema): ts.PropertySignature => {\n const { attributes } = schema;\n\n const properties = Object.entries(attributes)\n .sort((a, b) => a[0].localeCompare(b[0]))\n .map(([attributeName, attribute]) => {\n return attributeToPropertySignature(schema, attributeName, attribute);\n });\n\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier('attributes'),\n undefined,\n factory.createTypeLiteralNode(properties as any)\n );\n};\n\nconst generatePropertyLiteralDefinitionFactory = (schema: Schema) => (key: string) => {\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier(key),\n undefined,\n toTypeLiteral(schema[key])\n );\n};\n\n/**\n * Generate an interface declaration for a given schema\n */\nexport const generateSchemaDefinition = (schema: Schema): ts.InterfaceDeclaration => {\n const { uid } = schema;\n\n // Resolve the different interface names needed to declare the schema's interface\n const interfaceName = getSchemaInterfaceName(uid);\n const parentType = getSchemaExtendsTypeName(schema);\n\n // Make sure the Struct namespace is imported\n addImport(NAMESPACES.Struct);\n\n // Properties whose values can be mapped to a literal type expression\n const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']\n // Ignore non-existent or empty declarations\n .filter((key) => !isEmpty(schema[key]))\n // Generate literal definition for each property\n .map(generatePropertyLiteralDefinitionFactory(schema));\n\n // Generate the `attributes` literal type definition\n const attributesProp = generateAttributePropertySignature(schema);\n\n // Merge every schema's definition in a single list\n const schemaProperties = [...literalPropertiesDefinitions, attributesProp];\n\n // Generate the schema's interface declaration\n const schemaType = factory.createInterfaceDeclaration(\n [factory.createModifier(ts.SyntaxKind.ExportKeyword)],\n factory.createIdentifier(interfaceName),\n undefined,\n [\n factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n factory.createIdentifier(parentType as string) as any,\n ]),\n ],\n schemaProperties\n );\n\n return schemaType;\n};\n"],"names":["factory","ts","generateAttributePropertySignature","schema","attributes","properties","Object","entries","sort","a","b","localeCompare","map","attributeName","attribute","attributeToPropertySignature","createPropertySignature","undefined","createIdentifier","createTypeLiteralNode","generatePropertyLiteralDefinitionFactory","key","toTypeLiteral","generateSchemaDefinition","uid","interfaceName","getSchemaInterfaceName","parentType","getSchemaExtendsTypeName","addImport","NAMESPACES","Struct","literalPropertiesDefinitions","filter","isEmpty","attributesProp","schemaProperties","schemaType","createInterfaceDeclaration","createModifier","SyntaxKind","ExportKeyword","createHeritageClause","ExtendsKeyword"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;AAEpB;;IAGA,MAAMC,qCAAqC,CAACC,MAAAA,GAAAA;IAC1C,MAAM,cAAEC,YAAU,EAAE,GAAGD,MAAAA;IAEvB,MAAME,UAA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GAAAA,GAAQ,CAACa,UAAQ/B,MAAM,CAACkB,GAAAA,CAAI,CAAA,CACrC;AACCT,KAAAA,GAAG,CAACQ,wCAAAA,CAAyCjB,MAAAA,CAAAA,CAAAA;;AAGhD,IAAA,MAAMgC,iBAAiBjC,kCAAAA,CAAmCC,MAAAA,CAAAA;;AAG1D,IAAA,MAAMiC,gBAAAA,GAAmB;AAAIJ,QAAAA,GAAAA,4BAAAA;AAA8BG,QAAAA;AAAe,KAAA;;IAG1E,MAAME,UAAAA,GAAarC,OAAAA,CAAQsC,0BAA0B,CACnD;AAACtC,QAAAA,OAAAA,CAAQuC,cAAc,CAACtC,aAAAA,CAAGuC,UAAU,CAACC,aAAa;AAAE,KAAA,EACrDzC,OAAAA,CAAQkB,gBAAgB,CAACO,aAAAA,CAAAA,EACzBR,SAAAA,EACA;AACEjB,QAAAA,OAAAA,CAAQ0C,oBAAoB,CAACzC,aAAAA,CAAGuC,UAAU,CAACG,cAAc,EAAE;AACzD3C,YAAAA,OAAAA,CAAQkB,gBAAgB,CAACS,UAAAA;AAC1B,SAAA;KACF,EACDS,gBAAAA,CAAAA;IAGF,OAAOC,UAAAA;AACT;;;;"}
1
+ {"version":3,"file":"schema.js","sources":["../../../../src/generators/common/models/schema.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { isEmpty } from 'lodash';\n\nimport {\n getSchemaExtendsTypeName,\n getSchemaInterfaceName,\n toTypeLiteral,\n NAMESPACES,\n} from './utils';\nimport type { Schema } from './utils';\nimport { attributeToPropertySignature } from './attributes';\nimport { addImport } from '../imports';\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\n/**\n * Generate a property signature for the schema's `attributes` field\n */\nconst generateAttributePropertySignature = (schema: Schema): ts.PropertySignature => {\n const { attributes } = schema;\n\n const properties = Object.entries(attributes)\n .sort((a, b) => compareEnglish(a[0], b[0]))\n .map(([attributeName, attribute]) => {\n return attributeToPropertySignature(schema, attributeName, attribute);\n });\n\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier('attributes'),\n undefined,\n factory.createTypeLiteralNode(properties as any)\n );\n};\n\nconst generatePropertyLiteralDefinitionFactory = (schema: Schema) => (key: string) => {\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier(key),\n undefined,\n toTypeLiteral(schema[key])\n );\n};\n\n/**\n * Generate an interface declaration for a given schema\n */\nexport const generateSchemaDefinition = (schema: Schema): ts.InterfaceDeclaration => {\n const { uid } = schema;\n\n // Resolve the different interface names needed to declare the schema's interface\n const interfaceName = getSchemaInterfaceName(uid);\n const parentType = getSchemaExtendsTypeName(schema);\n\n // Make sure the Struct namespace is imported\n addImport(NAMESPACES.Struct);\n\n // Properties whose values can be mapped to a literal type expression\n const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']\n // Ignore non-existent or empty declarations\n .filter((key) => !isEmpty(schema[key]))\n // Generate literal definition for each property\n .map(generatePropertyLiteralDefinitionFactory(schema));\n\n // Generate the `attributes` literal type definition\n const attributesProp = generateAttributePropertySignature(schema);\n\n // Merge every schema's definition in a single list\n const schemaProperties = [...literalPropertiesDefinitions, attributesProp];\n\n // Generate the schema's interface declaration\n const schemaType = factory.createInterfaceDeclaration(\n [factory.createModifier(ts.SyntaxKind.ExportKeyword)],\n factory.createIdentifier(interfaceName),\n undefined,\n [\n factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n factory.createIdentifier(parentType as string) as any,\n ]),\n ],\n schemaProperties\n );\n\n return schemaType;\n};\n"],"names":["factory","ts","generateAttributePropertySignature","schema","attributes","properties","Object","entries","sort","a","b","compareEnglish","map","attributeName","attribute","attributeToPropertySignature","createPropertySignature","undefined","createIdentifier","createTypeLiteralNode","generatePropertyLiteralDefinitionFactory","key","toTypeLiteral","generateSchemaDefinition","uid","interfaceName","getSchemaInterfaceName","parentType","getSchemaExtendsTypeName","addImport","NAMESPACES","Struct","literalPropertiesDefinitions","filter","isEmpty","attributesProp","schemaProperties","schemaType","createInterfaceDeclaration","createModif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7C,IAAA,MAAMG,aAAaC,8BAAAA,CAAyBzB,MAAAA,CAAAA;;AAG5C0B,IAAAA,iBAAAA,CAAUC,iBAAWC,MAAM,CAAA;;AAG3B,IAAA,MAAMC,4BAAAA,GAA+B;AAAC,QAAA,gBAAA;AAAkB,QAAA,MAAA;AAAQ,QAAA,SAAA;AAAW,QAAA;AAAgB,KACzF;KACCC,MAAM,CAAC,CAACZ,GAAAA,GAAQ,CAACa,UAAQ/B,MAAM,CAACkB,GAAAA,CAAI,CAAA,CACrC;AACCT,KAAAA,GAAG,CAACQ,wCAAAA,CAAyCjB,MAAAA,CAAAA,CAAAA;;AAGhD,IAAA,MAAMgC,iBAAiBjC,kCAAAA,CAAmCC,MAAAA,CAAAA;;AAG1D,IAAA,MAAMiC,gBAAAA,GAAmB;AAAIJ,QAAAA,GAAAA,4BAAAA;AAA8BG,QAAAA;AAAe,KAAA;;IAG1E,MAAME,UAAAA,GAAarC,OAAAA,CAAQsC,0BAA0B,CACnD;AAACtC,QAAAA,OAAAA,CAAQuC,cAAc,CAACtC,aAAAA,CAAGuC,UAAU,CAACC,aAAa;AAAE,KAAA,EACrDzC,OAAAA,CAAQkB,gBAAgB,CAACO,aAAAA,CAAAA,EACzBR,SAAAA,EACA;AACEjB,QAAAA,OAAAA,CAAQ0C,oBAAoB,CAACzC,aAAAA,CAAGuC,UAAU,CAACG,cAAc,EAAE;AACzD3C,YAAAA,OAAAA,CAAQkB,gBAAgB,CAACS,UAAAA;AAC1B,SAAA;KACF,EACDS,gBAAAA,CAAAA;IAGF,OAAOC,UAAAA;AACT;;;;"}
@@ -3,13 +3,14 @@ import { isEmpty } from 'lodash';
3
3
  import { getSchemaInterfaceName, getSchemaExtendsTypeName, toTypeLiteral, NAMESPACES } from './utils.mjs';
4
4
  import { attributeToPropertySignature } from './attributes.mjs';
5
5
  import { addImport } from '../imports.mjs';
6
+ import { compareEnglish } from '../compare.mjs';
6
7
 
7
8
  const { factory } = ts;
8
9
  /**
9
10
  * Generate a property signature for the schema's `attributes` field
10
11
  */ const generateAttributePropertySignature = (schema)=>{
11
12
  const { attributes } = schema;
12
- const properties = Object.entries(attributes).sort((a, b)=>a[0].localeCompare(b[0])).map(([attributeName, attribute])=>{
13
+ const properties = Object.entries(attributes).sort((a, b)=>compareEnglish(a[0], b[0])).map(([attributeName, attribute])=>{
13
14
  return attributeToPropertySignature(schema, attributeName, attribute);
14
15
  });
15
16
  return factory.createPropertySignature(undefined, factory.createIdentifier('attributes'), undefined, factory.createTypeLiteralNode(properties));
@@ -1 +1 @@
1
- {"version":3,"file":"schema.mjs","sources":["../../../../src/generators/common/models/schema.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { isEmpty } from 'lodash';\n\nimport {\n getSchemaExtendsTypeName,\n getSchemaInterfaceName,\n toTypeLiteral,\n NAMESPACES,\n} from './utils';\nimport type { Schema } from './utils';\nimport { attributeToPropertySignature } from './attributes';\nimport { addImport } from '../imports';\n\nconst { factory } = ts;\n\n/**\n * Generate a property signature for the schema's `attributes` field\n */\nconst generateAttributePropertySignature = (schema: Schema): ts.PropertySignature => {\n const { attributes } = schema;\n\n const properties = Object.entries(attributes)\n .sort((a, b) => a[0].localeCompare(b[0]))\n .map(([attributeName, attribute]) => {\n return attributeToPropertySignature(schema, attributeName, attribute);\n });\n\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier('attributes'),\n undefined,\n factory.createTypeLiteralNode(properties as any)\n );\n};\n\nconst generatePropertyLiteralDefinitionFactory = (schema: Schema) => (key: string) => {\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier(key),\n undefined,\n toTypeLiteral(schema[key])\n );\n};\n\n/**\n * Generate an interface declaration for a given schema\n */\nexport const generateSchemaDefinition = (schema: Schema): ts.InterfaceDeclaration => {\n const { uid } = schema;\n\n // Resolve the different interface names needed to declare the schema's interface\n const interfaceName = getSchemaInterfaceName(uid);\n const parentType = getSchemaExtendsTypeName(schema);\n\n // Make sure the Struct namespace is imported\n addImport(NAMESPACES.Struct);\n\n // Properties whose values can be mapped to a literal type expression\n const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']\n // Ignore non-existent or empty declarations\n .filter((key) => !isEmpty(schema[key]))\n // Generate literal definition for each property\n .map(generatePropertyLiteralDefinitionFactory(schema));\n\n // Generate the `attributes` literal type definition\n const attributesProp = generateAttributePropertySignature(schema);\n\n // Merge every schema's definition in a single list\n const schemaProperties = [...literalPropertiesDefinitions, attributesProp];\n\n // Generate the schema's interface declaration\n const schemaType = factory.createInterfaceDeclaration(\n [factory.createModifier(ts.SyntaxKind.ExportKeyword)],\n factory.createIdentifier(interfaceName),\n undefined,\n [\n factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n factory.createIdentifier(parentType as string) as any,\n ]),\n ],\n schemaProperties\n );\n\n return schemaType;\n};\n"],"names":["factory","ts","generateAttributePropertySignature","schema","attributes","properties","Object","entries","sort","a","b","localeCompare","map","attributeName","attribute","attributeToPropertySignature","createPropertySignature","undefined","createIdentifier","createTypeLiteralNode","generatePropertyLiteralDefinitionFactory","key","toTypeLiteral","generateSchemaDefinition","uid","interfaceName","getSchemaInterfaceName","parentType","getSchemaExtendsTypeName","addImport","NAMESPACES","Struct","literalPropertiesDefinitions","filter","isEmpty","attributesProp","schemaProperties","schemaType","createInterfaceDeclaration","createModifier","SyntaxKind","ExportKeyword","createHeritageClause","ExtendsKeyword"],"mappings":";;;;;;AAaA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;AAEpB;;IAGA,MAAMC,qCAAqC,CAACC,MAAAA,GAAAA;IAC1C,MAAM,EAAEC,UAAU,EAAE,GAAGD,MAAAA;IAEvB,MAAME,UAAAA,GAAaC,MAAAA,CAAOC,OAAO,CAACH,UAAAA,CAAAA,CAC/BI,IAAI,CAAC,CAACC,CAAAA,EAAGC,CAAAA,GAAMD,CAAC,CAAC,EAAE,CAACE,aAAa,CAACD,CAAC,CAAC,CAAA,CAAE,CAAA,CAAA,CACtCE,GAAG,C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A;;AAG1D,IAAA,MAAMiC,gBAAAA,GAAmB;AAAIJ,QAAAA,GAAAA,4BAAAA;AAA8BG,QAAAA;AAAe,KAAA;;IAG1E,MAAME,UAAAA,GAAarC,OAAAA,CAAQsC,0BAA0B,CACnD;AAACtC,QAAAA,OAAAA,CAAQuC,cAAc,CAACtC,EAAAA,CAAGuC,UAAU,CAACC,aAAa;AAAE,KAAA,EACrDzC,OAAAA,CAAQkB,gBAAgB,CAACO,aAAAA,CAAAA,EACzBR,SAAAA,EACA;AACEjB,QAAAA,OAAAA,CAAQ0C,oBAAoB,CAACzC,EAAAA,CAAGuC,UAAU,CAACG,cAAc,EAAE;AACzD3C,YAAAA,OAAAA,CAAQkB,gBAAgB,CAACS,UAAAA;AAC1B,SAAA;KACF,EACDS,gBAAAA,CAAAA;IAGF,OAAOC,UAAAA;AACT;;;;"}
1
+ {"version":3,"file":"schema.mjs","sources":["../../../../src/generators/common/models/schema.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport { isEmpty } from 'lodash';\n\nimport {\n getSchemaExtendsTypeName,\n getSchemaInterfaceName,\n toTypeLiteral,\n NAMESPACES,\n} from './utils';\nimport type { Schema } from './utils';\nimport { attributeToPropertySignature } from './attributes';\nimport { addImport } from '../imports';\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\n/**\n * Generate a property signature for the schema's `attributes` field\n */\nconst generateAttributePropertySignature = (schema: Schema): ts.PropertySignature => {\n const { attributes } = schema;\n\n const properties = Object.entries(attributes)\n .sort((a, b) => compareEnglish(a[0], b[0]))\n .map(([attributeName, attribute]) => {\n return attributeToPropertySignature(schema, attributeName, attribute);\n });\n\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier('attributes'),\n undefined,\n factory.createTypeLiteralNode(properties as any)\n );\n};\n\nconst generatePropertyLiteralDefinitionFactory = (schema: Schema) => (key: string) => {\n return factory.createPropertySignature(\n undefined,\n factory.createIdentifier(key),\n undefined,\n toTypeLiteral(schema[key])\n );\n};\n\n/**\n * Generate an interface declaration for a given schema\n */\nexport const generateSchemaDefinition = (schema: Schema): ts.InterfaceDeclaration => {\n const { uid } = schema;\n\n // Resolve the different interface names needed to declare the schema's interface\n const interfaceName = getSchemaInterfaceName(uid);\n const parentType = getSchemaExtendsTypeName(schema);\n\n // Make sure the Struct namespace is imported\n addImport(NAMESPACES.Struct);\n\n // Properties whose values can be mapped to a literal type expression\n const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']\n // Ignore non-existent or empty declarations\n .filter((key) => !isEmpty(schema[key]))\n // Generate literal definition for each property\n .map(generatePropertyLiteralDefinitionFactory(schema));\n\n // Generate the `attributes` literal type definition\n const attributesProp = generateAttributePropertySignature(schema);\n\n // Merge every schema's definition in a single list\n const schemaProperties = [...literalPropertiesDefinitions, attributesProp];\n\n // Generate the schema's interface declaration\n const schemaType = factory.createInterfaceDeclaration(\n [factory.createModifier(ts.SyntaxKind.ExportKeyword)],\n factory.createIdentifier(interfaceName),\n undefined,\n [\n factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n factory.createIdentifier(parentType as string) as any,\n ]),\n ],\n schemaProperties\n );\n\n return schemaType;\n};\n"],"names":["factory","ts","generateAttributePropertySignature","schema","attributes","properties","Object","entries","sort","a","b","compareEnglish","map","attributeName","attribute","attributeToPropertySignature","createPropertySignature","undefined","createIdentifier","createTypeLiteralNode","generatePropertyLiteralDefinitionFactory","key","toTypeLiteral","generateSchemaDefinition","uid","interfaceName","getSchemaInterfaceName","parentType","getSchemaExtendsTypeName","addImport","NAMESPACES","Struct","literalPropertiesDefinitions","filter","isEmpty","attributesProp","schemaProperties","schemaType","createInterfaceDeclaration","createModifier","SyntaxKind","ExportKeyword","createHeritageClause","ExtendsKeyword"],"mappings":";;;;;;;AAcA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;AAEpB;;IAGA,MAAMC,qCAAqC,CAACC,MAAAA,GAAAA;IAC1C,MAAM,EAAEC,UAAU,EAAE,GAAGD,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ACzF;KACCC,MAAM,CAAC,CAACZ,GAAAA,GAAQ,CAACa,QAAQ/B,MAAM,CAACkB,GAAAA,CAAI,CAAA,CACrC;AACCT,KAAAA,GAAG,CAACQ,wCAAAA,CAAyCjB,MAAAA,CAAAA,CAAAA;;AAGhD,IAAA,MAAMgC,iBAAiBjC,kCAAAA,CAAmCC,MAAAA,CAAAA;;AAG1D,IAAA,MAAMiC,gBAAAA,GAAmB;AAAIJ,QAAAA,GAAAA,4BAAAA;AAA8BG,QAAAA;AAAe,KAAA;;IAG1E,MAAME,UAAAA,GAAarC,OAAAA,CAAQsC,0BAA0B,CACnD;AAACtC,QAAAA,OAAAA,CAAQuC,cAAc,CAACtC,EAAAA,CAAGuC,UAAU,CAACC,aAAa;AAAE,KAAA,EACrDzC,OAAAA,CAAQkB,gBAAgB,CAACO,aAAAA,CAAAA,EACzBR,SAAAA,EACA;AACEjB,QAAAA,OAAAA,CAAQ0C,oBAAoB,CAACzC,EAAAA,CAAGuC,UAAU,CAACG,cAAc,EAAE;AACzD3C,YAAAA,OAAAA,CAAQkB,gBAAgB,CAACS,UAAAA;AAC1B,SAAA;KACF,EACDS,gBAAAA,CAAAA;IAGF,OAAOC,UAAAA;AACT;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/generators/common/models/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAajC,eAAO,MAAM,UAAU;;;CAGtB,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE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EAAE,SAAS,CAAC,CAAC;IACtC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,KAAK,MAAM,KAAG,MACJ,CAAC;AAElD,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,KAAG,MAAM,GAAG,IAAI,GAAG,SAcnE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,MAAM,KAAG,MAAM,GAAG,IAQlE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,MAAM,EAAE,SAAQ,EAAE,CAAC,QAAQ,EAAO,KAAG,EAAE,CAAC,iBAE7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,GAAG,KAAG,GA2DzC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,GACvC,YAAY,EAAE,CAAC,oBAAoB,KAClC,MAAM,GAAG,IAUX,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,UAAU,MAAM,KAAG,MACZ,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MACV,CAAC"}
@@ -2,6 +2,7 @@
2
2
 
3
3
  var ts = require('typescript');
4
4
  var _ = require('lodash');
5
+ var compare = require('../compare.js');
5
6
 
6
7
  function _interopNamespace(e) {
7
8
  if (e && e.__esModule) return e;
@@ -84,7 +85,7 @@ const getSchemaModelType = (schema)=>{
84
85
  if (typeof data !== 'object') {
85
86
  throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
86
87
  }
87
- const entries = Object.entries(data).sort((a, b)=>a[0].localeCompare(b[0]));
88
+ const entries = Object.entries(data).sort((a, b)=>compare.compareEnglish(a[0], b[0]));
88
89
  const props = entries.reduce((acc, [key, value])=>{
89
90
  // Handle keys such as content-type-builder & co.
90
91
  const identifier = key.includes('-') ? factory.createStringLiteral(key, true) : factory.createIdentifier(key);
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","localeCompare","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAuBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,YAAAA,CAAWC,WAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,IAAAA;AACT,IAAA;IAEA,OAAO,IAAA;AACT;AAEA;;IAGO,MAAMC,wBAAAA,GAA2B,CAACH,MAAAA,GAAAA;AACvC,IAAA,MAAMI,OAAOL,kBAAAA,CAAmBC,MAAAA,CAAAA;AAEhC,IAAA,IAAII,SAAS,IAAA,EAAM;QACjB,OAAO,IAAA;AACT,IAAA;IAEA,OAAO,CAAA,EAAGb,WAAWC,MAAM,CAAC,CAAC,EAAEI,YAAAA,CAAWQ,IAAAA,CAAAA,CAAgB,MAAM,CAAC;AACnE;AAEA;;AAEC,IACM,MAAMC,WAAAA,GAAc,CAACC,QAAAA,EAAkBC,SAAwB,EAAE,GAAA;AACtE,IAAA,OAAOlB,QAAQmB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AAA,OAAOtB,OAAAA,CAAQ6B,mBAAmB,CAACP,IAAAA,CAAKqB,WAAW,EAAA,CAAA;AACrD,IAAA;IAEA,IAAI,OAAOrB,SAAS,QAAA,EAAU;QAC5B,MAAM,IAAIsB,MAAM,CAAC,gDAAgD,EAAE,OAAOtB,IAAAA,CAAK,CAAC,CAAC,CAAA;AACnF,IAAA;AAEA,IAAA,MAAMuB,UAAUC,MAAAA,CAAOD,OAAO,CAACvB,IAAAA,CAAAA,CAAMyB,IAAI,CAAC,CAACC,CAAAA,EAAGC,CAAAA,GAAMD,CAAC,CAAC,CAAA,CAAE,CAACE,aAAa,CAACD,CAAC,CAAC,CAAA,CAAE,CAAA,CAAA;IAE3E,MAAME,KAAAA,GAAQN,QAAQO,MAAM,CAAQ,CAACC,GAAAA,EAAK,CAACC,KAAKC,KAAAA,CAAM,GAAA;;AAEpD,QAAA,MAAMC,UAAAA,GAAaF,GAAAA,CAAIG,QAAQ,CAAC,GAAA,CAAA,GAC5BzD,OAAAA,CAAQ6B,mBAAmB,CAACyB,GAAAA,EAAK,IAAA,CAAA,GACjCtD,OAAAA,CAAQoB,gBAAgB,CAACkC,GAAAA,CAAAA;QAE7B,OAAO;AACFD,YAAAA,GAAAA,GAAAA;AACHrD,YAAAA,OAAAA,CAAQ0D,yBAAyB,CAC/BnC,SAAAA,EACAiC,UAAAA,EACAjC,SAAAA,EACAF,cAAckC,KAAAA,CAAAA,EACdhC,SAAAA;AAEH,SAAA;AACH,IAAA,CAAA,EAAG,EAAE,CAAA;IAEL,OAAOvB,OAAAA,CAAQ2D,qBAAqB,CAACR,KAAAA,CAAAA;AACvC;AAmBA;;AAEC,IACM,MAAMS,sBAAAA,GAAyB,CAAC3C,QAAAA,GACrC,CAAA,EAAGf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;;;;;;;"}
1
+ {"version":3,"file":"utils.js","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\n\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => compareEnglish(a[0], b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","compareEnglish","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAuBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,YAAAA,CAAWC,WAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,IAAAA;AACT,IAAA;IAEA,OAAO,IAAA;AACT;AAEA;;IAGO,MAAMC,wBAAAA,GAA2B,CAACH,MAAAA,GAAAA;AACvC,IAAA,MAAMI,OAAOL,kBAAAA,CAAmBC,MAAAA,CAAAA;AAEhC,IAAA,IAAII,SAAS,IAAA,EAAM;QACjB,OAAO,IAAA;AACT,IAAA;IAEA,OAAO,CAAA,EAAGb,WAAWC,MAAM,CAAC,CAAC,EAAEI,YAAAA,CAAWQ,IAAAA,CAAAA,CAAgB,MAAM,CAAC;AACnE;AAEA;;AAEC,IACM,MAAMC,WAAAA,GAAc,CAACC,QAAAA,EAAkBC,SAAwB,EAAE,GAAA;AACtE,IAAA,OAAOlB,QAAQmB,uBAAuB,CAACnB,OAAAA,CAAQoB,gBAAgB,CAACH,QAAAA,CAAAA,EAAWC,MAAAA,CAAAA;AAC7E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;IAEA,IAAI,OAAOrB,SAAS,QAAA,EAAU;QAC5B,MAAM,IAAIsB,MAAM,CAAC,gDAAgD,EAAE,OAAOtB,IAAAA,CAAK,CAAC,CAAC,CAAA;AACnF,IAAA;AAEA,IAAA,MAAMuB,UAAUC,MAAAA,CAAOD,OAAO,CAACvB,IAAAA,CAAAA,CAAMyB,IAAI,CAAC,CAACC,CAAAA,EAAGC,CAAAA,GAAMC,uBAAeF,CAAC,CAAC,EAAE,EAAEC,CAAC,CAAC,CAAA,CAAE,CAAA,CAAA;IAE7E,MAAME,KAAAA,GAAQN,QAAQO,MAAM,CAAQ,CAACC,GAAAA,EAAK,CAACC,KAAKC,KAAAA,CAAM,GAAA;;AAEpD,QAAA,MAAMC,UAAAA,GAAaF,GAAAA,CAAIG,QAAQ,CAAC,GAAA,CAAA,GAC5BzD,OAAAA,CAAQ6B,mBAAmB,CAACyB,GAAAA,EAAK,IAAA,CAAA,GACjCtD,OAAAA,CAAQoB,gBAAgB,CAACkC,GAAAA,CAAAA;QAE7B,OAAO;AACFD,YAAAA,GAAAA,GAAAA;AACHrD,YAAAA,OAAAA,CAAQ0D,yBAAyB,CAC/BnC,SAAAA,EACAiC,UAAAA,EACAjC,SAAAA,EACAF,cAAckC,KAAAA,CAAAA,EACdhC,SAAAA;AAEH,SAAA;AACH,IAAA,CAAA,EAAG,EAAE,CAAA;IAEL,OAAOvB,OAAAA,CAAQ2D,qBAAqB,CAACR,KAAAA,CAAAA;AACvC;AAmBA;;AAEC,IACM,MAAMS,sBAAAA,GAAyB,CAAC3C,QAAAA,GACrC,CAAA,EAAGf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;;;;;;;"}
@@ -1,5 +1,6 @@
1
1
  import * as ts from 'typescript';
2
2
  import { upperFirst, camelCase, isString, isNumber, isBoolean, isDate } from 'lodash';
3
+ import { compareEnglish } from '../compare.mjs';
3
4
 
4
5
  const { factory } = ts;
5
6
  const NAMESPACES = {
@@ -62,7 +63,7 @@ const getSchemaModelType = (schema)=>{
62
63
  if (typeof data !== 'object') {
63
64
  throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
64
65
  }
65
- const entries = Object.entries(data).sort((a, b)=>a[0].localeCompare(b[0]));
66
+ const entries = Object.entries(data).sort((a, b)=>compareEnglish(a[0], b[0]));
66
67
  const props = entries.reduce((acc, [key, value])=>{
67
68
  // Handle keys such as content-type-builder & co.
68
69
  const identifier = key.includes('-') ? factory.createStringLiteral(key, true) : factory.createIdentifier(key);
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","localeCompare","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;AAWA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAuBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,UAAAA,CAAWC,SAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,IAAAA;AACT,IAAA;IAEA,OAAO,IAAA;AACT;AAEA;;IAGO,MAAMC,wBAAAA,GAA2B,CAACH,MAAAA,GAAAA;AACvC,IAAA,MAAMI,OAAOL,kBAAAA,CAAmBC,MAAAA,CAAAA;AAEhC,I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OvB,OAAAA,CAAQ2D,qBAAqB,CAACR,KAAAA,CAAAA;AACvC;AAmBA;;AAEC,IACM,MAAMS,sBAAAA,GAAyB,CAAC3C,QAAAA,GACrC,CAAA,EAAGf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;"}
1
+ {"version":3,"file":"utils.mjs","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\n\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => compareEnglish(a[0], b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","compareEnglish","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;;AAaA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAuBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,UAAAA,CAAWC,SAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,IAAAA;AACT,IAAA;IAEA,OAAO,IAAA;AACT;AAEA;;IAGO,MAAMC,wBAAAA,GAA2B,CAACH,MAAAA,GAAAA;AACvC,IAAA,MAAMI,OAAOL,kBAAAA,CAAmBC,MAAAA,CAAAA;AAEhC,IAAA,IAAII,SAAS,IAAA,EAAM;QACjB,OAAO,IAAA;AACT,IAAA;IAEA,OAAO,CAAA,EAAGb,WAAWC,MAAM,CAAC,CAAC,EAAEI,UAAA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Gf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/typescript-utils",
3
- "version": "5.61.0",
3
+ "version": "5.62.0",
4
4
  "description": "Typescript support for Strapi",
5
5
  "keywords": [
6
6
  "strapi",
@@ -60,9 +60,9 @@
60
60
  "@types/jest": "29.5.2",
61
61
  "@types/lodash": "^4.14.191",
62
62
  "@types/node": "22.20.0",
63
- "eslint-config-custom": "5.61.0",
63
+ "eslint-config-custom": "5.62.0",
64
64
  "jest": "29.6.0",
65
- "tsconfig": "5.61.0"
65
+ "tsconfig": "5.62.0"
66
66
  },
67
67
  "engines": {
68
68
  "node": ">=22.0.0 <=26.x.x",