-
Notifications
You must be signed in to change notification settings - Fork 0
/
configBase.js
67 lines (57 loc) · 1.6 KB
/
configBase.js
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
'use strict'
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const path = require('path')
const HTTP_METHOD = process.env.HTTP_METHOD ?? "POST"
const SERVICE_NAME = process.env.SERVICE_NAME
const API_PASSWORD = process.env.API_PASSWORD
const SERVICE_API_PORT = process.env.SERVICE_API_PORT
const API_PATH = process.env.API_PATH
const API_USERNAME = process.env.API_USERNAME
const SSL = process.env.SSL
const MIME_TYPE = process.env.MIME_TYPE
const CONFIG_FILE = process.env.CONFIG_FILE
const ADDITIONAL_HEADERS = process.env.ADDITIONAL_HEADERS ?? ''
let data
let dataHeaders = {}
switch (MIME_TYPE) {
case 'multipart/form-data':
data = new FormData()
data.append('file', fs.createReadStream(path.resolve(__dirname, CONFIG_FILE)))
dataHeaders = data.getHeaders()
if (ADDITIONAL_HEADERS) {
dataHeaders = { ...dataHeaders, ...JSON.parse(ADDITIONAL_HEADERS) }
}
break;
case 'application/json':
const jsonData = JSON.parse(
fs.readFileSync(path.resolve(__dirname, CONFIG_FILE))
)
data = JSON.stringify(jsonData)
break;
default:
break;
}
const protocol = SSL == 'false' ? 'http' : 'https'
let config = {
method: HTTP_METHOD,
url: `${protocol}://${SERVICE_NAME}:${SERVICE_API_PORT}${API_PATH}`,
auth: {
username: API_USERNAME,
password: API_PASSWORD
},
headers: {
'Content-Type': MIME_TYPE,
...dataHeaders
},
data: data,
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data))
})
.catch(function (error) {
console.log(error)
process.exit(1)
})