From 560ea6f2fda5507b77cac1e5d261d19b5d221c62 Mon Sep 17 00:00:00 2001 From: ishowvel Date: Thu, 24 Oct 2024 11:43:45 +0000 Subject: [PATCH 1/2] feat: make twitter client be optional --- src/twitter/delete-tweet.ts | 20 ++++++++++++-------- src/twitter/post-tweet.ts | 16 ++++++++++------ src/twitter/twitter.ts | 20 ++++++++++++-------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/twitter/delete-tweet.ts b/src/twitter/delete-tweet.ts index 0fd12487c..a44340a32 100644 --- a/src/twitter/delete-tweet.ts +++ b/src/twitter/delete-tweet.ts @@ -1,14 +1,18 @@ import { twitterClient } from "./twitter"; export async function deleteTweet(id: string) { - try { - const { data } = await twitterClient.v2.deleteTweet(id); - if (data?.deleted) { - console.log(`Successfully deleted tweet, id: ${id}`); - } else { - console.log(`Could not delete tweet, id ${id}`); + if (twitterClient) { + try { + const { data } = await twitterClient.v2.deleteTweet(id); + if (data?.deleted) { + console.log(`Successfully deleted tweet, id: ${id}`); + } else { + console.log(`Could not delete tweet, id ${id}`); + } + } catch (error) { + console.error("Error deleting tweet", error); } - } catch (error) { - console.error("Error deleting tweet", error); + } else { + console.log("Skipping deleting a tweet due to missing env variables"); } } diff --git a/src/twitter/post-tweet.ts b/src/twitter/post-tweet.ts index 5e2c9e126..6c6ddd83e 100644 --- a/src/twitter/post-tweet.ts +++ b/src/twitter/post-tweet.ts @@ -1,11 +1,15 @@ import { twitterClient } from "./twitter"; export async function postTweet(status: string) { - try { - const { data } = await twitterClient.v2.tweet(status); - console.log(`Tweet posted successfully, id: ${data.id}, text: ${data.text}`); - return data; - } catch (error) { - console.error("Error posting tweet", error); + if (twitterClient) { + try { + const { data } = await twitterClient.v2.tweet(status); + console.log(`Tweet posted successfully, id: ${data.id}, text: ${data.text}`); + return data; + } catch (error) { + console.error("Error posting tweet", error); + } + } else { + console.log("Skipping posting a tweet due to missing env variables"); } } diff --git a/src/twitter/twitter.ts b/src/twitter/twitter.ts index 1ef601a4a..0e8855149 100644 --- a/src/twitter/twitter.ts +++ b/src/twitter/twitter.ts @@ -10,19 +10,23 @@ const apiKeySecret = process.env.TWITTER_API_KEY_SECRET; const accessToken = process.env.TWITTER_ACCESS_TOKEN; const accessTokenSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET; +let twitterClient: TwitterApi | undefined; + if (!apiKey || !apiKeySecret || !accessToken || !accessTokenSecret) { - throw new Error("Twitter environment variables are not set"); + console.log("Twitter environment variables not found! Skipping sync to social media."); +} else { + twitterClient = new TwitterApi({ + appKey: apiKey, + appSecret: apiKeySecret, + accessToken: accessToken, + accessSecret: accessTokenSecret, + }); } -export const twitterClient = new TwitterApi({ - appKey: apiKey, - appSecret: apiKeySecret, - accessToken: accessToken, - accessSecret: accessTokenSecret, -}); +export { twitterClient }; export default { postTweet, deleteTweet, - client: twitterClient, + client: twitterClient ?? undefined, }; From 2c5187dc71960d01a98e60408f880772592bdf2d Mon Sep 17 00:00:00 2001 From: ishowvel Date: Thu, 24 Oct 2024 17:22:25 +0530 Subject: [PATCH 2/2] fix: remove redundant code --- src/twitter/twitter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/twitter/twitter.ts b/src/twitter/twitter.ts index 0e8855149..317b0ff3c 100644 --- a/src/twitter/twitter.ts +++ b/src/twitter/twitter.ts @@ -28,5 +28,5 @@ export { twitterClient }; export default { postTweet, deleteTweet, - client: twitterClient ?? undefined, + client: twitterClient, };