@containify-platform/sdk 0.0.0-stage → 1.0.317

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,47 @@
1
+ /** A webhook body (version 1). New types may be added: ignore ones you don't handle. */
2
+ export interface WebhookEvent {
3
+ version: 1;
4
+ id: string;
5
+ deliveryId: string;
6
+ type: string;
7
+ kind: string;
8
+ severity: 'INFO' | 'WARNING' | 'CRITICAL';
9
+ title: string;
10
+ summary: string;
11
+ url: string | null;
12
+ count: number;
13
+ firstAt: string;
14
+ lastAt: string;
15
+ resolvedAt: string | null;
16
+ workspace: {
17
+ id: string;
18
+ name: string;
19
+ };
20
+ project: {
21
+ id: string;
22
+ name: string;
23
+ } | null;
24
+ environmentId: string | null;
25
+ appId: string | null;
26
+ labels?: {
27
+ project: Record<string, string> | null;
28
+ app: Record<string, string> | null;
29
+ };
30
+ data: Record<string, unknown>;
31
+ }
32
+ export declare class WebhookVerificationError extends Error {
33
+ constructor(message: string);
34
+ }
35
+ /**
36
+ * Check a webhook request and return its event. Pass the body exactly as it
37
+ * arrived (the raw text, before parsing) and the Containify-Signature header.
38
+ * Throws WebhookVerificationError if the signature is wrong or older than
39
+ * toleranceSeconds (300 by default), so a captured request can't be replayed.
40
+ */
41
+ export declare function verifyWebhook(input: {
42
+ secret: string;
43
+ body: string;
44
+ signature: string | null | undefined;
45
+ toleranceSeconds?: number;
46
+ now?: number;
47
+ }): Promise<WebhookEvent>;
@@ -0,0 +1,46 @@
1
+ // Webhook signatures (docs: /docs/workspace/notifications#verify-the-signature).
2
+ // Containify-Signature is `t=<unix seconds>,v1=<hex HMAC-SHA256 of "<t>.<body>">`,
3
+ // keyed with the destination's signing secret. Web Crypto only, so it runs in
4
+ // Node 18+, Bun, Deno, browsers and edge runtimes.
5
+ export class WebhookVerificationError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = 'WebhookVerificationError';
9
+ }
10
+ }
11
+ const encoder = new TextEncoder();
12
+ function toHex(buf) {
13
+ return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, '0')).join('');
14
+ }
15
+ /** Compare without stopping at the first difference. */
16
+ function sameText(a, b) {
17
+ if (a.length !== b.length)
18
+ return false;
19
+ let diff = 0;
20
+ for (let i = 0; i < a.length; i++)
21
+ diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
22
+ return diff === 0;
23
+ }
24
+ /**
25
+ * Check a webhook request and return its event. Pass the body exactly as it
26
+ * arrived (the raw text, before parsing) and the Containify-Signature header.
27
+ * Throws WebhookVerificationError if the signature is wrong or older than
28
+ * toleranceSeconds (300 by default), so a captured request can't be replayed.
29
+ */
30
+ export async function verifyWebhook(input) {
31
+ const parts = Object.fromEntries((input.signature ?? '').split(',').map((p) => {
32
+ const i = p.indexOf('=');
33
+ return [p.slice(0, i).trim(), p.slice(i + 1).trim()];
34
+ }));
35
+ const t = Number(parts.t);
36
+ if (!parts.t || !Number.isInteger(t) || !parts.v1)
37
+ throw new WebhookVerificationError('No Containify-Signature, or not t=…,v1=…');
38
+ const now = Math.floor((input.now ?? Date.now()) / 1000);
39
+ if (Math.abs(now - t) > (input.toleranceSeconds ?? 300))
40
+ throw new WebhookVerificationError('The signature is too old');
41
+ const key = await crypto.subtle.importKey('raw', encoder.encode(input.secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
42
+ const expected = toHex(await crypto.subtle.sign('HMAC', key, encoder.encode(`${t}.${input.body}`)));
43
+ if (!sameText(expected, parts.v1))
44
+ throw new WebhookVerificationError("The signature doesn't match");
45
+ return JSON.parse(input.body);
46
+ }
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@containify-platform/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.317",
4
+ "description": "TypeScript client for the Containify API: typed routes, safe retries, operations and webhook signatures",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "homepage": "https://containify.cloud/docs/api/sdks",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": ["dist", "README.md"],
17
+ "sideEffects": false,
18
+ "scripts": {
19
+ "typecheck": "tsc -p tsconfig.json",
20
+ "test": "bun test src",
21
+ "generate": "openapi-typescript ${CONTAINIFY_API_URL:-http://localhost:3000}/openapi.yaml -o src/schema.d.ts",
22
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && cp src/schema.d.ts dist/schema.d.ts",
23
+ "release": "bun scripts/release.ts"
24
+ },
25
+ "dependencies": {
26
+ "openapi-fetch": "^0.17.0"
27
+ },
28
+ "devDependencies": {
29
+ "@types/bun": "^1.4.2",
30
+ "openapi-typescript": "^7.13.0",
31
+ "typescript": "^5.8.3"
32
+ }
33
+ }