diff --git a/package.json b/package.json index 54a2fa8..d752261 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "moysklad-api-model", - "version": "0.4.1", + "version": "0.4.2", "description": "Объектная модель API МойСклад для TypeScript проектов", "main": "build/src/index.js", "types": "build/src/index.d.ts", diff --git a/src/model/utils/Expand.ts b/src/model/utils/Expand.ts index 051b8c6..9c0b744 100644 --- a/src/model/utils/Expand.ts +++ b/src/model/utils/Expand.ts @@ -26,8 +26,15 @@ export type ExpandedField = : K extends keyof T ? T[K] extends EntityRef | undefined + // Уже раскрытая сущность + ? T[K] extends { id: string } + ? T[K] - ? T[K] extends CollectionRef + // Уже раскрытая коллекция + : T[K] extends { rows: Array } + ? T[K] + + : T[K] extends CollectionRef ? Collection : T[K] extends CollectionRef | undefined @@ -54,8 +61,16 @@ export type ExpandField = { [P in keyof T]: K extends P // EntityRef | CollectionRef ? T[P] extends EntityRef | undefined + // Уже раскрытая сущность + ? T[P] extends { id: string } + ? T[P] + + // Уже раскрытая коллекция + : T[P] extends { rows: Array } + ? T[P] + // CollectionRef - ? T[P] extends CollectionRef + : T[P] extends CollectionRef ? Collection : T[P] extends CollectionRef | undefined diff --git a/test/model/utils/Expand.test.ts b/test/model/utils/Expand.test.ts index a6373a6..3e309eb 100644 --- a/test/model/utils/Expand.test.ts +++ b/test/model/utils/Expand.test.ts @@ -13,7 +13,9 @@ import { ExpandPath, Meta, Counterparty, - Organization + Organization, + AttributeBase, + Entity } from '../../../src' //#region @@ -207,6 +209,72 @@ const t50_name: string | undefined = t50.state?.name t50.agent.group.name // OK: 2-nd level expanded //#endregion +//#region Expand attributes.value +// String +const t55_1 = {} as ExpandField, 'value'> +const t55_2: string = t55_1.value +t55_2 + +// CustomEntity +const t55_3 = {} as ExpandField< + AttributeBase, + 'value' +> +const t55_4: Entity<'customentity'> = t55_3.value +t55_4 + +const t55_5 = {} as ExpandField< + AttributeBase, + 'value' +> +const t55_6: Counterparty = t55_5.value +t55_6 + +const t55_7 = {} as Expand + +const t55_attr = t55_7.attributes?.[0] + +if (t55_attr?.type === AttributeType.Counterparty) { + const t55_val: Counterparty = t55_attr.value + t55_val.inn +} +//#endregion + +//#region Expand deep agent.attributes.value +const t56_1 = {} as Expand +const t56_attr = t56_1.agent.attributes?.[0] + +if (typeof t56_attr?.value === 'object') { + t56_attr.value.id +} + +if (t56_attr?.type === AttributeType.Counterparty) { + t56_attr.value.inn +} +//#endregion + +//#region Expand attributes with intersection +const t57_1 = {} as Expand +const t57_attr = t57_1.agent.attributes?.[0] + +if (typeof t57_attr?.value === 'object') { + t57_attr.value.id +} + +if (t57_attr?.type === AttributeType.Counterparty) { + t57_attr.value.inn +} +//#endregion + +//#region Expand attributes with deep intersection +const t58_1 = {} as Expand +const t58_attr = t58_1.agent.attributes?.[0] + +if (t58_attr?.type === AttributeType.Counterparty) { + t58_attr.value.inn +} +//#endregion + //#region Комплексный Expand const t60 = {} as Expand< CustomerOrder, @@ -217,7 +285,7 @@ t60.owner.meta t60.agent.name // expand -// @ts-expect-error +// @ts-expect-error: project not expanded t60.project?.id const name4: string | undefined = t60.state?.name @@ -226,11 +294,10 @@ t60.agent.group.name // OK: 2-nd level expanded const t60_agentAttr = t60.agent.attributes?.[0] -/* TODO expand: attributes.value +// TODO expand: attributes.value if (t60_agentAttr?.type === AttributeType.Counterparty) { const inn: string | undefined = t60_agentAttr.value.inn } -*/ const t60_assortmentId: string = t60.positions.rows[0].assortment.id