-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
218 lines (208 loc) · 6.75 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict'
function New_ParametersForWalletRequest (address, view_key__private) {
return {
address: address,
view_key: view_key__private
}
}
exports.New_ParametersForWalletRequest = New_ParametersForWalletRequest
function AddUserAgentParamters (parameters, appUserAgent_product, appUserAgent_version) {
// setting these on params instead of as header field User-Agent so as to retain all info found in User-Agent, such as platform… and these are set so server has option to control delivery
parameters['app_name'] = appUserAgent_product
parameters['app_version'] = appUserAgent_version
}
exports.AddUserAgentParamters = AddUserAgentParamters
function HTTPRequest (
request_conformant_module, // such as 'request' or 'xhr' .. TODO: consider switching to 'fetch'
apiAddress_authority, // authority means [subdomain.]host.…[:…] with no trailing slash
endpointPath,
final_parameters,
fn
) {
// fn: (err?, data?) -> new Request
if (typeof final_parameters === 'undefined' || final_parameters == null) {
throw 'final_parameters must not be nil'
// return null
}
const completeURL = apiAddress_authority + endpointPath
console.log('📡 ' + completeURL)
//
const request_options = _new_requestOptions_base(
'POST',
completeURL,
final_parameters
)
const requestHandle = request_conformant_module(
request_options,
function (err_orProgressEvent, res, body) {
_new_HTTPRequestHandlerFunctionCallingFn(fn)(
// <- called manually instead of directly passed to request_conformant_module call to enable passing completeURL
completeURL,
err_orProgressEvent,
res,
body
)
}
)
//
return requestHandle
}
exports.HTTPRequest = HTTPRequest
// this function exists for MyMonero mobile to be able to facilitate Monero LWS compatiblityß
function HTTPRequestBypassCORS (
capacitorHttp,
apiAddress_authority, // authority means [subdomain.]host.…[:…] with no trailing slash
endpointPath,
final_parameters,
fn
) {
// fn: (err?, data?) -> new Request
if (typeof final_parameters === 'undefined' || final_parameters == null) {
throw 'final_parameters must not be nil'
// return null
}
const completeURL = apiAddress_authority + endpointPath
console.log('📡 ' + completeURL)
//
const request_options = _new_requestOptions_base(
'POST',
completeURL,
final_parameters
)
const err_orProgressEvent = function (error) {
console.log(error)
}
const requestHandle = capacitorHttp.Http.request({
method: 'POST',
url: completeURL,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
data: JSON.stringify(final_parameters)
}).then(res => {
_new_CapacitorRequestHandlerFunctionCallingFn(fn)(
// <- called manually instead of directly passed to request_conformant_module call to enable passing completeURL
completeURL,
err_orProgressEvent,
res,
res.data
)
})
return requestHandle
}
exports.HTTPRequestBypassCORS = HTTPRequestBypassCORS
function _new_APIAddress_baseURLString (
apiAddress_authority // authority means [subdomain.]host.…[:…]
) {
return apiAddress_authority
}
function _new_requestOptions_base (methodName, completeURL, json_parameters) {
return {
method: methodName,
url: completeURL,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
json: json_parameters,
useXDR: true, // CORS
withCredentials: true // CORS
}
}
function _new_HTTPRequestHandlerFunctionCallingFn (fn) {
return function (completeURL, err_orProgressEvent, res, body) {
// err appears to actually be a ProgressEvent
var err = null
const statusCode = typeof res !== 'undefined' ? res.statusCode : -1
if (statusCode == 0 || statusCode == -1) {
// we'll treat 0 as a lack of internet connection.. unless there's a better way to make use of err_orProgressEvent which is apparently going to be typeof ProgressEvent here
err = new Error('Connection Failure')
} else if (statusCode !== 200) {
const body_Error =
body && typeof body === 'object' ? body.Error : undefined
const statusMessage =
res && res.statusMessage ? res.statusMessage : undefined
if (typeof body_Error !== 'undefined' && body_Error) {
err = new Error(body_Error)
} else if (typeof statusMessage !== 'undefined' && statusMessage) {
err = new Error(statusMessage)
} else {
err = new Error('Unknown ' + statusCode + ' error')
}
}
if (err) {
console.error('❌ ' + err)
// console.error("Body:", body)
fn(err, null)
return
}
var json
if (typeof body === 'string') {
try {
json = JSON.parse(body)
} catch (e) {
console.error(
'❌ HostedMoneroAPIClient Error: Unable to parse json with exception:',
e,
'\nbody:',
body
)
fn(e, null)
}
} else {
json = body
}
console.log('✅ ' + completeURL + ' ' + statusCode)
fn(null, json)
}
}
function _new_CapacitorRequestHandlerFunctionCallingFn (fn) {
return function (completeURL, err_orProgressEvent, res, body) {
// err appears to actually be a ProgressEvent
console.log("Invoked capacitor request handler")
var err = null
const statusCode = typeof res !== 'undefined' ? res.status : -1
if (statusCode == 0 || statusCode == -1) {
// we'll treat 0 as a lack of internet connection.. unless there's a better way to make use of err_orProgressEvent which is apparently going to be typeof ProgressEvent here
err = new Error('Connection Failure')
} else if (statusCode !== 200) {
const body_Error =
body && typeof body === 'object' ? body.Error : undefined
const statusMessage =
res && res.status ? res.status : undefined
if (typeof body_Error !== 'undefined' && body_Error) {
err = new Error(body_Error)
} else if (typeof statusMessage !== 'undefined' && statusMessage) {
err = new Error(statusMessage)
} else {
err = new Error('Unknown ' + statusCode + ' error')
}
}
if (err) {
console.error('❌ ' + err)
// console.error("Body:", body)
fn(err, null)
return
}
var json
if (typeof body === 'string') {
console.log('body', body)
try {
json = JSON.parse(body)
} catch (e) {
console.error(
'❌ HostedMoneroAPIClient Error: Unable to parse json with exception:',
e,
'\nbody:',
body
)
fn(e, null)
}
} else {
json = body
}
console.log('✅ ' + completeURL + ' ' + statusCode)
fn(null, json)
}
}