identi-recognition
Version:
identiRecognition
121 lines (107 loc) • 2.9 kB
JavaScript
const http = require('http')
const axios = require('axios')
const config = require('../config')
// Create a custom agent to bypass SSL certificate verification
// IN CASE IS IT MANDATORY TO USE THIS AUTHENTICATION AGAIN
// const config = require('../config/config.json')[process.env.NODE_ENV || 'development']
// const AxiosDigestAuth = require('../utils/digestAuth').default
// const Digestcredentials = {
// username: config.xcore.auth.user,
// password: config.xcore.auth.password
// }
// const axiosDigest = new AxiosDigestAuth(Digestcredentials)
const agent = new http.Agent({ keepAlive: false })
const keyToRemove = ['accept', 'accept-encoding', 'connection', 'host'] // 'content-length', 'content-type',
async function requestCallBase(url, endpoint, method, data, header = {}) {
try {
for (const key of keyToRemove) {
if (Object.prototype.hasOwnProperty.call(header, key)) {
delete header[key]
}
}
const configAxios = {
method,
url: url + endpoint,
responseType: 'json',
responseEncoding: 'utf8',
headers: {
...header
},
data
}
return await axios(configAxios)
} catch (error) {
throw error
}
}
async function requestCallRest(url, endpoint, method, data, header = {}) {
try {
for (const key of keyToRemove) {
if (Object.prototype.hasOwnProperty.call(header, key)) {
delete header[key]
}
}
const configAxios = {
method,
url: url + endpoint,
headers: {
...header,
Connection: 'close'
},
httpAgent: agent,
data
}
responseRequest = await axios(configAxios)
return responseRequest
} catch (error) {
throw error
}
}
async function requestGetCall(url, endpoint, data = undefined) {
try {
const configAxios = {
method: 'GET',
url: url + endpoint,
headers: {
Connection: 'close'
},
httpAgent: agent,
params: data
}
responseRequest = await axios(configAxios)
return responseRequest
} catch (error) {
throw error
}
}
//------------------- DIGIT AUTH ---------------------------
async function requestCallDigit(url, endpoint, method, data, header = {}) {
try {
for (const key of keyToRemove) {
if (Object.prototype.hasOwnProperty.call(header, key)) {
delete header[key]
}
}
const configAxios = {
method,
url: url + endpoint,
headers: {
...header
},
data
}
return await axiosDigest(configAxios)
} catch (error) {
throw error
}
}
function isAxiosError(error) {
return axios.isAxiosError(error)
}
module.exports = {
requestCallRest,
requestCallBase,
requestCallDigit,
requestGetCall,
isAxiosError
}