Skip to content

Commit

Permalink
feat(PrefilledDocument): change and rename Template type interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 20, 2021
1 parent dd099ec commit c600599
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
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.2.11",
"version": "0.3.0",
"description": "Объектная модель API МойСклад для TypeScript проектов",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
11 changes: 11 additions & 0 deletions src/model/MetaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
CustomerOrder,
CustomerOrderPosition,
Demand,
Document,
DocumentMetaType,
Employee,
Entity,
Group,
Expand All @@ -19,6 +21,7 @@ import type {
PaymentIn,
PaymentOut,
RetailDemand,
SelectKeysByType,
State
} from '.'

Expand Down Expand Up @@ -204,3 +207,11 @@ export type EntityByMetaType = {
uom: Entity<'uom'>
variant: Entity<'variant'>
}

/**
* MetaType документов для которых реализованы типы.
* */
export type ImplementedDocumentsMetaType = SelectKeysByType<
EntityByMetaType,
Document<DocumentMetaType>
>
13 changes: 9 additions & 4 deletions src/model/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { Collection } from '../Collection'
import type { CompanyMetaType } from '../Company'
import type { Document, DocumentMetaType } from '../Document'
import type { Meta } from '../Meta'
import type { Expand, Patch, PatchCollection, Template } from '../utils'
import type {
Expand,
Patch,
PatchCollection,
PrefilledDocument
} from '../utils'
import type { DomineEntityMetaType } from './types'

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
Expand Down Expand Up @@ -46,7 +51,7 @@ export type DomineEntityEndpoint<
T extends DomineEntityMetaType = DomineEntityMetaType
> = `entity/${T}/${Id}`

export type DocumentTemplateEndpoint<
export type PrefilledDocumentEndpoint<
T extends DocumentMetaType = DocumentMetaType
> = `entity/${T}/new`

Expand Down Expand Up @@ -80,7 +85,7 @@ export type HttpMethodPath = {
POST: DomineEntityCollectionEndpoint | DocumentPositionCollectionEndpoint

// TODO Какие точно типы поддерживают new?
PUT: DomineEntityEndpoint | DocumentTemplateEndpoint
PUT: DomineEntityEndpoint | PrefilledDocumentEndpoint

DELETE: DomineEntityEndpoint
}
Expand Down Expand Up @@ -127,7 +132,7 @@ export type EndpointInterface<
// TODO Для разных типов могут быть разные поля
any,
// TODO Не прописаны сущности для всех документов
unknown, // Template<EntityByMetaType[EntityType]>,
unknown, // PrefilledDocument<EntityByMetaType[EntityType]>,
ExpandStr
>
: never
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Document, DocumentMetaType } from '../Document'
import type { DocumentWithPositionsMetaType } from '../DocumentWithPositions'
import type {
EntityByMetaType,
ImplementedDocumentsMetaType
} from '../MetaType'
import type { Expand } from './Expand'

// TODO Указать конкретные типы документов которые можно создавать этим методом

Expand All @@ -12,8 +17,10 @@ import type { Document, DocumentMetaType } from '../Document'
/**
* Шаблон нового документа `/entity/{type}/new`
*/
export type Template<T extends Document<DocumentMetaType>> = Omit<
T,
export type PrefilledDocument<T extends ImplementedDocumentsMetaType> = Omit<
T extends DocumentWithPositionsMetaType
? Expand<EntityByMetaType[T], 'positions'>
: EntityByMetaType[T],
'meta' | 'id' | 'accountId' | 'updated'
>

Expand Down
6 changes: 5 additions & 1 deletion src/model/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './Expand'
export * from './Patch'
export * from './Template'
export * from './PrefilledDocument'

// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir

Expand Down Expand Up @@ -36,3 +36,7 @@ export type RequireKeys<T, K extends keyof T> = {
{
[P in K]-?: T[P]
}

export type SelectKeysByType<T, V> = {
[K in keyof T]: T[K] extends V ? K : never
}[keyof T]
6 changes: 1 addition & 5 deletions test/model/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { InvoiceOut, Patch, Template } from '../../src'
import type { Patch, PrefilledDocument } from '../../src'
import { testTypeEqual } from '../tools'

const customerorder: Patch<'customerorder'> = {}
Expand All @@ -10,7 +10,3 @@ const invoiceOut: Patch<'invoiceout'> = {}
invoiceOut.positions?.[0].price
invoiceOut.customerOrder
invoiceOut.store

const invoiceOutTemplate = {} as Template<InvoiceOut>

invoiceOutTemplate.customerOrder
18 changes: 18 additions & 0 deletions test/model/utils/PrefilledDocument.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { PrefilledDocument } from '../../../src'

const t1 = {} as PrefilledDocument<'customerorder'>

// @ts-expect-error
t1.id

t1.agent.meta

const invoiceOutPrefilled = {} as PrefilledDocument<'invoiceout'>

invoiceOutPrefilled.customerOrder
invoiceOutPrefilled.positions.rows[0].price

const cashinPrefilled = {} as PrefilledDocument<'cashin'>

const incomingDate1: string | undefined = cashinPrefilled.incomingDate
incomingDate1
8 changes: 0 additions & 8 deletions test/model/utils/Template.test.ts

This file was deleted.

0 comments on commit c600599

Please sign in to comment.