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

oneai-nlp / oneai-node / 5058367577

pending completion
5058367577

push

github

michgur
collection create\delete

274 of 339 branches covered (80.83%)

Branch coverage included in aggregate %.

13 of 13 new or added lines in 3 files covered. (100.0%)

452 of 472 relevant lines covered (95.76%)

36.95 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
}
23

24
export type ApiReqParams = Partial<ApiClientParams>;
25

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

32
export interface HttpApiClient {
33
  params: ApiClientParams;
34

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

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

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

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

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

71
  params: ApiClientParams;
72

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

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

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

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

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

150
    return key;
37✔
151
  }
152
}
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