• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oneai-nlp / oneai-node / 5067134005

pending completion
5067134005

push

github

michgur
pdf

299 of 371 branches covered (80.59%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

460 of 481 relevant lines covered (95.63%)

37.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/src/api/client.ts
1
import fs from 'fs';
1✔
2
import axios from 'axios';
1✔
3
import { v4 as uuidv4 } from 'uuid';
1✔
4
import { version } from '../../package.json';
1✔
5
import { buildError } from './mapping';
1✔
6

7
type ISO639 = 'en' | 'es' | 'fr' | 'de' | 'it' | 'pt' | 'ja' | 'ko' | 'zh' | 'ar' | 'ru' | 'tr' | 'nl' | 'pl' | 'sv' | 'da' | 'fi' | 'no' | 'cs' | 'el' | 'hu' | 'ro' | 'sk' | 'sl' | 'bg' | 'et' | 'lv' | 'lt' | 'he' | string;
8

9
export interface MultilingualParams {
10
  enabled: boolean;
11
  allowed_input_languages?: (ISO639 | 'ALL')[];
12
  translate_output_to?: ISO639;
13
  expected_languages?: ISO639[];
14
  override_language_detection?: boolean;
15
}
16

17
export interface ApiClientParams {
18
  apiKey: string;
19
  baseURL: string;
20
  timeout: number;
21
  multilingual: boolean | MultilingualParams;
22
  sync: boolean;
23
  interval: number;
24
}
25

26
export type ApiReqParams = Partial<ApiClientParams>;
27

28
export interface HttpResponse {
29
  status: number;
30
  data: any;
31
  headers?: Record<string, string>;
32
}
33

34
export interface HttpApiClient {
35
  params: ApiClientParams;
36

37
  get(
38
    path: string,
39
    params?: ApiReqParams,
40
  ): Promise<HttpResponse>;
41

42
  post(
43
    path: string,
44
    data: string | Buffer,
45
    params?: ApiReqParams,
46
  ): Promise<HttpResponse>;
47

48
  delete(
49
    path: string,
50
    params?: ApiReqParams,
51
  ): Promise<HttpResponse>;
52
}
53

54
export class ApiClientAxios implements HttpApiClient {
1✔
55
  private static readonly uuid = (() => {
1✔
56
    try {
1✔
57
      const filePath = `${__dirname}/.uuid`;
1✔
58
      let result = '';
1✔
59
      if (fs.existsSync(filePath)) {
1!
60
        result = fs.readFileSync(filePath, 'utf8');
1✔
61
      } else {
62
        result = uuidv4().replace(/-/g, '');
×
63
        fs.writeFileSync(filePath, result);
×
64
      }
65
      return result;
1✔
66
    } catch (e) {
67
      return 'UUID_DISABLED';
×
68
    }
69
  })();
70

71
  private agent: string = `node-sdk/${version}/${ApiClientAxios.uuid}`;
7✔
72

73
  params: ApiClientParams;
74

75
  constructor(params: ApiClientParams) {
76
    this.params = params;
7✔
77
  }
78

79
  async get(
1✔
80
    path: string,
81
    params?: ApiReqParams,
82
  ): Promise<HttpResponse> {
83
    const apiKey = this.validateApiKey(params);
27✔
84
    try {
85
      return await axios({ // await to throw error if necessary
27✔
86
        url: `${this.params.baseURL}/${path}`,
87
        method: 'GET',
88
        headers: {
89
          'api-key': apiKey,
90
          'User-Agent': this.agent,
91
        },
92
        timeout: (params?.timeout || this.params.timeout) * 1000,
135✔
93
        maxBodyLength: Infinity,
94
        maxContentLength: Infinity,
95
      });
96
    } catch (error) {
97
      throw buildError(error);
×
98
    }
99
  }
100

101
  async post(
1✔
102
    path: string,
103
    data: string | Buffer,
104
    params?: ApiReqParams,
105
  ): Promise<HttpResponse> {
106
    const apiKey = this.validateApiKey(params);
20✔
107
    try {
108
      return await axios({ // await to throw error if necessary
20✔
109
        url: `${this.params.baseURL}/${path}`,
110
        method: 'POST',
111
        headers: {
112
          'api-key': apiKey,
113
          'User-Agent': this.agent,
114
          'Content-Type': 'application/json',
115
        },
116
        data,
117
        timeout: (params?.timeout || this.params.timeout) * 1000,
100✔
118
        maxBodyLength: Infinity,
119
        maxContentLength: Infinity,
120
      });
121
    } catch (error) {
122
      throw buildError(error);
4✔
123
    }
124
  }
125

126
  async delete(
1✔
127
    path: string,
128
    params?: ApiReqParams,
129
  ): Promise<HttpResponse> {
130
    const apiKey = this.validateApiKey(params);
1✔
131
    try {
132
      return await axios({ // await to throw error if necessary
1✔
133
        url: `${this.params.baseURL}/${path}`,
134
        method: 'DELETE',
135
        headers: {
136
          'api-key': apiKey,
137
          'User-Agent': this.agent,
138
        },
139
        timeout: (params?.timeout || this.params.timeout) * 1000,
5!
140
        maxBodyLength: Infinity,
141
        maxContentLength: Infinity,
142
      });
143
    } catch (error) {
144
      throw buildError(error);
×
145
    }
146
  }
147

148
  validateApiKey(params?: ApiReqParams): string {
1✔
149
    const key = params?.apiKey || this.params.apiKey;
48✔
150
    if (!key) throw new Error('missing API key');
48!
151

152
    return key;
48✔
153
  }
154
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc