Skip to content

Instantly share code, notes, and snippets.

@vojtechjurasek
Created May 31, 2023 18:02
Show Gist options
  • Save vojtechjurasek/95a8111e34149e583836839912d95218 to your computer and use it in GitHub Desktop.
Save vojtechjurasek/95a8111e34149e583836839912d95218 to your computer and use it in GitHub Desktop.
import axios from 'axios'
import XmlParser from 'xml-parser'
const url = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi'
const getUrl = (options: any) => {
options = Object.keys(options)
.map((key) => key + '=' + options[key])
.join('&')
return url + '?' + options
}
const find = ({
name,
value,
limit = 10,
type = 'free',
}: {
name: string
value: string | number | null
limit?: number
type?: string
}) => {
const options: any = {
czk: 'utf',
aktivni: true,
max_pocet: limit,
typ_vyhledani: type,
}
options[name] = value
if (limit === null) {
delete options.max_pocet
}
return new Promise((resolve, reject) => {
const requestUrl = getUrl(options)
axios
.get(requestUrl)
.then((response) => {
const xml = XmlParser(response.data)
const result = parse(xml)
resolve(result)
})
.catch((error) => {
reject(error)
})
})
}
const parse = (data: any) => {
let child: { name: string; children: any; content: any }
data = data.root.children[0].children
for (child of Array.from(data)) {
if (child.name === 'are:Error') {
for (let error of Array.from(child.children)) {
if (error.name === 'dtt:Error_text') {
throw new Error(error.content)
}
}
throw new Error() // just to be sure
}
}
const result = {
length: 0,
data: [],
}
for (child of Array.from(data)) {
if (child.name === 'are:Pocet_zaznamu') {
result.length = parseInt(child.content)
} else if (child.name === 'are:Zaznam') {
result.data.push(parseItem(child.children))
}
}
return result
}
const parseItem = (item: any) => {
const result: any = {
created: null,
validity: null,
name: null,
identification: null,
address: {
district: null,
city: null,
street: null,
descriptionNumber: null,
orientationNumber: null,
zipCode: null,
},
}
for (let child of Array.from(item)) {
switch (child.name) {
case 'are:Datum_vzniku':
result.created = new Date(child.content)
break
case 'are:Datum_platnosti':
result.validity = new Date(child.content)
break
case 'are:Obchodni_firma':
result.name = child.content
break
case 'are:ICO':
result.identification = parseInt(child.content)
break
case 'are:Identifikace':
for (let identification of Array.from(child.children)) {
if (identification.name === 'are:Adresa_ARES') {
for (let address of Array.from(identification.children)) {
switch (address.name) {
case 'dtt:Nazev_okresu':
result.address.district = address.content
break
case 'dtt:Nazev_obce':
result.address.city = address.content
break
case 'dtt:Nazev_ulice':
result.address.street = address.content
break
case 'dtt:Cislo_domovni':
result.address.descriptionNumber = parseInt(address.content)
break
case 'dtt:Cislo_orientacni':
result.address.orientationNumber = address.content
break
case 'dtt:PSC':
result.address.zipCode = parseInt(address.content)
break
}
}
}
}
break
}
}
return result
}
export const getCompanyByBusinessID = async (businessID: string) => {
return find({
name: 'ico',
value: businessID,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment