From 7ac52016d1c45bf4e39a34457f91a45ac3bf5157 Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Thu, 10 Mar 2016 08:18:38 -0800 Subject: [PATCH] don't throw runtime errors, fixes #16 --- graphqlhub-schemas/package.json | 2 +- graphqlhub-schemas/src/apis/twitter.js | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/graphqlhub-schemas/package.json b/graphqlhub-schemas/package.json index 1000071..6963b87 100644 --- a/graphqlhub-schemas/package.json +++ b/graphqlhub-schemas/package.json @@ -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": [ diff --git a/graphqlhub-schemas/src/apis/twitter.js b/graphqlhub-schemas/src/apis/twitter.js index fb99f39..cc04024 100644 --- a/graphqlhub-schemas/src/apis/twitter.js +++ b/graphqlhub-schemas/src/apis/twitter.js @@ -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 }); @@ -22,7 +30,7 @@ const __getPromise = (endpoint, parameters, resultPath = null) => { return new Promise((resolve, reject) => { - Twitter.get( + getTwitterClient().get( endpoint, parameters, (error, result) => {