From df766748b5df7e4f536ccbead6f64fd44fd0c54b Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Sat, 9 Oct 2021 09:16:44 +0530 Subject: [PATCH] refactor: rename add method of managers to _add --- src/books/BlocksBook.ts | 2 +- src/books/ComposedTweetsBook.ts | 2 +- src/books/FollowersBook.ts | 2 +- src/books/FollowingsBook.ts | 2 +- src/books/LikedTweetsBook.ts | 2 +- src/books/MentionsBook.ts | 2 +- src/books/MutesBook.ts | 2 +- src/books/SearchTweetsBook.ts | 2 +- src/client/Client.ts | 4 ++-- src/managers/BaseManager.ts | 2 +- src/managers/FilteredStreamRuleManager.ts | 6 +++--- src/managers/ListManager.ts | 2 +- src/managers/SpaceManager.ts | 8 ++++---- src/managers/TweetManager.ts | 4 ++-- src/managers/UserManager.ts | 4 ++-- src/managers/__tests__/BaseManager.test.ts | 6 +++--- 16 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/books/BlocksBook.ts b/src/books/BlocksBook.ts index f71f238..b21c6e7 100644 --- a/src/books/BlocksBook.ts +++ b/src/books/BlocksBook.ts @@ -101,7 +101,7 @@ export class BlocksBook extends BaseBook { const rawUsers = data.data; const rawIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }, false); + const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }, false); blockedUsersCollection.set(user.id, user); } return blockedUsersCollection; diff --git a/src/books/ComposedTweetsBook.ts b/src/books/ComposedTweetsBook.ts index 7eba0ba..06d3d23 100644 --- a/src/books/ComposedTweetsBook.ts +++ b/src/books/ComposedTweetsBook.ts @@ -144,7 +144,7 @@ export class ComposedTweetsBook extends BaseBook { const rawTweets = data.data; const rawIncludes = data.includes; for (const rawTweet of rawTweets) { - const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes }, false); + const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes }, false); tweetsCollection.set(tweet.id, tweet); } return tweetsCollection; diff --git a/src/books/FollowersBook.ts b/src/books/FollowersBook.ts index 0d3c245..4d06032 100644 --- a/src/books/FollowersBook.ts +++ b/src/books/FollowersBook.ts @@ -102,7 +102,7 @@ export class FollowersBook extends BaseBook { const rawUsers = data.data; const rawIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }); + const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }); followersCollection.set(user.id, user); } return followersCollection; diff --git a/src/books/FollowingsBook.ts b/src/books/FollowingsBook.ts index da07d40..4a2ef2a 100644 --- a/src/books/FollowingsBook.ts +++ b/src/books/FollowingsBook.ts @@ -102,7 +102,7 @@ export class FollowingsBook extends BaseBook { const rawUsers = data.data; const rawIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }); + const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }); followingsCollection.set(user.id, user); } return followingsCollection; diff --git a/src/books/LikedTweetsBook.ts b/src/books/LikedTweetsBook.ts index 4adac62..cda0fd9 100644 --- a/src/books/LikedTweetsBook.ts +++ b/src/books/LikedTweetsBook.ts @@ -105,7 +105,7 @@ export class LikedTweetsBook extends BaseBook { const rawTweets = data.data; const rawIncludes = data.includes; for (const rawTweet of rawTweets) { - const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); + const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); likedTweetsCollection.set(tweet.id, tweet); } return likedTweetsCollection; diff --git a/src/books/MentionsBook.ts b/src/books/MentionsBook.ts index 9a4325c..c7080bd 100644 --- a/src/books/MentionsBook.ts +++ b/src/books/MentionsBook.ts @@ -132,7 +132,7 @@ export class MentionsBook extends BaseBook { const rawTweets = data.data; const rawIncludes = data.includes; for (const rawTweet of rawTweets) { - const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); + const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); mentioningTweetsCollection.set(tweet.id, tweet); } return mentioningTweetsCollection; diff --git a/src/books/MutesBook.ts b/src/books/MutesBook.ts index 93baa87..26fcfdf 100644 --- a/src/books/MutesBook.ts +++ b/src/books/MutesBook.ts @@ -101,7 +101,7 @@ export class MutesBook extends BaseBook { const rawUsers = data.data; const rawIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }, false); + const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }, false); mutedUsersCollection.set(user.id, user); } return mutedUsersCollection; diff --git a/src/books/SearchTweetsBook.ts b/src/books/SearchTweetsBook.ts index a81bbf2..76f39ed 100644 --- a/src/books/SearchTweetsBook.ts +++ b/src/books/SearchTweetsBook.ts @@ -118,7 +118,7 @@ export class SearchTweetsBook extends BaseBook { const rawTweets = data.data; const rawIncludes = data.includes; for (const rawTweet of rawTweets) { - const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); + const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes }); tweetsCollection.set(tweet.id, tweet); } return tweetsCollection; diff --git a/src/client/Client.ts b/src/client/Client.ts index e94c4d6..763fec4 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -204,7 +204,7 @@ export class Client extends BaseClient { } try { const rawData: GetFilteredTweetStreamResponse = JSON.parse(data); - const tweet = this.tweets.add(rawData.data.id, rawData, false); + const tweet = this.tweets._add(rawData.data.id, rawData, false); const matchingRules = rawData.matching_rules.reduce((col, rule) => { col.set(rule.id, new MatchingRule(rule)); return col; @@ -245,7 +245,7 @@ export class Client extends BaseClient { } try { const rawTweet: GetSampledTweetStreamResponse = JSON.parse(data); - const tweet = this.tweets.add(rawTweet.data.id, rawTweet, false); + const tweet = this.tweets._add(rawTweet.data.id, rawTweet, false); this.emit(ClientEvents.SAMPLED_TWEET_CREATE, tweet); } catch (error) { // twitter sends corrupted data sometimes that throws error while parsing it diff --git a/src/managers/BaseManager.ts b/src/managers/BaseManager.ts index 3a1b0ed..38249d1 100644 --- a/src/managers/BaseManager.ts +++ b/src/managers/BaseManager.ts @@ -62,7 +62,7 @@ export class BaseManager { * @param data The raw data returned by the API for this structure * @param cacheAfterFetching Whether to store the structure in the manager's cache */ - add(id: K, data: RawData, cacheAfterFetching = true): T { + _add(id: K, data: RawData, cacheAfterFetching = true): T { const entry = new this._holds(this.client, data); if (cacheAfterFetching) this.cache.set(id, entry); return entry; diff --git a/src/managers/FilteredStreamRuleManager.ts b/src/managers/FilteredStreamRuleManager.ts index 6f0a68a..1edc4fe 100644 --- a/src/managers/FilteredStreamRuleManager.ts +++ b/src/managers/FilteredStreamRuleManager.ts @@ -81,7 +81,7 @@ export class FilteredStreamRuleManager extends BaseManager< requestData, ); return res.data.reduce((createdRules, rawRule) => { - const rule = this.add(rawRule.id, rawRule); + const rule = this._add(rawRule.id, rawRule); return createdRules.set(rule.id, rule); }, new Collection()); } @@ -138,7 +138,7 @@ export class FilteredStreamRuleManager extends BaseManager< const res: GetFilteredTweetStreamRulesResponse = await this.client._api.tweets.search.stream.rules.get(requestData); const rawRule = res.data?.[0]; if (!rawRule) throw new CustomError('RULE_NOT_FOUND'); - return this.add(rawRule.id, rawRule, options.cacheAfterFetching); + return this._add(rawRule.id, rawRule, options.cacheAfterFetching); } async #fetchMultipleRules( @@ -154,7 +154,7 @@ export class FilteredStreamRuleManager extends BaseManager< const rawRules = res.data; if (!rawRules?.length) return fetchedRules; for (const rawRule of rawRules) { - const rule = this.add(rawRule.id, rawRule, options?.cacheAfterFetching); + const rule = this._add(rawRule.id, rawRule, options?.cacheAfterFetching); fetchedRules.set(rule.id, rule); } return fetchedRules; diff --git a/src/managers/ListManager.ts b/src/managers/ListManager.ts index 21ad7f0..b30c2c9 100644 --- a/src/managers/ListManager.ts +++ b/src/managers/ListManager.ts @@ -46,7 +46,7 @@ export class ListManager extends BaseManager { }; const requestData = new RequestData({ body, isUserContext: true }); const res: PostListCreateResponse = await this.client._api.lists.post(requestData); - const list = this.add(res.data.id, res.data); + const list = this._add(res.data.id, res.data); return list; } diff --git a/src/managers/SpaceManager.ts b/src/managers/SpaceManager.ts index 21fde47..b6f828b 100644 --- a/src/managers/SpaceManager.ts +++ b/src/managers/SpaceManager.ts @@ -83,7 +83,7 @@ export class SpaceManager extends BaseManager const rawSpaces = data.data; const rawSpacesIncludes = data.includes; for (const rawSpace of rawSpaces) { - const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); + const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); fetchedSpaceCollection.set(space.id, space); } return fetchedSpaceCollection; @@ -111,7 +111,7 @@ export class SpaceManager extends BaseManager const rawSpaces = data.data; const rawSpacesIncludes = data.includes; for (const rawSpace of rawSpaces) { - const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); + const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); fetchedSpaceCollection.set(space.id, space); } return fetchedSpaceCollection; @@ -132,7 +132,7 @@ export class SpaceManager extends BaseManager }; const requestData = new RequestData({ query }); const data: GetSingleSpaceByIdResponse = await this.client._api.spaces(spaceId).get(requestData); - return this.add(data.data.id, data, options.cacheAfterFetching); + return this._add(data.data.id, data, options.cacheAfterFetching); } async #fetchMultipleSpaces( @@ -152,7 +152,7 @@ export class SpaceManager extends BaseManager const rawSpaces = data.data; const rawSpacesIncludes = data.includes; for (const rawSpace of rawSpaces) { - const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); + const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching); fetchedSpaceCollection.set(space.id, space); } return fetchedSpaceCollection; diff --git a/src/managers/TweetManager.ts b/src/managers/TweetManager.ts index bcbdf1d..9f069a1 100644 --- a/src/managers/TweetManager.ts +++ b/src/managers/TweetManager.ts @@ -333,7 +333,7 @@ export class TweetManager extends BaseManager }; const requestData = new RequestData({ query }); const data: GetSingleTweetByIdResponse = await this.client._api.tweets(tweetId).get(requestData); - return this.add(data.data.id, data, options.cacheAfterFetching); + return this._add(data.data.id, data, options.cacheAfterFetching); } async #fetchMultipleTweets( @@ -356,7 +356,7 @@ export class TweetManager extends BaseManager const rawTweets = data.data; const rawTweetsIncludes = data.includes; for (const rawTweet of rawTweets) { - const tweet = this.add(rawTweet.id, { data: rawTweet, includes: rawTweetsIncludes }, options.cacheAfterFetching); + const tweet = this._add(rawTweet.id, { data: rawTweet, includes: rawTweetsIncludes }, options.cacheAfterFetching); fetchedTweetCollection.set(tweet.id, tweet); } return fetchedTweetCollection; diff --git a/src/managers/UserManager.ts b/src/managers/UserManager.ts index 7797074..3cb87db 100644 --- a/src/managers/UserManager.ts +++ b/src/managers/UserManager.ts @@ -404,7 +404,7 @@ export class UserManager extends BaseManager { const rawUsers = data.data; const rawUsersIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching); + const user = this._add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching); fetchedUserCollection.set(user.id, user); } return fetchedUserCollection; @@ -443,7 +443,7 @@ export class UserManager extends BaseManager { const rawUsers = data.data; const rawUsersIncludes = data.includes; for (const rawUser of rawUsers) { - const user = this.add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching); + const user = this._add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching); fetchedUserCollection.set(user.id, user); } return fetchedUserCollection; diff --git a/src/managers/__tests__/BaseManager.test.ts b/src/managers/__tests__/BaseManager.test.ts index 1f12b2a..5ad50b7 100644 --- a/src/managers/__tests__/BaseManager.test.ts +++ b/src/managers/__tests__/BaseManager.test.ts @@ -7,11 +7,11 @@ const id: Snowflake = '1234567890'; const rawData = { id }; const client = new Client(); const baseManager = new BaseManager(client, BaseStructure); -const baseStructure = baseManager.add(id, rawData); +const baseStructure = baseManager._add(id, rawData); -// BaseManager#add +// BaseManager#_add test('Add base structure to cache', () => { - expect(baseManager.add(id, rawData)).toBeInstanceOf(BaseStructure); + expect(baseManager._add(id, rawData)).toBeInstanceOf(BaseStructure); }); // BaseManager#resolveId