Skip to content

Commit

Permalink
feat: export WebhooksEvents from @octokit/webhooks.js (#321)
Browse files Browse the repository at this point in the history
* fix: typscript import Events from @octokit/webhooks.js

* Install npm dependencies, just in case

* Rename this to WebhooksEvents as in future we will have Events which have all Github events

* Fix lint

* Rename to WebhooksEvents
  • Loading branch information
ankeetmaini committed Oct 13, 2020
1 parent 716ee63 commit 2323461
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion scripts/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface EventTypesPayload {
${eventPayloadMapping.map(([name, type]) => `"${name}": ${type}`).join(`,\n`)}
}
export type All = keyof EventTypesPayload
export type WebhookEvents = keyof EventTypesPayload
`;

generateFile(
Expand Down
4 changes: 2 additions & 2 deletions src/event-handler/on.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { All } from "../generated/get-webhook-payload-type-from-event";
import { WebhookEvents } from "../generated/get-webhook-payload-type-from-event";
import { webhookNames } from "../generated/webhook-names";
import { State } from "../types";

export function receiverOn(
state: State,
webhookNameOrNames: All | All[],
webhookNameOrNames: WebhookEvents | WebhookEvents[],
handler: Function
) {
if (Array.isArray(webhookNameOrNames)) {
Expand Down
4 changes: 2 additions & 2 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
WebhookError,
WebhookEventHandlerError,
} from "../types";
import { All } from "../generated/get-webhook-payload-type-from-event";
import { WebhookEvents } from "../generated/get-webhook-payload-type-from-event";

function getHooks(
state: State,
eventPayloadAction: string,
eventName: All
eventName: WebhookEvents
): Function[] {
const hooks = [state.hooks[`${eventName}.${eventPayloadAction}`]];

Expand Down
2 changes: 1 addition & 1 deletion src/generated/get-webhook-payload-type-from-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,4 @@ export interface EventTypesPayload {
>;
}

export type All = keyof EventTypesPayload;
export type WebhookEvents = keyof EventTypesPayload;
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import {
HandlerFunction,
} from "./types";
import { IncomingMessage, ServerResponse } from "http";
import { All } from "./generated/get-webhook-payload-type-from-event";
import { WebhookEvents } from "./generated/get-webhook-payload-type-from-event";

// U holds the return value of `transform` function in Options
class Webhooks<T extends WebhookEvent = WebhookEvent, U = {}> {
public sign: (payload: string | object) => string;
public verify: (eventPayload?: object, signature?: string) => boolean;
public on: <E extends All>(
public on: <E extends WebhookEvents>(
event: E | E[],
callback: HandlerFunction<E, U>
) => void;
public removeListener: <E extends All>(
public removeListener: <E extends WebhookEvents>(
event: E | E[],
callback: HandlerFunction<E, U>
) => void;
Expand Down Expand Up @@ -65,6 +65,7 @@ class Webhooks<T extends WebhookEvent = WebhookEvent, U = {}> {
const createWebhooksApi = Webhooks.prototype.constructor;

export { EventPayloads } from "./generated/event-payloads";
export { WebhookEvents } from "./generated/get-webhook-payload-type-from-event";

export {
createEventHandler,
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { verifyAndReceive } from "./verify-and-receive";
import { debug } from "debug";
import { IncomingMessage, ServerResponse } from "http";
import { State, WebhookEventHandlerError } from "../types";
import { All } from "../generated/get-webhook-payload-type-from-event";
import { WebhookEvents } from "../generated/get-webhook-payload-type-from-event";

const debugWebhooks = debug("webhooks:receiver");

Expand Down Expand Up @@ -41,7 +41,7 @@ export function middleware(
});
}

const eventName = request.headers["x-github-event"] as All;
const eventName = request.headers["x-github-event"] as WebhookEvents;
const signature = request.headers["x-hub-signature"] as string;
const id = request.headers["x-github-delivery"] as string;

Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { RequestError } from "@octokit/request-error";
import {
All,
WebhookEvents,
EventTypesPayload,
} from "./generated/get-webhook-payload-type-from-event";

export interface WebhookEvent<T = any> {
id: string;
name: All;
name: WebhookEvents;
payload: T;
}

Expand All @@ -20,7 +20,7 @@ type TransformMethod<T extends WebhookEvent, V = T> = (
event: WebhookEvent
) => V | PromiseLike<V>;

export type HandlerFunction<E extends All, U> = (
export type HandlerFunction<E extends WebhookEvents, U> = (
event: EventTypesPayload[E] & U
) => any;

Expand Down
4 changes: 2 additions & 2 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
EventPayloads,
WebhookEvent,
WebhookError,
WebhookEvents,
} from "../src/index";
import { createServer } from "http";
import { All } from "../src/generated/get-webhook-payload-type-from-event";

// ************************************************************
// THIS CODE IS NOT EXECUTED. IT IS FOR TYPECHECKING ONLY
Expand All @@ -24,7 +24,7 @@ const myWebhook: WebhookEvent<{ foo: string }> = {
},
};

const myEventName: All = "check_run.completed";
const myEventName: WebhookEvents = "check_run.completed";

const myEvenTPayload: EventPayloads.WebhookPayloadCheckRunCheckRunOutput = {
annotations_count: 0,
Expand Down

0 comments on commit 2323461

Please sign in to comment.