-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas_crud.js
70 lines (64 loc) · 2.01 KB
/
gas_crud.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
class GAS {
constructor (url) {
this.url = url
}
crud (action, scope, fields) {
let params = {}
return new Promise((res, rej) => {
params.action = action
params.scope = scope
if (fields) params.fields = fields
params = this.encodeObjURI(params)
if (params.fields) params.fields = JSON.stringify(params.fields)
let queryString = '?'+Object.keys(params).map(key => key + '=' + params[key]).join('&')
// console.log('...request '+params.action+': '+params.scope+': queryString: ', queryString);
this.get(this.url+queryString)
.then(payload => {
const obj = payload !== 'undefined' ? JSON.parse(payload) : 'No response data included in Payload'
// console.log(params.action+" "+params.scope+" : 🤓 👉 response data: ", obj)
res(obj)
})
.catch( err => rej(err) )
})
}
get (url) {
return new Promise((res, rej) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
res(xhr.responseText)
} else {
rej(xhr)
}
}
}
xhr.send(null)
})
}
encodeObjURI (obj) {
let encodedObj = {}
for (const key in obj) {
const keyName = encodeURIComponent(key)
if (typeof obj[key] === 'object' && !this.isArray(obj[key])) {
encodedObj[encodeURIComponent(key)] = this.encodeObjURI(obj[key])
} else {
let field = obj[key]
if (typeof field !== 'boolean' && typeof field !== 'number' && !this.isArray(obj[key])) {
field = JSON.stringify(field)
field = encodeURIComponent(field)
field = field.split('%22')
field.pop()
field.shift()
field = field.join('%22')
}
encodedObj[encodeURIComponent(key)] = field
}
}
return encodedObj
}
isArray (a) {
return (!!a) && (a.constructor === Array);
}
}