@gethydra/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Webhook signature verification utility.
3
+ *
4
+ * Separate entry point (`@gethydra/sdk/webhooks`) — not bundled with the main
5
+ * client since it's only needed server-side in webhook handlers.
6
+ *
7
+ * Uses crypto.subtle.verify() for constant-time comparison across all runtimes
8
+ * (Node 18+, Deno, Bun, Cloudflare Workers, browsers).
9
+ */
10
+ /**
11
+ * Verify a webhook signature against a payload and secret.
12
+ *
13
+ * @param payload - The raw request body string
14
+ * @param signature - The hex-encoded HMAC-SHA256 signature from the webhook header
15
+ * @param secret - The webhook signing secret
16
+ * @returns true if the signature is valid
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { verifyWebhookSignature } from '@gethydra/sdk/webhooks';
21
+ *
22
+ * const isValid = await verifyWebhookSignature(
23
+ * rawBody,
24
+ * request.headers.get('X-Hydra-Signature'),
25
+ * webhookSecret,
26
+ * );
27
+ * ```
28
+ */
29
+ declare function verifyWebhookSignature(payload: string, signature: string, secret: string): Promise<boolean>;
30
+
31
+ export { verifyWebhookSignature };
@@ -0,0 +1,20 @@
1
+ // src/webhooks.ts
2
+ async function verifyWebhookSignature(payload, signature, secret) {
3
+ if (!signature || signature.length % 2 !== 0) return false;
4
+ const encoder = new TextEncoder();
5
+ const key = await crypto.subtle.importKey(
6
+ "raw",
7
+ encoder.encode(secret),
8
+ { name: "HMAC", hash: "SHA-256" },
9
+ false,
10
+ ["verify"]
11
+ );
12
+ const sigBytes = new Uint8Array(signature.length / 2);
13
+ for (let i = 0; i < sigBytes.length; i++) {
14
+ sigBytes[i] = parseInt(signature.slice(i * 2, i * 2 + 2), 16);
15
+ }
16
+ return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
17
+ }
18
+ export {
19
+ verifyWebhookSignature
20
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@gethydra/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Official TypeScript SDK for the Hydra Commerce API",
5
+ "license": "MIT",
6
+ "author": "Grant Capital Group",
7
+ "homepage": "https://hydrajs.dev/docs",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/Hydra-headless-commerce/website.git",
11
+ "directory": "packages/sdk"
12
+ },
13
+ "keywords": [
14
+ "hydra",
15
+ "commerce",
16
+ "ecommerce",
17
+ "headless",
18
+ "api",
19
+ "sdk"
20
+ ],
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./dist/index.js",
25
+ "types": "./dist/index.d.ts"
26
+ },
27
+ "./webhooks": {
28
+ "import": "./dist/webhooks.js",
29
+ "types": "./dist/webhooks.d.ts"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "LICENSE",
35
+ "README.md"
36
+ ],
37
+ "engines": {
38
+ "node": ">=18"
39
+ },
40
+ "scripts": {
41
+ "generate": "bun run scripts/generate-types.ts",
42
+ "build": "tsup src/index.ts src/webhooks.ts --format esm --dts --clean",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run"
45
+ },
46
+ "devDependencies": {
47
+ "openapi-typescript": "^7.6.1",
48
+ "tsup": "^8.4.0",
49
+ "typescript": "^5.8.3",
50
+ "vitest": "^4.1.11"
51
+ }
52
+ }