Skip to content

Commit

Permalink
feat(endpoints):
Browse files Browse the repository at this point in the history
- add PUT /new (not implemented)
- add POST entity/{type} (collection only)
  • Loading branch information
wmakeev committed Jul 20, 2021
1 parent eec83bf commit 399add4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
20 changes: 8 additions & 12 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#TODO

- [ ] Расширить Expand для записи полей через запятую

```ts
Expand<CustomerOrder, 'agent.group,state'>
```
- [ ] Подумать как работать с обобщенными запросами

```ts
ms.GET(nextHref, query)
```
# TODO

- [ ] Нужен еще один слой Create как и для Patch?

- [ ] Нужно ли типизировать template литералами href в meta?

Подобная типизация позволит, к примеру, проверить, что корректно указаны
пользовательские аттрибуты в поле `attributes` по типу сущности в href

- [ ] Переделать тип `EndpointInterface` (response зависит не только от Method
и Endpoint, но и от Payload).

Скорее всего нужно ввести типы:
- `PayloadType<Method, Endpoint>`
- `ResponseType<Method, Endpoint, Payload>`
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.7",
"version": "0.2.8",
"description": "Объектная модель API МойСклад для TypeScript проектов",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
55 changes: 44 additions & 11 deletions src/model/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { EntityByMetaType, DocumentWithPositionsMetaType } from '..'
import type { Collection } from '../Collection'
import type { DocumentMetaType } from '../Document'
import type { Expand, Patch } from '../utils'
import type { Document, DocumentMetaType } from '../Document'
import type { Meta } from '../Meta'
import type { Expand, Patch, PatchCollection, Template } from '../utils'
import type { DomineEntityMetaType } from './types'

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
Expand Down Expand Up @@ -52,7 +53,7 @@ export type EndpointInterface<
string extends Endpoint
? EndpointInterfaceInfo<unknown, unknown, undefined>

// entity/{type}/{id}
// entity/{type}/{..}
: Endpoint extends `entity/${infer EntityType}/${string}`
? EntityType extends DomineEntityMetaType
// GET entity/{type}/{id}
Expand All @@ -63,13 +64,31 @@ export type EndpointInterface<
ExpandStr
>

// PUT entity/{type}/{id}
// PUT entity/{type}/{..}
: Method extends 'PUT'
? EndpointInterfaceInfo<
Patch<EntityType>,
EntityByMetaType[EntityType],
ExpandStr
>
? Endpoint extends `entity/${EntityType}/${infer Rest}`

// PUT entity/{type}/new
? Rest extends 'new'
? EntityType extends DocumentMetaType
? EntityByMetaType[EntityType] extends Document<EntityType>
? EndpointInterfaceInfo<
// TODO Для разных типов могут быть разные поля
any,
// TODO Не прописаны сущности для всех документов
unknown, // Template<EntityByMetaType[EntityType]>,
ExpandStr
>
: never
: never

// PUT entity/{type}/{id}
: EndpointInterfaceInfo<
Patch<EntityType>,
EntityByMetaType[EntityType],
ExpandStr
>
: never

// Unknown method
: EndpointInterfaceInfo<never, unknown, undefined>
Expand All @@ -83,7 +102,21 @@ export type EndpointInterface<
// GET entity/{type}
? Method extends 'GET'
? EndpointInterfaceInfo<never, Collection<EntityType>, ExpandStr>
: EndpointInterfaceInfo<never, unknown, undefined>
: EndpointInterfaceInfo<unknown, unknown, undefined>

// FIXME Нужно переделать с зависимостью от Payload
// POST entity/{type}
: Method extends 'POST'
? EndpointInterfaceInfo<
Array<Patch<EntityType> & { meta?: Meta<EntityType> }>,
EntityByMetaType[EntityType][],
ExpandStr
>

// Unknown method
: EndpointInterfaceInfo<never, unknown, undefined>

// Unknown {type}
: EndpointInterfaceInfo<unknown, unknown, undefined>

// Unknown endpoint
: EndpointInterfaceInfo<unknown, unknown, undefined>
21 changes: 18 additions & 3 deletions test/model/api/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const t2: 'foo' = {} as EndpointInterface<
'entity/customerorder'
>['response']

const t3: EndpointInterface<'POST', 'entity/customerorder'>['response'] =
'unknown'

const t4: IsExtends<
EndpointInterface<'GET', 'entity/customerorder'>['response'],
Collection<'customerorder'>
Expand Down Expand Up @@ -100,6 +97,9 @@ const t11_1: Partial<CustomerOrder> = {
// @ts-expect-error
const t11_2 = request('PUT', 'entity/customerorder/123-456', t11_1)

// TODO [PUT ../new] не реализован
const t11_3: unknown = request('PUT', 'entity/invoiceout/new', t11_1)

// Expand

const t12_1 = request(
Expand All @@ -112,3 +112,18 @@ const t12_1 = request(
t12_1.rows[0].agent.name

t12_1.rows[0].positions.rows[0].assortment.id

// POST

const t13_1: CustomerOrder[] = request('POST', 'entity/customerorder', [
{
meta: {
type: 'customerorder',
href: ''
},
name: 'foo'
},
{
description: 'bar'
}
])

0 comments on commit 399add4

Please sign in to comment.