All files / src/apis/detector detect.api.ts

100% Statements 12/12
100% Branches 1/1
100% Functions 3/3
100% Lines 12/12

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 54 55 56 57 58 59 60 61  5x 5x 5x 5x                 5x                 5x 1x     4x                   3x       3x                         2x       2x      
import type { AnyRecord } from "../shared/types";
import { url } from "../shared/url";
import { getOwnPropertyDeep } from "../shared/get";
import { DetectorApi } from "./api";
import { DetectorError } from "./detector.errors";
 
/**
 *
 *
 * @export
 * @class DetectorDetectApi
 * @extends {DetectorApi}
 */
export class DetectorDetectApi extends DetectorApi {
  /**
   * Вытаскивает ID провайдера из объекта ответа
   *
   * @protected
   * @param {AnyRecord} response
   * @return {number} ID провайдера
   */
  protected _extractProvider(response: AnyRecord): number {
    if (getOwnPropertyDeep(response, "code.value") !== "0") {
      throw new DetectorError(response.message as string);
    }
 
    return Number.parseInt(response.message as string);
  }
 
  /**
   * Возвращает ID провайдера QIWI по номеру телефона.
   * Используется для пополнения на счёта мобильного
   *
   * @param {string} phone
   */
  async byPhone(phone: string): Promise<number> {
    const response = await this.http.post<AnyRecord>(url`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<AnyRecord>(url`card/detect.action`(), {
      cardNumber
    });
 
    return this._extractProvider(response);
  }
}