Skip to content

Commit 4e891a0

Browse files
committed
(new Imagekit()).verifyWebhookEvent
1 parent 600ecdf commit 4e891a0

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { IKCallback } from "./libs/interfaces/IKCallback";
4040
import manage from "./libs/manage";
4141
import signature from "./libs/signature";
4242
import upload from "./libs/upload";
43-
import { verify as verifyWebhookSignature } from "./utils/webhook-signature";
43+
import { verify as verifyWebhookEvent } from "./utils/webhook-signature";
4444
import customMetadataField from "./libs/manage/custom-metadata-field";
4545
/*
4646
Implementations
@@ -76,16 +76,7 @@ const promisify = function <T = void>(thisContext: ImageKit, fn: Function) {
7676
}
7777
};
7878
};
79-
80-
const Webhook = {
81-
verify: verifyWebhookSignature,
82-
}
83-
84-
class ImageKitStaticUtils {
85-
static Webhook = Webhook;
86-
}
87-
88-
class ImageKit extends ImageKitStaticUtils {
79+
class ImageKit {
8980
options: ImageKitOptions = {
9081
uploadEndpoint: "https://upload.imagekit.io/api/v1/files/upload",
9182
publicKey: "",
@@ -95,7 +86,6 @@ class ImageKit extends ImageKitStaticUtils {
9586
};
9687

9788
constructor(opts: ImageKitOptions = {} as ImageKitOptions) {
98-
super()
9989
this.options = _.extend(this.options, opts);
10090
if (!this.options.publicKey) {
10191
throw new Error(errorMessages.MANDATORY_PUBLIC_KEY_MISSING.message);
@@ -677,6 +667,14 @@ class ImageKit extends ImageKitStaticUtils {
677667
pHashDistance(firstPHash: string, secondPHash: string): number | Error {
678668
return pHashUtils.pHashDistance(firstPHash, secondPHash);
679669
}
670+
671+
/**
672+
* @param payload - Raw webhook request body (Encoded as UTF8 string or Buffer)
673+
* @param signature - Webhook signature as UTF8 encoded strings (Stored in `x-ik-signature` header of the request)
674+
* @param secret - Webhook secret as UTF8 encoded string [Copy from ImageKit dashboard](https://imagekit.io/dashboard/developer/webhooks)
675+
* @returns \{ `timstamp`: Verified UNIX epoch timestamp if signature, `event`: Parsed webhook event payload \}
676+
*/
677+
verifyWebhookEvent = verifyWebhookEvent;
680678
}
681679

682-
export = ImageKit;
680+
export = ImageKit;

tests/webhook-signature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Webhook } from "../index";
1+
import ImageKit from "../index";
22
import { expect } from "chai";
33

44
// Sample webhook data
@@ -17,7 +17,7 @@ const WEBHOOK_REQUEST_SAMPLE = Object.seal({
1717
});
1818

1919
describe("WebhookSignature", function () {
20-
const { verify } = Webhook;
20+
const verify = (new ImageKit(require("./data").initializationParams)).verifyWebhookEvent;
2121

2222
context("Test Webhook.verify() - Positive cases", () => {
2323
it("Verify with body as string", () => {

utils/webhook-signature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const deserializeSignature = (
6868
};
6969

7070
/**
71-
* @param payload - Webhook request (Raw body)
71+
* @param payload - Raw webhook request body (Encoded as UTF8 string or Buffer)
7272
* @param signature - Webhook signature as UTF8 encoded strings (Stored in `x-ik-signature` header of the request)
7373
* @param secret - Webhook secret as UTF8 encoded string [Copy from ImageKit dashboard](https://imagekit.io/dashboard/developer/webhooks)
7474
* @returns \{ `timstamp`: Verified UNIX epoch timestamp if signature, `event`: Parsed webhook event payload \}

0 commit comments

Comments
 (0)