Skip to content

Commit c16890e

Browse files
Linted code
Signed-off-by: NeelParihar <[email protected]>
1 parent e69bc6a commit c16890e

File tree

4 files changed

+365
-333
lines changed

4 files changed

+365
-333
lines changed

lib/connectionConfig.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,33 @@ ConnectionConfig.prototype.user = function (ignoreJwt) {
212212
* @returns {string}
213213
*/
214214

215-
ConnectionConfig.prototype.parseServerURL = function(str) {
215+
ConnectionConfig.prototype.parseServerURL = function (str) {
216216
if (str && (str.substring(0, 7) === 'http://' || str.substring(0, 8) === 'https://')) {
217-
if (str.lastIndexOf('/') !== str.length - 1) {
218-
str += '/' //always append slash to ensure regularity
219-
}
220-
return this.serverUrlEncoding(str)
217+
if (str.lastIndexOf('/') !== str.length - 1) {
218+
// eslint-disable-next-line no-param-reassign
219+
str += '/'; // always append slash to ensure regularity
220+
}
221+
return this.serverUrlEncoding(str);
221222
}
222-
//throw an error this is the
223-
//return false
223+
// throw an error this is the
224+
// return false
224225
throw new Error(`Invalid Server URL: ${str}`);
225-
}
226+
};
226227

227-
ConnectionConfig.prototype.serverUrlEncoding = function(str){
228-
const orgArr = str.split("/")
229-
if(orgArr.length>3){
230-
//const org = encodeURI(orgArr[3])
231-
/*
232-
* if we pass the organization name like Francesca-Bit-9e73
233-
* I need to encode it 2 times because in the database
228+
ConnectionConfig.prototype.serverUrlEncoding = function (str) {
229+
const orgArr = str.split('/');
230+
if (orgArr.length > 3) {
231+
// const org = encodeURI(orgArr[3])
232+
/*
233+
* if we pass the organization name like Francesca-Bit-9e73
234+
* I need to encode it 2 times because in the database
234235
* when we create an id from a property it encodes the property value
235236
*/
236-
const org = encodeURISegment(orgArr[3])
237-
return str.replace(orgArr[3], org)
237+
const org = encodeURISegment(orgArr[3]);
238+
return str.replace(orgArr[3], org);
238239
}
239-
return str
240-
}
240+
return str;
241+
};
241242

242243
/**
243244
* Clear cursor for connection
@@ -376,15 +377,15 @@ ConnectionConfig.prototype.userURL = function (user) {
376377
* @param {string} [action] - the organization id
377378
* @returns {string}
378379
*/
379-
//encodeURIComponent
380-
ConnectionConfig.prototype.organizationURL = function(orgId, action) {
381-
let url = `${this.apiURL()}organization`
382-
//I have to encode the organization 2 times because it is saved encoded inside the database
383-
//
384-
if (orgId) url += `/${encodeURISegment(orgId)}`
385-
if (action) url += `/${encodeURISegment(action)}`
386-
return url
387-
}
380+
// encodeURIComponent
381+
ConnectionConfig.prototype.organizationURL = function (orgId, action) {
382+
let url = `${this.apiURL()}organization`;
383+
// I have to encode the organization 2 times because it is saved encoded inside the database
384+
//
385+
if (orgId) url += `/${encodeURISegment(orgId)}`;
386+
if (action) url += `/${encodeURISegment(action)}`;
387+
return url;
388+
};
388389

389390
/**
390391
* Generate URL for the user's organization api endpoint

lib/dispatchRequest.js

Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ function btoaImplementation(str) {
1717
}
1818

1919
/**
20-
* @param {object} response
20+
* @param {object} response
2121
* @returns {object} Object having two properties result and dataVersion
2222
*/
2323
function getResultWithDataVersion(response) {
24-
return {
25-
result: response.data,
26-
dataVersion: response.headers["terminusdb-data-version"]
27-
? response.headers["terminusdb-data-version"]
28-
: ""
29-
};
24+
return {
25+
result: response.data,
26+
dataVersion: response.headers['terminusdb-data-version']
27+
? response.headers['terminusdb-data-version']
28+
: '',
29+
};
3030
}
3131

