From 47739b723f3cf591338c0873fd3ae3e349b76a4e Mon Sep 17 00:00:00 2001 From: "Vitaliy V. Makeev" Date: Fri, 29 Jul 2022 10:57:44 +0500 Subject: [PATCH] feat: add fileAttr helper --- package.json | 9 +++++---- src/helpers/getHelpers.ts | 15 +++++++++++++++ tests/index.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a56505d..06e77bc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -33,6 +33,7 @@ "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", @@ -40,6 +41,6 @@ "typescript": "^4.3.5" }, "dependencies": { - "moysklad-api-model": "^0.5.4" + "moysklad-api-model": "^0.5.5" } } diff --git a/src/helpers/getHelpers.ts b/src/helpers/getHelpers.ts index 1a05916..8034831 100644 --- a/src/helpers/getHelpers.ts +++ b/src/helpers/getHelpers.ts @@ -65,6 +65,20 @@ export function getHelpers(ms: { buildUrl: (path: string) => string }) { } } + const fileAttr = (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(path: T): EntityRef> function ref( path: T | undefined @@ -142,6 +156,7 @@ export function getHelpers(ms: { buildUrl: (path: string) => string }) { return { href, attr, + fileAttr, meta, ref, positionRef, diff --git a/tests/index.test.ts b/tests/index.test.ts index 4322a12..a477c3d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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)