# All files DlcdClient.ts

64.71% Statements 22/34
55% Branches 11/20
85.71% Functions 6/7
64.71% Lines 22/34

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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    1x

    1x

    3x
    3x
    3x
    3x
    3x
    3x

    3x

    3x
    1x

    1x
    1x

    3x
    2x

    1x

    2x

    3x
    1x
    1x
    1x
    1x

     import { Logger } from '@node-dlc/logger';
    import axios, {
      AxiosBasicCredentials,
      AxiosError,
      AxiosRequestConfig,
      AxiosResponse,
    } from 'axios';

    export type Method =
      | 'get'
      | 'GET'
      | 'delete'
      | 'DELETE'
      | 'head'
      | 'HEAD'
      | 'options'
      | 'OPTIONS'
      | 'post'
      | 'POST'
      | 'put'
      | 'PUT'
      | 'patch'
      | 'PATCH'
      | 'purge'
      | 'PURGE'
      | 'link'
      | 'LINK'
      | 'unlink'
      | 'UNLINK';

    export type ApiPrefix = 'api' | 'api/v0';

    export default class DlcdClient {
      public host: string;

      public port: number;

      public ssl: boolean;

      public apiKey: string;

      public logger: Logger;

      public apiPrefix: ApiPrefix;

      constructor(
        host: string,
        port: number,
        logger: Logger,
        apiKey = '',
        apiPrefix: ApiPrefix = 'api',
        ssl = false,
      ) {
        this.host = host;
        this.port = port;
        this.logger = logger;
        this.ssl = ssl;
        this.apiKey = apiKey;
        this.apiPrefix = apiPrefix;
      }

      public async request(
        method: Method,
        endpoint: string,
        params: IParams = {},
        data: IParams = {},
      ): Promise<any> {
        const config: AxiosRequestConfig = {
          baseURL: `${this.ssl ? 'https' : 'http'}://${this.host}:${this.port}/${
            this.apiPrefix
          }/`,
          url: endpoint,
          timeout: 1000,
          method,
          params,
          data,
          responseType: 'json',
        };

        if (this.apiKey) {
          const auth: AxiosBasicCredentials = {
            username: 'admin',
            password: this.apiKey,
          };

          config.auth = auth;
          config.timeout = 10000;
        }

        return axios(config)
          .then((response: AxiosResponse) => response.data)
          .catch(this.handleError);
      }

      public get(endpoint: string, params: IParams = {}): any {
        return this.request('GET', endpoint, params);
      }

      public post(endpoint: string, params: IParams = {}): any {
        return this.request('POST', endpoint, {}, params);
      }

      public put(endpoint: string, params: IParams = {}): any {
        return this.request('PUT', endpoint, {}, params);
      }

      public handleError = (error: AxiosError): void => {
        Eif (error.response) {
          Eif (error.response.data.error) {
            this.logger.error(`Error: ${error.response.data.error}`);
            process.exit(1);
          } else {
            this.logger.log(error.response.status);
            this.logger.log(error.response.headers);
            throw new Error(error.response.data);
          }
        } else if (error.code === 'ECONNREFUSED') {
          this.logger.error(
            `Error: Could not connect to DLCd server ${this.host}:${this.port}`,
          );
          this.logger.error(
            'Make sure the DLCd server is running and that you are connecting to the correct port',
          );
          process.exit(1);
        } else if (error.code === 'EPIPE') {
          process.exit(0);
        } else {
          this.logger.log(error.message);
          throw new Error(error.message);
        }
      };
    }

    interface IParams {
      [x: string]: unknown;
    }