-
Notifications
You must be signed in to change notification settings - Fork 2
/
endpoints.js
198 lines (174 loc) · 5.56 KB
/
endpoints.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs')
const https = require('https')
const sdkPackage = require('../package.json')
const axios = require('axios')
const randomstring = require('randomstring')
class Endpoints {
constructor(options, constants) {
this.options = options
this.auth = null
this.constants = constants
this.axiosInstance = axios.create()
}
run(name, params, body) {
let endpoint
if (this.constants.APIS.DEFAULT.ENDPOINTS.hasOwnProperty(name)) {
endpoint = this.constants.APIS.DEFAULT.ENDPOINTS[name]
this.baseUrl = this.options.sandbox ? this.constants.APIS.DEFAULT.URL.SANDBOX : this.constants.APIS.DEFAULT.URL.PRODUCTION
this.authRoute = this.constants.APIS.DEFAULT.ENDPOINTS.authorize
} else {
Object.keys(this.constants.APIS).forEach((key) => {
if (this.constants.APIS[key].ENDPOINTS.hasOwnProperty(name)) {
endpoint = this.constants.APIS[key].ENDPOINTS[name]
this.baseUrl = this.options.sandbox ? this.constants.APIS[key].URL.SANDBOX : this.constants.APIS[key].URL.PRODUCTION
this.authRoute = this.constants.APIS[key].ENDPOINTS.authorize
return
}
})
try {
if (this.options.pemKey) {
this.agent = new https.Agent({
cert: fs.readFileSync(this.options.certificate),
key: fs.readFileSync(this.options.pemKey),
passphrase: '',
})
} else {
this.agent = new https.Agent({
pfx: fs.readFileSync(this.options.certificate),
passphrase: '',
})
}
} catch (error) {
if (this.options.pemKey)
throw `FALHA AO LER O CERTIFICADO OU A CHAVE, VERIFIQUE O CAMINHO INFORMADO:\n CAMINHO DO CERTIFICADO: ${this.options.certificate} \n CAMINHO DA CHAVE: ${this.options.pemKey}`
else
throw `FALHA AO LER O CERTIFICADO, VERIFIQUE O CAMINHO INFORMADO: ${this.options.certificate}`
}
}
this.params = params
return this.req(endpoint, body)
}
async req(endpoint, body) {
let req = await this.createRequest(endpoint, body)
// Request interceptor
this.axiosInstance.interceptors.request.use(
async (config) => {
if (!this.auth || this.isExpired()) {
await this.authenticate()
}
config.headers = {
Authorization: `Bearer ${this.auth.access_token}`,
'x-skip-mtls-checking': !this.options.validateMtls
}
if (this.options.partner_token) {
config.headers['partner-token'] = this.options.partner_token
}
if (this.baseUrl == this.constants.APIS.OPENFINANCE.URL.PRODUCTION || this.baseUrl == this.constants.APIS.OPENFINANCE.URL.SANDBOX) {
config.headers['x-idempotency-key'] = randomstring.generate({ length: 72, charset: 'alphanumeric' })
}
return config
},
(error) => {
Promise.reject(error)
},
)
return this.axiosInstance(req)
.then((res) => {
return res.data
})
.catch((error) => {
throw error.response.data
})
}
isExpired() {
let current_time = new Date().getTime() / 1000
if (current_time > this.auth.authDate + this.auth.expires_in) {
return true
}
return false
}
async authenticate() {
let authParams = {
method: 'POST',
url: this.baseUrl + this.authRoute.route,
headers: {
'api-sdk': 'node-' + sdkPackage.version,
},
data: {
grant_type: 'client_credentials',
},
}
if (this.constants.APIS.DEFAULT.URL.PRODUCTION == this.baseUrl || this.constants.APIS.DEFAULT.URL.SANDBOX == this.baseUrl) {
authParams.auth = {
username: this.options.client_id,
password: this.options.client_secret,
}
} else {
let token = Buffer.from(this.options.client_id + ':' + this.options.client_secret).toString('base64')
authParams.headers['Authorization'] = 'Basic ' + token
authParams.headers['Content-Type'] = 'application/json'
authParams.httpsAgent = this.agent
}
return axios(authParams)
.then((res) => {
this.auth = res.data
this.auth.authDate = new Date().getTime() / 1000
})
.catch((error) => {
throw error.data
})
}
async createRequest(endpoint, body) {
let { route, method } = endpoint
let regex = /\:(\w+)/g
let query = ''
let placeholders = route.match(regex) || []
let params = {}
for (let prop in this.params) {
params[prop] = this.params[prop]
}
let getVariables = function () {
return placeholders.map(function (item) {
return item.replace(':', '')
})
}
let updateRoute = function () {
let variables = getVariables()
variables.forEach(function (value, index) {
if (params[value]) {
route = route.replace(placeholders[index], params[value])
delete params[value]
}
})
}
let getQueryString = function () {
let keys = Object.keys(params)
let initial = keys.length >= 1 ? '?' : ''
return keys.reduce(function (previous, current, index, array) {
let next = index === array.length - 1 ? '' : '&'
return [previous, current, '=', params[current], next].join('')
}, initial)
}
updateRoute()
query = getQueryString()
let headers = new Object()
headers['x-skip-mtls-checking'] = !this.options.validateMtls
if (this.options.partner_token) {
headers['partner-token'] = this.options.partner_token
}
if (this.baseUrl == this.constants.APIS.OPENFINANCE.URL.PRODUCTION || this.baseUrl == this.constants.APIS.OPENFINANCE.URL.SANDBOX) {
headers['x-idempotency-key'] = randomstring.generate({ length: 72, charset: 'alphanumeric' })
}
let req = {
method,
url: String([this.baseUrl, route, query].join('')),
headers,
data: body,
}
if (this.baseUrl != this.constants.APIS.DEFAULT.URL.PRODUCTION && this.baseUrl != this.constants.APIS.DEFAULT.URL.SANDBOX) {
req['httpsAgent'] = this.agent
}
return req
}
}
module.exports = Endpoints