-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_axios_config.ts
74 lines (64 loc) 路 3.34 KB
/
base_axios_config.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
72
73
74
import { createStream } from 'rotating-file-stream'
import { resolve } from 'path'
import { stringify } from 'qs'
import debugHelper from './util/debug_helper'
import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
const { logError } = debugHelper(__filename)
const baseAxiosConfig = {
timeout: 10000,
headers: {
'User-Agent': 'ts-crypto-cli',
'content-type': 'application/x-www-form-urlencoded',
}
}
const { TS_CRYPTO_CLI_LOGS_PATH } = process.env
const dirPath = TS_CRYPTO_CLI_LOGS_PATH ? resolve(TS_CRYPTO_CLI_LOGS_PATH) : null
// Rotating filenames
const calculateFileName = (date: Date, index: number): string => {
const [YYYY, MM] = new Date().toISOString().slice(0,7).split('-')
const fileName = `ts-crypto-cli_${YYYY}_${MM}`
if (!date) return `${fileName}.csv`
return `${fileName}_${index}.csv`
}
const stream = dirPath ? createStream(calculateFileName, {
size: '100M', // rotate every 10 MegaBytes written
interval: '1M', // rotate monthly
path: dirPath,
immutable: true,
// compress: "gzip" // compress rotated files
}) : null;
const baseAxiosRequestInterceptor = (config: AxiosRequestConfig): AxiosRequestConfig => {
if (['PUT', 'POST', 'PATCH'].includes(config.method.toUpperCase())) {
delete config.params
config.data = stringify(config.data)
} else {
delete config.data
}
const { method, baseURL, url, params = '-', data = '-' } = config || {}
if (stream) { stream.write(`\n${new Date().toISOString()}; ${method.toUpperCase()}; ${baseURL}${baseURL.endsWith('/') || url.startsWith('/') ? '' : '/'}${url}; ${params}; ${data}; -; -; -`) }
return config
}
const baseAxiosResponseInterceptor = (response: AxiosResponse): AxiosResponse => {
const { config: { method, baseURL, url, params = '-' }, status = null, statusText = '-', data = '-' } = response || {}
if (stream) { stream.write(`\n${new Date().toISOString()}; ${method.toUpperCase()}; ${baseURL}${baseURL.endsWith('/') || url.startsWith('/') ? '' : '/'}${url}; ${params}; ${typeof data}; ${status}; ${statusText}; -`) }
return response
}
const baseAxiosRequestErrorInterceptor = (axiosRequestError: AxiosError): void => {
const { config: { method, baseURL, url, params = '-', data = '-' }, code = '-', response: { status = null, statusText = '-' }} = axiosRequestError || {}
if (stream) { stream.write(`\n${new Date().toISOString()}; ${method.toUpperCase()}; ${baseURL}${baseURL.endsWith('/') || url.startsWith('/') ? '' : '/'}${url}; ${params}; ${JSON.stringify(data)}; ${status}; ${statusText}; ${code}`) }
logError({ axiosRequestError })
throw axiosRequestError
}
const baseAxiosResponseErrorInterceptor = (axiosResponseError: AxiosError): void => {
const { config: { method, baseURL, url, params = '-', data = '-' }, code = '-', response: { status = null, statusText = '-' }} = axiosResponseError || {}
if (stream) { stream.write(`\n${new Date().toISOString()}; ${method.toUpperCase()}; ${baseURL}${baseURL.endsWith('/') || url.startsWith('/') ? '' : '/'}${url}; ${params}; ${typeof data}; ${status}; ${statusText}; ${code}`) }
logError({ axiosResponseError })
throw axiosResponseError
}
export {
baseAxiosConfig,
baseAxiosRequestInterceptor,
baseAxiosRequestErrorInterceptor,
baseAxiosResponseInterceptor,
baseAxiosResponseErrorInterceptor,
}