@strapi/typescript-utils
Advanced tools
Comparing version
@@ -152,2 +152,68 @@ 'use strict'; | ||
describe('Media', () => { | ||
test('Media with multiple and with no allowedTypes', () => { | ||
const attribute = { type: 'media', multiple: true }; | ||
const typeNode = getAttributeType('foo', attribute); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Media'); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
}); | ||
test('Media without multiple with allowedTypes', () => { | ||
const attribute = { type: 'media', allowedTypes: ['images', 'videos'] }; | ||
const typeNode = getAttributeType('foo', attribute); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Media'); | ||
expect(typeNode.typeArguments).toHaveLength(1); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType); | ||
const unionTypes = typeNode.typeArguments[0].types; | ||
attribute.allowedTypes.forEach((value, index) => { | ||
const element = unionTypes[index]; | ||
expect(element.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(element.text).toBe(value); | ||
}); | ||
}); | ||
test('Media with multiple and with allowedTypes', () => { | ||
const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] }; | ||
const typeNode = getAttributeType('foo', attribute); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Media'); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType); | ||
const unionTypes = typeNode.typeArguments[0].types; | ||
attribute.allowedTypes.forEach((value, index) => { | ||
const element = unionTypes[index]; | ||
expect(element.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(element.text).toBe(value); | ||
}); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
}); | ||
test('Media without multiple and with no allowedTypes', () => { | ||
const attribute = { type: 'media' }; | ||
const typeNode = getAttributeType('foo', attribute); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Media'); | ||
expect(typeNode.typeArguments).toBeUndefined(); | ||
}); | ||
}); | ||
describe('Enumeration', () => { | ||
@@ -618,4 +684,9 @@ test('Enumeration with an enum property', () => { | ||
const [min, max] = definition.members; | ||
const [max, min] = definition.members; | ||
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(max.name.escapedText).toBe('max'); | ||
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(max.type.text).toBe('12'); | ||
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
@@ -626,7 +697,2 @@ expect(min.name.escapedText).toBe('min'); | ||
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(max.name.escapedText).toBe('max'); | ||
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(max.type.text).toBe('12'); | ||
// Check for number keyword on the second typeArgument | ||
@@ -770,20 +836,15 @@ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword); | ||
const [maxLength, minLength] = modifiers[0].typeArguments[0].members; | ||
// Max | ||
expect(maxLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(maxLength.name.escapedText).toBe('maxLength'); | ||
expect(maxLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(maxLength.type.text).toBe('12'); | ||
// Min | ||
expect(modifiers[0].typeArguments[0].members[0].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('minLength'); | ||
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe( | ||
ts.SyntaxKind.NumericLiteral | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('4'); | ||
expect(modifiers[0].typeArguments[0].members[1].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[1].name.escapedText).toBe('maxLength'); | ||
expect(modifiers[0].typeArguments[0].members[1].type.kind).toBe( | ||
ts.SyntaxKind.NumericLiteral | ||
); | ||
expect(modifiers[0].typeArguments[0].members[1].type.text).toBe('12'); | ||
expect(minLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(minLength.name.escapedText).toBe('minLength'); | ||
expect(minLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(minLength.type.text).toBe('4'); | ||
}); | ||
@@ -790,0 +851,0 @@ }); |
@@ -250,9 +250,9 @@ 'use strict'; | ||
expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(objectNode.members[0].name.escapedText).toBe('foo'); | ||
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(objectNode.members[0].type.text).toBe('bar'); | ||
expect(objectNode.members[0].name.escapedText).toBe('bar'); | ||
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(objectNode.members[1].name.escapedText).toBe('bar'); | ||
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
expect(objectNode.members[1].name.escapedText).toBe('foo'); | ||
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(objectNode.members[1].type.text).toBe('bar'); | ||
}); | ||
@@ -266,16 +266,16 @@ | ||
const [firstMember, secondMember] = node.members; | ||
const [barMember, fooMember] = node.members; | ||
expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(firstMember.name.escapedText).toBe('foo'); | ||
expect(firstMember.type.kind).toBe(ts.SyntaxKind.TupleType); | ||
expect(firstMember.type.elements).toHaveLength(3); | ||
expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken); | ||
expect(barMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(barMember.name.escapedText).toBe('bar'); | ||
expect(barMember.type.kind).toBe(ts.SyntaxKind.LiteralType); | ||
expect(barMember.type.literal).toBe(ts.SyntaxKind.NullKeyword); | ||
expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(secondMember.name.escapedText).toBe('bar'); | ||
expect(secondMember.type.kind).toBe(ts.SyntaxKind.LiteralType); | ||
expect(secondMember.type.literal).toBe(ts.SyntaxKind.NullKeyword); | ||
expect(fooMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(fooMember.name.escapedText).toBe('foo'); | ||
expect(fooMember.type.kind).toBe(ts.SyntaxKind.TupleType); | ||
expect(fooMember.type.elements).toHaveLength(3); | ||
expect(fooMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(fooMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword); | ||
expect(fooMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken); | ||
}); | ||
@@ -289,15 +289,15 @@ | ||
const [firstMember, secondMember] = node.members; | ||
const [fooBar, fooDashBar] = node.members; | ||
expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(firstMember.name.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(firstMember.name.text).toBe('foo-bar'); | ||
expect(firstMember.type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(firstMember.type.text).toBe('foobar'); | ||
expect(fooBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(fooBar.name.kind).toBe(ts.SyntaxKind.Identifier); | ||
expect(fooBar.name.escapedText).toBe('foo'); | ||
expect(fooBar.type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(fooBar.type.text).toBe('bar'); | ||
expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(secondMember.name.kind).toBe(ts.SyntaxKind.Identifier); | ||
expect(secondMember.name.escapedText).toBe('foo'); | ||
expect(secondMember.type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(secondMember.type.text).toBe('bar'); | ||
expect(fooDashBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(fooDashBar.name.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(fooDashBar.name.text).toBe('foo-bar'); | ||
expect(fooDashBar.type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(fooDashBar.type.text).toBe('foobar'); | ||
}); | ||
@@ -304,0 +304,0 @@ |
@@ -21,5 +21,5 @@ 'use strict'; | ||
generateImportDefinition() { | ||
const formattedImports = imports.map((key) => | ||
factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)) | ||
); | ||
const formattedImports = imports | ||
.sort() | ||
.map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))); | ||
@@ -26,0 +26,0 @@ return [ |
@@ -90,4 +90,22 @@ 'use strict'; | ||
}, | ||
media() { | ||
return [withAttributeNamespace('Media')]; | ||
media({ attribute }) { | ||
const { allowedTypes, multiple } = attribute; | ||
const params = []; | ||
const typesParam = allowedTypes | ||
? factory.createUnionTypeNode( | ||
allowedTypes.map((allowedType) => factory.createStringLiteral(allowedType)) | ||
) | ||
: factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword); | ||
if (allowedTypes || multiple) { | ||
params.push(typesParam); | ||
} | ||
if (multiple) { | ||
params.push(factory.createTrue()); | ||
} | ||
return [withAttributeNamespace('Media'), params]; | ||
}, | ||
@@ -94,0 +112,0 @@ relation({ attribute }) { |
@@ -25,5 +25,7 @@ 'use strict'; | ||
const properties = Object.entries(attributes).map(([attributeName, attribute]) => { | ||
return attributeToPropertySignature(schema, attributeName, attribute); | ||
}); | ||
const properties = Object.entries(attributes) | ||
.sort((a, b) => a[0].localeCompare(b[0])) | ||
.map(([attributeName, attribute]) => { | ||
return attributeToPropertySignature(schema, attributeName, attribute); | ||
}); | ||
@@ -30,0 +32,0 @@ return factory.createPropertySignature( |
@@ -114,3 +114,3 @@ 'use strict'; | ||
const entries = Object.entries(data); | ||
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0])); | ||
@@ -117,0 +117,0 @@ const props = entries.reduce((acc, [key, value]) => { |
'use strict'; | ||
const { factory } = require('typescript'); | ||
const { pipe, values, sortBy, map } = require('lodash/fp'); | ||
@@ -8,2 +9,7 @@ const { models } = require('../common'); | ||
const NO_COMPONENT_PLACEHOLDER_COMMENT = `/* | ||
* The app doesn't have any components yet. | ||
*/ | ||
`; | ||
/** | ||
@@ -22,7 +28,17 @@ * Generate type definitions for Strapi Components | ||
const componentsDefinitions = Object.values(components).map((contentType) => ({ | ||
uid: contentType.uid, | ||
definition: models.schema.generateSchemaDefinition(contentType), | ||
})); | ||
const componentsDefinitions = pipe( | ||
values, | ||
sortBy('uid'), | ||
map((component) => ({ | ||
uid: component.uid, | ||
definition: models.schema.generateSchemaDefinition(component), | ||
})) | ||
)(components); | ||
options.logger.debug(`Found ${componentsDefinitions.length} components.`); | ||
if (componentsDefinitions.length === 0) { | ||
return { output: NO_COMPONENT_PLACEHOLDER_COMMENT, stats: {} }; | ||
} | ||
const formattedSchemasDefinitions = componentsDefinitions.reduce((acc, def) => { | ||
@@ -29,0 +45,0 @@ acc.push( |
'use strict'; | ||
const { factory } = require('typescript'); | ||
const { values, pipe, map, sortBy } = require('lodash/fp'); | ||
@@ -8,2 +9,7 @@ const { models } = require('../common'); | ||
const NO_CONTENT_TYPE_PLACEHOLDER_COMMENT = `/* | ||
* The app doesn't have any content-types yet. | ||
*/ | ||
`; | ||
/** | ||
@@ -22,7 +28,17 @@ * Generate type definitions for Strapi Content-Types | ||
const contentTypesDefinitions = Object.values(contentTypes).map((contentType) => ({ | ||
uid: contentType.uid, | ||
definition: models.schema.generateSchemaDefinition(contentType), | ||
})); | ||
const contentTypesDefinitions = pipe( | ||
values, | ||
sortBy('uid'), | ||
map((contentType) => ({ | ||
uid: contentType.uid, | ||
definition: models.schema.generateSchemaDefinition(contentType), | ||
})) | ||
)(contentTypes); | ||
options.logger.debug(`Found ${contentTypesDefinitions.length} content-types.`); | ||
if (contentTypesDefinitions.length === 0) { | ||
return { output: NO_CONTENT_TYPE_PLACEHOLDER_COMMENT, stats: {} }; | ||
} | ||
const formattedSchemasDefinitions = contentTypesDefinitions.reduce((acc, def) => { | ||
@@ -29,0 +45,0 @@ acc.push( |
@@ -5,3 +5,2 @@ 'use strict'; | ||
const compilers = require('./compilers'); | ||
const admin = require('./admin'); | ||
const utils = require('./utils'); | ||
@@ -13,6 +12,4 @@ const generators = require('./generators'); | ||
compilers, | ||
admin, | ||
generators, | ||
...utils, | ||
}; |
@@ -10,2 +10,3 @@ 'use strict'; | ||
const resolveOutDir = require('./resolve-outdir'); | ||
const resolveOutDirSync = require('./resolve-outdir-sync'); | ||
@@ -20,2 +21,3 @@ module.exports = { | ||
resolveOutDir, | ||
resolveOutDirSync, | ||
}; |
{ | ||
"name": "@strapi/typescript-utils", | ||
"version": "0.0.0-experimental.74c69aeafc770d59d5b3d5d37cd249934ef395ba", | ||
"version": "0.0.0-experimental.74ebdf402d90857bc7282394855d43aad6d283e6", | ||
"description": "Typescript support for Strapi", | ||
@@ -39,13 +39,15 @@ "keywords": [ | ||
"chalk": "4.1.2", | ||
"cli-table3": "0.6.2", | ||
"fs-extra": "10.1.0", | ||
"cli-table3": "0.6.5", | ||
"fs-extra": "11.2.0", | ||
"lodash": "4.17.21", | ||
"prettier": "3.2.5", | ||
"typescript": "5.3.2" | ||
"prettier": "3.3.3", | ||
"typescript": "5.4.4" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "11.0.4" | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0 <=20.x.x", | ||
"node": ">=18.0.0 <=22.x.x", | ||
"npm": ">=6.0.0" | ||
}, | ||
"gitHead": "74c69aeafc770d59d5b3d5d37cd249934ef395ba" | ||
} | ||
} |
@@ -14,2 +14,3 @@ { | ||
"tsBuildInfoFile": "./.tsbuildinfo", | ||
"incremental": true, | ||
@@ -16,0 +17,0 @@ "esModuleInterop": true, |
Sorry, the diff of this file is not supported yet
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
89968
5.91%2158
3.55%4
-20%1
Infinity%36
-2.7%2
100%80
-20%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated
Updated