From 79fcec23aad842f9e5107cf136801c6849c4e46c Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Sat, 26 Jan 2019 13:30:48 -0500 Subject: [PATCH] feat(typescript): definitions for .verify, .verifyAndReceive, .receive, .removeListener, .middleware methods (#55) --- index.d.ts | 37 +++++++++++++++++++++++++++++++++++-- package.json | 2 +- scripts/generate-types.js | 11 ++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index defc04f4..2ad8e650 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,14 @@ // DO NOT EDIT THIS FILE DIRECTLY // make edits in scripts/generate-types.js +import http = require("http"); + export type Options = { secret: string; path?: string; - transform?: (event: Webhooks.WebhookEvent) => Webhooks.WebhookEvent & { [key: string]: any }; + transform?: ( + event: Webhooks.WebhookEvent + ) => Webhooks.WebhookEvent & { [key: string]: any }; }; export type WebhookPayloadWatchSender = { @@ -3477,9 +3481,13 @@ export namespace Webhooks { public on(event: "error", callback: (event: Error) => void): void; public on( - event: "*" | string[], + event: "*" | string | string[], callback: (event: Webhooks.WebhookEvent) => void ): void; + public on( + event: "*" | string | string[], + callback: (event: Webhooks.WebhookEvent) => Promise + ): void; public on( event: @@ -3812,5 +3820,30 @@ export namespace Webhooks { ): void; public sign(data: any): string; + public verify(eventPayload: any, signature: string): boolean; + public verifyAndReceive(options: { + id: string; + name: string; + payload: any; + signature: string; + }): Promise; + public receive(options: { + id: string; + name: string; + payload: any; + }): Promise; + public removeListener( + event: string | string[], + callback: (event: Webhooks.WebhookEvent) => void + ): void; + public removeListener( + event: string | string[], + callback: (event: Webhooks.WebhookEvent) => Promise + ): void; + public middleware( + request: http.ClientRequest, + response: http.ServerResponse, + next: (err?: any) => void + ): (request: http.IncomingMessage, response: http.ServerResponse) => void; } } diff --git a/package.json b/package.json index 55aecb33..5c4bf3ff 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "pretest": "standard", "test": "tap --100 --coverage 'test/**/*-test.js'", "semantic-release": "semantic-release", - "generate-types": "node generate-types.js" + "generate-types": "node scripts/generate-types.js" }, "repository": { "type": "git", diff --git a/scripts/generate-types.js b/scripts/generate-types.js index a434105d..73d9f0d3 100644 --- a/scripts/generate-types.js +++ b/scripts/generate-types.js @@ -32,6 +32,8 @@ const definition = ` // DO NOT EDIT THIS FILE DIRECTLY // make edits in scripts/generate-types.js +import http = require('http') + export type Options = { secret: string path?: string @@ -53,10 +55,17 @@ export namespace Webhooks { constructor (options: Options) public on (event: 'error', callback: (event: Error) => void): void - public on (event: '*' | string[], callback: (event: Webhooks.WebhookEvent) => void): void + public on (event: '*' | string | string[], callback: (event: Webhooks.WebhookEvent) => void): void + public on (event: '*' | string | string[], callback: (event: Webhooks.WebhookEvent) => Promise): void ${signatures.join('\n')} public sign (data: any): string + public verify (eventPayload: any, signature: string): boolean + public verifyAndReceive (options: { id: string, name: string, payload: any, signature: string }): Promise + public receive (options: { id: string, name: string, payload: any }): Promise + public removeListener (event: string | string[], callback: (event: Webhooks.WebhookEvent) => void): void + public removeListener (event: string | string[], callback: (event: Webhooks.WebhookEvent) => Promise): void + public middleware (request: http.ClientRequest, response: http.ServerResponse, next: (err?: any) => void): (request: http.IncomingMessage, response: http.ServerResponse) => void } } `