Skip to content

Commit 7ac5201

Browse files
committed
don't throw runtime errors, fixes #16
1 parent 9258283 commit 7ac5201

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

graphqlhub-schemas/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "graphqlhub-schemas",
33
"repository": "clayallsopp/graphqlhub",
4-
"version": "0.1.0-rc6",
4+
"version": "0.1.0-rc7",
55
"description": "GraphQL Schemas for REST APIs like Github, Hacker News, Reddit, and Twitter",
66
"main": "lib/index.js",
77
"files": [

graphqlhub-schemas/src/apis/twitter.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ const {
66
TWITTER_CONSUMER_SECRET
77
} = process.env;
88

9-
const Twitter = new Twit({
10-
consumer_key : TWITTER_CONSUMER_KEY,
11-
consumer_secret : TWITTER_CONSUMER_SECRET,
12-
app_only_auth : true
13-
});
9+
// Twit throws a runtime error if you try to create a client
10+
// without API keys, so we do it lazily
11+
let twitterClient = undefined;
12+
const getTwitterClient = () => {
13+
if (!twitterClient) {
14+
twitterClient = new Twit({
15+
consumer_key : TWITTER_CONSUMER_KEY,
16+
consumer_secret : TWITTER_CONSUMER_SECRET,
17+
app_only_auth : true
18+
});
19+
}
20+
return twitterClient;
21+
};
1422

1523
export const getUser = (identifier, identity) => __getPromise('users/show', { [identifier]: identity });
1624
export const getTweets = (user_id, count) => __getPromise('statuses/user_timeline', { user_id, count });
@@ -22,7 +30,7 @@ const __getPromise = (endpoint, parameters, resultPath = null) => {
2230

2331
return new Promise((resolve, reject) => {
2432

25-
Twitter.get(
33+
getTwitterClient().get(
2634
endpoint,
2735
parameters,
2836
(error, result) => {

0 commit comments

Comments
 (0)