forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten-url.js
32 lines (28 loc) · 1008 Bytes
/
shorten-url.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import queryString from 'query-string';
import url from 'url';
export default function shortenUrl(urlToShorten: string): Promise<string> {
let longUrl = urlToShorten;
if (!longUrl.startsWith('https://perf-html.io/')) {
const parsedUrl = url.parse(longUrl);
const parsedUrlOnCanonicalHost = Object.assign({}, parsedUrl, {
protocol: 'https:',
host: 'perf-html.io',
});
longUrl = url.format(parsedUrlOnCanonicalHost);
}
const bitlyQueryUrl =
'https://api-ssl.bitly.com/v3/shorten?' +
queryString.stringify({
longUrl,
domain: 'perfht.ml',
format: 'json',
access_token: 'b177b00a130faf3ecda6960e8b59fde73e902422',
});
return fetch(bitlyQueryUrl)
.then(response => response.json())
.then(json => json.data.url);
}