diff --git a/package.json b/package.json index 6db1da7..4440d93 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "moysklad-api-model", - "version": "0.1.2", + "version": "0.1.3", "description": "Объектная модель API МойСклад для TypeScript проектов", "main": "build/src/index.js", "types": "build/src/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 116e668..367ad47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export * from './model' +export * from './tools' diff --git a/src/model/Demand.ts b/src/model/Demand.ts new file mode 100644 index 0000000..695661d --- /dev/null +++ b/src/model/Demand.ts @@ -0,0 +1,3 @@ +import type { DocumentWithPositions, HasVat, MetaType } from '.' + +export type Demand = DocumentWithPositions & HasVat diff --git a/src/model/Document.ts b/src/model/Document.ts index 3e27010..6e8ddec 100644 --- a/src/model/Document.ts +++ b/src/model/Document.ts @@ -13,6 +13,7 @@ import type { Owned } from './Owned' export type DocumentMetaType = | MetaType.CustomerOrder | MetaType.Demand + | MetaType.RetailDemand | MetaType.PurchaseOrder // TODO Описать остальные diff --git a/src/model/Position.ts b/src/model/Position.ts index 3225ba4..011c127 100644 --- a/src/model/Position.ts +++ b/src/model/Position.ts @@ -10,6 +10,7 @@ export type PositionMetaType = export type PositionTypeByDocument = { [MetaType.CustomerOrder]: MetaType.CustomerOrderPosition [MetaType.Demand]: MetaType.DemandPosition + [MetaType.RetailDemand]: MetaType.DemandPosition [MetaType.PurchaseOrder]: MetaType.PurchaseOrderPosition } diff --git a/src/model/RetailDemand.ts b/src/model/RetailDemand.ts new file mode 100644 index 0000000..f1d26d8 --- /dev/null +++ b/src/model/RetailDemand.ts @@ -0,0 +1,7 @@ +import type { DocumentWithPositions, MetaType } from '.' + +export interface RetailDemand + extends DocumentWithPositions { + cashSum: number + noCashSum: number +} diff --git a/src/model/index.ts b/src/model/index.ts index 9b5f56b..039f1d0 100644 --- a/src/model/index.ts +++ b/src/model/index.ts @@ -12,6 +12,7 @@ export * from './CustomerOrder' export * from './CustomerOrderPosition' export * from './Discount' export * from './Document' +export * from './Demand' export * from './DocumentWithPositions' export * from './Employee' export * from './Entity' @@ -38,5 +39,6 @@ export * from './Owned' export * from './Person' export * from './Position' export * from './PriceType' +export * from './RetailDemand' export * from './State' export * from './TaxSystem' diff --git a/src/tools.ts b/src/tools.ts new file mode 100644 index 0000000..14859b2 --- /dev/null +++ b/src/tools.ts @@ -0,0 +1,8 @@ +import type { EntityByMetaType, MetaType } from './model' + +export function isType( + metaType: T, + entity: unknown +): entity is EntityByMetaType[T] { + return (entity as any)?.meta?.type === metaType +} diff --git a/test/model/isType.test.ts b/test/model/isType.test.ts new file mode 100644 index 0000000..b9cbb12 --- /dev/null +++ b/test/model/isType.test.ts @@ -0,0 +1,13 @@ +import { CustomerOrder, Demand, MetaType, RetailDemand } from '../../build/src' +import { isType } from '../../src/tools' +import { getTypedVar } from '../tools' + +const doc = getTypedVar() + +if (isType(MetaType.RetailDemand, doc)) { + const type: MetaType.RetailDemand = doc.meta.type + + doc.cashSum +} else { + const type: MetaType.CustomerOrder | MetaType.Demand = doc.meta.type +} diff --git a/test/tools.ts b/test/tools.ts new file mode 100644 index 0000000..07ba844 --- /dev/null +++ b/test/tools.ts @@ -0,0 +1,3 @@ +export function getTypedVar(): T { + return null as any +}