2022-12-16 06:35:51 +00:00
|
|
|
import PQueue from 'p-queue'
|
|
|
|
import { stringify as toQuery } from 'qs'
|
|
|
|
import { BitrixResult } from './types'
|
|
|
|
|
|
|
|
const queue = new PQueue({
|
2023-01-04 17:58:54 +00:00
|
|
|
intervalCap: 2,
|
2022-12-16 06:35:51 +00:00
|
|
|
interval: 1000
|
|
|
|
})
|
|
|
|
export class BitrixClient {
|
|
|
|
constructor (readonly url: string) {}
|
|
|
|
|
|
|
|
async call (method: string, params: any): Promise<BitrixResult> {
|
|
|
|
return await queue.add(async () => {
|
|
|
|
let query: string = toQuery(params)
|
|
|
|
if (query.length > 0) {
|
|
|
|
query = `?${query}`
|
|
|
|
}
|
|
|
|
return await (
|
|
|
|
await fetch(`${this.url}/${method}${query}`, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'anticrm'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).json()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|