-
Notifications
You must be signed in to change notification settings - Fork 14
/
twitter.js
61 lines (58 loc) · 1.63 KB
/
twitter.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
const crypto = require('crypto')
const OAuth = require('oauth-1.0a')
const axios = require('axios')
class Twitter {
constructor (consumerKey, consumerSecret) {
this.baseUrl = 'https://api.twitter.com/'
this.token = {}
let oauth = OAuth({
consumer: {
key: consumerKey,
secret: consumerSecret
},
signature_method: 'HMAC-SHA1',
hash_function (baseString, key) {
return crypto.createHmac('sha1', key).update(baseString).digest('base64')
}
})
axios.interceptors.request.use((config) => {
config.headers = oauth.toHeader(oauth.authorize({
url: `${config.baseURL}${config.url}`,
method: config.method,
data: config.data
}, this.token))
config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
return config
})
axios.defaults.baseURL = this.baseUrl
}
setToken (key, secret) {
this.token = { key, secret }
}
async get (api) {
try {
let response = await axios.get(api)
return response.data
} catch (error) {
handleTwitterError(error)
}
}
async post (api, data) {
try {
let response = await axios.post(api, data)
return response.data
} catch (error) {
handleTwitterError(error)
}
}
}
function handleTwitterError (error) {
if (error.message.includes('401')) {
throw new Error('Invalid Twitter credentials -- try running \'configure\' again')
} else if (error.message.includes('429')) {
throw new Error('Twitter rate limit reached -- try again later')
} else {
throw new Error(`Twitter: ${error.message}`)
}
}
module.exports = Twitter