Skip to content

Commit

Permalink
enable autologin
Browse files Browse the repository at this point in the history
  • Loading branch information
pawel-id committed Feb 18, 2024
1 parent f775129 commit 1529d41
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// {
Expand Down
1 change: 0 additions & 1 deletion examples/basic.mjs
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down
1 change: 0 additions & 1 deletion examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
14 changes: 13 additions & 1 deletion src/bir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* // {
Expand Down Expand Up @@ -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)
Expand All @@ -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)))
Expand All @@ -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)))
Expand All @@ -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)))
Expand Down
6 changes: 0 additions & 6 deletions test/bir.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
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()
})

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()
})

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()
})

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, {
Expand All @@ -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',
Expand Down

0 comments on commit 1529d41

Please sign in to comment.