mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 08:52:26 +00:00
Merge pull request #19335 from strapi/v5/enhancement/types/remove-attribute-relation-param
This commit is contained in:
commit
b1576b572e
@ -91,8 +91,7 @@ export type GetValue<TAttribute extends Attribute.Attribute> = Utils.Expression.
|
|||||||
// Relation
|
// Relation
|
||||||
[
|
[
|
||||||
Utils.Expression.Extends<TAttribute, Attribute.OfType<'relation'>>,
|
Utils.Expression.Extends<TAttribute, Attribute.OfType<'relation'>>,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
TAttribute extends Attribute.Relation<infer TRelationKind, infer TTarget>
|
||||||
TAttribute extends Attribute.Relation<infer _TOrigin, infer TRelationKind, infer TTarget>
|
|
||||||
? Utils.Expression.If<
|
? Utils.Expression.If<
|
||||||
Utils.Expression.IsNotNever<TTarget>,
|
Utils.Expression.IsNotNever<TTarget>,
|
||||||
Attribute.RelationPluralityModifier<TRelationKind, ID>
|
Attribute.RelationPluralityModifier<TRelationKind, ID>
|
||||||
|
@ -63,8 +63,7 @@ export type GetValue<
|
|||||||
// Relation
|
// Relation
|
||||||
[
|
[
|
||||||
Utils.Expression.Extends<TAttribute, Attribute.OfType<'relation'>>,
|
Utils.Expression.Extends<TAttribute, Attribute.OfType<'relation'>>,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
TAttribute extends Attribute.Relation<infer TRelationKind, infer TTarget>
|
||||||
TAttribute extends Attribute.Relation<infer _TOrigin, infer TRelationKind, infer TTarget>
|
|
||||||
? Utils.Expression.If<
|
? Utils.Expression.If<
|
||||||
Utils.Expression.IsNotNever<TTarget>,
|
Utils.Expression.IsNotNever<TTarget>,
|
||||||
RelationInputValue<TRelationKind>
|
RelationInputValue<TRelationKind>
|
||||||
|
@ -3,12 +3,6 @@ import type { Attribute, Common } from '..';
|
|||||||
import type { Utils } from '../..';
|
import type { Utils } from '../..';
|
||||||
|
|
||||||
export type Relation<
|
export type Relation<
|
||||||
// TODO: TOrigin was originally needed to infer precise attribute literal types by doing a reverse lookup
|
|
||||||
// on TTarget -> TOrigin relations. Due to errors because of Attribute.Any [relation] very generic
|
|
||||||
// representation, type mismatches were encountered and mappedBy/inversedBy are now regular strings.
|
|
||||||
// It is kept to allow for future iterations without breaking the current type API
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
_TOrigin extends Common.UID.Schema = Common.UID.Schema,
|
|
||||||
TRelationKind extends RelationKind.Any = RelationKind.Any,
|
TRelationKind extends RelationKind.Any = RelationKind.Any,
|
||||||
TTarget extends Common.UID.ContentType = Common.UID.ContentType
|
TTarget extends Common.UID.ContentType = Common.UID.ContentType
|
||||||
> = Attribute.OfType<'relation'> &
|
> = Attribute.OfType<'relation'> &
|
||||||
@ -119,8 +113,6 @@ export type RelationValue<
|
|||||||
> = RelationPluralityModifier<TRelationKind, Attribute.GetValues<TTarget>>;
|
> = RelationPluralityModifier<TRelationKind, Attribute.GetValues<TTarget>>;
|
||||||
|
|
||||||
export type GetRelationValue<TAttribute extends Attribute.Attribute> = TAttribute extends Relation<
|
export type GetRelationValue<TAttribute extends Attribute.Attribute> = TAttribute extends Relation<
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
infer _TOrigin,
|
|
||||||
infer TRelationKind,
|
infer TRelationKind,
|
||||||
infer TTarget
|
infer TTarget
|
||||||
>
|
>
|
||||||
@ -128,8 +120,6 @@ export type GetRelationValue<TAttribute extends Attribute.Attribute> = TAttribut
|
|||||||
: never;
|
: never;
|
||||||
|
|
||||||
export type GetRelationTarget<TAttribute extends Attribute.Attribute> = TAttribute extends Relation<
|
export type GetRelationTarget<TAttribute extends Attribute.Attribute> = TAttribute extends Relation<
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
infer _TOrigin,
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
infer _TRelationKind,
|
infer _TRelationKind,
|
||||||
infer TTarget
|
infer TTarget
|
||||||
|
@ -256,16 +256,13 @@ describe('Attributes', () => {
|
|||||||
|
|
||||||
defaultAssertions(typeNode, 'Attribute.Relation');
|
defaultAssertions(typeNode, '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].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].kind).toBe(ts.SyntaxKind.StringLiteral);
|
||||||
expect(typeNode.typeArguments[1].text).toBe('oneToOne');
|
expect(typeNode.typeArguments[1].text).toBe('api::bar.bar');
|
||||||
|
|
||||||
expect(typeNode.typeArguments[2].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
||||||
expect(typeNode.typeArguments[2].text).toBe('api::bar.bar');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Polymorphic relation', () => {
|
test('Polymorphic relation', () => {
|
||||||
@ -274,13 +271,10 @@ describe('Attributes', () => {
|
|||||||
|
|
||||||
defaultAssertions(typeNode, 'Attribute.Relation');
|
defaultAssertions(typeNode, '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].kind).toBe(ts.SyntaxKind.StringLiteral);
|
||||||
expect(typeNode.typeArguments[0].text).toBe('api::foo.foo');
|
expect(typeNode.typeArguments[0].text).toBe('morphMany');
|
||||||
|
|
||||||
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
||||||
expect(typeNode.typeArguments[1].text).toBe('morphMany');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ module.exports = {
|
|||||||
params.push(...targetFieldParams);
|
params.push(...targetFieldParams);
|
||||||
|
|
||||||
// If the options property is defined, transform it to
|
// 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)) {
|
if (_.isObject(options)) {
|
||||||
params.push(toTypeLiteral(options));
|
params.push(toTypeLiteral(options));
|
||||||
}
|
}
|
||||||
@ -93,25 +93,18 @@ module.exports = {
|
|||||||
media() {
|
media() {
|
||||||
return [withAttributeNamespace('Media')];
|
return [withAttributeNamespace('Media')];
|
||||||
},
|
},
|
||||||
relation({ uid, attribute }) {
|
relation({ attribute }) {
|
||||||
const { relation, target } = attribute;
|
const { relation, target } = attribute;
|
||||||
|
|
||||||
const isMorphRelation = relation.toLowerCase().includes('morph');
|
const isMorphRelation = relation.toLowerCase().includes('morph');
|
||||||
|
|
||||||
if (isMorphRelation) {
|
if (isMorphRelation) {
|
||||||
return [
|
return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]];
|
||||||
withAttributeNamespace('Relation'),
|
|
||||||
[factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)],
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
withAttributeNamespace('Relation'),
|
withAttributeNamespace('Relation'),
|
||||||
[
|
[factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)],
|
||||||
factory.createStringLiteral(uid, true),
|
|
||||||
factory.createStringLiteral(relation, true),
|
|
||||||
factory.createStringLiteral(target, true),
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
component({ attribute }) {
|
component({ attribute }) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user