3232
/**
@@ -39,8 +39,9 @@ function getResultWithDataVersion(response) {
3939
* @param {string} key optional basic auth string to be passed
4040
* @param {object} customHeaders the unique reqID
4141
*/
42+
// eslint-disable-next-line max-len
4243
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, customHeaders = null, getDataVersion = false) {
43-
/*
44+
/*
4445
*CORS is only required when trying to fetch data from a browser,
4546
*as browsers by default will block requests to different origins
4647
*/
@@ -98,85 +99,87 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
9899
options.headers['User-Agent'] = `terminusdb-client-js/${version}`;
99100
}
100101

101-
switch (action) {
102-
case CONST.DELETE:
103-
if (payload) {
104-
options.headers = options.headers ? options.headers : {}
105-
options.headers['Content-Type'] = 'application/json; charset=utf-8'
106-
options.data = payload
107-
}
108-
return axiosInstance
109-
.delete(url, options)
110-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
111-
.catch(err => {
112-
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
113-
if (err.response && err.response.data) e.data = err.response.data
114-
throw e
115-
})
116-
case CONST.HEAD:
117-
return axiosInstance
118-
.head(url, options)
119-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
120-
.catch(err => {
121-
let e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
122-
if (err.response && err.response.data) {
123-
e.data = err.response.data
124-
}
125-
throw e
126-
})
127-
case CONST.GET:
128-
if (payload) {
129-
const ext = UTILS.URIEncodePayload(payload)
130-
if (ext) url += `?${ext}`
131-
}
132-
return axiosInstance
133-
.get(url, options)
134-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
135-
.catch(err => {
136-
let e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
137-
if (err.response && err.response.data) {
138-
e.data = err.response.data
139-
}
140-
throw e
141-
})
142-
case CONST.ADD_CSV:
143-
case CONST.INSERT_TRIPLES:
144-
options.headers = options.headers ? options.headers : {}
145-
options.headers['Content-Type'] = 'application/form-data; charset=utf-8'
146-
return axiosInstance
147-
.put(url, payload, options)
148-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
149-
.catch(err => {
150-
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
151-
if (err.response && err.response.data) e.data = err.response.data
152-
throw e
153-
})
154-
case CONST.PUT:
155-
options.headers = options.headers ? options.headers : {}
156-
options.headers['Content-Type'] = 'application/json; charset=utf-8'
157-
return axiosInstance
158-
.put(url, payload, options)
159-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
160-
.catch(err => {
161-
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
162-
if (err.response && err.response.data) e.data = err.response.data
163-
throw e
164-
})
165-
case CONST.QUERY_DOCUMENT:
166-
options.headers = options.headers ? options.headers : {}
167-
options.headers['X-HTTP-Method-Override'] = 'GET'
168-
default:
169-
options.headers = options.headers ? options.headers : {}
170-
options.headers['Content-Type'] = 'application/json; charset=utf-8'
171-
return axiosInstance
172-
.post(url, payload, options)
173-
.then(response => getDataVersion ? getResultWithDataVersion(response) : response.data)
174-
.catch(err => {
175-
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
176-
if (err.response && err.response.data) e.data = err.response.data
177-
throw e
178-
})
179-
}
102+
switch (action) {
103+
case CONST.DELETE:
104+
if (payload) {
105+
options.headers = options.headers ? options.headers : {};
106+
options.headers['Content-Type'] = 'application/json; charset=utf-8';
107+
options.data = payload;
108+
}
109+
return axiosInstance
110+
.delete(url, options)
111+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
112+
.catch((err) => {
113+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
114+
if (err.response && err.response.data) e.data = err.response.data;
115+
throw e;
116+
});
117+
case CONST.HEAD:
118+
return axiosInstance
119+
.head(url, options)
120+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
121+
.catch((err) => {
122+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
123+
if (err.response && err.response.data) {
124+
e.data = err.response.data;
125+
}
126+
throw e;
127+
});
128+
case CONST.GET:
129+
if (payload) {
130+
const ext = UTILS.URIEncodePayload(payload);
131+
// eslint-disable-next-line no-param-reassign
132+
if (ext) url += `?${ext}`;
133+
}
134+
return axiosInstance
135+
.get(url, options)
136+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
137+
.catch((err) => {
138+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
139+
if (err.response && err.response.data) {
140+
e.data = err.response.data;
141+
}
142+
throw e;
143+
});
144+
case CONST.ADD_CSV:
145+
case CONST.INSERT_TRIPLES:
146+
options.headers = options.headers ? options.headers : {};
147+
options.headers['Content-Type'] = 'application/form-data; charset=utf-8';
148+
return axiosInstance
149+
.put(url, payload, options)
150+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
151+
.catch((err) => {
152+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
153+
if (err.response && err.response.data) e.data = err.response.data;
154+
throw e;
155+
});
156+
case CONST.PUT:
157+
options.headers = options.headers ? options.headers : {};
158+
options.headers['Content-Type'] = 'application/json; charset=utf-8';
159+
return axiosInstance
160+
.put(url, payload, options)
161+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
162+
.catch((err) => {
163+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
164+
if (err.response && err.response.data) e.data = err.response.data;
165+
throw e;
166+
});
167+
case CONST.QUERY_DOCUMENT:
168+
options.headers = options.headers ? options.headers : {};
169+
options.headers['X-HTTP-Method-Override'] = 'GET';
170+
// eslint-disable-next-line no-fallthrough
171+
default:
172+
options.headers = options.headers ? options.headers : {};
173+
options.headers['Content-Type'] = 'application/json; charset=utf-8';
174+
return axiosInstance
175+
.post(url, payload, options)
176+
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
177+
.catch((err) => {
178+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
179+
if (err.response && err.response.data) e.data = err.response.data;
180+
throw e;
181+
});
182+
}
180183
}
181184

