-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdispatchRequest.js
235 lines (222 loc) · 8.31 KB
/
dispatchRequest.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* eslint-disable camelcase */
const pako = require('pako');
const axiosInstance = require('./axiosInstance');
const UTILS = require('./utils');
const CONST = require('./const');
const ErrorMessage = require('./errorMessage');
const { version } = require('../package.json');
// eslint-disable-next-line no-unused-vars
const typedef = require('./typedef');
/**
* base 64 encodes a string using either the btoa implementation if available or the Buffer object.
* @param {String} str string to be base 64 encoded
*/
function btoaImplementation(str) {
try {
return btoa(str);
} catch (err) {
return Buffer.from(str).toString('base64');
}
}
/**
* @param {object} response
* @returns {object} Object having two properties result and dataVersion
*/
function getResultWithDataVersion(response) {
return {
result: response.data,
dataVersion: response.headers['terminusdb-data-version']
? response.headers['terminusdb-data-version']
: '',
};
}
/**
* Create the authorization header string
* @param {object} auth_obj
* @returns {object} Object with the Authorization header
*/
function formatAuthHeader(auth_obj) {
if (!auth_obj) return '';
const authType = { jwt: 'Bearer', basic: 'Basic', apikey: 'Token' };
let auth_key = auth_obj.key;
if (auth_obj.type === 'basic') {
auth_key = btoaImplementation(`${auth_obj.user}:${auth_obj.key}`);
}
return `${authType[auth_obj.type]} ${auth_key}`;
}
function checkPayload(payload, options, compress) {
if (!payload || typeof payload !== 'object') return false;
const jsonStringPost = JSON.stringify(payload);
if (jsonStringPost && jsonStringPost.length > 1024 && compress) {
// eslint-disable-next-line no-param-reassign
options.headers['Content-Encoding'] = 'gzip';
return pako.gzip(jsonStringPost);
}
return false;
}
/**
* @file Dispatch Request
* @license Apache Version 2
* @description Functions for dispatching API requests via the axios library.
* @param {string} url API endpoint URL
* @param {string} action API action
* @param {object} payload data to be transmitted to endpoint
* @param {typedef.CredentialObj} local_auth the local authorization object
* @param {typedef.CredentialObj} remote_auth the remote authoriuzation object
* @param {object} customHeaders all the custom header to add to your call
* @param {boolean} [getDataVersion] If true the function will return object having result
* and dataVersion.
* @param {boolean} [compress] If true, compress the data with gzip if its size is bigger than 1024
*/
// eslint-disable-next-line max-len
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, customHeaders = null, getDataVersion = false, compress = false) {
/*
*CORS is only required when trying to fetch data from a browser,
*as browsers by default will block requests to different origins
*/
const options = {
mode: 'cors', // no-cors, cors, *same-origin
redirect: 'follow', // manual, *follow, error
referrer: 'client',
maxContentLength: Infinity,
maxBodyLength: Infinity,
headers: {},
// url:url,
// no-referrer, *client
};
// Only allow self signed certs on 127.0.0.1
// and on node
if (url.startsWith('https://127.0.0.1') && typeof window === 'undefined') {
// eslint-disable-next-line global-require
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false,
});
options.httpsAgent = agent;
}
/*
* I can call the local database using the local authorization key or
* a jwt token
*/
/* if (local_auth && local_auth.type === 'basic') {
const encoded = btoaImplementation(`${local_auth.user}:${local_auth.key}`);
options.headers = { Authorization: `Basic ${encoded}` };
} else if (local_auth && local_auth.type === 'jwt') {
options.headers = { Authorization: `Bearer ${local_auth.key}` };
} else if (local_auth && local_auth.type === 'apikey') {
options.headers = { Authorization: `Token ${local_auth.key}` };
} */
/*
* I can call the local database or a custom installation using the local authorization key or
* I Can call TerminusX using the jwt token or an apiToken
*/
if (local_auth && typeof local_auth === 'object') {
options.headers.Authorization = formatAuthHeader(local_auth);
}
/*
* pass the Authorization information of another
* terminusdb server to the local terminusdb
* for authentication you can use jwt or the apiKey token in TerminusX or
* the Basic autentication if is allowed in the custom server
*/
if (remote_auth && typeof remote_auth === 'object') {
options.headers['Authorization-Remote'] = formatAuthHeader(remote_auth);
}
if (customHeaders && typeof customHeaders === 'object') {
// eslint-disable-next-line array-callback-return
Object.keys(customHeaders).map((key) => {
options.headers[key] = customHeaders[key];
});
}
if (typeof window === 'undefined') {
options.headers['User-Agent'] = `terminusdb-client-js/${version}`;
}
switch (action) {
case CONST.DELETE: {
if (payload) {
options.headers = options.headers ? options.headers : {};
options.headers['Content-Type'] = 'application/json; charset=utf-8';
options.data = payload;
}
return axiosInstance
.delete(url, options)
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
case CONST.HEAD: {
return axiosInstance
.head(url, options)
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
case CONST.GET: {
if (payload) {
const ext = UTILS.URIEncodePayload(payload);
// eslint-disable-next-line no-param-reassign
if (ext) url += `?${ext}`;
}
return axiosInstance
.get(url, options)
.then((response) => {
const r = getDataVersion ? getResultWithDataVersion(response) : response.data;
return r;
})
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
case CONST.ADD_CSV:
case CONST.INSERT_TRIPLES: {
options.headers = options.headers ? options.headers : {};
options.headers['Content-Type'] = 'application/form-data; charset=utf-8';
return axiosInstance
.put(url, payload, options)
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
case CONST.PUT: {
options.headers = options.headers ? options.headers : {};
options.headers['Content-Type'] = 'application/json; charset=utf-8';
let compressedContent = null;
const jsonString = JSON.stringify(payload);
if (jsonString.length > 1024 && compress) {
options.headers['Content-Encoding'] = 'gzip';
compressedContent = pako.gzip(jsonString);
}
return axiosInstance
.put(url, compressedContent || payload, options)
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
case CONST.QUERY_DOCUMENT: {
options.headers = options.headers ? options.headers : {};
options.headers['X-HTTP-Method-Override'] = 'GET';
// eslint-disable-next-line no-fallthrough
}
default: {
options.headers = options.headers ? options.headers : {};
if (!options.headers['content-type'] && !options.headers['Content-Type']) {
options.headers['Content-Type'] = 'application/json; charset=utf-8';
}
const compressedContentPost = checkPayload(payload, options, compress);
return axiosInstance
.post(url, compressedContentPost || payload || {}, options)
.then((response) => {
const r = getDataVersion ? getResultWithDataVersion(response) : response.data;
return r;
})
.catch((err) => {
throw ErrorMessage.apiErrorFormatted(url, options, err);
});
}
}
}
module.exports = DispatchRequest;