All files / apis/detector detect.api.ts

30% Statements 3/10
0% Branches 0/1
0% Functions 0/3
33.33% Lines 3/9

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  1x 1x                 1x                                                                                  
import type { AnyResponse } from "../shared";
import { DetectorApi } from "./api";
import { DetectorError } from "./detector.errors";
 
/**
 *
 *
 * @export
 * @class DetectorDetectApi
 * @extends {DetectorApi}
 */
export class DetectorDetectApi extends DetectorApi {
  /**
   * Вытаскивает ID провайдера из объекта ответа
   *
   * @protected
   * @param {*} response
   * @return {number} ID провайдера
   */
  protected _extractProvider(response: AnyResponse): number {
    Iif (response.code.value !== "0") throw new DetectorError(response.message);
 
    return Number.parseInt(response.message);
  }
 
  /**
   * Возвращает ID провайдера QIWI по номеру телефона.
   * Используется для пополнения на счёта мобильного
   *
   * @param {string} phone
   */
  async byPhone(phone: string): Promise<number> {
    const response = await this.http.post<any>("mobile/detect.action", { phone });
 
    return this._extractProvider(response);
  }
 
  /**
   * Возвращает ID провайдера QIWI по номеру карты.
   * Используется для переводов на карту
   *
   * @param {string} cardNumber
   *
   * @note После ухода VISA и MasterCard из РФ стал работать хуже.
   * Советую использовать константу `Recipients.AnyRusCard` вместо вызова метода.
   */
  async byCardNumber(cardNumber: string): Promise<number> {
    const response = await this.http.post<any>("card/detect.action", { cardNumber });
 
    return this._extractProvider(response);
  }
}