182185
module.exports = DispatchRequest;

lib/utils.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ const Utils = {};
1515
// ˆˆˆˆˆ&&**(((()*(*#&#&&#$*#*(#(#(#)#)#)_
1616
// 'Doc%2F%3D%26test'
1717
//
18-
//%CB%86%CB%86%CB%86%CB%86%CB%86&&**(((()*(*%23&%23&&%23$*%23*(%23(%23(%23)%23)%23)_
19-
//encodeURI() will not encode: ~!@#$&*()=:/,;?+
20-
//%3D%25team =%team
21-
Utils.encodeURISegment = function(str){
22-
if(typeof str !== 'string') return str
23-
str = encodeURI(str)
24-
str = str.replace(/\?/g,'%3F')
25-
str = str.replace(/=/g,'%3D')
26-
str = str.replace(/&/g,'%26')
27-
str = str.replace(/\+/g,'%2B')
28-
str = str.replace(/\#/g,'%23')
29-
str = str.replace(/@/g, '%40');
30-
return str
31-
}
32-
33-
Utils.removeDocType = function(str){
34-
if(typeof str === "string" && str.lastIndexOf(`/`)>-1){
35-
return str.substr(str.lastIndexOf(`/`)+1)
36-
}
37-
return str
38-
}
18+
// %CB%86%CB%86%CB%86%CB%86%CB%86&&**(((()*(*%23&%23&&%23$*%23*(%23(%23(%23)%23)%23)_
19+
// encodeURI() will not encode: ~!@#$&*()=:/,;?+
20+
// %3D%25team =%team
21+
Utils.encodeURISegment = function (str) {
22+
if (typeof str !== 'string') return str;
23+
str = encodeURI(str);
24+
str = str.replace(/\?/g, '%3F');
25+
str = str.replace(/=/g, '%3D');
26+
str = str.replace(/&/g, '%26');
27+
str = str.replace(/\+/g, '%2B');
28+
str = str.replace(/#/g, '%23');
29+
str = str.replace(/@/g, '%40');
30+
return str;
31+
};
32+
33+
Utils.removeDocType = function (str) {
34+
if (typeof str === 'string' && str.lastIndexOf('/') > -1) {
35+
return str.substr(str.lastIndexOf('/') + 1);
36+
}
37+
return str;
38+
};
3939

40-
//%253D%25team
40+
// %253D%25team
4141

4242
/**
4343
* default set of prefixes that will be used for URL compression

0 commit comments

Comments
 (0)