-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.ts
71 lines (64 loc) · 1.65 KB
/
client.ts
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
import https, { Agent } from 'https';
import fs from 'fs';
import axios, { AxiosInstance } from 'axios';
import { Settings } from './types';
/**
* Represent client for sending queries to remote service
*/
export default class Client {
/**
* Instance of axios for connection to CodeX Accounting API
*/
private readonly apiInstance: AxiosInstance;
/**
* Create axios instance with https agent
*
* @param settings - settings for client module
*/
constructor(settings: Settings) {
let httpsAgent: Agent | null = null;
if (settings.tlsVerify) {
try {
httpsAgent = new https.Agent({
ca: fs.readFileSync(settings.tlsVerify.tlsCaCertPath),
cert: fs.readFileSync(settings.tlsVerify.tlsCertPath),
key: fs.readFileSync(settings.tlsVerify.tlsKeyPath),
});
} catch (error) {
if (error.code === 'ENOENT') {
console.error('Cert files not found!');
} else {
console.error(error);
}
}
}
this.apiInstance = axios.create({
baseURL: settings.baseURL,
timeout: 1000,
httpsAgent: httpsAgent,
});
}
/**
* Calls remote service and returns response
*
* @param query - request to send
* @param variables - request variables
*/
public async call(
query: string,
variables?: object
// eslint-disable-next-line
): Promise<any> {
const response = await this.apiInstance.post(this.baseURL, {
query,
variables,
});
return response.data.data;
}
/**
* Returns base URL endpoint
*/
private get baseURL(): string {
return this.apiInstance.defaults.baseURL || '';
}
}