@strapi/typescript-utils 4.20.4 → 5.0.0-alpha.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.
|
@@ -61,9 +61,10 @@ describe('Attributes', () => {
|
|
|
61
61
|
expect(prop.type.types).toHaveLength(1);
|
|
62
62
|
expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
63
63
|
expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.Component');
|
|
64
|
-
expect(prop.type.types[0].typeArguments).toHaveLength(
|
|
64
|
+
expect(prop.type.types[0].typeArguments).toHaveLength(2);
|
|
65
65
|
expect(prop.type.types[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
66
66
|
expect(prop.type.types[0].typeArguments[0].text).toBe('default.comp');
|
|
67
|
+
expect(prop.type.types[0].typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword);
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
test('Attribute with type argument and options', () => {
|
|
@@ -186,27 +187,23 @@ describe('Attributes', () => {
|
|
|
186
187
|
defaultAssertions(typeNode, 'Attribute.UID');
|
|
187
188
|
|
|
188
189
|
expect(typeNode.typeArguments).not.toBeUndefined();
|
|
189
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
190
|
+
expect(typeNode.typeArguments).toHaveLength(1);
|
|
190
191
|
|
|
191
192
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
192
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
193
|
-
|
|
194
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
195
|
-
expect(typeNode.typeArguments[1].text).toBe('bar');
|
|
193
|
+
expect(typeNode.typeArguments[0].text).toBe('bar');
|
|
196
194
|
});
|
|
197
195
|
|
|
198
196
|
test('UID with partial options and no target field', () => {
|
|
199
197
|
const attribute = { type: 'uid', options: { separator: '_' } };
|
|
200
|
-
const typeNode = getAttributeType('foo', attribute);
|
|
198
|
+
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
201
199
|
|
|
202
200
|
defaultAssertions(typeNode, 'Attribute.UID');
|
|
203
201
|
|
|
204
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
202
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
205
203
|
|
|
206
204
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
207
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
208
205
|
|
|
209
|
-
const optionsLiteralNode = typeNode.typeArguments[
|
|
206
|
+
const optionsLiteralNode = typeNode.typeArguments[1];
|
|
210
207
|
|
|
211
208
|
expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
212
209
|
expect(optionsLiteralNode.members).toHaveLength(1);
|
|
@@ -226,15 +223,12 @@ describe('Attributes', () => {
|
|
|
226
223
|
|
|
227
224
|
defaultAssertions(typeNode, 'Attribute.UID');
|
|
228
225
|
|
|
229
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
226
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
230
227
|
|
|
231
228
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
232
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
233
|
-
|
|
234
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
235
|
-
expect(typeNode.typeArguments[1].text).toBe('bar');
|
|
229
|
+
expect(typeNode.typeArguments[0].text).toBe('bar');
|
|
236
230
|
|
|
237
|
-
const optionsLiteralNode = typeNode.typeArguments[
|
|
231
|
+
const optionsLiteralNode = typeNode.typeArguments[1];
|
|
238
232
|
|
|
239
233
|
expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
240
234
|
expect(optionsLiteralNode.members).toHaveLength(1);
|
|
@@ -256,16 +250,13 @@ describe('Attributes', () => {
|
|
|
256
250
|
|
|
257
251
|
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
258
252
|
|
|
259
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
253
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
260
254
|
|
|
261
255
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
262
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
256
|
+
expect(typeNode.typeArguments[0].text).toBe('oneToOne');
|
|
263
257
|
|
|
264
258
|
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
265
|
-
expect(typeNode.typeArguments[1].text).toBe('
|
|
266
|
-
|
|
267
|
-
expect(typeNode.typeArguments[2].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
268
|
-
expect(typeNode.typeArguments[2].text).toBe('api::bar.bar');
|
|
259
|
+
expect(typeNode.typeArguments[1].text).toBe('api::bar.bar');
|
|
269
260
|
});
|
|
270
261
|
|
|
271
262
|
test('Polymorphic relation', () => {
|
|
@@ -274,13 +265,10 @@ describe('Attributes', () => {
|
|
|
274
265
|
|
|
275
266
|
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
276
267
|
|
|
277
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
268
|
+
expect(typeNode.typeArguments).toHaveLength(1);
|
|
278
269
|
|
|
279
270
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
280
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
281
|
-
|
|
282
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
283
|
-
expect(typeNode.typeArguments[1].text).toBe('morphMany');
|
|
271
|
+
expect(typeNode.typeArguments[0].text).toBe('morphMany');
|
|
284
272
|
});
|
|
285
273
|
});
|
|
286
274
|
|
|
@@ -305,10 +293,12 @@ describe('Attributes', () => {
|
|
|
305
293
|
|
|
306
294
|
defaultAssertions(typeNode, 'Attribute.Component');
|
|
307
295
|
|
|
308
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
296
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
309
297
|
|
|
310
298
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
311
299
|
expect(typeNode.typeArguments[0].text).toBe('default.comp');
|
|
300
|
+
|
|
301
|
+
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword);
|
|
312
302
|
});
|
|
313
303
|
});
|
|
314
304
|
|
|
@@ -836,6 +826,16 @@ describe('Attributes', () => {
|
|
|
836
826
|
ts.SyntaxKind.TrueKeyword
|
|
837
827
|
);
|
|
838
828
|
});
|
|
829
|
+
|
|
830
|
+
test('Default: <function>', () => {
|
|
831
|
+
const anyFunction = jest.fn();
|
|
832
|
+
const attribute = { default: anyFunction };
|
|
833
|
+
|
|
834
|
+
const modifiers = getAttributeModifiers(attribute);
|
|
835
|
+
|
|
836
|
+
// The default modifier shouldn't be processed when encountering a function
|
|
837
|
+
expect(modifiers).toHaveLength(0);
|
|
838
|
+
});
|
|
839
839
|
});
|
|
840
840
|
});
|
|
841
841
|
});
|
|
@@ -151,8 +151,8 @@ const getAttributeModifiers = (attribute) => {
|
|
|
151
151
|
);
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
// Default
|
|
155
|
-
if (!_.isNil(attribute.default)) {
|
|
154
|
+
// Default (ignore if default is a function)
|
|
155
|
+
if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {
|
|
156
156
|
const defaultLiteral = toTypeLiteral(attribute.default);
|
|
157
157
|
|
|
158
158
|
modifiers.push(
|
|
@@ -47,7 +47,7 @@ module.exports = {
|
|
|
47
47
|
decimal() {
|
|
48
48
|
return [withAttributeNamespace('Decimal')];
|
|
49
49
|
},
|
|
50
|
-
uid({ attribute
|
|
50
|
+
uid({ attribute }) {
|
|
51
51
|
const { targetField, options } = attribute;
|
|
52
52
|
|
|
53
53
|
// If there are no params to compute, then return the attribute type alone
|
|
@@ -58,18 +58,15 @@ module.exports = {
|
|
|
58
58
|
const params = [];
|
|
59
59
|
|
|
60
60
|
// If the targetField property is defined, then reference it,
|
|
61
|
-
// otherwise, put `undefined` keyword type
|
|
62
|
-
const
|
|
63
|
-
?
|
|
64
|
-
|
|
65
|
-
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
|
|
66
|
-
]
|
|
67
|
-
: [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)];
|
|
61
|
+
// otherwise, put `undefined` keyword type node as placeholder
|
|
62
|
+
const targetFieldParam = _.isUndefined(targetField)
|
|
63
|
+
? factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
|
|
64
|
+
: factory.createStringLiteral(targetField);
|
|
68
65
|
|
|
69
|
-
params.push(
|
|
66
|
+
params.push(targetFieldParam);
|
|
70
67
|
|
|
71
68
|
// If the options property is defined, transform it to
|
|
72
|
-
// a type
|
|
69
|
+
// a type literal node and add it to the params list
|
|
73
70
|
if (_.isObject(options)) {
|
|
74
71
|
params.push(toTypeLiteral(options));
|
|
75
72
|
}
|
|
@@ -93,25 +90,18 @@ module.exports = {
|
|
|
93
90
|
media() {
|
|
94
91
|
return [withAttributeNamespace('Media')];
|
|
95
92
|
},
|
|
96
|
-
relation({
|
|
93
|
+
relation({ attribute }) {
|
|
97
94
|
const { relation, target } = attribute;
|
|
98
95
|
|
|
99
96
|
const isMorphRelation = relation.toLowerCase().includes('morph');
|
|
100
97
|
|
|
101
98
|
if (isMorphRelation) {
|
|
102
|
-
return [
|
|
103
|
-
withAttributeNamespace('Relation'),
|
|
104
|
-
[factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)],
|
|
105
|
-
];
|
|
99
|
+
return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]];
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
return [
|
|
109
103
|
withAttributeNamespace('Relation'),
|
|
110
|
-
[
|
|
111
|
-
factory.createStringLiteral(uid, true),
|
|
112
|
-
factory.createStringLiteral(relation, true),
|
|
113
|
-
factory.createStringLiteral(target, true),
|
|
114
|
-
],
|
|
104
|
+
[factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)],
|
|
115
105
|
];
|
|
116
106
|
},
|
|
117
107
|
component({ attribute }) {
|
|
@@ -120,6 +110,8 @@ module.exports = {
|
|
|
120
110
|
|
|
121
111
|
if (attribute.repeatable) {
|
|
122
112
|
params.push(factory.createTrue());
|
|
113
|
+
} else {
|
|
114
|
+
params.push(factory.createFalse());
|
|
123
115
|
}
|
|
124
116
|
|
|
125
117
|
return [withAttributeNamespace('Component'), params];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.0",
|
|
4
4
|
"description": "Typescript support for Strapi",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -37,14 +37,14 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"chalk": "4.1.2",
|
|
39
39
|
"cli-table3": "0.6.2",
|
|
40
|
-
"fs-extra": "10.
|
|
40
|
+
"fs-extra": "10.1.0",
|
|
41
41
|
"lodash": "4.17.21",
|
|
42
42
|
"prettier": "2.8.4",
|
|
43
|
-
"typescript": "5.
|
|
43
|
+
"typescript": "5.3.2"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=18.0.0 <=20.x.x",
|
|
47
47
|
"npm": ">=6.0.0"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "ceeb3f383cbaf51eeaa373750a27a2880a10f727"
|
|
50
50
|
}
|