@stewardhq/sdk 0.1.0-rc.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.
Files changed (48) hide show
  1. package/README.md +437 -0
  2. package/dist/_chunks/errors.js +73 -0
  3. package/dist/_chunks/events.d.ts +537 -0
  4. package/dist/_chunks/events.js +350 -0
  5. package/dist/_chunks/index.d.ts +6005 -0
  6. package/dist/_chunks/locale.d.ts +17 -0
  7. package/dist/_chunks/locale.js +17 -0
  8. package/dist/_chunks/src.js +1202 -0
  9. package/dist/_chunks/steward.d.ts +2588 -0
  10. package/dist/_chunks/steward.js +578 -0
  11. package/dist/_chunks/validators.d.ts +23 -0
  12. package/dist/_chunks/validators.js +106 -0
  13. package/dist/_chunks/webhook-core.d.ts +32 -0
  14. package/dist/_chunks/webhook-core.js +64 -0
  15. package/dist/_chunks/webhook.d.ts +22 -0
  16. package/dist/_chunks/webhook.js +53 -0
  17. package/dist/contract.d.ts +4 -0
  18. package/dist/contract.js +4 -0
  19. package/dist/index.d.ts +268 -0
  20. package/dist/index.js +784 -0
  21. package/dist/server.d.ts +236 -0
  22. package/dist/server.js +384 -0
  23. package/dist/testing/fixtures/events/LOCK.json +27 -0
  24. package/dist/testing/fixtures/events/account.deleted.json +36 -0
  25. package/dist/testing/fixtures/events/account.state_changed.json +53 -0
  26. package/dist/testing/fixtures/events/account.updated.json +37 -0
  27. package/dist/testing/fixtures/events/checkout.completed.json +36 -0
  28. package/dist/testing/fixtures/events/checkout.expired.json +26 -0
  29. package/dist/testing/fixtures/events/checkout.failed.json +27 -0
  30. package/dist/testing/fixtures/events/invoice.created.json +37 -0
  31. package/dist/testing/fixtures/events/invoice.issued.json +38 -0
  32. package/dist/testing/fixtures/events/invoice.voided.json +39 -0
  33. package/dist/testing/fixtures/events/subscription.activated.json +40 -0
  34. package/dist/testing/fixtures/events/subscription.cancel_scheduled.json +38 -0
  35. package/dist/testing/fixtures/events/subscription.canceled.json +29 -0
  36. package/dist/testing/fixtures/events/subscription.expired.json +27 -0
  37. package/dist/testing/fixtures/events/subscription.payment_failed.json +38 -0
  38. package/dist/testing/fixtures/events/subscription.reactivated.json +36 -0
  39. package/dist/testing/fixtures/events/subscription.renewed.json +39 -0
  40. package/dist/testing/fixtures/events/subscription.suspended.json +36 -0
  41. package/dist/testing/fixtures/events/subscription.terminated.json +28 -0
  42. package/dist/testing.d.ts +629 -0
  43. package/dist/testing.js +3759 -0
  44. package/dist/webhook/web.d.ts +17 -0
  45. package/dist/webhook/web.js +66 -0
  46. package/dist/webhook.d.ts +3 -0
  47. package/dist/webhook.js +3 -0
  48. package/package.json +70 -0
