fix(types): attributes filtering when no registries are extended (#19145)

* fix: improved registries extensions detection & attribute filtering

Co-Authored-By: Jean-Sébastien Herbaux <25851739+Convly@users.noreply.github.com>

* chore: remove unnecessary ts-expect-error directives

Co-Authored-By: Jean-Sébastien Herbaux <25851739+Convly@users.noreply.github.com>
This commit is contained in:
Jean-Sébastien Herbaux 2024-01-04 15:21:17 +01:00 committed by GitHub
parent 68e91e14ec
commit f6ae15bd8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 23 deletions

View File

@ -54,7 +54,6 @@ const ListPage = () => {
} = useRBAC(permissions.settings?.roles);
const { roles, refetch: refetchRoles } = useAdminRoles(
// @ts-expect-error we know that the `admin::role` content-type has a name attribute, but the EntityService uses a registry which we don't have.
{ filters: query?._q ? { name: { $containsi: query._q } } : undefined },
{
cacheTime: 0,

View File

@ -20,7 +20,6 @@ const StageFilter = ({ value, onChange, uid }: StageFilterProps) => {
const {
workflows: [workflow],
isLoading,
// @ts-expect-error we know that the `admin::review-workflow` content-type has a contentTypes attribute, but the EntityService uses a registry which we don't have.
} = useReviewWorkflows({ filters: { contentTypes: uid } });
return (

View File

@ -43,22 +43,21 @@ export type RootLevelOperatorFiltering<TSchemaUID extends Common.UID.Schema> = {
* Represents a type for filtering on attributes based on a given schema.
* @template TSchemaUID - The UID of the schema.
*/
export type AttributesFiltering<TSchemaUID extends Common.UID.Schema> = Utils.Guard.Never<
// Combines filtering for scalar and nested attributes based on schema UID
ScalarAttributesFiltering<TSchemaUID> & NestedAttributeFiltering<TSchemaUID>,
// Abstract representation of the filter object tree in case we don't have access to the attributes' list
{
[TKey in string]?:
| AttributeCondition<TSchemaUID, never>
| NestedAttributeCondition<TSchemaUID, never>;
}
>;
export type AttributesFiltering<TSchemaUID extends Common.UID.Schema> =
// Manually added filtering on virtual ID attribute
IDFiltering &
Utils.Expression.If<
Common.AreSchemaRegistriesExtended,
// Combines filtering for scalar and nested attributes based on schema UID
ScalarAttributesFiltering<TSchemaUID> & NestedAttributeFiltering<TSchemaUID>,
// Abstract representation of the filter object tree in case we don't have access to the attributes' list
AbstractAttributesFiltering<TSchemaUID>
>;
/**
* Definition of scalar attribute filtering for a given schema UID.
* @template TSchemaUID - The UID of the schema.
*/
export type ScalarAttributesFiltering<TSchemaUID extends Common.UID.Schema> = IDFiltering & {
export type ScalarAttributesFiltering<TSchemaUID extends Common.UID.Schema> = {
[TKey in AttributeUtils.GetScalarKeys<TSchemaUID>]?: AttributeCondition<TSchemaUID, TKey>;
};
@ -141,3 +140,9 @@ type NestedAttributeCondition<
// Ensure the resolved target isn't `never`, else, fallback to Common.UID.Schema
Utils.Guard.Never<Attribute.GetTarget<TSchemaUID, TAttributeName>, Common.UID.Schema>
>;
export type AbstractAttributesFiltering<TSchemaUID extends Common.UID.Schema> = {
[TKey in string]?:
| AttributeCondition<TSchemaUID, never>
| NestedAttributeCondition<TSchemaUID, never>;
};

View File

@ -1,4 +1,4 @@
import type { Utils, Attribute, Common } from '../..';
import type { Utils, Common, UID } from '../..';
export * from './controller';
export * from './middleware';
@ -11,12 +11,17 @@ export * from './plugin';
export * from './module';
export * from './api';
/**
* Determines if the shared registries for components and content types have been extended or if they're still represented as loose mapped types
*
* Here we use the fact that once the registries are extended, Attribute.GetKeys<Common.UID.Schema> will resolve to either never or a more
* deterministic value rather than string | number which represent the keys of the initial mapped type (Component & ContentType's registries)
*/
export type AreSchemaRegistriesExtended = Utils.Expression.Not<
Utils.Expression.Extends<string | number, Attribute.GetKeys<Common.UID.Schema>>
export type AreSchemaRegistriesExtended = Utils.Expression.Or<
IsComponentRegistryExtended,
IsContentTypeRegistryExtended
>;
export type IsContentTypeRegistryExtended = Utils.Expression.NotStrictEqual<
UID.ContentType,
Common.UID.ContentType
>;
export type IsComponentRegistryExtended = Utils.Expression.NotStrictEqual<
UID.Component,
Common.UID.Component
>;

View File

@ -14,6 +14,8 @@ export type IsFalse<TValue> = [TValue] extends [False] ? True : False;
export type StrictEqual<TValue, TMatch> = And<Extends<TValue, TMatch>, Extends<TMatch, TValue>>;
export type NotStrictEqual<TValue, TMatch> = Not<StrictEqual<TValue, TMatch>>;
export type Extends<TLeft, TRight> = [TLeft] extends [TRight] ? True : False;
export type DoesNotExtends<TLeft, TRight> = Not<Extends<TLeft, TRight>>;