Repository: octokit/webhooks.js
Pull request #373 (2020-12-04 22:19:30)

fix(typescript): mark `verify` parameters as required
The parameters for `verify` are not optional - this matches the pattern used for `sign`.


Patch:
diff --git a/package.json b/package.json
index 6c1ea0b..ab5753d 100644
--- a/package.json
+++ b/package.json
@@ -81,7 +81,7 @@
     },
     "globals": {
       "ts-jest": {
-        "tsConfig": {
+        "tsconfig": {
           "esModuleInterop": true
         }
       }
diff --git a/src/event-handler/receive.ts b/src/event-handler/receive.ts
index 90051c3..b90ceb3 100644
--- a/src/event-handler/receive.ts
+++ b/src/event-handler/receive.ts
@@ -20,8 +20,7 @@ function getHooks(
   hooks.push(state.hooks[eventName]);
   hooks.push(state.hooks["*"]);
 
-  // @ts-ignore
-  return [].concat(...hooks.filter(Boolean));
+  return ([] as Function[]).concat(...hooks.filter(Boolean));
 }
 
 // main handler function
diff --git a/src/index.ts b/src/index.ts
index 18bdc1b..59e6d0a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -18,7 +18,7 @@ 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 verify: (eventPayload: object, signature: string) => boolean;
   public on: <E extends WebhookEvents>(
     event: E | E[],
     callback: HandlerFunction<E, U>
diff --git a/src/middleware/get-payload.ts b/src/middleware/get-payload.ts
index 51243ba..7595cc1 100644
--- a/src/middleware/get-payload.ts
+++ b/src/middleware/get-payload.ts
@@ -10,7 +10,7 @@ export function getPayload(
   // If request.body already exists we can stop here
   // See https://github.com/octokit/webhooks.js/pull/23
 
-  // @ts-ignore
+  // @ts-expect-error
   if (request.body) return Promise.resolve(request.body);
 
   return new Promise((resolve, reject) => {
diff --git a/src/middleware/verify-and-receive.ts b/src/middleware/verify-and-receive.ts
index add8a9a..613b223 100644
--- a/src/middleware/verify-and-receive.ts
+++ b/src/middleware/verify-and-receive.ts
@@ -5,7 +5,12 @@ export function verifyAndReceive(
   state: State,
   event: WebhookEvent & { signature: string }
 ): any {
-  const matchesSignature = verify(state.secret, event.payload, event.signature);
+  // verify will validate that the secret is not undefined
+  const matchesSignature = verify(
+    state.secret!,
+    event.payload,
+    event.signature
+  );
 
   if (!matchesSignature) {
     const error = new Error(
diff --git a/src/sign/index.ts b/src/sign/index.ts
index cb0baff..6e5edce 100644
--- a/src/sign/index.ts
+++ b/src/sign/index.ts
@@ -22,7 +22,6 @@ export function sign(
           algorithm: options.algorithm || Algorithm.SHA1,
         };
 
-  // @ts-ignore throw friendly error message when required options are missing
   if (!secret || !payload) {
     throw new TypeError("[@octokit/webhooks] secret & payload required");
   }
diff --git a/src/verify/README.md b/src/verify/README.md
index 3fb0315..de6e415 100644
--- a/src/verify/README.md
+++ b/src/verify/README.md
@@ -4,7 +4,7 @@ The `verify` method can be used as a standalone method.
 
 ```js
 const { verify } = require('@octokit/webhooks')
-const matchesSignature = verify(secret, eventData, signature)
+const matchesSignature = verify(secret, eventPayload, signature)
 // true or false
 ```
 
@@ -12,7 +12,7 @@ const matchesSignature = verify(secret, eventData, signature)
   <tr>
     <td>
       <code>
-        options.secret
+        secret
       </code>
       <em>(String)</em>
     </td>
@@ -51,6 +51,6 @@ const matchesSignature = verify(secret, eventData, signature)
   </tr>
 </table>
 
-Returns `true` or `false`. Throws error if `secret, ``eventPayload` or `signature` not passed.
+Returns `true` or `false`. Throws error if `secret`, `eventPayload` or `signature` not passed.
 
 Back to [@octokit/webhooks README](..).
diff --git a/src/verify/index.ts b/src/verify/index.ts
index af689cb..e769a9d 100644
--- a/src/verify/index.ts
+++ b/src/verify/index.ts
@@ -7,9 +7,9 @@ const getAlgorithm = (signature: string) => {
 };
 
 export function verify(
-  secret?: string,
-  eventPayload?: object,
-  signature?: string
+  secret: string,
+  eventPayload: object,
+  signature: string
 ): boolean {
   if (!secret || !eventPayload || !signature) {
     throw new TypeError(


Test patch:
diff --git a/test/integration/server-test.ts b/test/integration/server-test.ts
index 57834bb..06a241d 100644
--- a/test/integration/server-test.ts
+++ b/test/integration/server-test.ts
@@ -182,7 +182,7 @@ describe("server-test", () => {
     const server = http.createServer((req, res) => {
       req.once("data", (chunk) => dataChunks.push(chunk));
       req.once("end", () => {
-        // @ts-ignore
+        // @ts-expect-error
         req.body = JSON.parse(Buffer.concat(dataChunks).toString());
         api.middleware(req, res);
 
diff --git a/test/integration/sign-test.ts b/test/integration/sign-test.ts
index 1fff22a..5800f1e 100644
--- a/test/integration/sign-test.ts
+++ b/test/integration/sign-test.ts
@@ -6,22 +6,21 @@ const eventPayload = {
 const secret = "mysecret";
 
 test("sign() without options throws", () => {
-  // @ts-ignore
+  // @ts-expect-error
   expect(() => sign()).toThrow();
 });
 
 test("sign(undefined, eventPayload) without secret throws", () => {
-  // @ts-ignore
+  // @ts-expect-error
   expect(() => sign.bind(null, undefined, eventPayload)()).toThrow();
 });
 
 test("sign(secret) without eventPayload throws", () => {
-  // @ts-ignore
+  // @ts-expect-error
   expect(() => sign.bind(null, secret)()).toThrow();
 });
 
 test("sign({secret, algorithm}) with invalid algorithm throws", () => {
-  // @ts-ignore
   expect(() =>
     sign.bind(null, { secret, algorithm: "sha2" }, eventPayload)()
   ).toThrow();
diff --git a/test/integration/verify-test.ts b/test/integration/verify-test.ts
index 163a94e..5d4464e 100644
--- a/test/integration/verify-test.ts
+++ b/test/integration/verify-test.ts
@@ -9,18 +9,22 @@ const signatureSHA256 =
   "sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3";
 
 test("verify() without options throws", () => {
+  // @ts-expect-error
   expect(() => verify()).toThrow();
 });
 
 test("verify(undefined, eventPayload) without secret throws", () => {
+  // @ts-expect-error
   expect(() => verify.bind(null, undefined, eventPayload)()).toThrow();
 });
 
 test("verify(secret) without eventPayload throws", () => {
+  // @ts-expect-error
   expect(() => verify.bind(null, secret)()).toThrow();
 });
 
 test("verify(secret, eventPayload) without options.signature throws", () => {
+  // @ts-expect-error
   expect(() => verify.bind(null, secret, eventPayload)()).toThrow();
 });
 
diff --git a/test/unit/event-handler-on-test.ts b/test/unit/event-handler-on-test.ts
index c94738f..89d7a29 100644
--- a/test/unit/event-handler-on-test.ts
+++ b/test/unit/event-handler-on-test.ts
@@ -11,7 +11,7 @@ const state: State = {
 // Test broken with TypeScript without the ignore
 test("receiver.on with invalid event name", () => {
   simple.mock(console, "warn").callFn(function () {});
-  // @ts-ignore
+  // @ts-expect-error
   receiverOn(state, "foo", noop);
   expect((console.warn as simple.Stub<void>).callCount).toBe(1);
   expect((console.warn as simple.Stub<void>).lastCall.arg).toBe(
diff --git a/test/unit/event-handler-receive-test.ts b/test/unit/event-handler-receive-test.ts
index d889f0e..9c3415b 100644
--- a/test/unit/event-handler-receive-test.ts
+++ b/test/unit/event-handler-receive-test.ts
@@ -7,20 +7,20 @@ const state: State = {
 };
 
 test("options: none", () => {
-  // @ts-ignore
+  // @ts-expect-error
   expect(() => receive(state)).toThrow();
 });
 
 test("options: name", () => {
   expect(() => {
-    // @ts-ignore
+    // @ts-expect-error
     receive(state, { name: "foo" });
   }).toThrow();
 });
 
 test("options: name, payload", () => {
   expect(() => {
-    // @ts-ignore
+    // @ts-expect-error
     receive(state, { name: "foo", payload: {} });
   }).not.toThrow();
 });