Skip to content

Commit

Permalink
feat(Metadata): add Metadata type
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 31, 2022
1 parent ba0b4eb commit 1e970a5
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/model/AbstarctDictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type DictionaryMetaType =
| 'counterparty'
| 'product'
| 'productfolder'
| 'variant'
| 'project'
| 'store'
65 changes: 65 additions & 0 deletions src/model/Metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type {
DocumentMetaType,
DictionaryMetaType,
AttributeType,
Entity,
CollectionRef,
State
} from '.'

/**
* Типы сущностей у которых есть метаданные
*/
export type MetadataMetaType = DocumentMetaType | DictionaryMetaType

export type MetadataMeta<T extends MetadataMetaType = MetadataMetaType> = {
meta: {
/** `https://online.moysklad.ru/api/remap/1.2/entity/product/metadata` */
href: `https://${string}/api/remap/1.2/entity/${T}/metadata`
mediaType: 'application/json'
}
}

export interface MetadataAttribute extends Entity<'attributemetadata'> {
/** Наименование пользовательского поля */
readonly name: string

/** Тип значения пользовательского поля */
readonly type: AttributeType

readonly required: boolean

readonly show: true
}

export type DocumentMetadata<T extends DocumentMetaType = DocumentMetaType> =
MetadataMeta<T> & {
attributes: CollectionRef<'attributemetadata'>
states: State<T>[]
createShared: boolean
}

// TODO
export type DictionaryMetadata<
T extends DictionaryMetaType = DictionaryMetaType
> = MetadataMeta<T> & {
attributes: CollectionRef<'attributemetadata'>
createShared: boolean
} & (T extends 'counterparty' ? { states: State<T>[] } : {})

// prettier-ignore

export type Metadata<T extends MetadataMetaType> =
// Документы
T extends DocumentMetaType
? DocumentMetadata<T>

// Справочники
: T extends DictionaryMetaType
? T extends 'project' | 'store' | 'product' | 'counterparty'
? DictionaryMetadata<T>

// Пустые метаданные
: MetadataMeta<T>

: never
2 changes: 2 additions & 0 deletions src/model/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export type EndpointInterface<
ExpandStr
>

// GET ..

// PUT entity/{type}/{..}
: Method extends 'PUT'
? Endpoint extends `entity/${EntityType}/${infer Rest}`
Expand Down
10 changes: 6 additions & 4 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Добавить новые сущности аналогично в MetaType.ts

// Model
export * from './AbstarctDictionary'
export * from './AbstractDemand'
export * from './AbstractFile'
export * from './AbstractGood'
Expand Down Expand Up @@ -44,15 +45,16 @@ export * from './HasUpdated'
export * from './HasVat'
export * from './Id'
export * from './Image'
export * from './Inventory'
export * from './InventoryPosition'
export * from './Invoice'
export * from './InvoiceIn'
export * from './InvoiceOut'
export * from './InvoicePosition'
export * from './Inventory'
export * from './InventoryPosition'
export * from './Legal'
export * from './MediaType'
export * from './Meta'
export * from './Metadata'
export * from './MetaType'
export * from './MoyskladObject'
export * from './Order'
Expand All @@ -69,10 +71,10 @@ export * from './Product'
export * from './ProductFolder'
export * from './Rate'
export * from './RetailDemand'
export * from './State'
export * from './SalesReturn'
export * from './RetailSalesReturn'
export * from './SalesReturn'
export * from './SalesReturnPosition'
export * from './State'
export * from './TaxSystem'

// Utility types
Expand Down
6 changes: 6 additions & 0 deletions src/model/utils/Expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CollectionRef,
EntityByMetaType,
EntityRef,
MetadataAttribute,
MetaType
} from '../'

Expand Down Expand Up @@ -68,6 +69,11 @@ export type ExpandField<T, K extends keyof T> = {
? T[P]

// CollectionRef

// - исключение для attributemetadata (если коллекция то, другая структура)
: T[P] extends CollectionRef<'attributemetadata'>
? Collection<MetadataAttribute>

: T[P] extends CollectionRef<M>
? Collection<EntityByMetaType[M]>

Expand Down
57 changes: 57 additions & 0 deletions test/model/Metadata.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expectType } from 'tsd'
import type { CollectionRef, Metadata, State } from '../../src'

// @ts-expect-error account не имеет метаданных
const t0 = {} as Metadata<'account'>

// Document

const t1 = {} as Metadata<'demand'>

expectType<`https://${string}/api/remap/1.2/entity/demand/metadata`>(
t1.meta.href
)

expectType<CollectionRef<'attributemetadata'>>(t1.attributes)

expectType<State<'demand'>>(t1.states[0])

expectType<boolean>(t1.createShared)

// Dictionary

const t3 = {} as Metadata<'product'>

expectType<CollectionRef<'attributemetadata'>>(t3.attributes)

// @ts-expect-error
t3.states

expectType<boolean>(t3.createShared)

// Counterparty

const t4 = {} as Metadata<'counterparty'>

expectType<CollectionRef<'attributemetadata'>>(t4.attributes)

expectType<State<'counterparty'>>(t4.states[0])

expectType<boolean>(t3.createShared)

// Other

const t5 = {} as Metadata<'variant'>

// @ts-expect-error
t5.attributes

// @ts-expect-error
t5.states

// @ts-expect-error
t5.createShared

expectType<`https://${string}/api/remap/1.2/entity/variant/metadata`>(
t5.meta.href
)
10 changes: 9 additions & 1 deletion test/model/utils/Expand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
AttributeBase,
Entity,
Product,
DocumentWithPositions
DocumentWithPositions,
Metadata
} from '../../../src'

//#region
Expand Down Expand Up @@ -338,3 +339,10 @@ t80.positions.rows[0].assortment.id
// TODO 'positions.assortment,agent.attributes.value'
// TODO 'attributes.value,agent.attributes.value'
// TODO 'agent,operations,operations.customerOrder'

//#region Expand Metadata
const t90 = {} as Expand<Metadata<'demand'>, 'attributes'>

t90.attributes.meta.href
t90.attributes.rows[0].show
//#endregion

0 comments on commit 1e970a5

Please sign in to comment.