From 90bf4d9d948267e0a9935b3a3faf6b1cc6dd0cd9 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Mon, 17 Jun 2024 21:28:03 -0700 Subject: [PATCH] feat: update SocialDataClient return types --- legacy/src/services/social-data-client.ts | 156 +++++++++++++--------- 1 file changed, 94 insertions(+), 62 deletions(-) diff --git a/legacy/src/services/social-data-client.ts b/legacy/src/services/social-data-client.ts index a30b2191..0dd40ec5 100644 --- a/legacy/src/services/social-data-client.ts +++ b/legacy/src/services/social-data-client.ts @@ -205,11 +205,13 @@ export class SocialDataClient extends AIFunctionsProvider { async getTweetById(idOrOpts: string | socialdata.GetTweetByIdOptions) { const options = typeof idOrOpts === 'string' ? { id: idOrOpts } : idOrOpts - return this.ky - .get('twitter/statuses/show', { - searchParams: sanitizeSearchParams(options) - }) - .json() + return this._handleResponse( + this.ky + .get('twitter/statuses/show', { + searchParams: sanitizeSearchParams(options) + }) + .json() + ) } /** @@ -221,11 +223,13 @@ export class SocialDataClient extends AIFunctionsProvider { const { tweetId, ...params } = typeof idOrOpts === 'string' ? { tweetId: idOrOpts } : idOrOpts - return this.ky - .get(`twitter/tweets/${tweetId}/liking_users`, { - searchParams: sanitizeSearchParams(params) - }) - .json() + return this._handleResponse( + this.ky + .get(`twitter/tweets/${tweetId}/liking_users`, { + searchParams: sanitizeSearchParams(params) + }) + .json() + ) } /** @@ -237,11 +241,13 @@ export class SocialDataClient extends AIFunctionsProvider { const { tweetId, ...params } = typeof idOrOpts === 'string' ? { tweetId: idOrOpts } : idOrOpts - return this.ky - .get(`twitter/tweets/${tweetId}/retweeted_by`, { - searchParams: sanitizeSearchParams(params) - }) - .json() + return this._handleResponse( + this.ky + .get(`twitter/tweets/${tweetId}/retweeted_by`, { + searchParams: sanitizeSearchParams(params) + }) + .json() + ) } /** @@ -255,14 +261,16 @@ export class SocialDataClient extends AIFunctionsProvider { const options = typeof queryOrOpts === 'string' ? { query: queryOrOpts } : queryOrOpts - return this.ky - .get('twitter/search', { - searchParams: sanitizeSearchParams({ - type: 'top', - ...options + return this._handleResponse( + this.ky + .get('twitter/search', { + searchParams: sanitizeSearchParams({ + type: 'top', + ...options + }) }) - }) - .json() + .json() + ) } /** @@ -286,9 +294,9 @@ export class SocialDataClient extends AIFunctionsProvider { ? { username: usernameOrOptions } : usernameOrOptions - return this.ky - .get(`twitter/user/${username}`) - .json() + return this._handleResponse( + this.ky.get(`twitter/user/${username}`).json() + ) } /** @@ -306,14 +314,16 @@ export class SocialDataClient extends AIFunctionsProvider { ...params } = typeof idOrOpts === 'string' ? { userId: idOrOpts } : idOrOpts - return this.ky - .get( - `twitter/user/${userId}/${replies ? 'tweets-and-replies' : 'tweets'}`, - { - searchParams: sanitizeSearchParams(params) - } - ) - .json() + return this._handleResponse( + this.ky + .get( + `twitter/user/${userId}/${replies ? 'tweets-and-replies' : 'tweets'}`, + { + searchParams: sanitizeSearchParams(params) + } + ) + .json() + ) } /** @@ -327,11 +337,13 @@ export class SocialDataClient extends AIFunctionsProvider { const { userId, ...params } = typeof idOrOpts === 'string' ? { userId: idOrOpts } : idOrOpts - return this.ky - .get(`twitter/user/${userId}/likes`, { - searchParams: sanitizeSearchParams(params) - }) - .json() + return this._handleResponse( + this.ky + .get(`twitter/user/${userId}/likes`, { + searchParams: sanitizeSearchParams(params) + }) + .json() + ) } /** @@ -343,14 +355,16 @@ export class SocialDataClient extends AIFunctionsProvider { const { userId: user_id, ...params } = typeof idOrOpts === 'string' ? { userId: idOrOpts } : idOrOpts - return this.ky - .get('twitter/followers/list', { - searchParams: sanitizeSearchParams({ - user_id, - ...params + return this._handleResponse( + this.ky + .get('twitter/followers/list', { + searchParams: sanitizeSearchParams({ + user_id, + ...params + }) }) - }) - .json() + .json() + ) } /** @@ -362,14 +376,16 @@ export class SocialDataClient extends AIFunctionsProvider { const { userId: user_id, ...params } = typeof idOrOpts === 'string' ? { userId: idOrOpts } : idOrOpts - return this.ky - .get('twitter/friends/list', { - searchParams: sanitizeSearchParams({ - user_id, - ...params + return this._handleResponse( + this.ky + .get('twitter/friends/list', { + searchParams: sanitizeSearchParams({ + user_id, + ...params + }) }) - }) - .json() + .json() + ) } /** @@ -381,11 +397,13 @@ export class SocialDataClient extends AIFunctionsProvider { async isUserFollowingUser(opts: socialdata.UserFollowingOptions) { const { sourceUserId, targetUserId, ...params } = opts - return this.ky - .get(`twitter/user/${sourceUserId}/following/${targetUserId}`, { - searchParams: sanitizeSearchParams(params) - }) - .json() + return this._handleResponse( + this.ky + .get(`twitter/user/${sourceUserId}/following/${targetUserId}`, { + searchParams: sanitizeSearchParams(params) + }) + .json() + ) } /** @@ -397,10 +415,24 @@ export class SocialDataClient extends AIFunctionsProvider { const params = typeof queryOrOpts === 'string' ? { query: queryOrOpts } : queryOrOpts - return this.ky - .get('twitter/search-users', { - searchParams: sanitizeSearchParams(params) - }) - .json() + return this._handleResponse( + this.ky + .get('twitter/search-users', { + searchParams: sanitizeSearchParams(params) + }) + .json() + ) + } + + protected async _handleResponse( + resP: Promise + ): Promise> { + const res = await resP + + if ((res as socialdata.ErrorResponse).status === 'error') { + throw new Error((res as socialdata.ErrorResponse).message) + } + + return res as unknown as Exclude } }