Skip to content

Commit

Permalink
Merge pull request #20 from Keyrxng/config
Browse files Browse the repository at this point in the history
feat: webhook type
  • Loading branch information
whilefoo authored Feb 14, 2024
2 parents 10887c3 + 5f0e142 commit ea1b1d7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@
]
},
"packageManager": "[email protected]"
}
}
4 changes: 2 additions & 2 deletions src/github/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type as T } from "@sinclair/typebox";
import { StaticDecode } from "@sinclair/typebox";
import { GitHubEvent } from "./github-events";
import { githubWebhookEvents } from "./webhook-events";

enum Commands {
Start = "start",
Expand All @@ -24,7 +24,7 @@ export const configSchema = T.Object({
handlers: T.Object(
{
commands: T.Record(T.Enum(Commands), handlerSchema, { default: {} }),
events: T.Record(T.Enum(GitHubEvent), handlerSchema, { default: {} }),
events: T.Record(T.Enum(githubWebhookEvents), handlerSchema, { default: {} }),
},
{ default: {} }
),
Expand Down
24 changes: 22 additions & 2 deletions src/github/types/webhook-events.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
import { emitterEventNames } from "@octokit/webhooks/dist-types/generated/webhook-names.js";
export type GitHubEventClassName = (typeof emitterEventNames)[number];
import { emitterEventNames, EmitterWebhookEventName as GitHubEventClassName } from "@octokit/webhooks";

type Formatted<T extends string> = T extends `${infer Prefix}.${infer Rest}` ? `${Prefix}_${Formatted<Rest>}` : T;

type GithubEventWebHookEvents = {
[K in GitHubEventClassName as Formatted<Uppercase<K>>]: K;
};

type Prettify<T> = {
[K in keyof T]: T[K];
// this just spreads the object into a type

// we need to use {} otherwise it'll type it as an object
// eslint-disable-next-line @typescript-eslint/ban-types
} & {};

export const githubWebhookEvents: Prettify<GithubEventWebHookEvents> = emitterEventNames.reduce((acc: GithubEventWebHookEvents, cur) => {
const formatted = cur.replace(/\./g, "_");
const upper = formatted.toUpperCase() as Formatted<Uppercase<GitHubEventClassName>>;
acc[upper] = cur as Extract<GitHubEventClassName, Uppercase<GitHubEventClassName>>;
return acc;
}, {} as GithubEventWebHookEvents);

0 comments on commit ea1b1d7

Please sign in to comment.