-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.js
211 lines (199 loc) · 8.1 KB
/
Client.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
const fetch = require("node-fetch");
class Client {
constructor(key) {
if (typeof key != "string") {
throw new TypeError('argument "key" should be a string');
}
async function check() {
var response = await fetch("https://api.dsc.gg/v2/app", {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
});
var responseData = await response.json();
if (!responseData.success) throw new TypeError('argument "key" is not a valid API key');
}
check()
this.base_url = "https://api.dsc.gg";
this.api_key = key;
}
async fetchLink(link_id) {
if (typeof link_id != "string") throw new TypeError('argument "link" should be a string');
var response = await fetch("https://api.dsc.gg/v2/link/" + link_id, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async deleteLink(link_id) {
if (typeof link_id != "string") throw new TypeError('argument "link" should be a string');
var response = await fetch("https://api.dsc.gg/v2/link/" + link_id, {
method: "delete",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async createLink(request) {
if (typeof request.link != "string") throw new TypeError('argument "link" should be a string');
if (typeof request.type != "string") throw new TypeError('argument "type" should be a string');
if (typeof request.redirect != "string") throw new TypeError('argument "redirect" should be a string');
var body = request;
var response = await fetch("https://api.dsc.gg/v2/link/" + request.link, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async updateLink(request) {
if (typeof request.link != "string") throw new TypeError('argument "link" should be a string');
if (typeof request.type != "string") throw new TypeError('argument "type" should be a string');
if (typeof request.redirect != "string") throw new TypeError('argument "redirect" should be a string');
var body = request;
var response = await fetch("https://api.dsc.gg/v2/link/" + request.link, {
method: "patch",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async fetchUser(user_id) {
if (typeof user_id != "string") throw new TypeError('argument "link" should be a string');
var response = await fetch("https://api.dsc.gg/v2/user/" + user_id, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async fetchUserLinks(user_id) {
if (typeof user_id != "string") throw new TypeError('argument "link" should be a string');
var response = await fetch("https://api.dsc.gg/v2/user/" + user_id + '/links', {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async fetchUserApps(user_id) {
if (typeof user_id != "string") throw new TypeError('argument "link" should be a string');
var response = await fetch("https://api.dsc.gg/v2/user/" + user_id + '/apps', {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async fetchApp(app_id) {
if (typeof app_id != "string") throw new TypeError('argument "app" should be a string');
var response = await fetch("https://api.dsc.gg/v2/app/" + app_id, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async fetchTopLinks() {
var response = await fetch("https://api.dsc.gg/v2/top", {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
async search(link_id, json) {
if (typeof link_id != "string") throw new TypeError('argument "link" should be a string');
if (json) {
if (json && json.limit && typeof json.limit != "number") throw new TypeError('argument "limit" should be a number');
if (json && json.type && typeof json.type != "string") throw new TypeError('argument "type" should be a string');
if (json && json.type !== 'bot' && json.type !== 'server' && json.type !== 'template') throw new TypeError('argument "type" should be equal to bot, server, or template');
if (json.limit && json.type) {
var response = await fetch("https://api.dsc.gg/v2/search?q=" + link_id + "&limit=" + Number(json.limit) + "&type=" + json.type, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
} else
if (json.limit) {
var response = await fetch("https://api.dsc.gg/v2/search?q=" + link_id + "&limit=" + Number(json.limit), {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
} else
if (json.type) {
var response = await fetch("https://api.dsc.gg/v2/search?q=" + link_id + "&type=" + json.type, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
} else {
var response = await fetch("https://api.dsc.gg/v2/search?q=" + link_id + "&limit=" + Number(json.limit), {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
} else {
var response = await fetch("https://api.dsc.gg/v2/search?q=" + link_id, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: this.api_key,
},
});
var responseData = await response.json();
return responseData;
}
}
}
module.exports.Client = Client;