diff --git a/package.json b/package.json index ebf3840..b1a64b4 100644 --- a/package.json +++ b/package.json @@ -78,4 +78,4 @@ ] }, "packageManager": "bun@1.0.23" -} +} \ No newline at end of file diff --git a/src/github/types/config.ts b/src/github/types/config.ts index 87ad131..9176d12 100644 --- a/src/github/types/config.ts +++ b/src/github/types/config.ts @@ -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", @@ -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: {} } ), diff --git a/src/github/types/webhook-events.ts b/src/github/types/webhook-events.ts index 51e00fa..1570712 100644 --- a/src/github/types/webhook-events.ts +++ b/src/github/types/webhook-events.ts @@ -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 `${infer Prefix}.${infer Rest}` ? `${Prefix}_${Formatted}` : T; + +type GithubEventWebHookEvents = { + [K in GitHubEventClassName as Formatted>]: K; +}; + +type Prettify = { + [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 = emitterEventNames.reduce((acc: GithubEventWebHookEvents, cur) => { + const formatted = cur.replace(/\./g, "_"); + const upper = formatted.toUpperCase() as Formatted>; + acc[upper] = cur as Extract>; + return acc; +}, {} as GithubEventWebHookEvents);