@strapi/typescript-utils 0.0.0-next.c5f067b5650921187770124e9b6c8186e805e242 → 0.0.0-next.c80d4d9b205f7844215758ba333894819685e6c1

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.
@@ -148,6 +148,72 @@ describe('Attributes', () => {
148
148
  expect(addImport).toHaveBeenCalledWith('Attribute');
149
149
  };
150
150
 
151
+ describe('Media', () => {
152
+ test('Media with multiple and with no allowedTypes', () => {
153
+ const attribute = { type: 'media', multiple: true };
154
+ const typeNode = getAttributeType('foo', attribute);
155
+
156
+ defaultAssertions(typeNode, 'Attribute.Media');
157
+
158
+ expect(typeNode.typeArguments).toHaveLength(2);
159
+
160
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
161
+
162
+ expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
163
+ });
164
+
165
+ test('Media without multiple with allowedTypes', () => {
166
+ const attribute = { type: 'media', allowedTypes: ['images', 'videos'] };
167
+ const typeNode = getAttributeType('foo', attribute);
168
+
169
+ defaultAssertions(typeNode, 'Attribute.Media');
170
+
171
+ expect(typeNode.typeArguments).toHaveLength(1);
172
+
173
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
174
+
175
+ const unionTypes = typeNode.typeArguments[0].types;
176
+
177
+ attribute.allowedTypes.forEach((value, index) => {
178
+ const element = unionTypes[index];
179
+
180
+ expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
181
+ expect(element.text).toBe(value);
182
+ });
183
+ });
184
+
185
+ test('Media with multiple and with allowedTypes', () => {
186
+ const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] };
187
+ const typeNode = getAttributeType('foo', attribute);
188
+
189
+ defaultAssertions(typeNode, 'Attribute.Media');
190
+
191
+ expect(typeNode.typeArguments).toHaveLength(2);
192
+
193
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
194
+
195
+ const unionTypes = typeNode.typeArguments[0].types;
196
+
197
+ attribute.allowedTypes.forEach((value, index) => {
198
+ const element = unionTypes[index];
199
+
200
+ expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
201
+ expect(element.text).toBe(value);
202
+ });
203
+
204
+ expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
205
+ });
206
+
207
+ test('Media without multiple and with no allowedTypes', () => {
208
+ const attribute = { type: 'media' };
209
+ const typeNode = getAttributeType('foo', attribute);
210
+
211
+ defaultAssertions(typeNode, 'Attribute.Media');
212
+
213
+ expect(typeNode.typeArguments).toBeUndefined();
214
+ });
215
+ });
216
+
151
217
  describe('Enumeration', () => {
152
218
  test('Enumeration with an enum property', () => {
153
219
  const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
@@ -671,6 +737,38 @@ describe('Attributes', () => {
671
737
  // Check for string keyword on the second typeArgument
672
738
  expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword);
673
739
  });
740
+
741
+ test('Min: 0', () => {
742
+ const attribute = { min: 0 };
743
+ const modifiers = getAttributeModifiers(attribute);
744
+
745
+ expect(modifiers).toHaveLength(1);
746
+
747
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
748
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
749
+
750
+ const [setMinMax] = modifiers;
751
+ const { typeArguments } = setMinMax;
752
+
753
+ expect(typeArguments).toBeDefined();
754
+ expect(typeArguments).toHaveLength(2);
755
+
756
+ const [definition, typeofMinMax] = typeArguments;
757
+
758
+ // Min/Max
759
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
760
+ expect(definition.members).toHaveLength(1);
761
+
762
+ const [min] = definition.members;
763
+
764
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
765
+ expect(min.name.escapedText).toBe('min');
766
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
767
+ expect(min.type.text).toBe('0');
768
+
769
+ // Check for string keyword on the second typeArgument
770
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
771
+ });
674
772
  });
675
773
 
676
774
  describe('MinLength / MaxLength', () => {
@@ -114,21 +114,21 @@ const getAttributeModifiers = (attribute) => {
114
114
  throw new Error('typeof min/max values mismatch');
115
115
  }
116
116
 
117
- const typeofMinMax = (max && typeofMax) ?? (min && typeofMin);
118
117
  let typeKeyword;
119
118
 
120
- // Determines type keyword (string/number) based on min/max options, throws error for invalid types
121
- switch (typeofMinMax) {
122
- case 'string':
123
- typeKeyword = ts.SyntaxKind.StringKeyword;
124
- break;
125
- case 'number':
126
- typeKeyword = ts.SyntaxKind.NumberKeyword;
127
- break;
128
- default:
129
- throw new Error(
130
- `Invalid data type for min/max options. Must be string or number, but found ${typeofMinMax}`
131
- );
119
+ // use 'string'
120
+ if (typeofMin === 'string' || typeofMax === 'string') {
121
+ typeKeyword = ts.SyntaxKind.StringKeyword;
122
+ }
123
+ // use 'number'
124
+ else if (typeofMin === 'number' || typeofMax === 'number') {
125
+ typeKeyword = ts.SyntaxKind.NumberKeyword;
126
+ }
127
+ // invalid type
128
+ else {
129
+ throw new Error(
130
+ `Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`
131
+ );
132
132
  }
133
133
 
134
134
  modifiers.push(
@@ -90,8 +90,26 @@ module.exports = {
90
90
  blocks() {
91
91
  return [withAttributeNamespace('Blocks')];
92
92
  },
93
- media() {
94
- return [withAttributeNamespace('Media')];
93
+ media({ attribute }) {
94
+ const { allowedTypes, multiple } = attribute;
95
+
96
+ const params = [];
97
+
98
+ const typesParam = allowedTypes
99
+ ? factory.createUnionTypeNode(
100
+ allowedTypes.map((allowedType) => factory.createStringLiteral(allowedType))
101
+ )
102
+ : factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
103
+
104
+ if (allowedTypes || multiple) {
105
+ params.push(typesParam);
106
+ }
107
+
108
+ if (multiple) {
109
+ params.push(factory.createTrue());
110
+ }
111
+
112
+ return [withAttributeNamespace('Media'), params];
95
113
  },
96
114
  relation({ uid, attribute }) {
97
115
  const { relation, target } = attribute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/typescript-utils",
3
- "version": "0.0.0-next.c5f067b5650921187770124e9b6c8186e805e242",
3
+ "version": "0.0.0-next.c80d4d9b205f7844215758ba333894819685e6c1",
4
4
  "description": "Typescript support for Strapi",
5
5
  "keywords": [
6
6
  "strapi",
@@ -46,5 +46,5 @@
46
46
  "node": ">=18.0.0 <=20.x.x",
47
47
  "npm": ">=6.0.0"
48
48
  },
49
- "gitHead": "c5f067b5650921187770124e9b6c8186e805e242"
49
+ "gitHead": "c80d4d9b205f7844215758ba333894819685e6c1"
50
50
  }