All files / base request.base.ts

75% Statements 9/12
100% Branches 2/2
57.14% Functions 4/7
72.73% Lines 8/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 306x 6x   6x                       12x 1x   12x       9x       12x      
import Queue from 'promise-queue'
import axios, { AxiosRequestConfig } from 'axios'
 
export class RequestBase {
  static queue: Queue
 
  private static sendRequest (config: AxiosRequestConfig) {
    return new Promise((resolve, reject) => {
      axios(config)
        .then(response => resolve(response.data))
        .catch(reject)
    })
  }
 
  private static getQueue () {
    if (!RequestBase.queue) {
      RequestBase.queue = new Queue(Infinity, Infinity)
    }
    return RequestBase.queue
  }
 
  static setConcurrency (concurrency: number) {
    RequestBase.queue = new Queue(concurrency, Infinity)
  }
 
  static request<T> (config: AxiosRequestConfig): Promise<T> {
    return RequestBase.getQueue().add(() => RequestBase.sendRequest(config) as any)
  }
}