-
Notifications
You must be signed in to change notification settings - Fork 6
/
bitly.js
131 lines (114 loc) · 3.76 KB
/
bitly.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
/*
Copyright 2020 - 2024, Robin de Gruijter (gruijter@hotmail.com)
This file is part of com.gruijter.hyundai_kia.
com.gruijter.hyundai_kia is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.hyundai_kia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with com.gruijter.hyundai_kia. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const https = require('https');
// const qs = require('querystring');
// const util = require('util');
const apiHost = 'api-ssl.bitly.com';
const shortenEP = '/v4/shorten';
// Bitly represents a session to create a small URL
class Bitly {
constructor(opts) {
this.host = opts.host || apiHost;
this.port = opts.port || 443;
this.timeout = opts.timeout || 10000;
this.lastResponse = undefined;
this.apiKey = opts.apiKey;
if (!opts.apiKey) throw Error('apiKey is required');
}
async shorten(longURL) {
try {
const body = {
// group_guid: 'Ba1bc23dE4F',
// domain: 'bit.ly',
long_url: longURL,
};
const path = shortenEP;
const result = await this._makeRequest(path, body);
return Promise.resolve(result.link);
} catch (error) {
return Promise.reject(error);
}
}
async _makeRequest(path, message) {
try {
// const postData = message; // ? qs.stringify(message) : '';
const postData = message ? JSON.stringify(message) : '';
const headers = {
Authorization: `Bearer ${this.apiKey}`,
'content-length': Buffer.byteLength(postData),
connection: 'Keep-Alive',
'Content-Type': 'application/json',
};
const options = {
hostname: this.host,
port: this.port,
path,
headers,
method: 'POST',
};
const result = await this._makeHttpsRequest(options, postData);
if ((result.statusCode !== 200 && result.statusCode !== 201) || !result.body) throw Error(result.body || result.statusCode);
const json = JSON.parse(result.body);
if (!json.link) throw Error(json);
return Promise.resolve(json);
} catch (error) {
return Promise.reject(error);
}
}
_makeHttpsRequest(options, postData, timeout) {
return new Promise((resolve, reject) => {
const opts = options;
opts.timeout = timeout || this.timeout;
const req = https.request(opts, (res) => {
let resBody = '';
res.on('data', (chunk) => {
resBody += chunk;
});
res.once('end', () => {
if (!res.complete) {
return reject(Error('The connection was terminated while the message was still being sent'));
}
res.body = resBody;
return resolve(res); // resolve the request
});
});
req.on('error', (e) => {
req.destroy();
this.lastResponse = e; // e.g. ECONNREFUSED on wrong soap port or wrong IP // ECONNRESET on wrong IP
return reject(e);
});
req.on('timeout', () => {
req.destroy();
});
// req.write(postData);
req.end(postData);
});
}
}
module.exports = Bitly;
/*
{
"created_at":"2020-08-24T12:59:47+0000",
"id":"bit.ly/3hsAAaP",
"link":"https://bit.ly/3hsAAaP",
"custom_bitlinks":[],
"long_url":"https://www.google.com/",
"archived":false,
"tags":[],
"deeplinks":[],
"references": { "group":"https://api-ssl.bitly.com/v4/groups/Bk8oceqnWmo" }
}
*/