@strapi/typescript-utils
Advanced tools
Comparing version 0.0.0-experimental.32ee0c141ca683711cae2027c4e0efc5daf129ec to 0.0.0-experimental.332a7d5b6b1d23635d7e205657f0ff39ec133c9e
@@ -51,3 +51,3 @@ 'use strict'; | ||
expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.String'); | ||
expect(prop.type.types[0].typeName.escapedText).toBe('Schema.Attribute.String'); | ||
expect(prop.type.types[0].typeArguments).toBeUndefined(); | ||
@@ -64,6 +64,7 @@ }); | ||
expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.Component'); | ||
expect(prop.type.types[0].typeArguments).toHaveLength(1); | ||
expect(prop.type.types[0].typeName.escapedText).toBe('Schema.Attribute.Component'); | ||
expect(prop.type.types[0].typeArguments).toHaveLength(2); | ||
expect(prop.type.types[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(prop.type.types[0].typeArguments[0].text).toBe('default.comp'); | ||
expect(prop.type.types[0].typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword); | ||
}); | ||
@@ -87,3 +88,3 @@ | ||
expect(attributeType.kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(attributeType.typeName.escapedText).toBe('Attribute.Enumeration'); | ||
expect(attributeType.typeName.escapedText).toBe('Schema.Attribute.Enumeration'); | ||
expect(attributeType.typeArguments).toHaveLength(1); | ||
@@ -95,3 +96,3 @@ expect(attributeType.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType); | ||
expect(requiredOptionType.kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(requiredOptionType.typeName.escapedText).toBe('Attribute.DefaultTo'); | ||
expect(requiredOptionType.typeName.escapedText).toBe('Schema.Attribute.DefaultTo'); | ||
expect(requiredOptionType.typeArguments).toHaveLength(1); | ||
@@ -115,18 +116,18 @@ expect(requiredOptionType.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
test.each([ | ||
['string', 'Attribute.String'], | ||
['text', 'Attribute.Text'], | ||
['richtext', 'Attribute.RichText'], | ||
['password', 'Attribute.Password'], | ||
['email', 'Attribute.Email'], | ||
['date', 'Attribute.Date'], | ||
['time', 'Attribute.Time'], | ||
['datetime', 'Attribute.DateTime'], | ||
['timestamp', 'Attribute.Timestamp'], | ||
['integer', 'Attribute.Integer'], | ||
['biginteger', 'Attribute.BigInteger'], | ||
['float', 'Attribute.Float'], | ||
['decimal', 'Attribute.Decimal'], | ||
['boolean', 'Attribute.Boolean'], | ||
['json', 'Attribute.JSON'], | ||
['media', 'Attribute.Media'], | ||
['string', 'Schema.Attribute.String'], | ||
['text', 'Schema.Attribute.Text'], | ||
['richtext', 'Schema.Attribute.RichText'], | ||
['password', 'Schema.Attribute.Password'], | ||
['email', 'Schema.Attribute.Email'], | ||
['date', 'Schema.Attribute.Date'], | ||
['time', 'Schema.Attribute.Time'], | ||
['datetime', 'Schema.Attribute.DateTime'], | ||
['timestamp', 'Schema.Attribute.Timestamp'], | ||
['integer', 'Schema.Attribute.Integer'], | ||
['biginteger', 'Schema.Attribute.BigInteger'], | ||
['float', 'Schema.Attribute.Float'], | ||
['decimal', 'Schema.Attribute.Decimal'], | ||
['boolean', 'Schema.Attribute.Boolean'], | ||
['json', 'Schema.Attribute.JSON'], | ||
['media', 'Schema.Attribute.Media'], | ||
])('Basic %p attribute should map to a %p type', (type, expectedType) => { | ||
@@ -142,3 +143,3 @@ const typeNode = getAttributeType('foo', { type }); | ||
expect(consoleWarnMock).not.toHaveBeenCalled(); | ||
expect(addImport).toHaveBeenCalledWith('Attribute'); | ||
expect(addImport).toHaveBeenCalledWith('Schema'); | ||
}); | ||
@@ -154,5 +155,71 @@ | ||
expect(consoleWarnMock).not.toHaveBeenCalled(); | ||
expect(addImport).toHaveBeenCalledWith('Attribute'); | ||
expect(addImport).toHaveBeenCalledWith('Schema'); | ||
}; | ||
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', () => { | ||
@@ -163,3 +230,3 @@ test('Enumeration with an enum property', () => { | ||
defaultAssertions(typeNode, 'Attribute.Enumeration'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Enumeration'); | ||
@@ -185,3 +252,3 @@ expect(typeNode.typeArguments).toHaveLength(1); | ||
defaultAssertions(typeNode, 'Attribute.UID'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.UID'); | ||
@@ -195,12 +262,9 @@ expect(typeNode.typeArguments).toBeUndefined(); | ||
defaultAssertions(typeNode, 'Attribute.UID'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.UID'); | ||
expect(typeNode.typeArguments).not.toBeUndefined(); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments).toHaveLength(1); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[0].text).toBe('api::bar.bar'); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[1].text).toBe('bar'); | ||
expect(typeNode.typeArguments[0].text).toBe('bar'); | ||
}); | ||
@@ -210,12 +274,11 @@ | ||
const attribute = { type: 'uid', options: { separator: '_' } }; | ||
const typeNode = getAttributeType('foo', attribute); | ||
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo'); | ||
defaultAssertions(typeNode, 'Attribute.UID'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.UID'); | ||
expect(typeNode.typeArguments).toHaveLength(3); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.UndefinedKeyword); | ||
const optionsLiteralNode = typeNode.typeArguments[2]; | ||
const optionsLiteralNode = typeNode.typeArguments[1]; | ||
@@ -238,14 +301,11 @@ expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
defaultAssertions(typeNode, 'Attribute.UID'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.UID'); | ||
expect(typeNode.typeArguments).toHaveLength(3); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[0].text).toBe('api::bar.bar'); | ||
expect(typeNode.typeArguments[0].text).toBe('bar'); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[1].text).toBe('bar'); | ||
const optionsLiteralNode = typeNode.typeArguments[1]; | ||
const optionsLiteralNode = typeNode.typeArguments[2]; | ||
expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
@@ -269,14 +329,11 @@ expect(optionsLiteralNode.members).toHaveLength(1); | ||
defaultAssertions(typeNode, 'Attribute.Relation'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Relation'); | ||
expect(typeNode.typeArguments).toHaveLength(3); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[0].text).toBe('api::foo.foo'); | ||
expect(typeNode.typeArguments[0].text).toBe('oneToOne'); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[1].text).toBe('oneToOne'); | ||
expect(typeNode.typeArguments[2].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[2].text).toBe('api::bar.bar'); | ||
expect(typeNode.typeArguments[1].text).toBe('api::bar.bar'); | ||
}); | ||
@@ -288,11 +345,8 @@ | ||
defaultAssertions(typeNode, 'Attribute.Relation'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Relation'); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments).toHaveLength(1); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[0].text).toBe('api::foo.foo'); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[1].text).toBe('morphMany'); | ||
expect(typeNode.typeArguments[0].text).toBe('morphMany'); | ||
}); | ||
@@ -306,3 +360,3 @@ }); | ||
defaultAssertions(typeNode, 'Attribute.Component'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Component'); | ||
@@ -321,8 +375,10 @@ expect(typeNode.typeArguments).toHaveLength(2); | ||
defaultAssertions(typeNode, 'Attribute.Component'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.Component'); | ||
expect(typeNode.typeArguments).toHaveLength(1); | ||
expect(typeNode.typeArguments).toHaveLength(2); | ||
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(typeNode.typeArguments[0].text).toBe('default.comp'); | ||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword); | ||
}); | ||
@@ -336,3 +392,3 @@ }); | ||
defaultAssertions(typeNode, 'Attribute.DynamicZone'); | ||
defaultAssertions(typeNode, 'Schema.Attribute.DynamicZone'); | ||
@@ -379,3 +435,3 @@ expect(typeNode.typeArguments).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Required'); | ||
}); | ||
@@ -405,3 +461,3 @@ }); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Private'); | ||
}); | ||
@@ -431,3 +487,3 @@ }); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Unique'); | ||
}); | ||
@@ -457,3 +513,3 @@ }); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Configurable'); | ||
}); | ||
@@ -479,3 +535,3 @@ }); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField'); | ||
expect(modifiers[0].typeArguments).toHaveLength(1); | ||
@@ -498,3 +554,3 @@ expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField'); | ||
expect(modifiers[0].typeArguments).toHaveLength(2); | ||
@@ -533,3 +589,3 @@ expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetPluginOptions'); | ||
expect(modifiers[0].typeArguments).toHaveLength(1); | ||
@@ -573,17 +629,25 @@ expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax'); | ||
expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(modifiers[0].typeArguments[0].members).toHaveLength(1); | ||
const [setMinMax] = modifiers; | ||
const { typeArguments } = setMinMax; | ||
expect(typeArguments).toBeDefined(); | ||
expect(typeArguments).toHaveLength(2); | ||
const [definition, typeofMinMax] = typeArguments; | ||
// Min | ||
expect(modifiers[0].typeArguments[0].members[0].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('min'); | ||
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe( | ||
ts.SyntaxKind.NumericLiteral | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('2'); | ||
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(definition.members).toHaveLength(1); | ||
const [min] = definition.members; | ||
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(min.name.escapedText).toBe('min'); | ||
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(min.type.text).toBe('2'); | ||
// Check for number keyword on the second typeArgument | ||
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword); | ||
}); | ||
@@ -598,17 +662,25 @@ | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax'); | ||
expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(modifiers[0].typeArguments[0].members).toHaveLength(1); | ||
const [setMinMax] = modifiers; | ||
const { typeArguments } = setMinMax; | ||
// Min | ||
expect(modifiers[0].typeArguments[0].members[0].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('max'); | ||
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe( | ||
ts.SyntaxKind.NumericLiteral | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('3'); | ||
expect(typeArguments).toBeDefined(); | ||
expect(typeArguments).toHaveLength(2); | ||
const [definition, typeofMinMax] = typeArguments; | ||
// Max | ||
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(definition.members).toHaveLength(1); | ||
const [max] = 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('3'); | ||
// Check for number keyword on the second typeArgument | ||
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword); | ||
}); | ||
@@ -623,27 +695,95 @@ | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax'); | ||
expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(modifiers[0].typeArguments[0].members).toHaveLength(2); | ||
const [setMinMax] = modifiers; | ||
const { typeArguments } = setMinMax; | ||
// Min | ||
expect(modifiers[0].typeArguments[0].members[0].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('min'); | ||
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(typeArguments).toBeDefined(); | ||
expect(typeArguments).toHaveLength(2); | ||
expect(modifiers[0].typeArguments[0].members[1].kind).toBe( | ||
ts.SyntaxKind.PropertyDeclaration | ||
); | ||
expect(modifiers[0].typeArguments[0].members[1].name.escapedText).toBe('max'); | ||
expect(modifiers[0].typeArguments[0].members[1].type.kind).toBe( | ||
ts.SyntaxKind.NumericLiteral | ||
); | ||
expect(modifiers[0].typeArguments[0].members[1].type.text).toBe('12'); | ||
const [definition, typeofMinMax] = typeArguments; | ||
// Min/Max | ||
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(definition.members).toHaveLength(2); | ||
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); | ||
expect(min.name.escapedText).toBe('min'); | ||
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(min.type.text).toBe('4'); | ||
// Check for number keyword on the second typeArgument | ||
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword); | ||
}); | ||
test('Min: "1"', () => { | ||
const attribute = { min: '1' }; | ||
const modifiers = getAttributeModifiers(attribute); | ||
expect(modifiers).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax'); | ||
const [setMinMax] = modifiers; | ||
const { typeArguments } = setMinMax; | ||
expect(typeArguments).toBeDefined(); | ||
expect(typeArguments).toHaveLength(2); | ||
const [definition, typeofMinMax] = typeArguments; | ||
// Min/Max | ||
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(definition.members).toHaveLength(1); | ||
const [min] = definition.members; | ||
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(min.name.escapedText).toBe('min'); | ||
expect(min.type.kind).toBe(ts.SyntaxKind.StringLiteral); | ||
expect(min.type.text).toBe('1'); | ||
// Check for string keyword on the second typeArgument | ||
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword); | ||
}); | ||
test('Min: 0', () => { | ||
const attribute = { min: 0 }; | ||
const modifiers = getAttributeModifiers(attribute); | ||
expect(modifiers).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax'); | ||
const [setMinMax] = modifiers; | ||
const { typeArguments } = setMinMax; | ||
expect(typeArguments).toBeDefined(); | ||
expect(typeArguments).toHaveLength(2); | ||
const [definition, typeofMinMax] = typeArguments; | ||
// Min/Max | ||
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral); | ||
expect(definition.members).toHaveLength(1); | ||
const [min] = definition.members; | ||
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration); | ||
expect(min.name.escapedText).toBe('min'); | ||
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral); | ||
expect(min.type.text).toBe('0'); | ||
// Check for string keyword on the second typeArgument | ||
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword); | ||
}); | ||
}); | ||
@@ -666,3 +806,3 @@ | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength'); | ||
@@ -691,3 +831,3 @@ expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength'); | ||
@@ -716,3 +856,3 @@ expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength'); | ||
@@ -723,20 +863,15 @@ expect(modifiers[0].typeArguments).toHaveLength(1); | ||
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'); | ||
}); | ||
@@ -760,3 +895,3 @@ }); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo'); | ||
@@ -774,3 +909,3 @@ expect(modifiers[0].typeArguments).toHaveLength(1); | ||
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference); | ||
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo'); | ||
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo'); | ||
@@ -788,2 +923,12 @@ expect(modifiers[0].typeArguments).toHaveLength(1); | ||
}); | ||
test('Default: <function>', () => { | ||
const anyFunction = jest.fn(); | ||
const attribute = { default: anyFunction }; | ||
const modifiers = getAttributeModifiers(attribute); | ||
// The default modifier shouldn't be processed when encountering a function | ||
expect(modifiers).toHaveLength(0); | ||
}); | ||
}); | ||
@@ -790,0 +935,0 @@ }); |
@@ -123,5 +123,5 @@ 'use strict'; | ||
test.each([ | ||
[{ modelType: 'component', kind: null }, 'Schema.Component'], | ||
[{ modelType: 'contentType', kind: 'singleType' }, 'Schema.SingleType'], | ||
[{ modelType: 'contentType', kind: 'collectionType' }, 'Schema.CollectionType'], | ||
[{ modelType: 'component', kind: null }, 'Struct.ComponentSchema'], | ||
[{ modelType: 'contentType', kind: 'singleType' }, 'Struct.SingleTypeSchema'], | ||
[{ modelType: 'contentType', kind: 'collectionType' }, 'Struct.CollectionTypeSchema'], | ||
[{ modelType: 'invalidType', kind: 'foo' }, null], | ||
@@ -251,9 +251,9 @@ ])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => { | ||
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'); | ||
}); | ||
@@ -267,16 +267,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); | ||
}); | ||
@@ -290,15 +290,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'); | ||
}); | ||
@@ -305,0 +305,0 @@ |
@@ -6,10 +6,6 @@ 'use strict'; | ||
module.exports = async (srcDir, { watch = false, configOptions = {} } = {}) => { | ||
// TODO: Use the Strapi debug logger instead or don't log at all | ||
console.log(`Starting the compilation for TypeScript files in ${srcDir}`); | ||
const compiler = watch ? compilers.watch : compilers.basic; | ||
module.exports = async (srcDir, { configOptions = {} } = {}) => { | ||
const configPath = getConfigPath(srcDir); | ||
compiler.run(configPath, configOptions); | ||
compilers.basic.run(configPath, configOptions); | ||
}; |
@@ -16,11 +16,19 @@ 'use strict'; | ||
* @param {Object} configOptions.options | ||
* @param {boolean} configOptions.ignoreDiagnostics | ||
*/ | ||
run(tsConfigPath, configOptions = {}) { | ||
const { ignoreDiagnostics = false } = configOptions; | ||
// Parse the tsconfig.json file & resolve the configuration options | ||
const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath); | ||
const compilerOptions = merge(options, configOptions.options); | ||
if (ignoreDiagnostics) { | ||
Object.assign(compilerOptions, { noEmit: false, noEmitOnError: false }); | ||
} | ||
const program = ts.createProgram({ | ||
rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames, | ||
projectReferences, | ||
options: merge(options, configOptions.options), | ||
options: compilerOptions, | ||
}); | ||
@@ -34,8 +42,8 @@ | ||
if (diagnostics.length > 0) { | ||
if (!ignoreDiagnostics && diagnostics.length > 0) { | ||
reportDiagnostics(diagnostics); | ||
} | ||
// If the compilation failed, exit early | ||
if (emitResults.emitSkipped) { | ||
// If the compilation failed and diagnostics are not ignored, exit early | ||
if (!ignoreDiagnostics && emitResults.emitSkipped) { | ||
process.exit(1); | ||
@@ -42,0 +50,0 @@ } |
'use strict'; | ||
const basic = require('./basic'); | ||
const watch = require('./watch'); | ||
module.exports = { | ||
basic, | ||
watch, | ||
}; |
@@ -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 [ |
'use strict'; | ||
const { factory } = require('typescript'); | ||
const ts = require('typescript'); | ||
const _ = require('lodash/fp'); | ||
@@ -10,2 +10,4 @@ | ||
const { factory } = ts; | ||
/** | ||
@@ -30,4 +32,4 @@ * Create the base type node for a given attribute | ||
// Make sure the attribute namespace is imported | ||
addImport(NAMESPACES.attribute); | ||
// Make sure the schema namespace is imported | ||
addImport(NAMESPACES.Schema); | ||
@@ -105,10 +107,35 @@ return getTypeNode(attributeType, typeParams); | ||
// Min / Max | ||
// TODO: Always provide a second type argument for min/max (ie: resolve the attribute scalar type with a `GetAttributeType<${mappers[attribute][0]}>` (useful for biginter (string values))) | ||
if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) { | ||
const minMaxProperties = _.pick(['min', 'max'], attribute); | ||
const { min, max } = minMaxProperties; | ||
const typeofMin = typeof min; | ||
const typeofMax = typeof max; | ||
// Throws error if min/max exist but have different types to prevent unexpected behavior | ||
if (min !== undefined && max !== undefined && typeofMin !== typeofMax) { | ||
throw new Error('typeof min/max values mismatch'); | ||
} | ||
let typeKeyword; | ||
// use 'string' | ||
if (typeofMin === 'string' || typeofMax === 'string') { | ||
typeKeyword = ts.SyntaxKind.StringKeyword; | ||
} | ||
// use 'number' | ||
else if (typeofMin === 'number' || typeofMax === 'number') { | ||
typeKeyword = ts.SyntaxKind.NumberKeyword; | ||
} | ||
// invalid type | ||
else { | ||
throw new Error( | ||
`Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }` | ||
); | ||
} | ||
modifiers.push( | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(withAttributeNamespace('SetMinMax')), | ||
[toTypeLiteral(minMaxProperties)] | ||
[toTypeLiteral(minMaxProperties), factory.createKeywordTypeNode(typeKeyword)] | ||
) | ||
@@ -130,4 +157,4 @@ ); | ||
// Default | ||
if (!_.isNil(attribute.default)) { | ||
// Default (ignore if default is a function) | ||
if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) { | ||
const defaultLiteral = toTypeLiteral(attribute.default); | ||
@@ -175,4 +202,2 @@ | ||
module.exports.mappers = mappers; | ||
module.exports.getAttributeType = getAttributeType; | ||
module.exports.getAttributeModifiers = getAttributeModifiers; | ||
Object.assign(module.exports, { mappers, getAttributeModifiers, getAttributeType }); |
@@ -50,3 +50,3 @@ 'use strict'; | ||
}, | ||
uid({ attribute, uid }) { | ||
uid({ attribute }) { | ||
const { targetField, options } = attribute; | ||
@@ -62,14 +62,11 @@ | ||
// If the targetField property is defined, then reference it, | ||
// otherwise, put `undefined` keyword type nodes as placeholders | ||
const targetFieldParams = _.isUndefined(targetField) | ||
? [ | ||
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), | ||
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), | ||
] | ||
: [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)]; | ||
// otherwise, put `undefined` keyword type node as placeholder | ||
const targetFieldParam = _.isUndefined(targetField) | ||
? factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) | ||
: factory.createStringLiteral(targetField); | ||
params.push(...targetFieldParams); | ||
params.push(targetFieldParam); | ||
// If the options property is defined, transform it to | ||
// a type literral node and add it to the params list | ||
// a type literal node and add it to the params list | ||
if (_.isObject(options)) { | ||
@@ -92,6 +89,27 @@ params.push(toTypeLiteral(options)); | ||
}, | ||
media() { | ||
return [withAttributeNamespace('Media')]; | ||
blocks() { | ||
return [withAttributeNamespace('Blocks')]; | ||
}, | ||
relation({ uid, attribute }) { | ||
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]; | ||
}, | ||
relation({ attribute }) { | ||
const { relation, target } = attribute; | ||
@@ -102,6 +120,3 @@ | ||
if (isMorphRelation) { | ||
return [ | ||
withAttributeNamespace('Relation'), | ||
[factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)], | ||
]; | ||
return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]]; | ||
} | ||
@@ -111,7 +126,3 @@ | ||
withAttributeNamespace('Relation'), | ||
[ | ||
factory.createStringLiteral(uid, true), | ||
factory.createStringLiteral(relation, true), | ||
factory.createStringLiteral(target, true), | ||
], | ||
[factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)], | ||
]; | ||
@@ -125,2 +136,4 @@ }, | ||
params.push(factory.createTrue()); | ||
} else { | ||
params.push(factory.createFalse()); | ||
} | ||
@@ -127,0 +140,0 @@ |
@@ -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); | ||
}); | ||
@@ -60,4 +62,4 @@ return factory.createPropertySignature( | ||
// Make sure the Schema namespace is imported | ||
addImport(NAMESPACES.schema); | ||
// Make sure the Struct namespace is imported | ||
addImport(NAMESPACES.Struct); | ||
@@ -64,0 +66,0 @@ // Properties whose values can be mapped to a literal type expression |
@@ -21,4 +21,4 @@ 'use strict'; | ||
const NAMESPACES = { | ||
schema: 'Schema', | ||
attribute: 'Attribute', | ||
Struct: 'Struct', | ||
Schema: 'Schema', | ||
}; | ||
@@ -54,3 +54,3 @@ | ||
* @param {object} schema | ||
* @returns {string} | ||
* @returns {string|null} | ||
*/ | ||
@@ -64,3 +64,3 @@ const getSchemaExtendsTypeName = (schema) => { | ||
return `${NAMESPACES.schema}.${upperFirst(base)}`; | ||
return `${NAMESPACES.Struct}.${upperFirst(base)}Schema`; | ||
}; | ||
@@ -117,3 +117,3 @@ | ||
const entries = Object.entries(data); | ||
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0])); | ||
@@ -152,3 +152,3 @@ const props = entries.reduce((acc, [key, value]) => { | ||
/** | ||
* Add the attribute namespace before the typename | ||
* Add the Schema.Attribute namespace before the typename | ||
* | ||
@@ -158,3 +158,3 @@ * @param {string} typeName | ||
*/ | ||
const withAttributeNamespace = (typeName) => `${NAMESPACES.attribute}.${typeName}`; | ||
const withAttributeNamespace = (typeName) => `${NAMESPACES.Schema}.Attribute.${typeName}`; | ||
@@ -161,0 +161,0 @@ /** |
'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) => { | ||
@@ -51,3 +67,3 @@ acc.push( | ||
// Global | ||
generateSharedExtensionDefinition('Components', componentsDefinitions), | ||
generateSharedExtensionDefinition('ComponentSchemas', componentsDefinitions), | ||
]; | ||
@@ -54,0 +70,0 @@ |
'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) => { | ||
@@ -51,3 +67,3 @@ acc.push( | ||
// Global | ||
generateSharedExtensionDefinition('ContentTypes', contentTypesDefinitions), | ||
generateSharedExtensionDefinition('ContentTypeSchemas', contentTypesDefinitions), | ||
]; | ||
@@ -54,0 +70,0 @@ |
@@ -103,3 +103,3 @@ 'use strict'; | ||
const outPath = await saveDefinitionToFileSystem(registryPwd, filename, report.output); | ||
const relativeOutPath = path.relative(__dirname, outPath); | ||
const relativeOutPath = path.relative(process.cwd(), outPath); | ||
@@ -106,0 +106,0 @@ artifactFsTimer.end(); |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const ts = require('typescript'); | ||
const prettier = require('prettier'); | ||
const fse = require('fs-extra'); | ||
@@ -13,2 +12,5 @@ const chalk = require('chalk'); | ||
const MODULE_DECLARATION = '@strapi/strapi'; | ||
const PUBLIC_NAMESPACE = 'Public'; | ||
/** | ||
@@ -63,2 +65,5 @@ * Aggregate the given TypeScript nodes into a single string | ||
const format = async (content) => { | ||
// eslint-disable-next-line node/no-unsupported-features/es-syntax | ||
const prettier = await import('prettier'); // ESM-only | ||
const configFile = await prettier.resolveConfigFile(); | ||
@@ -98,7 +103,7 @@ const config = configFile | ||
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)], | ||
factory.createStringLiteral('@strapi/strapi', true), | ||
factory.createStringLiteral(MODULE_DECLARATION, true), | ||
factory.createModuleBlock([ | ||
factory.createModuleDeclaration( | ||
[factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
factory.createIdentifier('Shared'), | ||
factory.createIdentifier(PUBLIC_NAMESPACE), | ||
factory.createModuleBlock( | ||
@@ -105,0 +110,0 @@ properties.length > 0 |
@@ -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.32ee0c141ca683711cae2027c4e0efc5daf129ec", | ||
"version": "0.0.0-experimental.332a7d5b6b1d23635d7e205657f0ff39ec133c9e", | ||
"description": "Typescript support for Strapi", | ||
"keywords": [ | ||
"strapi", | ||
"generators" | ||
], | ||
"repository": { | ||
@@ -10,6 +14,2 @@ "type": "git", | ||
}, | ||
"keywords": [ | ||
"strapi", | ||
"generators" | ||
], | ||
"license": "SEE LICENSE IN LICENSE", | ||
@@ -40,13 +40,15 @@ "author": { | ||
"chalk": "4.1.2", | ||
"cli-table3": "0.6.2", | ||
"fs-extra": "10.0.0", | ||
"cli-table3": "0.6.5", | ||
"fs-extra": "11.2.0", | ||
"lodash": "4.17.21", | ||
"prettier": "2.8.4", | ||
"typescript": "5.1.3" | ||
"prettier": "3.3.3", | ||
"typescript": "5.3.2" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "11.0.4" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0 <=20.x.x", | ||
"node": ">=18.0.0 <=22.x.x", | ||
"npm": ">=6.0.0" | ||
}, | ||
"gitHead": "32ee0c141ca683711cae2027c4e0efc5daf129ec" | ||
} | ||
} |
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"moduleResolution": "node", | ||
"moduleResolution": "Bundler", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noImplicitAny": false, | ||
"strict": true, | ||
"allowJs": true, | ||
"sourceMap": true, | ||
"incremental": true, | ||
"skipLibCheck": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx" | ||
} | ||
} |
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"lib": ["ES2020"], | ||
"target": "ES2019", | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"strict": false, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"lib": ["ES2020"], | ||
"target": "ES2019", | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true, | ||
"noEmitOnError": true | ||
} | ||
} | ||
"strict": false, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"tsBuildInfoFile": "./.tsbuildinfo", | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true, | ||
"noEmitOnError": true, | ||
"noImplicitThis": 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
2158
3
1
36
1
80
+ Addedcli-table3@0.6.5(transitive)
+ Addedfs-extra@11.2.0(transitive)
+ Addedprettier@3.3.3(transitive)
+ Addedtypescript@5.3.2(transitive)
- Removedcli-table3@0.6.2(transitive)
- Removedfs-extra@10.0.0(transitive)
- Removedprettier@2.8.4(transitive)
- Removedtypescript@5.1.3(transitive)
Updatedcli-table3@0.6.5
Updatedfs-extra@11.2.0
Updatedprettier@3.3.3
Updatedtypescript@5.3.2