|
| 1 | +# @orfon/didomi-types |
| 2 | + |
| 3 | +TypeScript type definitions for the [Didomi Web SDK](https://developers.didomi.io/cmp/web-sdk/reference/api). This package augments the global `Window` interface so `window.Didomi`, `window.didomiOnReady`, `window.didomiConfig`, and `window.__tcfapi` are properly typed. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @orfon/didomi-types --save-dev |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +After installing, the global `Window` interface is augmented automatically — no manual `import` needed for window properties. |
| 14 | + |
| 15 | +### Typed `didomiOnReady` callback |
| 16 | + |
| 17 | +The callback parameter is typed as `IDidomiObject`, so all SDK methods and their return types are available: |
| 18 | + |
| 19 | +```typescript |
| 20 | +window.didomiOnReady = window.didomiOnReady || []; |
| 21 | +window.didomiOnReady.push((Didomi) => { |
| 22 | + // Didomi is typed as IDidomiObject |
| 23 | + // .getCurrentUserStatus() returns CurrentUserStatus |
| 24 | + const status = Didomi.getCurrentUserStatus(); |
| 25 | + status.user_id; // string |
| 26 | + status.regulation; // string |
| 27 | + status.purposes; // Map<PurposeStatus> |
| 28 | + status.vendors; // Map<VendorStatus> |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +### Type-checked events |
| 33 | + |
| 34 | +`EventType` is a string union — typos and invalid event names are caught at compile time: |
| 35 | + |
| 36 | +```typescript |
| 37 | +window.didomiOnReady?.push((Didomi) => { |
| 38 | + Didomi.on('consent.changed', () => {}); // ok |
| 39 | + Didomi.on('notice.shown', () => {}); // ok |
| 40 | + Didomi.on('conset.changed', () => {}); // TS error: not assignable to EventType |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### Importing types directly |
| 45 | + |
| 46 | +You can import types for use in your own function signatures: |
| 47 | + |
| 48 | +```typescript |
| 49 | +import type { CurrentUserStatus, Vendor, Purpose, EventType } from '@orfon/didomi-types'; |
| 50 | + |
| 51 | +function handleConsentChange(status: CurrentUserStatus): void { |
| 52 | + const enabledVendors = Object.values(status.vendors) |
| 53 | + .filter((v) => v.enabled); |
| 54 | + // ... |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Typed `didomiConfig` |
| 59 | + |
| 60 | +Configuration is typed as `DidomiConfig`, giving you autocomplete and validation: |
| 61 | + |
| 62 | +```typescript |
| 63 | +window.didomiConfig = { |
| 64 | + app: { |
| 65 | + apiKey: 'your-api-key', |
| 66 | + name: 'Your App', |
| 67 | + }, |
| 68 | + languages: { |
| 69 | + enabled: ['en', 'de'], |
| 70 | + default: 'en', |
| 71 | + }, |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +## Exported Types |
| 76 | + |
| 77 | +The main types provided by this package: |
| 78 | + |
| 79 | +| Type | Description | |
| 80 | +| --- | --- | |
| 81 | +| `IDidomiObject` | The main SDK object (`window.Didomi`) | |
| 82 | +| `CurrentUserStatus` | Return type of `getCurrentUserStatus()` | |
| 83 | +| `Vendor` / `VendorStatus` | Vendor definitions and consent status | |
| 84 | +| `Purpose` / `PurposeStatus` | Purpose definitions and consent status | |
| 85 | +| `EventType` | String union of all SDK event names | |
| 86 | +| `DidomiConfig` | Configuration object for `window.didomiConfig` | |
| 87 | +| `CurrentUserStatusTransaction` | Transaction for batch consent updates | |
| 88 | +| `TcfApi` | IAB TCF v2 API (`window.__tcfapi`) | |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +[Modified BSD License (BSD-3)](LICENSE.md) |
0 commit comments