UNPKG

moysklad-api-model

Version:

Объектная модель API МойСклад для TypeScript проектов

39 lines (38 loc) 4.19 kB
import type { EntityByMetaType, DocumentWithPositionsMetaType, Id } from '..'; 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 } from '../utils'; import type { DomineEntityMetaType } from './types'; export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'; 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 PrefilledDocumentEndpoint<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}`; export type HttpMethodPath = { GET: AuditCollectionEndpoint | AuditEndpoint | AuditEventCollectionEndpoint | EmployeeContextEndpoint | DownloadEndpoint | AssortmentCollectionEndpoint | CustomEntityCollectionEndpoint | CompanyAccountCollectionEndpoint | DomineEntityCollectionEndpoint | EntityMetadataEndpoint | EntityAttributeMetadataEndpoint | DomineEntityEndpoint | DocumentPositionCollectionEndpoint | DocumentPositionEndpoint; POST: DomineEntityCollectionEndpoint | DocumentPositionCollectionEndpoint; PUT: DomineEntityEndpoint | PrefilledDocumentEndpoint; DELETE: DomineEntityEndpoint; }; export type EndpointInterfaceInfo<Payload, Response, ExpandStr extends string | undefined> = { payload: Payload; response: ExpandStr extends string ? Expand<Response, ExpandStr> : Response; }; export type EndpointInterface<Method extends HttpMethod, Endpoint extends HttpMethodPath[Method], ExpandStr extends string | undefined = undefined> = string extends Endpoint ? EndpointInterfaceInfo<unknown, unknown, undefined> : Endpoint extends DomineEntityEndpoint<infer EntityType> ? EntityType extends DomineEntityMetaType ? Method extends 'GET' ? EndpointInterfaceInfo<never, EntityByMetaType[EntityType], ExpandStr> : Method extends 'PUT' ? Endpoint extends `entity/${EntityType}/${infer Rest}` ? Rest extends 'new' ? EntityType extends DocumentMetaType ? EntityByMetaType[EntityType] extends Document<EntityType> ? EndpointInterfaceInfo<any, unknown, // PrefilledDocument<EntityByMetaType[EntityType]>, ExpandStr> : never : never : EndpointInterfaceInfo<Patch<EntityType>, EntityByMetaType[EntityType], ExpandStr> : never : EndpointInterfaceInfo<never, unknown, undefined> : EndpointInterfaceInfo<unknown, unknown, undefined> : Endpoint extends DomineEntityCollectionEndpoint<infer EntityType> ? EntityType extends DomineEntityMetaType ? Method extends 'GET' ? EndpointInterfaceInfo<never, Collection<EntityByMetaType[EntityType]>, ExpandStr> : Method extends 'POST' ? EndpointInterfaceInfo<Array<Patch<EntityType> & { meta?: Meta<EntityType>; }>, EntityByMetaType[EntityType][], ExpandStr> : EndpointInterfaceInfo<never, unknown, undefined> : EndpointInterfaceInfo<unknown, unknown, undefined> : EndpointInterfaceInfo<unknown, unknown, undefined>;