-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
The GitHub Feed types are not type safe, caused by broken Octokit types.
dsns.dev/src/components/GithubFeed.astro
Lines 94 to 102 in e18d09a
| if (type === "PullRequestEvent" && (payload as any).action === "closed" && pr.merged) { | |
| verb = "merged"; | |
| } else if (type === "PullRequestReviewEvent") { | |
| const state = (payload as any).review.state?.toLowerCase(); | |
| if (state === "commented") continue; | |
| verb = state.replace("_", " "); | |
| } else if (type === "PullRequestEvent" && allowedActions.includes((payload as any).action)) { | |
| verb = (payload as any).action; | |
| } else continue; |
I will have to fork it and update the types myself, since apparently there is no maintainer for the official Octokit JS repositories anymore.
octokit/openapi-types.ts#232 (comment)
octokit/rest.js#128
My Fix
Use my fork, add this to package.json1:
"overrides": {
"@octokit/openapi-types": "github:dsnsgithub/openapi-types.ts#main"
},Use this hack to typeguard:
export type EventPayloadMap = {
CreateEvent: components["schemas"]["create-event"];
DeleteEvent: components["schemas"]["delete-event"];
DiscussionEvent: components["schemas"]["discussion-event"];
IssuesEvent: components["schemas"]["issues-event"];
IssueCommentEvent: components["schemas"]["issue-comment-event"];
ForkEvent: components["schemas"]["fork-event"];
GollumEvent: components["schemas"]["gollum-event"];
MemberEvent: components["schemas"]["member-event"];
PublicEvent: components["schemas"]["public-event"];
PushEvent: components["schemas"]["push-event"];
PullRequestEvent: components["schemas"]["pull-request-event"];
PullRequestReviewCommentEvent: components["schemas"]["pull-request-review-comment-event"];
PullRequestReviewEvent: components["schemas"]["pull-request-review-event"];
CommitCommentEvent: components["schemas"]["commit-comment-event"];
ReleaseEvent: components["schemas"]["release-event"];
WatchEvent: components["schemas"]["watch-event"];
};
function isEvent<T extends keyof EventPayloadMap>(
event: components["schemas"]["event"],
type: T
): event is components["schemas"]["event"] & { type: T; payload: EventPayloadMap[T] } {
return event.type === type;
}Example Usage:
import { Octokit } from "@octokit/rest";
import type { components } from "@octokit/openapi-types";
...
const res = await octokit.rest.activity.listPublicEventsForUser({
username: "dsnsgithub"
})
for (const event of res.data) {
if (isEvent(event, "PullRequestEvent")) {
// event is now PullRequestEvent and is typeguarded.
} else if (isEvent(event, "IssuesEvent")) {
// event is now IssuesEvent and is typeguarded.
}
}Footnotes
-
You may have to delete
node_modules/package-lock.jsonand reset Vercel build cache for the override to take effect. ↩
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels