-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (112 loc) · 3.1 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
const fetch = require('node-fetch')
class EasyBoxClient {
constructor(boxHost = `https://easybox.bots.wtf`, boxID, auth = null) {
this.boxHost = boxHost
this.boxID = boxID
this.boxAuthorization = auth
}
async newBox() {
var b = await fetch(`${this.boxHost}/new`, {
method: `POST`
})
if (!b.ok) {
throw new Error(`Internal Server Error: Status Code ${b.status}`)
}
var j = await b.json()
this.boxID = j.boxID
return j
}
async get(collectionName){
var b = await fetch(`${this.boxHost}/box/${this.boxID}/${collectionName}`, {
method: `GET`,
headers: {
'Authorization': this.boxAuthorization
}
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code ${b.status}`)
}
var json = await b.json()
return json
}
async insertOne(collectionName, data){
var b = await fetch(`${this.boxHost}/box/${this.boxID}/${collectionName}`, {
method: `POST`,
headers: {
'Authorization': this.boxAuthorization,
'Content-Type': `application/json`
},
body: JSON.stringify(data)
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code ${b.status}`)
}
var json = await b.json()
return json
}
async insert(collectionName, data){
var isArray = false
if(data[0]){
isArray = true
}
if(!isArray){
return await this.insert(collectionName, data)
}
var b = await fetch(`${this.boxHost}/box/${this.boxID}/${collectionName}`, {
method: `POST`,
headers: {
'Authorization': this.boxAuthorization,
'Content-Type': `application/json`
},
body: JSON.stringify(data)
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code ${b.status}`)
}
var json = await b.json()
return json
}
async deleteOne(collectionName, documentID){
var b = await fetch(`${this.boxHost}/box/${this.boxID}/${collectionName}/${documentID}`, {
method: `DELETE`,
headers: {
'Authorization': this.boxAuthorization
}
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code: ${b.status}`)
}
return true
}
async updateOne(collectionName, documentID, newData){
var b = await fetch(`${this.boxHost}/box/${this.boxID}/${collectionName}/${documentID}`, {
method: `PUT`,
headers: {
'Authorization': this.boxAuthorization,
'Content-Type': `application/json`
},
body: JSON.stringify(newData)
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code: ${b.status}`)
}
return newData
}
async destroy(){
var b = await fetch(`${this.boxHost}/box/${this.boxID}`, {
method: `DELETE`,
headers: {
'Authorization': this.boxAuthorization,
'Content-Type': 'application/json'
}
})
if(!b.ok){
throw new Error(`Internal Server Error: Status Code: ${b.status}`)
}
this.boxID = undefined
this.boxHost = undefined
this.boxAuthorization = undefined
return true
}
}
module.exports = EasyBoxClient