Skip to content

Commit

Permalink
Tidy up types
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jan 2, 2024
1 parent 19db415 commit 93c7bc7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ interface GitHubRequestData {
signature: string;
}

interface WebhooksExpressRequest extends Request {
github?: GitHubRequestData;
}

export class Webhooks extends EventEmitter {

public readonly expressRouter = Router();
Expand Down Expand Up @@ -152,7 +156,7 @@ export class Webhooks extends EventEmitter {
}
}

private onPayload(req: Request, res: Response) {
private onPayload(req: WebhooksExpressRequest, res: Response) {
try {
let eventName: string|null = null;
const body = req.body;
Expand All @@ -168,7 +172,10 @@ export class Webhooks extends EventEmitter {
return;
}
this.handledGuids.set(githubGuid);
const githubData = (req as any).github as GitHubRequestData;
const githubData = req.github as GitHubRequestData;
if (!githubData) {
throw Error('Expected github data to be set on request');
}
this.ghWebhooks.verifyAndReceive({
id: githubGuid as string,
name: req.headers["x-github-event"] as EmitterWebhookEventName,
Expand Down Expand Up @@ -292,7 +299,7 @@ export class Webhooks extends EventEmitter {
}
}

private verifyRequest(req: Request, res: Response, buffer: Buffer, encoding: BufferEncoding) {
private verifyRequest(req: WebhooksExpressRequest, res: Response, buffer: Buffer, encoding: BufferEncoding) {
if (req.headers['x-gitlab-token']) {
// GitLab
if (!this.config.gitlab) {
Expand All @@ -319,10 +326,10 @@ export class Webhooks extends EventEmitter {
} catch (ex) {
throw new ApiError("Could not decode buffer", ErrCode.BadValue, 400);
}
(req as any).github = {
req.github = {
payload: jsonStr,
signature: req.headers["x-hub-signature-256"]
} satisfies GitHubRequestData;
};
return true;
} else if (JiraWebhooksRouter.IsJIRARequest(req)) {
// JIRA
Expand Down

0 comments on commit 93c7bc7

Please sign in to comment.