-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: add TwitterBaseAPI.User * chore: add getUserByScreenNameShow * refactor: code style * refactor: cache duration * refactor: stale * refactor: adjust sequence
- Loading branch information
1 parent
2de662b
commit 77c920f
Showing
14 changed files
with
233 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/web3-providers/src/Twitter/apis/getUserByScreenNameShow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import urlcat from 'urlcat' | ||
import { getHeaders } from './getTokens.js' | ||
import { Duration, staleCached } from '../../helpers/fetchCached.js' | ||
import { Expiration } from '../../helpers/fetchSquashed.js' | ||
import { fetchCachedJSON } from '../../helpers/fetchJSON.js' | ||
import type { TwitterBaseAPI } from '../../entry-types.js' | ||
|
||
function createUser(response: TwitterBaseAPI.ShowResult): TwitterBaseAPI.User { | ||
return { | ||
verified: response.verified, | ||
has_nft_avatar: false, | ||
userId: response.id_str, | ||
nickname: response.name, | ||
screenName: response.screen_name, | ||
avatarURL: response.profile_image_url_https.replace(/_normal(\.\w+)$/, '_400x400$1'), | ||
bio: response.description, | ||
location: response.location, | ||
homepage: response.entities?.url?.urls[0]?.expanded_url, | ||
} | ||
} | ||
|
||
async function createRequest(screenName: string) { | ||
const url = urlcat('https://api.twitter.com/1.1/users/show.json', { | ||
screen_name: screenName, | ||
}) | ||
return new Request(url, { | ||
headers: await getHeaders(), | ||
credentials: 'include', | ||
}) | ||
} | ||
|
||
export async function getUserByScreenNameShow(screenName: string): Promise<TwitterBaseAPI.User | null> { | ||
const request = await createRequest(screenName) | ||
const response = await fetchCachedJSON<TwitterBaseAPI.ShowResult>(request, undefined, { | ||
cacheDuration: Duration.ONE_DAY, | ||
squashExpiration: Expiration.ONE_SECOND, | ||
}) | ||
return createUser(response) | ||
} | ||
|
||
export async function staleUserByScreenNameShow(screenName: string): Promise<TwitterBaseAPI.User | null> { | ||
const request = await createRequest(screenName) | ||
if (!request) return null | ||
|
||
const response = await staleCached(request) | ||
if (!response?.ok) return null | ||
|
||
const json: TwitterBaseAPI.ShowResult = await response.json() | ||
return createUser(json) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.