Skip to content

Commit

Permalink
feat: адаптация для модели
Browse files Browse the repository at this point in the history
- адаптация хелперов для использования совместо с моделью МойСклад
- расширенная типизация

BREAKING CHANGE
  • Loading branch information
wmakeev committed Jul 17, 2021
1 parent ddc18bc commit 6aad759
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 206 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moysklad-helpers",
"version": "2.0.4",
"version": "3.0.0-beta.1",
"description": "Вспомогательные функции для работы с библиотекой moysklad",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -10,7 +10,9 @@
"scripts": {
"format": "prettier --write \"src/**/*.ts\"",
"build": "npm run format && rm -rf dist/* && tsc --build tsconfig.deploy.json",
"test": "node dist/tests/index.test.js | tap-spec"
"test": "npm run build && node dist/tests/index.test.js | tap-spec",
"git:tag": "git tag \"v$(cat package.json | json version)\"",
"npm:publish": "npm run test && npm publish && npm run git:tag"
},
"repository": {
"type": "git",
Expand All @@ -29,10 +31,11 @@
},
"homepage": "https://github.com/wmakeev/moysklad-helpers#readme",
"devDependencies": {
"@types/tape": "^4.13.0",
"moysklad": "0.9.2",
"prettier": "^2.2.1",
"@types/tape": "^4.13.1",
"moysklad": "^0.10.0",
"moysklad-api-model": "^0.2.0",
"prettier": "^2.3.2",
"tape": "^5.2.2",
"typescript": "^4.2.3"
"typescript": "^4.3.5"
}
}
155 changes: 0 additions & 155 deletions src/getHelpers.ts

This file was deleted.

138 changes: 138 additions & 0 deletions src/helpers/getHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import type { Instance } from 'moysklad'
import {
DocumentPositionType,
DocumentWithPositionsMetaType,
MetaType
} from 'moysklad-api-model'
import { EntityRef, isEntityRef, HrefMetaType, Meta } from '../types'
import { getRefMetaType } from './getRefMetaType'

export function getHelpers(ms: Instance) {
/**
* Возвращает href для некого пути
*/
function href<T extends string>(ref: T): string

function href<M extends MetaType>(entityRef: EntityRef<M>): string

function href(path: any) {
if (isEntityRef(path)) {
return ms.buildUrl(path.meta.href)
} else {
return ms.buildUrl(path)
}
}

function meta<T extends string>(path: T): Meta<HrefMetaType<T>>

function meta<M extends MetaType>(entityRef?: EntityRef<M>): Meta<M>

function meta(path: any) {
return {
type: getRefMetaType(path),
href: href(path)
}
}

const attr = <T>(path: string, value: T) => {
if (getRefMetaType(path) !== 'attributemetadata') {
throw new Error('attr: Href не соответствует типу атрибута')
}

return {
meta: {
type: 'attributemetadata',
href: href(path)
},
value
}
}

function ref<T extends string>(path: T): EntityRef<HrefMetaType<T>>
function ref<T extends string>(
path: T | undefined
): EntityRef<HrefMetaType<T>> | undefined

function ref<M extends MetaType>(entityRef?: EntityRef<M>): EntityRef<M>
function ref<M extends MetaType>(
entityRef?: EntityRef<M> | undefined
): EntityRef<M> | undefined

function ref(path: any) {
return path != null ? { meta: meta(path) } : undefined
}

function positionRef<T extends string>(
documentRef: T,
positionId: string
): HrefMetaType<T> extends DocumentWithPositionsMetaType
? EntityRef<DocumentPositionType[HrefMetaType<T>]>
: never

function positionRef<M extends DocumentWithPositionsMetaType>(
documentRef: EntityRef<M>,
positionId: string
): EntityRef<DocumentPositionType[M]>

function positionRef(...args: any[]) {
const docHref = href(args[0])
return ref(`${docHref}/positions/${args[1]}`)
}

const refEqual = (
entityRef1: string | EntityRef | null | undefined,
entityRef2: string | EntityRef | null | undefined
): boolean => {
return entityRef1 == null || entityRef2 == null
? false
: href(entityRef1 as any) === href(entityRef2 as any)
}

function copyFieldsRefs<T extends EntityRef, K extends keyof T>(
srcEntity: T,
fieldNames: K[]
) {
return fieldNames.reduce(
(res, fieldName) => {
const curFieldVal = srcEntity[fieldName]

if (isEntityRef(curFieldVal)) {
res[fieldName] = ref(curFieldVal as EntityRef<MetaType>) as any
} else {
res[fieldName] = curFieldVal as any
}

return res
},
{} as {
[P in K]: T[P] extends EntityRef<infer M> ? EntityRef<M> : T[P]
}
)
}

function copyFields<T extends EntityRef, K extends keyof T>(
srcEntity: T,
fieldNames: Array<K>
) {
return fieldNames.reduce((res, fieldName) => {
const curFieldVal = srcEntity[fieldName]

if (curFieldVal != null) {
res[fieldName] = curFieldVal
}

return res
}, {} as { [P in K]: T[P] })
}

return {
href,
attr,
meta,
ref,
positionRef,
refEqual,
copyFields,
copyFieldsRefs
}
}
Loading

0 comments on commit 6aad759

Please sign in to comment.