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 | 1x 1x 1x 1x 3x 4x 4x 4x 4x 3x 3x 4x 3x 1x 3x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x | import axios, {
AxiosBasicCredentials,
AxiosError,
AxiosPromise,
AxiosRequestConfig,
AxiosRequestHeaders,
AxiosResponse,
Method,
} from 'axios';
import { IOptions } from './options';
const API_PREFIX = 'api/v1';
const DEFAULT_TIMEOUT = 20000;
/**
* Oracle client
*/
export class BaseClient {
public opts: IOptions;
constructor(opts: IOptions) {
this.opts = opts;
}
public async request(
method: Method,
endpoint: string,
params: IParams = {},
data: IParams = {},
headers: AxiosRequestHeaders = {},
): Promise<any> {
const config: AxiosRequestConfig = {
baseURL: `${this.opts.uri}/${this.opts.prefix || API_PREFIX}/`,
url: endpoint,
timeout: this.opts.timeout || DEFAULT_TIMEOUT,
method,
params,
data: method === 'GET' ? undefined : data,
responseType: 'json',
};
Eif (headers) {
config.headers = headers;
}
if (this.opts.apiKey) {
const auth: AxiosBasicCredentials = {
username: 'admin',
password: this.opts.apiKey,
};
config.auth = auth;
}
return this.axiosCall(config)
.then((response: AxiosResponse) => response.data)
.catch(this.handleError);
}
public axiosCall(config: AxiosRequestConfig): AxiosPromise<any> {
return axios(config);
}
public get(
endpoint: string,
params: IParams = {},
headers: AxiosRequestHeaders = {},
): any {
return this.request('GET', endpoint, params, {}, headers);
}
public post(
endpoint: string,
params: IParams = {},
headers: AxiosRequestHeaders = {},
): any {
return this.request('POST', endpoint, {}, params, headers);
}
public put(
endpoint: string,
params: IParams = {},
headers: AxiosRequestHeaders = {},
): any {
return this.request('PUT', endpoint, {}, params, headers);
}
public handleError = (error: AxiosError): void => {
if (error.code === 'ECONNREFUSED') {
this.opts.logger.debug(
'Make sure the Service is running and that you are connecting to the correct port',
);
throw Error(`Could not connect to Service ${this.opts.uri}`);
} else Iif (error.code === 'EPIPE') {
throw Error(`EPIPE error ${this.opts.uri}`);
} else Iif (error.code === 'ERR_NETWORK') {
if (error.config) {
const { method, baseURL, url, params } = error.config;
throw Error(
`Network Error ${method} ${baseURL}${url} with params ${JSON.stringify(
params,
)}`,
);
} else {
throw new Error(
`Network Error ${this.opts.uri} ${JSON.stringify(error)}`,
);
}
} else Eif (error.response) {
Eif (error.response.data) {
if (typeof error.response.data === 'string') {
throw new Error(error.response.data);
} else Eif (typeof (error.response.data as any).error === 'string') {
throw new Error((error.response.data as any).error as string);
} else {
throw new Error(JSON.stringify(error.response.data));
}
} else {
throw new Error(JSON.stringify(error.response));
}
} else {
throw new Error(error.message);
}
};
}
export interface IParams {
[x: string]: unknown;
}
|