Skip to content

Commit

Permalink
don't throw runtime errors, fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp committed Mar 10, 2016
1 parent 9258283 commit 7ac5201
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion graphqlhub-schemas/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphqlhub-schemas",
"repository": "clayallsopp/graphqlhub",
"version": "0.1.0-rc6",
"version": "0.1.0-rc7",
"description": "GraphQL Schemas for REST APIs like Github, Hacker News, Reddit, and Twitter",
"main": "lib/index.js",
"files": [
Expand Down
20 changes: 14 additions & 6 deletions graphqlhub-schemas/src/apis/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ const {
TWITTER_CONSUMER_SECRET
} = process.env;

const Twitter = new Twit({
consumer_key : TWITTER_CONSUMER_KEY,
consumer_secret : TWITTER_CONSUMER_SECRET,
app_only_auth : true
});
// Twit throws a runtime error if you try to create a client
// without API keys, so we do it lazily
let twitterClient = undefined;
const getTwitterClient = () => {
if (!twitterClient) {
twitterClient = new Twit({
consumer_key : TWITTER_CONSUMER_KEY,
consumer_secret : TWITTER_CONSUMER_SECRET,
app_only_auth : true
});
}
return twitterClient;
};

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

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

Twitter.get(
getTwitterClient().get(
endpoint,
parameters,
(error, result) => {
Expand Down

0 comments on commit 7ac5201

Please sign in to comment.