Skip to content

Commit

Permalink
refactor(typescript): remove need for webhook payload type map
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Feb 7, 2021
1 parent 560ff73 commit 0069a1e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 507 deletions.
69 changes: 12 additions & 57 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ interface Schema extends JSONSchema7 {

const schema = require("@octokit/webhooks-definitions/schema.json") as Schema;

const titleCase = (str: string) => `${str[0].toUpperCase()}${str.substring(1)}`;

const guessAtInterfaceName = (str: string) =>
str.split(/[$_-]/u).map(titleCase).join("");

const guessAtEventName = (name: string) => {
const [, eventName] = /^(.+)[$_-]event/u.exec(name) ?? [];

Expand All @@ -42,33 +37,14 @@ const getDefinitionName = (ref: string): string => {
};

type NameAndActions = [name: string, actions: string[]];
type Property = [key: string, value: string];
type ImportsAndProperties = [imports: string[], properties: Property[]];

const buildEventProperties = ([
eventName,
actions,
]: NameAndActions): ImportsAndProperties => {
const interfaceName = guessAtInterfaceName(eventName);
const importsAndProperties: ImportsAndProperties = [
[interfaceName],
[[guessAtEventName(eventName), interfaceName]],
];

if (actions.length) {
actions.forEach((actionName) => {
const actionInterfaceName = guessAtInterfaceName(`${actionName}_event`);

importsAndProperties[0].push(actionInterfaceName);
importsAndProperties[1].push([
guessAtActionName(actionName),
actionInterfaceName,
]);
});
}

return importsAndProperties;
};
]: NameAndActions): string[] => [
guessAtEventName(eventName),
...actions.map(guessAtActionName),
];

const isJSONSchemaWithRef = (
object: JSONSchema7Definition
Expand All @@ -90,17 +66,10 @@ const listEvents = () => {
});
};

const getImportsAndProperties = (): ImportsAndProperties => {
const importsAndProperties = listEvents().map(buildEventProperties);

return importsAndProperties.reduce<ImportsAndProperties>(
(allImportsAndProperties, [imports, properties]) => {
return [
allImportsAndProperties[0].concat(imports),
allImportsAndProperties[1].concat(properties),
];
},
[[], []]
const getEmitterEvents = (): string[] => {
return listEvents().reduce<string[]>(
(properties, event) => properties.concat(buildEventProperties(event)),
[]
);
};

Expand Down Expand Up @@ -173,32 +142,18 @@ const updateReadme = (properties: string[]) => {
};

const run = () => {
const [imports, properties] = getImportsAndProperties();

const lines: string[] = [
"// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY",
"// make edits in scripts/generate-types.ts",
"",
"import {",
...imports.map((str) => ` ${str},`),
'} from "@octokit/webhooks-definitions/schema";',
"",
"export interface EmitterEventWebhookPayloadMap {",
...properties.map(([key, value]) => `"${key}": ${value}`),
"}",
];
const emitterEvents = getEmitterEvents();

generateTypeScriptFile("get-webhook-payload-type-from-event", lines);
generateTypeScriptFile("webhook-names", [
"// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY",
"// make edits in scripts/generate-types.ts",
"",
"export const emitterEventNames = [",
...properties.map(([key]) => `"${key}",`),
"];",
...emitterEvents.map((key) => `"${key}",`),
"] as const;",
]);

updateReadme(properties.map(([key]) => key));
updateReadme(emitterEvents);
};

run();
3 changes: 1 addition & 2 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-ignore to address #245
import AggregateError from "aggregate-error";
import { EmitterEventWebhookPayloadMap } from "../generated/get-webhook-payload-type-from-event";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
Expand All @@ -11,7 +10,7 @@ import type {
import { wrapErrorHandler } from "./wrap-error-handler";

type EventAction = Extract<
EmitterEventWebhookPayloadMap[keyof EmitterEventWebhookPayloadMap],
EmitterWebhookEvent["payload"],
{ action: string }
>["action"];

Expand Down
Loading

0 comments on commit 0069a1e

Please sign in to comment.