mirror of
https://github.com/hcengineering/platform.git
synced 2025-02-06 19:11:01 +00:00
29 lines
671 B
TypeScript
29 lines
671 B
TypeScript
|
import PQueue from 'p-queue'
|
||
|
import { stringify as toQuery } from 'qs'
|
||
|
import { BitrixResult } from './types'
|
||
|
|
||
|
const queue = new PQueue({
|
||
|
intervalCap: 1,
|
||
|
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()
|
||
|
})
|
||
|
}
|
||
|
}
|