Merge pull request #17937 from strapi/fix/missing-id-property-in-filters-object

This commit is contained in:
Jean-Sébastien Herbaux 2023-09-06 10:02:31 +02:00 committed by GitHub
commit deb0c4d12c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,12 @@ import type { Attribute, Common, Utils } from '@strapi/strapi';
import type * as Operator from './operators'; import type * as Operator from './operators';
import type * as AttributeUtils from '../attributes'; import type * as AttributeUtils from '../attributes';
import type Params from '../index';
export { Operator }; export { Operator };
type IDKey = 'id';
/** /**
* Filter representation for nested attributes * Filter representation for nested attributes
*/ */
@ -20,11 +23,17 @@ type NestedAttributeCondition<
*/ */
type AttributeCondition< type AttributeCondition<
TSchemaUID extends Common.UID.Schema, TSchemaUID extends Common.UID.Schema,
TAttributeName extends Attribute.GetKeys<TSchemaUID> TAttributeName extends IDKey | Attribute.GetKeys<TSchemaUID>
> = Utils.Expression.If< > = Utils.Expression.MatchFirst<
Utils.Expression.IsNotNever<TAttributeName>, [
// Get the filter attribute value for the given attribute // Special catch for manually added ID attributes
AttributeUtils.GetValue<Attribute.Get<TSchemaUID, TAttributeName>>, [Utils.Expression.StrictEqual<TAttributeName, IDKey>, Params.Attribute.ID],
[
Utils.Expression.IsNotNever<TAttributeName>,
// Get the filter attribute value for the given attribute
AttributeUtils.GetValue<Attribute.Get<TSchemaUID, Exclude<TAttributeName, IDKey>>>
]
],
// Fallback to the list of all possible scalar attributes' value if the attribute is not valid (never) // Fallback to the list of all possible scalar attributes' value if the attribute is not valid (never)
AttributeUtils.ScalarValues AttributeUtils.ScalarValues
> extends infer TAttributeValue > extends infer TAttributeValue
@ -64,11 +73,17 @@ export type ObjectNotation<TSchemaUID extends Common.UID.Schema> = {
Utils.Expression.IsNotNever<TNestedKeys> Utils.Expression.IsNotNever<TNestedKeys>
>, >,
// Strongly typed representation of the filter object tree // Strongly typed representation of the filter object tree
| { [TIter in TScalarKeys]?: AttributeCondition<TSchemaUID, TIter> } {
| { [TIter in TNestedKeys]?: NestedAttributeCondition<TSchemaUID, TIter> }, [TIter in IDKey | TScalarKeys]?: AttributeCondition<TSchemaUID, TIter>;
} & {
[TIter in TNestedKeys]?: NestedAttributeCondition<TSchemaUID, TIter>;
},
// Generic representation of the filter object tree in case we don't have access to the attributes' list // Generic representation of the filter object tree in case we don't have access to the attributes' list
| { [TKey in string]?: AttributeCondition<TSchemaUID, never> } {
| { [TKey in string]?: NestedAttributeCondition<TSchemaUID, never> } [TKey in string]?:
| AttributeCondition<TSchemaUID, never>
| NestedAttributeCondition<TSchemaUID, never>;
}
> >
: never); : never);