From 1529d41f45bea01486c18cd937f70f313d761a3d Mon Sep 17 00:00:00 2001 From: Pawel Idczak Date: Mon, 19 Feb 2024 00:07:51 +0100 Subject: [PATCH] enable autologin --- README.md | 1 - examples/basic.mjs | 1 - examples/basic.ts | 1 - src/bir.ts | 14 +++++++++++++- test/bir.ts | 6 ------ 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c160f74..1aab71e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ npm install bir1 import Bir from 'bir1' const bir = new Bir() -await bir.login() console.log(await bir.search({ nip: '5261040567' })) // output: // { diff --git a/examples/basic.mjs b/examples/basic.mjs index d2a9aba..db871e6 100644 --- a/examples/basic.mjs +++ b/examples/basic.mjs @@ -1,7 +1,6 @@ import Bir from 'bir1' const bir = new Bir({ key: process.env.KEY }) -await bir.login() console.log('StanDanych: ', await bir.value('StanDanych')) console.log('StatusUslugi:', await bir.value('StatusUslugi')) console.log('KomunikatUslugi:', await bir.value('KomunikatUslugi')) diff --git a/examples/basic.ts b/examples/basic.ts index 1c9d9a0..03eea8f 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -2,7 +2,6 @@ import Bir from '../src/bir' ;(async function main() { const bir = new Bir({ key: process.env.KEY }) - await bir.login() console.log('StanDanych: ', await bir.value('StanDanych')) console.log('StatusUslugi:', await bir.value('StatusUslugi')) console.log('KomunikatUslugi:', await bir.value('KomunikatUslugi')) diff --git a/src/bir.ts b/src/bir.ts index e7ee32d..9febf80 100644 --- a/src/bir.ts +++ b/src/bir.ts @@ -16,7 +16,6 @@ import { * ```js * import Bir from 'bir1' * const bir = new Bir() - * await bir.login() * console.log(await bir.search({ nip: '5261040567' })) * // output: * // { @@ -91,11 +90,21 @@ export default class Bir { this.sid = sid } + /** + * Automatically login to the API if not already logged in + */ + async autologin() { + if (!this.sid) { + await this.login() + } + } + /** * Get diagnostic information (method: GetValue) * @param value value to retrieve */ async value(value: GetValueOptions) { + await this.autologin() const body = await template('GetValue', { value }) const response = await this.api({ body }) return unsoap(response) @@ -112,6 +121,7 @@ export default class Bir { * Only one of the query parameters can be used at a time. */ async search(query: { nip: string } | { regon: string } | { krs: string }) { + await this.autologin() const body = await template('DaneSzukajPodmioty', query) const response = await this.api({ body }) return this.normalize(parse(unsoap(response))) @@ -126,6 +136,7 @@ export default class Bir { regon: string report: DanePobierzPelnyRaportOptions }) { + await this.autologin() const body = await template('DanePobierzPelnyRaport', query) const response = await this.api({ body }) return this.normalize(parse(unsoap(response))) @@ -140,6 +151,7 @@ export default class Bir { date: string report: DanePobierzRaportZbiorczyOptions }) { + await this.autologin() const body = await template('DanePobierzRaportZbiorczy', query) const response = await this.api({ body }) return this.normalize(parse(unsoap(response))) diff --git a/test/bir.ts b/test/bir.ts index 6331e01..735c048 100644 --- a/test/bir.ts +++ b/test/bir.ts @@ -1,10 +1,8 @@ import t from 'tap' import Bir from '../src/index' - t.test('search by REGON for example company', async (t) => { const bir = new Bir() - await bir.login() const result = await bir.search({ regon: '012100784' }) t.match(result, { Nazwa: 'ORANGE POLSKA SPÓŁKA AKCYJNA' }) t.end() @@ -12,7 +10,6 @@ t.test('search by REGON for example company', async (t) => { t.test('search by NIP for example company', async (t) => { const bir = new Bir() - await bir.login() const result = await bir.search({ nip: '5261040567' }) t.match(result, { Nazwa: 'T-MOBILE POLSKA SPÓŁKA AKCYJNA' }) t.end() @@ -20,7 +17,6 @@ t.test('search by NIP for example company', async (t) => { t.test('search by KRS for example company', async (t) => { const bir = new Bir() - await bir.login() const result = await bir.search({ krs: '0000391193' }) t.match(result, { Nazwa: 'T-MOBILE POLSKA SPÓŁKA AKCYJNA' }) t.end() @@ -28,7 +24,6 @@ t.test('search by KRS for example company', async (t) => { t.test('search fail for non existing company', async (t) => { const bir = new Bir() - await bir.login() const searchNotExisting = async () => await bir.search({ regon: 'notExisting' }) await t.rejects(searchNotExisting, { @@ -39,7 +34,6 @@ t.test('search fail for non existing company', async (t) => { t.test('report existing company', async (t) => { const bir = new Bir() - await bir.login() const result = await bir.report({ regon: '011417295', report: 'BIR11OsPrawna',