Skip to content

Commit

Permalink
feat(endpoints): add endpoints string template type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 20, 2021
1 parent 2d499b4 commit 2b02b7b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 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.9",
"version": "0.2.10",
"description": "Объектная модель API МойСклад для TypeScript проектов",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
95 changes: 73 additions & 22 deletions src/model/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,81 @@ import type { DomineEntityMetaType } from './types'

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'

//#region Endpoints

export type AuditCollectionEndpoint = `audit`

export type AuditEndpoint = `audit/${Id}`

export type AuditEventCollectionEndpoint = `audit/${Id}/events`

export type EmployeeContextEndpoint = `context/employee`

export type DownloadEndpoint = `download/${Id}`

export type AssortmentCollectionEndpoint = `entity/assortment`

export type CustomEntityCollectionEndpoint = `entity/customentity/${Id}`

export type CustomEntityEndpoint = `entity/customentity/${Id}/${Id}`

export type CompanyAccountCollectionEndpoint<
T extends CompanyMetaType = CompanyMetaType
> = `entity/${T}/${Id}/accounts`

export type DomineEntityCollectionEndpoint<
T extends DomineEntityMetaType = DomineEntityMetaType
> = `entity/${T}`

export type EntityMetadataEndpoint<
T extends DomineEntityMetaType = DomineEntityMetaType
> = `entity/${T}/metadata`

export type EntityAttributeMetadataEndpoint<
T extends DomineEntityMetaType = DomineEntityMetaType
> = `entity/${T}/metadata/attributes`

export type DomineEntityEndpoint<
T extends DomineEntityMetaType = DomineEntityMetaType
> = `entity/${T}/${Id}`

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

export type DocumentPositionCollectionEndpoint<
T extends DocumentWithPositionsMetaType = DocumentWithPositionsMetaType
> = `entity/${T}/${Id}/positions`

export type DocumentPositionEndpoint<
T extends DocumentWithPositionsMetaType = DocumentWithPositionsMetaType
> = `entity/${T}/${Id}/positions/${Id}`

//#endregion

export type HttpMethodPath = {
GET:
| `audit`
| `audit/${Id}`
| `audit/${Id}/events`
| `context/employee`
| `download/${Id}`
| `entity/assortment`
| `entity/customentity/${Id}`
| `entity/${CompanyMetaType}/${Id}/accounts`
| `entity/${DomineEntityMetaType}` // +
| `entity/${DomineEntityMetaType}/metadata`
| `entity/${DomineEntityMetaType}/metadata/attributes`
| `entity/${DomineEntityMetaType}/${Id}`
| `entity/${DocumentWithPositionsMetaType}/${Id}/positions`
| `entity/${DocumentWithPositionsMetaType}/${Id}/positions/${Id}`

POST:
| `entity/${DomineEntityMetaType}`
| `entity/${DocumentWithPositionsMetaType}/${Id}/positions`
| AuditCollectionEndpoint
| AuditEndpoint
| AuditEventCollectionEndpoint
| EmployeeContextEndpoint
| DownloadEndpoint
| AssortmentCollectionEndpoint
| CustomEntityCollectionEndpoint
| CompanyAccountCollectionEndpoint
| DomineEntityCollectionEndpoint
| EntityMetadataEndpoint
| EntityAttributeMetadataEndpoint
| DomineEntityEndpoint
| DocumentPositionCollectionEndpoint
| DocumentPositionEndpoint

POST: DomineEntityCollectionEndpoint | DocumentPositionCollectionEndpoint

// TODO Какие точно типы поддерживают new?
PUT: `entity/${DomineEntityMetaType}/${Id}` | `entity/${DocumentMetaType}/new`
PUT: DomineEntityEndpoint | DocumentTemplateEndpoint

DELETE: `entity/${DomineEntityMetaType}/${Id}`
DELETE: DomineEntityEndpoint
}

export type EndpointInterfaceInfo<
Expand All @@ -55,7 +105,7 @@ export type EndpointInterface<
? EndpointInterfaceInfo<unknown, unknown, undefined>

// entity/{type}/{..}
: Endpoint extends `entity/${infer EntityType}/${Id}`
: Endpoint extends DomineEntityEndpoint<infer EntityType>
? EntityType extends DomineEntityMetaType
// GET entity/{type}/{id}
? Method extends 'GET'
Expand Down Expand Up @@ -98,7 +148,8 @@ export type EndpointInterface<
: EndpointInterfaceInfo<unknown, unknown, undefined>

// entity/{type}
: Endpoint extends `entity/${infer EntityType}`
: Endpoint extends DomineEntityCollectionEndpoint<infer EntityType>
// TODO Эту ветку можно убрать? Стоит ограничение на тип в DomineEntityCollectionEndpoint.
? EntityType extends DomineEntityMetaType
// GET entity/{type}
? Method extends 'GET'
Expand Down

0 comments on commit 2b02b7b

Please sign in to comment.