-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (27 loc) · 775 Bytes
/
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
'use strict'
require('dotenv').config({ path: __dirname + '/variables.env' })
const accessToken = process.env.BITLY_ACCESS_TOKEN || BITLY_ACCESS_TOKEN
if (!Boolean(accessToken)) {
console.log('No access token defined')
return
}
// Get url from clipboard and check if is url or return
const Clipboardy = require('clipboardy')
const validUrl = require('valid-url')
const clipboard = Clipboardy.readSync()
if (!validUrl.isUri(clipboard)) {
console.log('No url to shorten')
return
}
const { BitlyClient } = require('bitly')
const bitly = new BitlyClient(accessToken)
bitly
.shorten(clipboard)
.then(function(result) {
const shortenUrl = result.url
Clipboardy.writeSync(shortenUrl)
console.log(shortenUrl)
})
.catch(function(error) {
console.error(error)
})