Skip to content

Commit

Permalink
feat: add fileAttr helper
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 29, 2022
1 parent 993d9cf commit 47739b7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"update": "npm i moysklad-api-model@latest",
"format": "prettier --write \"src/**/*.ts\"",
"build": "npm run format && rm -rf dist/* && tsc --build tsconfig.deploy.json",
"test": "npm run build && node dist/tests | tap-spec",
"git:tag": "git tag \"v$(cat package.json | json version)\"",
"npm:publish": "npm run test && npm publish && npm run git:tag && git push"
"test": "npm run build && node dist/tests",
"version": "auto-changelog -p && git add CHANGELOG.md",
"release": "np"
},
"repository": {
"type": "git",
Expand All @@ -33,13 +33,14 @@
"homepage": "https://github.com/wmakeev/moysklad-helpers#readme",
"devDependencies": {
"@types/tape": "^4.13.1",
"auto-changelog": "^2.4.0",
"json": "^11.0.0",
"moysklad": "^0.10.0",
"prettier": "^2.3.2",
"tape": "^5.2.2",
"typescript": "^4.3.5"
},
"dependencies": {
"moysklad-api-model": "^0.5.4"
"moysklad-api-model": "^0.5.5"
}
}
15 changes: 15 additions & 0 deletions src/helpers/getHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ export function getHelpers(ms: { buildUrl: (path: string) => string }) {
}
}

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

return {
meta: {
type: 'attributemetadata' as const,
href: href(path)
},
file
}
}

function ref<T extends string>(path: T): EntityRef<HrefMetaType<T>>
function ref<T extends string>(
path: T | undefined
Expand Down Expand Up @@ -142,6 +156,7 @@ export function getHelpers(ms: { buildUrl: (path: string) => string }) {
return {
href,
attr,
fileAttr,
meta,
ref,
positionRef,
Expand Down
36 changes: 36 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ test('attr', t => {
t.end()
})

test('fileAttr', t => {
const ms = Moysklad({ apiVersion: '1.2' })
const { fileAttr } = getHelpers(ms)

const ref1 =
'entity/customerorder/metadata/attributes/39f9f7bc-d4da-11e4-95df-0cc47a05161a'

const HREF1 = ms.buildUrl(ref1)

const file = {
filename: 'file.jpg',
content: '123'
}

const fileAttr1 = {
meta: {
type: 'attributemetadata',
href: HREF1
},
file
}

t.deepEqual(fileAttr(HREF1, file), fileAttr1, 'should return href for href')
t.deepEqual(fileAttr(ref1, file), fileAttr1, 'should return href for ref')

t.throws(
() => {
fileAttr('entity/customerorder', 'bar')
},
/Href не соответствует типу атрибута/,
'should throw error on not attribute href'
)

t.end()
})

test('ref', t => {
const ms = Moysklad({ apiVersion: '1.2' })
const { ref } = getHelpers(ms)
Expand Down

0 comments on commit 47739b7

Please sign in to comment.