@cookiemunch/sdk 0.7.1 → 0.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cookiemunch/sdk",
3
- "version": "0.7.1",
3
+ "version": "0.9.0",
4
4
  "description": "Typed REST client for the Cookie Munch Developer API (/v1)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "access": "public"
33
33
  },
34
34
  "scripts": {
35
- "build": "tsup",
35
+ "build": "tsup && tsc -p tsconfig.json --emitDeclarationOnly --declaration",
36
36
  "test": "vitest run"
37
37
  },
38
38
  "homepage": "https://cookiemunch.net/docs",
@@ -1,169 +0,0 @@
1
- /**
2
- * Server-side consent reading. The Cookie Munch embed stores the visitor's decision in a
3
- * first-party `CookieMunch` cookie (URL-encoded JSON). This lets a server / edge worker read
4
- * that decision from the incoming request — to gate server-rendered tags or SSR'd third-party
5
- * scripts BEFORE they hit the browser — without shipping the whole embed. Zero-dependency and
6
- * runtime-agnostic: accepts a raw Cookie header, a Node `req` (`req.headers.cookie`), or a
7
- * Fetch `Request` (`request.headers.get('cookie')`).
8
- *
9
- * import { readConsent, isAllowed } from '@cookiemunch/sdk/consent';
10
- * const consent = readConsent(req.headers.cookie); // ServerConsent | null
11
- * if (isAllowed(consent, 'statistics')) { …render analytics tag… }
12
- *
13
- * The contract is documented at https://cookiemunch.net/docs/consent-cookie. It accepts
14
- * exactly what the embed accepts, so a server never grants what the browser would refuse:
15
- * strict JSON (URL-encoded or not) and the relaxed object literal Cookiebot writes.
16
- *
17
- * This module imports nothing — `@cookiemunch/sdk/consent` pulls in no API client.
18
- */
19
- /** The first-party cookie the embed writes (mirrors @cookiemunch/core COOKIE_NAME). */
20
- declare const CONSENT_COOKIE_NAME = "CookieMunch";
21
- /** A visitor's consent decision, read from the request cookie. `necessary` is always true. */
22
- interface ServerConsent {
23
- necessary: true;
24
- preferences: boolean;
25
- statistics: boolean;
26
- marketing: boolean;
27
- /** How consent was given. `implied` = not yet an explicit choice. */
28
- method: 'explicit' | 'implied';
29
- /** Custom (non-standard) categories, when the site defines any. */
30
- custom?: Record<string, boolean>;
31
- /** Consent-policy version the decision was made under (see `minVersion`). */
32
- ver: number;
33
- /** When the decision was made, epoch milliseconds (see `expiryDays`). */
34
- utc: number;
35
- /** Region code the decision was made in (`unknown` when undetected). */
36
- region: string;
37
- /** The anonymous consent-receipt id tying this browser to the consent log. */
38
- stamp: string;
39
- /**
40
- * `<kid>.<base64url>`: the API's Ed25519 signature over this decision, issued when the
41
- * ledger recorded it. Absent on a decision no ledger holds (a hand-written cookie).
42
- */
43
- sig?: string;
44
- }
45
- /** The site's consent policy, so the server applies the same rules the embed does. */
46
- interface ReadConsentOptions {
47
- /** Cookie name. Default `CookieMunch`. */
48
- cookieName?: string;
49
- /**
50
- * The site's current consent version (`consent.version`, or the config `ver` when that is
51
- * higher). A decision made under an older version is not honoured by the embed — the
52
- * visitor is asked again — so it reads as `null` here too.
53
- */
54
- minVersion?: number;
55
- /** The site's `consent.expiryDays`: a decision older than this reads as `null`. */
56
- expiryDays?: number;
57
- /** Clock for `expiryDays`, epoch ms. Default `Date.now()`. */
58
- now?: number;
59
- }
60
- /** Where the platform's public signing keys come from, for signatures and consent tokens. */
61
- interface KeySetOptions {
62
- /** A key set you already hold (`{ keys: [...] }`). Skips the fetch. */
63
- jwks?: JsonWebKeySet;
64
- /** Default `https://api.cookiemunch.net/v1/partner/jwks.json`. */
65
- jwksUrl?: string;
66
- /** Injectable fetch. Default: the global one. */
67
- fetch?: typeof fetch;
68
- }
69
- /** `readConsent` with `requireSigned: true`: only a decision the API signed counts. */
70
- interface ReadSignedConsentOptions extends ReadConsentOptions, KeySetOptions {
71
- requireSigned: true;
72
- }
73
- /** Anything we can pull a Cookie header off of. */
74
- type ConsentSource = string | null | undefined | {
75
- headers?: {
76
- cookie?: string | null;
77
- get?(name: string): string | null;
78
- } | Record<string, unknown>;
79
- };
80
- /**
81
- * Parse a raw cookie value into a validated ServerConsent, or null. Accepts the two formats
82
- * the embed reads: URL-encoded (or plain) strict JSON, and Cookiebot's relaxed object
83
- * literal — `{stamp:'…',necessary:true,…}` — left behind by a site migrating from Cookiebot.
84
- */
85
- declare function parseConsentValue(raw: string): ServerConsent | null;
86
- /**
87
- * Read the visitor's consent from a request: a raw `Cookie` header, a Node `req`, or a Fetch
88
- * `Request`. Returns null when there is no decision the embed would honour — no cookie, an
89
- * unparseable one, or (with the options) one made under an older version or past expiry.
90
- */
91
- declare function readConsent(source: ConsentSource, opts: ReadSignedConsentOptions): Promise<ServerConsent | null>;
92
- declare function readConsent(source: ConsentSource, opts?: ReadConsentOptions & {
93
- requireSigned?: false;
94
- }): ServerConsent | null;
95
- /**
96
- * Read the visitor's consent from a request. The original spelling of {@link readConsent},
97
- * kept for existing callers; the second argument is the cookie name.
98
- */
99
- declare function getConsent(source: ConsentSource, cookieName?: string): ServerConsent | null;
100
- /** Standard + custom consent categories. */
101
- type ConsentCategory = 'necessary' | 'preferences' | 'statistics' | 'marketing' | (string & {});
102
- /**
103
- * Whether a category is allowed for this visitor. `necessary` is always allowed. A missing
104
- * consent (`null` — visitor hasn't decided) is treated as NOT allowed for non-necessary
105
- * categories (fail-closed / prior-blocking posture), matching the embed.
106
- */
107
- declare function isAllowed(consent: ServerConsent | null, category: ConsentCategory): boolean;
108
- declare const DEFAULT_JWKS_URL = "https://api.cookiemunch.net/v1/partner/jwks.json";
109
- /** The fields of a decision its signature covers. */
110
- interface SignableDecision {
111
- stamp: string;
112
- ver: number;
113
- utc: number;
114
- preferences: boolean;
115
- statistics: boolean;
116
- marketing: boolean;
117
- custom?: Record<string, boolean> | undefined;
118
- }
119
- /**
120
- * The exact line the API signs for a recorded decision. Identical to @cookiemunch/core's
121
- * `consentSignaturePayload` (a server test pins the two together).
122
- */
123
- declare function consentSignaturePayload(d: SignableDecision): string;
124
- interface Jwk {
125
- kty?: string;
126
- crv?: string;
127
- x?: string;
128
- kid?: string;
129
- alg?: string;
130
- }
131
- interface JsonWebKeySet {
132
- keys: Jwk[];
133
- }
134
- /** Does the cookie's `sig` verify for this decision? False on anything malformed. */
135
- declare function verifyDecisionSignature(consent: ServerConsent, opts?: KeySetOptions): Promise<boolean>;
136
- /** What a consent token (`CookieMunch.getConsentToken()`) asserts. */
137
- interface ConsentTokenClaims {
138
- iss: string;
139
- /** The decision's stamp. */
140
- sub: string;
141
- /** `cookiemunch:<cbid>`. */
142
- aud: string;
143
- token_use: 'consent';
144
- cbid: string;
145
- stamp: string;
146
- /** Every category the decision answered, `necessary` included. */
147
- choices: Record<string, boolean>;
148
- ver: number;
149
- /** When the decision was made, epoch ms. */
150
- decidedAt: number;
151
- cause?: string;
152
- iat: number;
153
- exp: number;
154
- }
155
- interface VerifyTokenOptions extends KeySetOptions {
156
- /** Require the token to be for this site. */
157
- cbid?: string;
158
- /** Clock, epoch ms. Default `Date.now()`. */
159
- now?: number;
160
- /** Allowed clock skew, seconds. Default 30. */
161
- leewaySeconds?: number;
162
- }
163
- /**
164
- * Verify a consent token from `CookieMunch.getConsentToken()` (CMU-F06) against the published
165
- * key set and return its claims. Throws on a bad signature, a wrong site, or an expired token.
166
- */
167
- declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<ConsentTokenClaims>;
168
-
169
- export { CONSENT_COOKIE_NAME, type ConsentCategory, type ConsentSource, type ConsentTokenClaims, DEFAULT_JWKS_URL, type JsonWebKeySet, type KeySetOptions, type ReadConsentOptions, type ReadSignedConsentOptions, type ServerConsent, type SignableDecision, type VerifyTokenOptions, consentSignaturePayload, getConsent, isAllowed, parseConsentValue, readConsent, verifyDecisionSignature, verifyToken };