Skip to content

Commit

Permalink
feat(model)
Browse files Browse the repository at this point in the history
+ Demand
+ RetailDemand
+ tools/isType
  • Loading branch information
wmakeev committed Jul 2, 2021
1 parent 9da6747 commit a599fdc
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './model'
export * from './tools'
3 changes: 3 additions & 0 deletions src/model/Demand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { DocumentWithPositions, HasVat, MetaType } from '.'

export type Demand = DocumentWithPositions<MetaType.Demand> & HasVat
1 change: 1 addition & 0 deletions src/model/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Owned } from './Owned'
export type DocumentMetaType =
| MetaType.CustomerOrder
| MetaType.Demand
| MetaType.RetailDemand
| MetaType.PurchaseOrder
// TODO Описать остальные

Expand Down
1 change: 1 addition & 0 deletions src/model/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions src/model/RetailDemand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { DocumentWithPositions, MetaType } from '.'

export interface RetailDemand
extends DocumentWithPositions<MetaType.RetailDemand> {
cashSum: number
noCashSum: number
}
2 changes: 2 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
8 changes: 8 additions & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { EntityByMetaType, MetaType } from './model'

export function isType<T extends MetaType>(
metaType: T,
entity: unknown
): entity is EntityByMetaType[T] {
return (entity as any)?.meta?.type === metaType
}
13 changes: 13 additions & 0 deletions test/model/isType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CustomerOrder, Demand, MetaType, RetailDemand } from '../../build/src'
import { isType } from '../../src/tools'
import { getTypedVar } from '../tools'

const doc = getTypedVar<CustomerOrder | Demand | RetailDemand>()

if (isType(MetaType.RetailDemand, doc)) {
const type: MetaType.RetailDemand = doc.meta.type

doc.cashSum
} else {
const type: MetaType.CustomerOrder | MetaType.Demand = doc.meta.type
}
3 changes: 3 additions & 0 deletions test/tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getTypedVar<T>(): T {
return null as any
}

0 comments on commit a599fdc

Please sign in to comment.