-
Notifications
You must be signed in to change notification settings - Fork 0
Add make twitter authenticated requests #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7488826
d86f11b
a814e17
06c3174
51ba0cf
d28d7e1
f631e66
50a0636
6664b15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@everipedia/iq-utils": minor | ||
| --- | ||
|
|
||
| Add sendTwitterApiRequest lib function |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ coverage | |
|
|
||
| # API keys and secrets | ||
| .env | ||
| .env.local | ||
|
|
||
| # Dependency directory | ||
| node_modules | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pnpm lint-staged |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from "./lib/wiki-score"; | ||
| export * from "./lib/check-deep-equal"; | ||
| export * from "./lib/check-wiki-validity"; | ||
| export * from "./lib/twitter-api-client"; | ||
| export * from "./data/constants"; | ||
| export * from "./schema"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import crypto from "node:crypto"; | ||
| import OAuth from "oauth-1.0a"; | ||
|
|
||
| type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; | ||
|
|
||
| interface IDiscordLogger { | ||
| log(data: DiscordLogData): Promise<void>; | ||
| } | ||
|
|
||
| type DiscordLogData = { | ||
| message: string; | ||
| title?: string; | ||
| color?: number; | ||
| }; | ||
|
|
||
| export const oauth = new OAuth({ | ||
| consumer: { | ||
| key: process.env.TWITTER_CONSUMER_KEY ?? "", | ||
| secret: process.env.TWITTER_CONSUMER_SECRET ?? "", | ||
| }, | ||
| signature_method: "HMAC-SHA1", | ||
| hash_function: (base_string, key) => { | ||
| return crypto.createHmac("sha1", key).update(base_string).digest("base64"); | ||
| }, | ||
| }); | ||
|
|
||
| const twitterAuthConfig = { | ||
| key: process.env.TWITTER_ACCESS_TOKEN ?? "", | ||
| secret: process.env.TWITTER_ACCESS_TOKEN_SECRET ?? "", | ||
| }; | ||
|
|
||
| export async function sendTwitterApiRequest( | ||
| url: string, | ||
| method: HttpMethod, | ||
| logger?: IDiscordLogger, | ||
| body?: string, | ||
| ): Promise<Response> { | ||
| if (!url.startsWith("https://api.twitter.com/")) { | ||
| throw new Error("Invalid Twitter API URL"); | ||
| } | ||
| const oauth_headers = oauth.toHeader( | ||
| oauth.authorize( | ||
| { | ||
| url, | ||
| method, | ||
| }, | ||
| twitterAuthConfig, | ||
| ), | ||
| ); | ||
|
|
||
| const response = await fetch(url, { | ||
| method, | ||
| headers: { | ||
| ...oauth_headers, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorBody = await response.text(); | ||
| logger?.log({ | ||
| message: `🚨 HTTP error! status: ${response.status} ${response.statusText}. Body: ${errorBody}`, | ||
| title: "Twitter Authentication: makeAuthenticatedRequest", | ||
| }); | ||
| throw new Error( | ||
| `HTTP error! status: ${response.status} ${response.statusText}. Body: ${errorBody}`, | ||
| ); | ||
| } | ||
|
|
||
| return response; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |
| MediaType, | ||
| type MetaData, | ||
| Tag, | ||
| Wiki, | ||
| } from "../schema"; | ||
|
|
||
| // =============================== | ||
|
|
@@ -103,12 +104,15 @@ export function isMediaContentAndCountValid(media: Media[]): boolean { | |
| // =============================== | ||
| // Wiki-specific validation helpers | ||
| // =============================== | ||
| export function isEventWikiValid(wiki: any): boolean { | ||
| if (wiki.tags.some((tag: any) => tag.id === "Events")) { | ||
| export function isEventWikiValid(wiki: { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why these changes? Are they related to this PR?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got a failed build due to any usage
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird |
||
| tags: { id: string }[]; | ||
| metadata: { id: string; value: string }[]; | ||
| events: unknown[]; | ||
| }): boolean { | ||
| if (wiki.tags.some((tag) => tag.id === "Events")) { | ||
| const referencesData = | ||
| wiki.metadata.find( | ||
| (meta: any) => meta.id === CommonMetaIds.Enum.references, | ||
| )?.value || "[]"; | ||
| wiki.metadata.find((meta) => meta.id === CommonMetaIds.Enum.references) | ||
| ?.value || "[]"; | ||
| const references: { description: string }[] = JSON.parse( | ||
| referencesData, | ||
| ) as { description: string }[]; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we introducing husky?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was getting a failed build because it was not catching some errors here. So I setup a pre-commit hook to help prevent future occurrences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could add in another PR. 👍 Keeps the PR lean