-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (68 loc) · 2.19 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
const fetch = require("node-fetch");
const FormData = require("form-data");
const { APIError, ParamError } = require("./errors.js");
const BASE_URL = "https://api.shrtco.de/v2/";
class Shorter {
/**
* shrtco.de
*
* @param {String} url URL to short.
*/
constructor(url) {
if (!url) throw new ParamError({
code: 0,
message: 'You forgot "URL" argument.'
})
this.url = encodeURI(url)
};
/**
* Shorten link.
*
* @param {Object} params? Object with params.
* @param {String} params.code? Your code.
* @param {String} params.pass? Your password.
* @param {Boolean} params.emoji? Make code as emoji
* @returns {Object} JSON with shorted urls
*/
async short(params) {
let form = new FormData();
if (params.code) form.append("custom_code", params.code);
if (params.pass) form.append("password", params.pass)
if (params.code || params.pass) form.append("url", this.url)
const result = (await (await fetch(BASE_URL + "shorten" + ((!params.pass && !params.code) ? `?url=` + this.url : "") + (params.emoji ? `${(params.code || params.pass) ? "?" : "&"}emoji` : ""), (params.code || params.pass) ? {
method: "POST",
body: form
} : undefined)).json());
if (!result.ok) throw new APIError({
code: result.error_code,
message: result.error
});
return result
};
};
class Info {
/**
* Get information about link from code.
* @param {String} code Link code
* @returns {JSON} Info about code.
*/
constructor(code) {
if (!code) throw new ParamError({
code: 0,
message: 'You forgot "code" argument.'
})
return this.info(encodeURI(code))
};
async info(code) {
const result = (await (await fetch(BASE_URL + "info?code=" + code)).json());
if (!result.ok) throw new APIError({
code: result.error_code,
message: result.error
});
return result
}
}
module.exports = {
Shorter,
Info
}