From 25ae83213a6b29bed85ffe0cf2e1eb3416f0ff70 Mon Sep 17 00:00:00 2001 From: Alex Step Date: Mon, 15 Mar 2021 00:13:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D0=B8=20?= =?UTF-8?q?=D0=BF=D1=83=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=20npmjs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/npmpublish.yml | 35 +++++++++++++++++++++ .gitignore | 1 + README.md | 18 +++++++++-- index.js | 8 +++-- package.json | 2 +- tests/index.test.js | 52 ++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/npmpublish.yml create mode 100644 tests/index.test.js diff --git a/.github/workflows/npmpublish.yml b/.github/workflows/npmpublish.yml new file mode 100644 index 0000000..2e07be2 --- /dev/null +++ b/.github/workflows/npmpublish.yml @@ -0,0 +1,35 @@ +name: Check and Publish Package + +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + env: + USERPASS: ${{secrets.USERPASS}} + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 12 + - run: npm ci + - run: npm test + + publish-npm: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} diff --git a/.gitignore b/.gitignore index 6704566..bbdb0cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + # Logs logs *.log diff --git a/README.md b/README.md index 869bbb3..4e1c463 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ -# moy-nalog -Обёртка для API сервиса lknpd.nalog.ru +# Отправка чеков в налоговую +Неофициальная обёртка для API сервиса lknpd.nalog.ru + +Пригодится для автоматизации отправки информации о доходах самозанятых. + + +## Использование + +... + + +[Подробное описание методов класса](/blob/main/docs/nalogAPIClass.md) + + +## Разработка +... diff --git a/index.js b/index.js index e0b0b12..f58d281 100644 --- a/index.js +++ b/index.js @@ -107,6 +107,8 @@ class NalogAPI { return this.token } + await this.authPromise + const tokenPayload = { deviceInfo: { appVersion: '1.0.0', @@ -118,7 +120,7 @@ class NalogAPI { }, refreshToken: this.refreshToken } - await this.authPromise + const response = await fetch(this.apiUrl + '/auth/token', { method: 'POST', headers: { @@ -165,7 +167,7 @@ class NalogAPI { } if (method === 'GET') delete params.body - await this.authPromise + return fetch(this.apiUrl + '/' + endpoint, params).then(r => r.json()) } @@ -198,7 +200,7 @@ class NalogAPI { services: [{ name: name, // 'Предоставление информационных услуг #970/2495', - amount: Number(amount), + amount: Number(amount.toFixed(2)), quantity: Number(quantity) }], diff --git a/package.json b/package.json index 4a7936d..f3f3b4f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "docs": "npx jsdoc-to-markdown ./index.js > docs/nalogAPIClass.md", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node tests/index.test.js" }, "repository": { "type": "git", diff --git a/tests/index.test.js b/tests/index.test.js new file mode 100644 index 0000000..7f5326c --- /dev/null +++ b/tests/index.test.js @@ -0,0 +1,52 @@ +const NalogAPI = require('../index.js') + +if (!process.env.USERPASS) { + process.exit(1) +} + +const [USER, PASS] = process.env.USERPASS.split(':') + +const tests = { + 'Логин и пароль - обязательны при autologin': async () => { + try { + // eslint-disable-next-line no-unused-vars + const ok = new NalogAPI({ password: PASS }) + } catch (err) { + return true + } + }, + + 'Проверка авто-логина': async () => { + const MyNalog = new NalogAPI({ login: USER, password: PASS }) + const profile = await MyNalog.userInfo() + + if (!profile || !profile.id) { + console.error(profile) + throw new Error('Ошибка получения информации о пользователе') + } + return true + }, + + 'Самостоятельный логин': async () => { + const MyNalog = new NalogAPI({ autologin: false }) + if (MyNalog.authPromise) { throw new Error('сработал автологин') } + const resp = await MyNalog.auth(USER, PASS) + if (!resp || !resp.refreshToken) { + console.error(resp) + throw new Error('Ошибка логина') + } + return true + } +}; + +(async () => { + for (const testName in tests) { + const result = await tests[testName]() + if (result) { + console.info(`[OK] - ${testName}`) + } else { + console.error(`[FAIL] - ${testName}`, result) + process.exit(1) + } + } +})()