@cookieyes/core 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.
- package/LICENSE +21 -0
- package/README.md +133 -0
- package/dist/index.cjs +596 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +210 -0
- package/dist/index.d.ts +210 -0
- package/dist/index.js +585 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CookieYes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# @cookieyes/core
|
|
2
|
+
|
|
3
|
+
The headless consent engine powering the CookieYes SDK. Zero UI, zero runtime dependencies. This is the single source of truth for all consent logic — every framework adapter imports from this package exclusively.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cookieyes/core
|
|
9
|
+
pnpm add @cookieyes/core
|
|
10
|
+
yarn add @cookieyes/core
|
|
11
|
+
bun add @cookieyes/core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
The recommended entry point is `getOrCreateConsentRuntime()`. It returns a
|
|
17
|
+
process-wide singleton with a `consentStore` (reactive state) and a
|
|
18
|
+
`consentManager` (imperative API).
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { getOrCreateConsentRuntime } from "@cookieyes/core";
|
|
22
|
+
|
|
23
|
+
const { consentManager, consentStore } = getOrCreateConsentRuntime({
|
|
24
|
+
mode: "offline", // "offline" (cookie-only) | "self-hosted"
|
|
25
|
+
overrides: { regulation: "GDPR" }, // "GDPR" | "CCPA" | "DEFAULT"
|
|
26
|
+
colorScheme: "system", // "light" | "dark" | "system"
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// React to every saved state change
|
|
30
|
+
const unsubscribe = consentStore.subscribe((state) => {
|
|
31
|
+
if (state.has("analytics")) {
|
|
32
|
+
// load analytics scripts (gtag, Mixpanel, …)
|
|
33
|
+
}
|
|
34
|
+
if (state.has("advertisement")) {
|
|
35
|
+
// load ad scripts (Meta Pixel, Google Ads, …)
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// React only to saved preference changes (not transient UI toggles)
|
|
40
|
+
consentStore
|
|
41
|
+
.getState()
|
|
42
|
+
.subscribeToConsentChanges(({ allowedCategories, deniedCategories }) => {
|
|
43
|
+
console.log("Allowed:", allowedCategories);
|
|
44
|
+
console.log("Denied:", deniedCategories);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Imperative actions
|
|
48
|
+
consentStore.getState().has("analytics"); // → boolean
|
|
49
|
+
consentStore.getState().saveConsents("all"); // accept all
|
|
50
|
+
consentStore.getState().saveConsents("necessary"); // reject all (necessary only)
|
|
51
|
+
consentStore.getState().setConsent("analytics", true);
|
|
52
|
+
consentManager.showPreferences(); // open the preferences dialog
|
|
53
|
+
consentManager.resetConsent(); // clear + re-prompt
|
|
54
|
+
|
|
55
|
+
unsubscribe();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Self-hosted mode
|
|
59
|
+
|
|
60
|
+
Pass `mode: "self-hosted"` with either a `backendURL` (the SDK POSTs a
|
|
61
|
+
`ConsentPayload` to it) or a custom `backend` adapter for full control:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
getOrCreateConsentRuntime({
|
|
65
|
+
mode: "self-hosted",
|
|
66
|
+
backend: {
|
|
67
|
+
async persist(payload) {
|
|
68
|
+
await fetch("https://your-backend.example.com/v1/consent", {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "Content-Type": "application/json" },
|
|
71
|
+
body: JSON.stringify(payload),
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
### `getOrCreateConsentRuntime(options)`
|
|
81
|
+
|
|
82
|
+
Returns `{ consentManager, consentStore }` (a singleton — call
|
|
83
|
+
`resetConsentRuntime()` to clear it, primarily for tests).
|
|
84
|
+
|
|
85
|
+
**`options`** (`ConsentRuntimeOptions`):
|
|
86
|
+
|
|
87
|
+
| Option | Type | Notes |
|
|
88
|
+
|--------|------|-------|
|
|
89
|
+
| `mode` | `"offline" \| "self-hosted"` | **Required.** |
|
|
90
|
+
| `backendURL` | `string` | Self-hosted: endpoint the payload is POSTed to. |
|
|
91
|
+
| `backend` | `ConsentBackend` | Self-hosted: custom `persist(payload)` adapter. |
|
|
92
|
+
| `apiKey` | `string` | Optional auth key. |
|
|
93
|
+
| `overrides.regulation` | `"GDPR" \| "CCPA" \| "DEFAULT"` | Force the applicable regulation. |
|
|
94
|
+
| `colorScheme` | `"light" \| "dark" \| "system"` | |
|
|
95
|
+
| `theme` | `ThemeConfig` | Color / spacing tokens. |
|
|
96
|
+
| `i18n` | `I18nConfig` | Translation messages / locale. |
|
|
97
|
+
| `networkBlocker` | `NetworkBlockerConfig` | Block network requests by category. |
|
|
98
|
+
| `reloadOnRevoke` | `boolean` | Reload the page when consent is revoked. |
|
|
99
|
+
| `onConsentReady` / `onConsentUpdate` | `(state) => void` | Lifecycle callbacks. |
|
|
100
|
+
|
|
101
|
+
**`consentStore`** — `subscribe(listener)` and `getState()`. State
|
|
102
|
+
(`ConsentStoreState`) includes `consentId`, `hasActed`, `categories`,
|
|
103
|
+
`regulation`, `lastRenewed`, `activeUI`, plus the methods `has()`,
|
|
104
|
+
`saveConsents()`, `setConsent()`, and `subscribeToConsentChanges()`.
|
|
105
|
+
|
|
106
|
+
### `createConsentManager(config)` (low-level)
|
|
107
|
+
|
|
108
|
+
The underlying manager, if you want to bypass the store. Returns a
|
|
109
|
+
`ConsentManager` with:
|
|
110
|
+
|
|
111
|
+
- **State**: `consentId`, `hasActed`, `categories`, `regulation`, `lastRenewed`, `isPreferencesOpen`
|
|
112
|
+
- **Methods**: `acceptAll()`, `rejectAll()`, `acceptSelected(cats)`, `updateCategory(cat, val)`, `savePreferences()`, `resetConsent()`, `showPreferences()`, `hidePreferences()`, `subscribe(fn)`, `registerScript(entry)`
|
|
113
|
+
|
|
114
|
+
`config` (`ConsentConfig`) accepts: `regulation`, `colorScheme`, `theme`,
|
|
115
|
+
`apiUrl`, `apiKey`, `backend`, `reloadOnRevoke`, `onConsentReady`,
|
|
116
|
+
`onConsentUpdate`.
|
|
117
|
+
|
|
118
|
+
> The applicable regulation comes from your configuration
|
|
119
|
+
> (`overrides.regulation` / `config.regulation`) and defaults to `"DEFAULT"`.
|
|
120
|
+
> The core engine does not perform IP-based geo-detection.
|
|
121
|
+
|
|
122
|
+
## Consent categories
|
|
123
|
+
|
|
124
|
+
`necessary` (always on), `functional`, `analytics`, `performance`, `advertisement`.
|
|
125
|
+
|
|
126
|
+
## Cookie
|
|
127
|
+
|
|
128
|
+
Consent is persisted in the `cookieyes-consent` cookie (`SameSite=Lax`, `path=/`).
|
|
129
|
+
Use `parseCookie` / `serializeCookie` from this package to read or write it directly.
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT — see [LICENSE](./LICENSE).
|