fix: build process errors from merger

This commit is contained in:
Josh 2024-01-02 13:52:43 +00:00
parent ddbb55f2eb
commit 30ae2308e8
11 changed files with 156 additions and 1805 deletions

File diff suppressed because one or more lines are too long

View File

@ -163,7 +163,7 @@ const ListViewPage = ({
}, []) ?? [];
const { users, isLoading: isLoadingAdminUsers } = useAdminUsers(
{ filters: { id: { in: selectedUserIds } } },
{ filters: { id: { $in: selectedUserIds } } },
{
// fetch the list of admin users only if the filter contains users and the
// current user has permissions to display users

View File

@ -156,7 +156,6 @@ type Action =
| onResetListHeadersAction;
const reducer = (state: ListViewLayoutManagerState = initialState, action: Action) =>
// @ts-expect-error recursive types!
produce(state, (draftState) => {
switch (action.type) {
case GET_DATA: {
@ -189,7 +188,7 @@ const reducer = (state: ListViewLayoutManagerState = initialState, action: Actio
fieldSchema: attributes[name],
metadatas: metas,
key: `__${name}_key__`,
};
} satisfies ListLayoutRow;
const attribute = attributes[name];

View File

@ -54,6 +54,7 @@ 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,6 +20,7 @@ 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

@ -512,6 +512,7 @@ describe('User', () => {
const findOne = jest.fn();
const fakeEmail = 'admin@admin.com';
// @ts-expect-error - test purpose
global.strapi = { query: () => ({ findOne }) };
await userService.findOneByEmail(fakeEmail);

View File

@ -20,7 +20,7 @@ const { PUBLISHED_AT_ATTRIBUTE } = contentTypes.constants;
const omitPublishedAtField = omit(PUBLISHED_AT_ATTRIBUTE);
// Types reused from entity service
type Entity = EntityService.Result<Common.UID.ContentType>;
export type Entity = EntityService.Result<Common.UID.ContentType>;
type Body = EntityService.Params.Data.Input<Common.UID.ContentType>;
const emitEvent = async (uid: Common.UID.ContentType, event: string, entity: Entity) => {
@ -46,10 +46,39 @@ const buildDeepPopulate = (uid: Common.UID.ContentType) => {
);
};
/**
* @type {import('./entity-manager').default}
*/
export default ({ strapi }: { strapi: Strapi }) => ({
type EntityManager = (opts: { strapi: Strapi }) => {
mapEntity<T = unknown>(entity: T): T;
mapEntitiesResponse(entities: any, uid: Common.UID.ContentType): any;
find(
opts: Parameters<typeof strapi.entityService.findMany>[1],
uid: Common.UID.ContentType
): Promise<ReturnType<typeof strapi.entityService.findMany>>;
findPage(
opts: Parameters<typeof strapi.entityService.findPage>[1],
uid: Common.UID.ContentType
): Promise<ReturnType<typeof strapi.entityService.findPage>>;
findOne(id: Entity['id'], uid: Common.UID.ContentType, opts?: any): Promise<Entity>;
create(body: Body, uid: Common.UID.ContentType): Promise<Entity>;
update(entity: Entity, body: Partial<Body>, uid: Common.UID.ContentType): Promise<Entity | null>;
clone(entity: Entity, body: Partial<Body>, uid: Common.UID.ContentType): Promise<Entity | null>;
delete(entity: Entity, uid: Common.UID.ContentType): Promise<Entity | null>;
deleteMany(
opts: Parameters<typeof strapi.entityService.deleteMany>[1],
uid: Common.UID.ContentType
): Promise<{ count: number } | null>;
publish(entity: Entity, uid: Common.UID.ContentType, body?: any): Promise<Entity | null>;
publishMany(entities: Entity[], uid: Common.UID.ContentType): Promise<{ count: number } | null>;
unpublish(entity: Entity, uid: Common.UID.ContentType, body?: any): Promise<Entity | null>;
unpublishMany(entities: Entity[], uid: Common.UID.ContentType): Promise<{ count: number } | null>;
countDraftRelations(id: Entity['id'], uid: Common.UID.ContentType): Promise<number>;
countManyEntriesDraftRelations(
ids: number[],
uid: Common.UID.ContentType,
locale?: string
): Promise<number>;
};
const entityManager: EntityManager = ({ strapi }) => ({
/**
* Extend this function from other plugins to add custom mapping of entity
* responses
@ -161,7 +190,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
const clonedEntity = await strapi.entityService.clone(uid, entity.id, params);
// If relations were populated, relations count will be returned instead of the array of relations.
if (isWebhooksPopulateRelationsEnabled()) {
if (clonedEntity && isWebhooksPopulateRelationsEnabled()) {
return getDeepRelationsCount(clonedEntity, uid);
}
@ -172,7 +201,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
const deletedEntity = await strapi.entityService.delete(uid, entity.id, { populate });
// If relations were populated, relations count will be returned instead of the array of relations.
if (isWebhooksPopulateRelationsEnabled()) {
if (deletedEntity && isWebhooksPopulateRelationsEnabled()) {
return getDeepRelationsCount(deletedEntity, uid);
}
@ -214,7 +243,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
const mappedEntity = await this.mapEntity(updatedEntity, uid);
// If relations were populated, relations count will be returned instead of the array of relations.
if (isWebhooksPopulateRelationsEnabled()) {
if (mappedEntity && isWebhooksPopulateRelationsEnabled()) {
return getDeepRelationsCount(mappedEntity, uid);
}
@ -312,7 +341,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
const mappedEntity = await this.mapEntity(updatedEntity, uid);
// If relations were populated, relations count will be returned instead of the array of relations.
if (isWebhooksPopulateRelationsEnabled()) {
if (mappedEntity && isWebhooksPopulateRelationsEnabled()) {
return getDeepRelationsCount(mappedEntity, uid);
}
@ -356,3 +385,6 @@ export default ({ strapi }: { strapi: Strapi }) => ({
return totalNumberDraftRelations;
},
});
export default entityManager;
export type { EntityManager };

View File

@ -72,6 +72,7 @@ describe('getDeepRelationsCount', () => {
test('with many to many', () => {
const count = getDeepRelationsCount(
{
id: 1,
relationAttrName: [
{
id: 2,
@ -83,10 +84,12 @@ describe('getDeepRelationsCount', () => {
},
],
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'relationMTM'
);
expect(count).toEqual({
id: 1,
relationAttrName: {
count: 2,
},
@ -96,15 +99,18 @@ describe('getDeepRelationsCount', () => {
test('with one to one', () => {
const count = getDeepRelationsCount(
{
id: 1,
relationAttrName: {
id: 2,
name: 'rel1',
},
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'relationOTO'
);
expect(count).toEqual({
id: 1,
relationAttrName: {
count: 1,
},
@ -115,8 +121,10 @@ describe('getDeepRelationsCount', () => {
describe('media fields', () => {
test('with media', () => {
const mediaEntity = {
id: 1,
mediaAttrName: { id: 1, name: 'img1' },
};
// @ts-expect-error fake model, the data would have to change to satisfy the type
const count = getDeepRelationsCount(mediaEntity, 'media');
expect(count).toEqual(mediaEntity);
@ -127,6 +135,7 @@ describe('getDeepRelationsCount', () => {
test('with component', () => {
const count = getDeepRelationsCount(
{
id: 1,
componentAttrName: {
relationAttrName: [
{
@ -140,10 +149,12 @@ describe('getDeepRelationsCount', () => {
],
},
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'component'
);
expect(count).toEqual({
id: 1,
componentAttrName: {
relationAttrName: {
count: 2,
@ -155,12 +166,15 @@ describe('getDeepRelationsCount', () => {
test('with empty component', () => {
const count = getDeepRelationsCount(
{
id: 1,
componentAttrName: null,
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'component'
);
expect(count).toEqual({
id: 1,
componentAttrName: null,
});
});
@ -168,6 +182,7 @@ describe('getDeepRelationsCount', () => {
test('with repeatable component', () => {
const count = getDeepRelationsCount(
{
id: 1,
repeatableComponentAttrName: [
{
relationAttrName: [
@ -183,10 +198,12 @@ describe('getDeepRelationsCount', () => {
},
],
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'repeatableComponent'
);
expect(count).toEqual({
id: 1,
repeatableComponentAttrName: [
{
relationAttrName: {
@ -202,6 +219,7 @@ describe('getDeepRelationsCount', () => {
test('with dynamic zone', () => {
const count = getDeepRelationsCount(
{
id: 1,
dynZoneAttrName: [
{
__component: 'component',
@ -220,10 +238,12 @@ describe('getDeepRelationsCount', () => {
},
],
},
// @ts-expect-error fake model, the data would have to change to satisfy the type
'dynZone'
);
expect(count).toEqual({
id: 1,
dynZoneAttrName: [
{
__component: 'component',

View File

@ -1,8 +1,14 @@
import { Common, Schema } from '@strapi/types';
import { contentTypes } from '@strapi/utils';
import type { Entity } from '../entity-manager';
const { isVisibleAttribute } = contentTypes;
function getCountForRelation(attributeName: any, entity: any, model: any) {
function getCountForRelation(
attributeName: string,
entity: Entity[string],
model: Schema.ContentType | Schema.Component
) {
// do not count createdBy, updatedBy, localizations etc.
if (!isVisibleAttribute(model, attributeName)) {
return entity;
@ -15,13 +21,17 @@ function getCountForRelation(attributeName: any, entity: any, model: any) {
return entity ? { count: 1 } : { count: 0 };
}
function getCountForDZ(entity: any) {
function getCountForDZ(entity: Entity[string]) {
return entity.map((component: any) => {
return getDeepRelationsCount(component, component.__component);
});
}
function getCountFor(attributeName: any, entity: any, model: any): any {
function getCountFor(
attributeName: string,
entity: Entity[string],
model: Schema.ContentType | Schema.Component
): any {
const attribute = model.attributes[attributeName];
switch (attribute?.type) {
@ -42,15 +52,15 @@ function getCountFor(attributeName: any, entity: any, model: any): any {
}
}
const getDeepRelationsCount = (entity: any, uid: any) => {
const getDeepRelationsCount = (entity: Entity, uid: Common.UID.Schema): Entity => {
const model = strapi.getModel(uid);
return Object.keys(entity).reduce(
return Object.keys(entity).reduce<Entity>(
(relationCountEntity, attributeName) =>
Object.assign(relationCountEntity, {
[attributeName]: getCountFor(attributeName, entity[attributeName], model),
}),
{}
{} as Entity
);
};

View File

@ -7,16 +7,16 @@ export type JSON = Attribute.OfType<'json'> &
Attribute.PrivateOption &
Attribute.WritableOption &
Attribute.VisibleOption &
Attribute.DefaultOption<JsonValue>;
Attribute.DefaultOption<JSONValue>;
type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
type JSONValue = string | number | boolean | null;
type JSONArray = Array<JSONValue>;
type JSONArray = JsonValue[] | readonly JsonValue[];
export interface JSONObject {
[key: string]: JSONValue;
}
export type JsonValue<T extends JSONValue = JSONValue> = T;
export type JsonValue = JSONValue | JSONObject | JSONArray;
export type GetJsonValue<T extends Attribute.Attribute> = T extends JSON ? JsonValue : never;

1849
yarn.lock

File diff suppressed because it is too large Load Diff