@@ -0,0 +1,17 @@
1
+ import { a as HeaderSource, c as VerifyRawOptions, i as HEADER_TIMESTAMP, l as VerifyRawResult, n as HEADER_EVENT_ID, o as VerifyFailure, r as HEADER_SIGNATURE, s as VerifyRawFailure, t as DEFAULT_TOLERANCE_SECONDS, u as VerifyResult } from "../_chunks/webhook-core.js";
2
+ //#region ../contract/src/webhook-web.d.ts
3
+ export declare function computeSignature(secret: string, eventId: string, timestamp: number, rawBody: string): Promise<string>;
4
+ /** Gönderen tarafı: teslimat başlıkları. `secrets[0]` güncel sır. */
5
+ export declare function signWebhook(secrets: readonly string[], eventId: string, rawBody: string, timestamp?: number): Promise<Record<string, string>>;
6
+ /** Alıcı tarafı, şemasız: `./webhook`'taki `verifyWebhookRaw`'ın async karşılığı. */
7
+ export declare function verifyWebhookRaw(secrets: readonly string[], headers: HeaderSource, rawBody: string, options?: VerifyRawOptions): Promise<VerifyRawResult>;
8
+ /** Alıcı tarafı: imzayı ham gövde üzerinde doğrular, SONRA sözleşmeyle parse eder. */
9
+ export declare function verifyWebhook(input: {
10
+ secrets: readonly string[];
11
+ headers: HeaderSource;
12
+ rawBody: string;
13
+ nowSeconds?: number;
14
+ toleranceSeconds?: number;
15
+ }): Promise<VerifyResult>;
16
+ //#endregion
17
+ export { DEFAULT_TOLERANCE_SECONDS, HEADER_EVENT_ID, HEADER_SIGNATURE, HEADER_TIMESTAMP, type VerifyFailure, type VerifyRawFailure, type VerifyRawOptions, type VerifyRawResult, type VerifyResult, type HeaderSource as WebhookHeaders };
@@ -0,0 +1,66 @@
1
+ import { a as checkHeaders, i as HEADER_TIMESTAMP, n as HEADER_EVENT_ID, o as parseVerified, r as HEADER_SIGNATURE, s as signedPayload, t as DEFAULT_TOLERANCE_SECONDS } from "../_chunks/webhook-core.js";
2
+ //#region ../contract/src/webhook-web.ts
3
+ const encoder = new TextEncoder();
4
+ const SHA256_BYTES = 32;
5
+ function hmacKey(secret) {
6
+ const raw = secret.length === 0 ? /* @__PURE__ */ new Uint8Array(1) : encoder.encode(secret);
7
+ return crypto.subtle.importKey("raw", raw, {
8
+ name: "HMAC",
9
+ hash: "SHA-256"
10
+ }, false, ["sign", "verify"]);
11
+ }
12
+ function toHex(bytes) {
13
+ return Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
14
+ }
15
+ function fromHex(hex) {
16
+ if (hex.length !== 64 || !/^[0-9a-f]+$/.test(hex)) return null;
17
+ const out = new Uint8Array(SHA256_BYTES);
18
+ for (let i = 0; i < SHA256_BYTES; i++) out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
19
+ return out;
20
+ }
21
+ async function computeSignature(secret, eventId, timestamp, rawBody) {
22
+ const key = await hmacKey(secret);
23
+ return toHex(await crypto.subtle.sign("HMAC", key, encoder.encode(signedPayload(eventId, timestamp, rawBody))));
24
+ }
25
+ /** Gönderen tarafı: teslimat başlıkları. `secrets[0]` güncel sır. */
26
+ async function signWebhook(secrets, eventId, rawBody, timestamp = Math.floor(Date.now() / 1e3)) {
27
+ if (secrets.length === 0) throw new Error("en az bir imza sırrı gerekli");
28
+ const signatures = await Promise.all(secrets.map((s) => computeSignature(s, eventId, timestamp, rawBody)));
29
+ return {
30
+ [HEADER_EVENT_ID]: eventId,
31
+ [HEADER_TIMESTAMP]: String(timestamp),
32
+ [HEADER_SIGNATURE]: signatures.map((s) => `v1=${s}`).join(",")
33
+ };
34
+ }
35
+ /** Alıcı tarafı, şemasız: `./webhook`'taki `verifyWebhookRaw`'ın async karşılığı. */
36
+ async function verifyWebhookRaw(secrets, headers, rawBody, options = {}) {
37
+ const checked = checkHeaders(secrets, headers, options);
38
+ if (!checked.ok) return checked;
39
+ const { eventId, timestamp, signatures } = checked;
40
+ const given = signatures.map(fromHex).filter((s) => s !== null);
41
+ if (given.length > 0) {
42
+ const data = encoder.encode(signedPayload(eventId, timestamp, rawBody));
43
+ for (const secret of secrets) {
44
+ const key = await hmacKey(secret);
45
+ for (const signature of given) if (await crypto.subtle.verify("HMAC", key, signature, data)) return {
46
+ ok: true,
47
+ eventId,
48
+ timestamp
49
+ };
50
+ }
51
+ }
52
+ return {
53
+ ok: false,
54
+ reason: "bad_signature"
55
+ };
56
+ }
57
+ /** Alıcı tarafı: imzayı ham gövde üzerinde doğrular, SONRA sözleşmeyle parse eder. */
58
+ async function verifyWebhook(input) {
59
+ const verified = await verifyWebhookRaw(input.secrets, input.headers, input.rawBody, {
60
+ ...input.nowSeconds !== void 0 ? { now: input.nowSeconds } : {},
61
+ ...input.toleranceSeconds !== void 0 ? { toleranceSeconds: input.toleranceSeconds } : {}
62
+ });
63
+ return verified.ok ? parseVerified(verified.eventId, input.rawBody) : verified;
64
+ }
65
+ //#endregion
66
+ export { DEFAULT_TOLERANCE_SECONDS, HEADER_EVENT_ID, HEADER_SIGNATURE, HEADER_TIMESTAMP, computeSignature, signWebhook, verifyWebhook, verifyWebhookRaw };
@@ -0,0 +1,3 @@
1
+ import { a as HeaderSource, c as VerifyRawOptions, i as HEADER_TIMESTAMP, l as VerifyRawResult, n as HEADER_EVENT_ID, o as VerifyFailure, r as HEADER_SIGNATURE, s as VerifyRawFailure, t as DEFAULT_TOLERANCE_SECONDS, u as VerifyResult } from "./_chunks/webhook-core.js";
2
+ import { i as verifyWebhookRaw, n as signWebhook, r as verifyWebhook, t as computeSignature } from "./_chunks/webhook.js";
3
+ export { DEFAULT_TOLERANCE_SECONDS, HEADER_EVENT_ID, HEADER_SIGNATURE, HEADER_TIMESTAMP, type VerifyFailure, type VerifyRawFailure, type VerifyRawOptions, type VerifyRawResult, type VerifyResult, type HeaderSource as WebhookHeaders, computeSignature, signWebhook, verifyWebhook, verifyWebhookRaw };
@@ -0,0 +1,3 @@
1
+ import { i as HEADER_TIMESTAMP, n as HEADER_EVENT_ID, r as HEADER_SIGNATURE, t as DEFAULT_TOLERANCE_SECONDS } from "./_chunks/webhook-core.js";
2
+ import { i as verifyWebhookRaw, n as signWebhook, r as verifyWebhook, t as computeSignature } from "./_chunks/webhook.js";
3
+ export { DEFAULT_TOLERANCE_SECONDS, HEADER_EVENT_ID, HEADER_SIGNATURE, HEADER_TIMESTAMP, computeSignature, signWebhook, verifyWebhook, verifyWebhookRaw };
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@stewardhq/sdk",
3
+ "version": "0.1.0-rc.0",
4
+ "description": "Steward SDK: contract schemas, typed API client and webhook signing.",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/z9cloud/steward.git",
9
+ "directory": "sdk"
10
+ },
11
+ "engines": {
12
+ "node": ">=22"
13
+ },
14
+ "sideEffects": false,
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./contract": {
21
+ "types": "./dist/contract.d.ts",
22
+ "default": "./dist/contract.js"
23
+ },
24
+ "./webhook": {
25
+ "types": "./dist/webhook.d.ts",
26
+ "default": "./dist/webhook.js"
27
+ },
28
+ "./server": {
29
+ "types": "./dist/server.d.ts",
30
+ "default": "./dist/server.js"
31
+ },
32
+ "./webhook/web": {
33
+ "types": "./dist/webhook/web.d.ts",
34
+ "default": "./dist/webhook/web.js"
35
+ },
36
+ "./testing": {
37
+ "types": "./dist/testing.d.ts",
38
+ "default": "./dist/testing.js"
39
+ },
40
+ "./testing/fixtures/events/*": "./dist/testing/fixtures/events/*",
41
+ "./package.json": "./package.json"
42
+ },
43
+ "files": [
44
+ "dist"
45
+ ],
46
+ "publishConfig": {
47
+ "access": "public",
48
+ "registry": "https://registry.npmjs.org",
49
+ "provenance": true
50
+ },
51
+ "dependencies": {
52
+ "zod": "^4.0.0"
53
+ },
54
+ "devDependencies": {
55
+ "@types/node": "^24.13.3",
56
+ "tsdown": "^0.23.0",
57
+ "typescript": "^5.9.0",
58
+ "vitest": "^3.0.0",
59
+ "workerd": "^1.20260916.1",
60
+ "@z9cloud/steward-contract": "0.2.0"
61
+ },
62
+ "scripts": {
63
+ "build": "tsdown && node scripts/check-dist.mjs",
64
+ "check:pack": "node scripts/check-pack.mjs",
65
+ "check:workerd": "node scripts/check-workerd.mjs",
66
+ "test": "pnpm run build && vitest run",
67
+ "typecheck": "tsc --noEmit",
68
+ "lint": "eslint src scripts tsdown.config.ts"
69
+ }
70
+ }