-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (271 loc) · 13 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const fetch = require("node-fetch");
const { LinksCollector } = require("./classes/LinksCollector");
class shortio {
/**
* This is the main class of the package, it holds every functions.
* @param {String} domain [required] Managed domain on short.io
* @param {number} domainId [required] API Key to access user account
* @param {String} api_key [required] Managed domain ID
*/
constructor(domain, domainId, api_key) {
this.domain = domain;
this.api_key = api_key;
this.domainId = domainId;
}
/**
* This function gets a list of 150 (API Max) links from a single domain. Endpoint: GET https://api.short.io/api/links
* @param {number} limit Number of links returned by the API search. (Max 150)
* @param {String} tag Tag to filter in the links list
* @param {number} offset Link offset (Default: 0)
* @returns {Promise<LinksCollector>} Links collection object returned by the API
*/
getLinks(limit = 150, tag = "", offset = 0) {
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { accept: 'application/json', authorization: this.api_key }
};
fetch(`https://api.short.io/api/links?domain_id=${this.domainId}&offset=${offset}&limit=${limit}${(tag != "") ? "&tag=" + tag : ""}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject("Error: " + json.error);
let links = new LinksCollector(json.links, json.count);
resolve(links);
});
});
}
/**
* This function gets a link object from the specified domain (filtered by the link path). Endpoint: GET https://api.short.io/links/expand
* @param {String} path [required] Link's path
* @returns {Promise<Object>} Link JSON Object returned by the API
*/
getLink(path) {
if (path == "") throw new Error("path is undefined")
return new Promise((resolve, reject) => {
let data = {
method: 'GET',
headers: { accept: 'application/json', authorization: this.api_key }
};
fetch(`https://api.short.io/links/expand?domain=${this.domain}&path=${path}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject("Error: " + json.error);
resolve(json);
});
});
}
/**
* This function gets a link by searching by it's originalURL. Endpoint: GET https://api.short.io/links/by-original-url
* @param {string} originalURL [required] The originalURL of the queried link
* @returns {Promise<Object>} The link object behind the queried originalURL
*/
getByOriginalURL(originalURL) {
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { accept: 'application/json', 'content-type': "application/json", authorization: this.api_key },
};
fetch(`https://api.short.io/links/by-original-url?domain=${this.domain}&originalURL=${originalURL}`, data)
.then(response => response.json())
.then(json => {
if (json.error != undefined) return reject("Error: " + json.error);
resolve(json);
});
});
}
/**
* This function create a link on the specified domain and returns a link object. Endpoint: POST https://api.short.io/links
* @param {Object} options [required] The link object to create on the specified domain. option.originalURL is mendatory.
* @returns {Promise<Object>} Created Link's JSON object returned by the API
*/
createLink(options) {
if (!options.originalURL) throw new Error("option.originalURL is undefined");
options.domain = this.domain;
return new Promise((resolve, reject) => {
let data = {
method: "POST",
headers: { accept: 'application/json', 'content-type': "application/json", authorization: this.api_key },
body: JSON.stringify(options),
json: true
};
fetch("https://api.short.io/links", data)
.then(response => response.json())
.then(json => {
if (json.error) return reject("Error: " + json.error);
resolve(json);
});
});
}
/**
* This function create up to 1000 different links and returns a LinksCollector Object. Endpoint: POST https://api.short.io/links/bulk
* @param {Object[]} links [required] Array of Links Objects. In each Object, the Object.originalURL must be defined.
* @returns {Promise<LinksCollector>} LinksCollector Object returned by the API call.
*/
createLinkBulk(links) {
if (links.length < 2) throw new Error("Cannot send less than two links, please use the createLink method");
if (links.length > 1000) throw new Error("Cannot send more than one thousand links, please split your link array");
return new Promise((resolve, reject) => {
let data = {
method: "POST",
headers: { accept: 'application/json', 'content-type': "application/json", authorization: this.api_key },
body: JSON.stringify({ domain: this.domain, links: links }),
json: true
};
fetch("https://api.short.io/links/bulk", data)
.then(response => response.json())
.then(json => {
if (json.error != undefined) return reject("Error: " + json.error);
resolve(new LinksCollector(json, json.length));
});
});
}
/**
* This function updates a link from the specified domain to change it's params. Endpoint: POST https://api.short.io/links/:link_id
* @param {number} id This is the ID of the link you want to update.
* @param {Object} linkObject This is the new link object of the updated link.
* @returns {Promise<Object>} Updated link's object returned by the API.
*/
updateLink(id, linkObject) {
if (!linkObject.originalURL) throw new Error("originalURL is not defined");
linkObject.domain = this.domain;
return new Promise((resolve, reject) => {
let data = {
method: "POST",
headers: { accept: 'application/json', 'content-type': "application/json", authorization: this.api_key },
body: JSON.stringify(linkObject),
json: true
};
fetch(`https://api.short.cm/links/${id}`, data)
.then(response => response.json())
.then(json => {
if (json.error != undefined) return reject("Error: " + json.error);
resolve(json);
});
});
}
/**
* This function archive a link from the specified domain. Endpoint: POST https://api.short.cm/links/archive
* @param {number} link_id The ID of the link you want to archive.
* @returns {Promise<Object>} Request response object into a promise.
*/
archiveLink(link_id) {
return new Promise((resolve, reject) => {
let data = {
method: 'POST',
headers: { 'content-type': "application/json", authorization: this.api_key },
body: JSON.stringify({ link_id: link_id }),
json: true
};
fetch("https://api.short.cm/links/archive", data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve({ action: "archive", result: true });
});
});
}
/**
* This function deletes a link from the specified domain. Endpoint: DELETE https://api.short.cm/links/:link_id
* @param {number} link_id The ID of the link you want to delete.
* @returns {Promise<Object>} Request response object into a promise.
*/
deleteLink(link_id) {
return new Promise((resolve, reject) => {
let data = {
method: 'DELETE',
headers: { 'content-type': "application/json", authorization: this.api_key }
};
fetch(`https://api.short.io/links/${link_id}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve({ action: "delete", result: true });
});
});
}
/**
* This functions calls the registered domain's stats. GET https://api-v2.short.cm/statistics/domain/:domainId
* @param {string} period The observed period for the stats.
* @param {number} tzOffset The difference between your timezone & the GMT timezone.
* @returns {Object} The selected domain's stats returned by the API.
*/
getDomainStats(period = "", tzOffset = 0) {
if (["today", "yesterday", "week", "month", "lastmonth", "last7", "last30", "total"].indexOf(period) < 0) throw new Error("The period is either invalid or undefined");
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { 'content-type': "application/json", authorization: this.api_key }
}
fetch(`https://api-v2.short.cm/statistics/domain/${this.domainId}?period=${period}&tzOffset=${tzOffset}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve(json);
});
});
}
/**
* This function gets the clicks stats for the requested IDs. GET https://api-v2.short.cm/statistics/domain/:domainID/link_clicks
* @param {Array<number>} ids All the IDs you want to get the stats for
* @returns {Promise<Object>} The JSON object corresponding to the stats of the requested IDs. Format: { ID: value, ... }
*/
getLinksClicks(ids) {
if (ids.length < 1) throw new Error("The provided IDs are not in an Array format");
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { Authorization: this.api_key }
}
fetch(`https://api-v2.short.cm/statistics/domain/${this.domainId}/link_clicks?ids=${ids.join(",")}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve(json);
});
});
}
/**
* This functions calls the most clicked links during the selected period. GET https://api-v2.short.cm/statistics/domain/:domainId/paths
* @param {string} period The observed period for the stats.
* @param {number} offset The difference between your timezone & the GMT timezone.
* @returns {Object} The selected period's stats returned by the API.
*/
getPopularPaths(period, offset = 0) {
if (["today", "yesterday", "week", "month", "lastmonth", "last7", "last30", "total"].indexOf(period) < 0) throw new Error("The period is either invalid or undefined");
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { Authorization: this.api_key }
}
fetch(`https://api-v2.short.cm/statistics/domain/${this.domainId}/paths?period=${period}&tzOffset=${offset}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve(json);
});
});
}
/**
* This function gets the stats from a precise link. GET https://api-v2.short.cm/statistics/link/:linkId
* @param {number} id The ID of the link you want to get the stats from
* @param {string} period The observed period for the start
* @param {number} offset The difference between your timezone & the GMT timezone
* @returns {Promise<Object>} The returned link's stats from the API
*/
getLinkStats(id, period, offset = 0) {
if (["today", "yesterday", "week", "month", "lastmonth", "last7", "last30", "total"].indexOf(period) < 0) throw new Error("The period is either invalid or undefined");
return new Promise((resolve, reject) => {
let data = {
method: "GET",
headers: { Authorization: this.api_key }
}
fetch(`https://api-v2.short.cm/statistics/link/${id}?period=${period}&tzOffset=${offset}`, data)
.then(response => response.json())
.then(json => {
if (json.error) return reject(json.error);
resolve(json);
});
});
}
}
module.exports = shortio;