@vllnt/convex-consent 0.1.0-canary.591da41
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 +143 -0
- package/dist/client/index.d.ts +156 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +142 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +79 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +40 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +102 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +16 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +19 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/mutations.d.ts +60 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +149 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +78 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +119 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +53 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +40 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +80 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +62 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +31 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +29 -0
- package/dist/shared.js.map +1 -0
- package/package.json +101 -0
- package/src/client/index.ts +298 -0
- package/src/client/types.ts +85 -0
- package/src/component/_generated/api.ts +56 -0
- package/src/component/_generated/component.ts +127 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/crons.ts +23 -0
- package/src/component/mutations.ts +158 -0
- package/src/component/queries.ts +142 -0
- package/src/component/schema.ts +41 -0
- package/src/component/validators.ts +70 -0
- package/src/shared.ts +36 -0
- package/src/test.ts +15 -0
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Shared constants used by both `client/` and `component/`. */
|
|
2
|
+
export declare const COMPONENT_NAME = "consent";
|
|
3
|
+
/**
|
|
4
|
+
* The consent decisions a subject can hold for a purpose. `granted` and `denied`
|
|
5
|
+
* are explicit answers to the consent prompt; `withdrawn` revokes a prior grant.
|
|
6
|
+
* Every decision is appended to the immutable ledger and projected onto the
|
|
7
|
+
* current-state row.
|
|
8
|
+
*/
|
|
9
|
+
export declare const CONSENT_DECISIONS: readonly ["granted", "denied", "withdrawn"];
|
|
10
|
+
/** A single consent decision. */
|
|
11
|
+
export type ConsentDecision = (typeof CONSENT_DECISIONS)[number];
|
|
12
|
+
/**
|
|
13
|
+
* The default policy version stamped on a decision when the host supplies none.
|
|
14
|
+
* Consent is always versioned so a later policy change can invalidate a stale
|
|
15
|
+
* grant; `"1"` is the sensible zero-config default for a host that hasn't started
|
|
16
|
+
* versioning its policy yet.
|
|
17
|
+
*/
|
|
18
|
+
export declare const DEFAULT_VERSION = "1";
|
|
19
|
+
/**
|
|
20
|
+
* Default retention (ms) for ledger events before the prune cron sweeps them:
|
|
21
|
+
* 365 days. Consent proof must survive long enough to answer an audit or a data
|
|
22
|
+
* subject request, so the default window is deliberately long. The current-state
|
|
23
|
+
* projection is NEVER pruned — only superseded historical ledger events past the
|
|
24
|
+
* cutoff are removed. A host with a stricter or looser legal window drives
|
|
25
|
+
* `prune` from its own scheduler with an explicit `before` cutoff.
|
|
26
|
+
*/
|
|
27
|
+
export declare const DEFAULT_RETENTION_MS = 31536000000;
|
|
28
|
+
/** Default page size for a `prune` pass before the sweep self-reschedules. */
|
|
29
|
+
export declare const DEFAULT_PRUNE_BATCH = 200;
|
|
30
|
+
export declare const MAX_PRUNE_BATCH = 500;
|
|
31
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,eAAO,MAAM,cAAc,YAAY,CAAC;AAExC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,YAAI,SAAS,EAAE,QAAQ,EAAE,WAAW,CAAU,CAAC;AAE7E,iCAAiC;AACjC,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjE;;;;;GAKG;AACH,eAAO,MAAM,eAAe,MAAM,CAAC;AAEnC;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,cAAiB,CAAC;AAEnD,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,MAAM,CAAC;AACvC,eAAO,MAAM,eAAe,MAAM,CAAC"}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Shared constants used by both `client/` and `component/`. */
|
|
2
|
+
export const COMPONENT_NAME = "consent";
|
|
3
|
+
/**
|
|
4
|
+
* The consent decisions a subject can hold for a purpose. `granted` and `denied`
|
|
5
|
+
* are explicit answers to the consent prompt; `withdrawn` revokes a prior grant.
|
|
6
|
+
* Every decision is appended to the immutable ledger and projected onto the
|
|
7
|
+
* current-state row.
|
|
8
|
+
*/
|
|
9
|
+
export const CONSENT_DECISIONS = ["granted", "denied", "withdrawn"];
|
|
10
|
+
/**
|
|
11
|
+
* The default policy version stamped on a decision when the host supplies none.
|
|
12
|
+
* Consent is always versioned so a later policy change can invalidate a stale
|
|
13
|
+
* grant; `"1"` is the sensible zero-config default for a host that hasn't started
|
|
14
|
+
* versioning its policy yet.
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_VERSION = "1";
|
|
17
|
+
/**
|
|
18
|
+
* Default retention (ms) for ledger events before the prune cron sweeps them:
|
|
19
|
+
* 365 days. Consent proof must survive long enough to answer an audit or a data
|
|
20
|
+
* subject request, so the default window is deliberately long. The current-state
|
|
21
|
+
* projection is NEVER pruned — only superseded historical ledger events past the
|
|
22
|
+
* cutoff are removed. A host with a stricter or looser legal window drives
|
|
23
|
+
* `prune` from its own scheduler with an explicit `before` cutoff.
|
|
24
|
+
*/
|
|
25
|
+
export const DEFAULT_RETENTION_MS = 31_536_000_000;
|
|
26
|
+
/** Default page size for a `prune` pass before the sweep self-reschedules. */
|
|
27
|
+
export const DEFAULT_PRUNE_BATCH = 200;
|
|
28
|
+
export const MAX_PRUNE_BATCH = 500;
|
|
29
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AAExC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,WAAW,CAAU,CAAC;AAK7E;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,cAAc,CAAC;AAEnD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AACvC,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vllnt/convex-consent",
|
|
3
|
+
"version": "0.1.0-canary.591da41",
|
|
4
|
+
"description": "Append-only consent ledger (GDPR Art. 6/7) — record, withdraw, and gate consent per subject + purpose as a Convex component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@9.15.4",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/client/index.d.ts",
|
|
16
|
+
"default": "./dist/client/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./test": "./src/test.ts",
|
|
19
|
+
"./_generated/component.js": {
|
|
20
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./_generated/component": {
|
|
23
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./convex.config": {
|
|
26
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
27
|
+
"default": "./dist/component/convex.config.js"
|
|
28
|
+
},
|
|
29
|
+
"./convex.config.js": {
|
|
30
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
31
|
+
"default": "./dist/component/convex.config.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"types": "./dist/client/index.d.ts",
|
|
35
|
+
"module": "./dist/client/index.js",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
38
|
+
"build:codegen": "pnpm codegen && pnpm build",
|
|
39
|
+
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
42
|
+
"lint": "pnpm check:agents && eslint .",
|
|
43
|
+
"test": "vitest run --passWithNoTests",
|
|
44
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
45
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
46
|
+
"generate:llms": "node scripts/generate-llms.mjs",
|
|
47
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
48
|
+
"prepublishOnly": "npm whoami || npm login",
|
|
49
|
+
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
50
|
+
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
51
|
+
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
52
|
+
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"convex": "^1.45.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@edge-runtime/vm": "^5.0.0",
|
|
59
|
+
"@typescript/native": "npm:typescript@7.0.2",
|
|
60
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
61
|
+
"@vllnt/eslint-config": "2.0.0",
|
|
62
|
+
"@vllnt/typescript": "2.0.0",
|
|
63
|
+
"convex": "^1.45.0",
|
|
64
|
+
"convex-test": "^0.0.56",
|
|
65
|
+
"eslint": "^9.39.5",
|
|
66
|
+
"prettier": "^3.9.6",
|
|
67
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
68
|
+
"vite": "^8.2.2",
|
|
69
|
+
"vitest": "^4.1.11"
|
|
70
|
+
},
|
|
71
|
+
"homepage": "https://github.com/vllnt/convex-consent#readme",
|
|
72
|
+
"bugs": {
|
|
73
|
+
"url": "https://github.com/vllnt/convex-consent/issues"
|
|
74
|
+
},
|
|
75
|
+
"author": {
|
|
76
|
+
"name": "bntvllnt",
|
|
77
|
+
"url": "https://bntvllnt.com"
|
|
78
|
+
},
|
|
79
|
+
"funding": {
|
|
80
|
+
"type": "github",
|
|
81
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
82
|
+
},
|
|
83
|
+
"repository": {
|
|
84
|
+
"type": "git",
|
|
85
|
+
"url": "https://github.com/vllnt/convex-consent"
|
|
86
|
+
},
|
|
87
|
+
"engines": {
|
|
88
|
+
"node": ">=18"
|
|
89
|
+
},
|
|
90
|
+
"publishConfig": {
|
|
91
|
+
"access": "public"
|
|
92
|
+
},
|
|
93
|
+
"sideEffects": false,
|
|
94
|
+
"keywords": [
|
|
95
|
+
"convex",
|
|
96
|
+
"convex-component",
|
|
97
|
+
"consent",
|
|
98
|
+
"gdpr",
|
|
99
|
+
"privacy"
|
|
100
|
+
]
|
|
101
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FunctionArgs,
|
|
3
|
+
FunctionReference,
|
|
4
|
+
FunctionReturnType,
|
|
5
|
+
PaginationOptions,
|
|
6
|
+
PaginationResult,
|
|
7
|
+
} from "convex/server";
|
|
8
|
+
import type {
|
|
9
|
+
ConsentCheck,
|
|
10
|
+
ConsentDecision,
|
|
11
|
+
ConsentEvent,
|
|
12
|
+
ConsentOptions,
|
|
13
|
+
ConsentState,
|
|
14
|
+
Parser,
|
|
15
|
+
RecordOptions,
|
|
16
|
+
} from "./types.js";
|
|
17
|
+
import { DEFAULT_PRUNE_BATCH, DEFAULT_VERSION } from "../shared.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The component's raw ledger event, before the client narrows opaque host proof.
|
|
21
|
+
* `proof` is `unknown` here; the {@link Consent} client runs the host validator
|
|
22
|
+
* over it at its typed boundary.
|
|
23
|
+
*/
|
|
24
|
+
type RawEvent = {
|
|
25
|
+
subjectRef: string;
|
|
26
|
+
purpose: string;
|
|
27
|
+
decision: ConsentDecision;
|
|
28
|
+
version: string;
|
|
29
|
+
proof?: unknown;
|
|
30
|
+
at: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The consent component's function references, as exposed on the host via
|
|
35
|
+
* `components.consent`. The host's stored `proof` is opaque here (`unknown`); the
|
|
36
|
+
* {@link Consent} client narrows it at its own typed boundary.
|
|
37
|
+
*/
|
|
38
|
+
export interface ConsentComponent {
|
|
39
|
+
mutations: {
|
|
40
|
+
record: FunctionReference<
|
|
41
|
+
"mutation",
|
|
42
|
+
"internal",
|
|
43
|
+
{
|
|
44
|
+
subjectRef: string;
|
|
45
|
+
purpose: string;
|
|
46
|
+
decision: ConsentDecision;
|
|
47
|
+
version: string;
|
|
48
|
+
proof?: unknown;
|
|
49
|
+
},
|
|
50
|
+
{ at: number }
|
|
51
|
+
>;
|
|
52
|
+
withdraw: FunctionReference<
|
|
53
|
+
"mutation",
|
|
54
|
+
"internal",
|
|
55
|
+
{ subjectRef: string; purpose: string; proof?: unknown },
|
|
56
|
+
{ at: number }
|
|
57
|
+
>;
|
|
58
|
+
prune: FunctionReference<
|
|
59
|
+
"mutation",
|
|
60
|
+
"internal",
|
|
61
|
+
{ before?: number; batch: number },
|
|
62
|
+
number
|
|
63
|
+
>;
|
|
64
|
+
};
|
|
65
|
+
queries: {
|
|
66
|
+
check: FunctionReference<
|
|
67
|
+
"query",
|
|
68
|
+
"internal",
|
|
69
|
+
{ subjectRef: string; purpose: string; requiredVersion?: string },
|
|
70
|
+
ConsentCheck
|
|
71
|
+
>;
|
|
72
|
+
getState: FunctionReference<
|
|
73
|
+
"query",
|
|
74
|
+
"internal",
|
|
75
|
+
{ subjectRef: string; purpose: string },
|
|
76
|
+
ConsentState | null
|
|
77
|
+
>;
|
|
78
|
+
getStatesForSubject: FunctionReference<
|
|
79
|
+
"query",
|
|
80
|
+
"internal",
|
|
81
|
+
{ subjectRef: string },
|
|
82
|
+
ConsentState[]
|
|
83
|
+
>;
|
|
84
|
+
history: FunctionReference<
|
|
85
|
+
"query",
|
|
86
|
+
"internal",
|
|
87
|
+
{ subjectRef: string; purpose: string; paginationOpts: PaginationOptions },
|
|
88
|
+
PaginationResult<RawEvent>
|
|
89
|
+
>;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface RunQueryCtx {
|
|
94
|
+
runQuery<Q extends FunctionReference<"query", "internal">>(
|
|
95
|
+
reference: Q,
|
|
96
|
+
args: FunctionArgs<Q>,
|
|
97
|
+
): Promise<FunctionReturnType<Q>>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface RunMutationCtx {
|
|
101
|
+
runMutation<M extends FunctionReference<"mutation", "internal">>(
|
|
102
|
+
reference: M,
|
|
103
|
+
args: FunctionArgs<M>,
|
|
104
|
+
): Promise<FunctionReturnType<M>>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Consumer-facing client for the consent ledger (GDPR Art. 6/7). The host records
|
|
109
|
+
* a subject's decision for a purpose with `record` (or revokes one with
|
|
110
|
+
* `withdraw`) — each call appends an immutable ledger event (the legal proof) and
|
|
111
|
+
* updates the O(1) current-state projection. Before processing, the host calls
|
|
112
|
+
* `check` to gate: "may I process `<purpose>` for `<subject>`?". `subjectRef` and
|
|
113
|
+
* `purpose` are opaque host strings; the host owns meaning and auth. Pass
|
|
114
|
+
* `proofValidator` to narrow the opaque stored `proof` to `TProof` at the
|
|
115
|
+
* boundary — there is no unchecked cast.
|
|
116
|
+
*
|
|
117
|
+
* @typeParam TProof - The host's consent-proof type (defaults to `unknown`).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const consent = new Consent(components.consent, {
|
|
122
|
+
* defaultVersion: "2024-policy",
|
|
123
|
+
* proofValidator: v.object({ policyHash: v.string() }).parse,
|
|
124
|
+
* });
|
|
125
|
+
* await consent.record(ctx, "user_42", "analytics", "granted", {
|
|
126
|
+
* proof: { policyHash },
|
|
127
|
+
* });
|
|
128
|
+
* const gate = await consent.check(ctx, "user_42", "analytics", "2024-policy");
|
|
129
|
+
* if (gate.granted) {
|
|
130
|
+
* // process the personal data
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export class Consent<TProof = unknown> {
|
|
135
|
+
private readonly defaultVersion: string;
|
|
136
|
+
private readonly proofValidator: Parser<TProof> | undefined;
|
|
137
|
+
|
|
138
|
+
constructor(
|
|
139
|
+
private readonly component: ConsentComponent,
|
|
140
|
+
options: ConsentOptions<TProof> = {},
|
|
141
|
+
) {
|
|
142
|
+
this.defaultVersion = options.defaultVersion ?? DEFAULT_VERSION;
|
|
143
|
+
this.proofValidator = options.proofValidator;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Narrow an opaque value through the host proof parser; pass `undefined` and an unset parser through. */
|
|
147
|
+
private parseProof(value: unknown): TProof | undefined {
|
|
148
|
+
if (value === undefined) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
if (this.proofValidator === undefined) {
|
|
152
|
+
return value as TProof;
|
|
153
|
+
}
|
|
154
|
+
return this.proofValidator(value);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Project a raw component event into the typed, validated client event. */
|
|
158
|
+
private event(raw: RawEvent): ConsentEvent<TProof> {
|
|
159
|
+
return {
|
|
160
|
+
subjectRef: raw.subjectRef,
|
|
161
|
+
purpose: raw.purpose,
|
|
162
|
+
decision: raw.decision,
|
|
163
|
+
version: raw.version,
|
|
164
|
+
proof: this.parseProof(raw.proof),
|
|
165
|
+
at: raw.at,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Record a consent decision for `(subjectRef, purpose)`. Appends an immutable
|
|
171
|
+
* ledger event and updates the current-state projection. `opts.version` defaults
|
|
172
|
+
* to the client's `defaultVersion`; `opts.proof` is opaque host evidence
|
|
173
|
+
* validated against `proofValidator` before storage. Returns the server
|
|
174
|
+
* timestamp stamped on the event.
|
|
175
|
+
*/
|
|
176
|
+
record(
|
|
177
|
+
ctx: RunMutationCtx,
|
|
178
|
+
subjectRef: string,
|
|
179
|
+
purpose: string,
|
|
180
|
+
decision: ConsentDecision,
|
|
181
|
+
opts: RecordOptions<TProof> = {},
|
|
182
|
+
): Promise<{ at: number }> {
|
|
183
|
+
return ctx.runMutation(this.component.mutations.record, {
|
|
184
|
+
subjectRef,
|
|
185
|
+
purpose,
|
|
186
|
+
decision,
|
|
187
|
+
version: opts.version ?? this.defaultVersion,
|
|
188
|
+
proof: opts.proof === undefined ? undefined : this.parseProof(opts.proof),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Withdraw a previously-granted consent (GDPR Art. 7(3)). Appends a `withdrawn`
|
|
194
|
+
* event and updates the projection, but only when the subject currently holds a
|
|
195
|
+
* `granted` decision — the component throws a coded `ConvexError` (`NO_CONSENT`
|
|
196
|
+
* / `NOT_GRANTED`) otherwise so the audit trail stays meaningful.
|
|
197
|
+
*/
|
|
198
|
+
withdraw(
|
|
199
|
+
ctx: RunMutationCtx,
|
|
200
|
+
subjectRef: string,
|
|
201
|
+
purpose: string,
|
|
202
|
+
proof?: TProof,
|
|
203
|
+
): Promise<{ at: number }> {
|
|
204
|
+
return ctx.runMutation(this.component.mutations.withdraw, {
|
|
205
|
+
subjectRef,
|
|
206
|
+
purpose,
|
|
207
|
+
proof: proof === undefined ? undefined : this.parseProof(proof),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The runtime gate: may the host process `purpose` for `subjectRef`? Pass
|
|
213
|
+
* `requiredVersion` to also require the consent be for the current policy
|
|
214
|
+
* version — when the subject granted an older version the result is `stale`
|
|
215
|
+
* (and not `granted`) so the host re-prompts.
|
|
216
|
+
*/
|
|
217
|
+
check(
|
|
218
|
+
ctx: RunQueryCtx,
|
|
219
|
+
subjectRef: string,
|
|
220
|
+
purpose: string,
|
|
221
|
+
requiredVersion?: string,
|
|
222
|
+
): Promise<ConsentCheck> {
|
|
223
|
+
return ctx.runQuery(this.component.queries.check, {
|
|
224
|
+
subjectRef,
|
|
225
|
+
purpose,
|
|
226
|
+
requiredVersion,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The raw current decision for one `(subjectRef, purpose)` pair, or `null`. */
|
|
231
|
+
getState(
|
|
232
|
+
ctx: RunQueryCtx,
|
|
233
|
+
subjectRef: string,
|
|
234
|
+
purpose: string,
|
|
235
|
+
): Promise<ConsentState | null> {
|
|
236
|
+
return ctx.runQuery(this.component.queries.getState, {
|
|
237
|
+
subjectRef,
|
|
238
|
+
purpose,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Every current decision held by one subject, across all purposes. */
|
|
243
|
+
getStatesForSubject(
|
|
244
|
+
ctx: RunQueryCtx,
|
|
245
|
+
subjectRef: string,
|
|
246
|
+
): Promise<ConsentState[]> {
|
|
247
|
+
return ctx.runQuery(this.component.queries.getStatesForSubject, {
|
|
248
|
+
subjectRef,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Page the immutable consent ledger for one `(subjectRef, purpose)` pair,
|
|
254
|
+
* newest-first. Returns the standard Convex pagination envelope with each event
|
|
255
|
+
* narrowed to the typed view — the full proof trail for an audit or a data
|
|
256
|
+
* subject request.
|
|
257
|
+
*/
|
|
258
|
+
async history(
|
|
259
|
+
ctx: RunQueryCtx,
|
|
260
|
+
subjectRef: string,
|
|
261
|
+
purpose: string,
|
|
262
|
+
paginationOpts: PaginationOptions,
|
|
263
|
+
): Promise<PaginationResult<ConsentEvent<TProof>>> {
|
|
264
|
+
const result = await ctx.runQuery(this.component.queries.history, {
|
|
265
|
+
subjectRef,
|
|
266
|
+
purpose,
|
|
267
|
+
paginationOpts,
|
|
268
|
+
});
|
|
269
|
+
return { ...result, page: result.page.map((raw) => this.event(raw)) };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Delete superseded ledger events whose `at < before` in bounded batches,
|
|
274
|
+
* oldest first. `before` defaults to the server clock; `batch` caps each pass
|
|
275
|
+
* and the sweep self-reschedules until the tail is clean. The current-state
|
|
276
|
+
* projection is never pruned. Returns the count removed in the first pass. The
|
|
277
|
+
* built-in daily cron drives this automatically.
|
|
278
|
+
*/
|
|
279
|
+
prune(
|
|
280
|
+
ctx: RunMutationCtx,
|
|
281
|
+
opts: { before?: number; batch?: number } = {},
|
|
282
|
+
): Promise<number> {
|
|
283
|
+
return ctx.runMutation(this.component.mutations.prune, {
|
|
284
|
+
before: opts.before,
|
|
285
|
+
batch: opts.batch ?? DEFAULT_PRUNE_BATCH,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export type {
|
|
291
|
+
ConsentCheck,
|
|
292
|
+
ConsentDecision,
|
|
293
|
+
ConsentEvent,
|
|
294
|
+
ConsentOptions,
|
|
295
|
+
ConsentState,
|
|
296
|
+
Parser,
|
|
297
|
+
RecordOptions,
|
|
298
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Public TypeScript surface for the consent client. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The three consent decisions a subject can hold for a purpose. `granted` and
|
|
5
|
+
* `denied` are explicit answers to the prompt; `withdrawn` revokes a prior grant.
|
|
6
|
+
*/
|
|
7
|
+
export type ConsentDecision = "granted" | "denied" | "withdrawn";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Validates and narrows an opaque stored value to a host type `T` at the client
|
|
11
|
+
* boundary. Receives the raw value the component returned (`unknown`) and MUST
|
|
12
|
+
* return a typed `T` or throw. A `convex/values` validator's `.parse` (or a Zod
|
|
13
|
+
* `.parse`) fits directly; omit it to keep the value unvalidated.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam T - The host's stored type (the consent `proof`).
|
|
16
|
+
*/
|
|
17
|
+
export type Parser<T> = (value: unknown) => T;
|
|
18
|
+
|
|
19
|
+
/** One immutable row in the consent ledger — a recorded decision plus its proof. */
|
|
20
|
+
export interface ConsentEvent<TProof = unknown> {
|
|
21
|
+
/** The opaque host-supplied subject identifier. */
|
|
22
|
+
subjectRef: string;
|
|
23
|
+
/** The opaque host-supplied purpose key. */
|
|
24
|
+
purpose: string;
|
|
25
|
+
/** The recorded decision. */
|
|
26
|
+
decision: ConsentDecision;
|
|
27
|
+
/** The policy version the subject consented to. */
|
|
28
|
+
version: string;
|
|
29
|
+
/** The opaque host proof recorded with the decision (narrowed if a `proofValidator` is set). */
|
|
30
|
+
proof?: TProof;
|
|
31
|
+
/** Absolute ms timestamp the decision was recorded (server clock). */
|
|
32
|
+
at: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The current decision for one `(subjectRef, purpose)` pair. */
|
|
36
|
+
export interface ConsentState {
|
|
37
|
+
/** The opaque host-supplied subject identifier. */
|
|
38
|
+
subjectRef: string;
|
|
39
|
+
/** The opaque host-supplied purpose key. */
|
|
40
|
+
purpose: string;
|
|
41
|
+
/** The latest decision. */
|
|
42
|
+
decision: ConsentDecision;
|
|
43
|
+
/** The policy version of the latest decision. */
|
|
44
|
+
version: string;
|
|
45
|
+
/** Absolute ms timestamp of the latest decision (server clock). */
|
|
46
|
+
at: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The answer to "may I process `purpose` for `subjectRef`?" returned by `check`. */
|
|
50
|
+
export interface ConsentCheck {
|
|
51
|
+
/** The gate: `true` only when the latest decision is `granted` and the version matches any requirement. */
|
|
52
|
+
granted: boolean;
|
|
53
|
+
/** `true` when a `granted` decision is for an older version than the one now required — re-prompt. */
|
|
54
|
+
stale: boolean;
|
|
55
|
+
/** The current decision, or `null` if none was ever recorded. */
|
|
56
|
+
decision: ConsentDecision | null;
|
|
57
|
+
/** The current version, or `null` if none was ever recorded. */
|
|
58
|
+
version: string | null;
|
|
59
|
+
/** The current decision timestamp, or `null` if none was ever recorded. */
|
|
60
|
+
at: number | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Per-call options for {@link Consent.record}. */
|
|
64
|
+
export interface RecordOptions<TProof> {
|
|
65
|
+
/** The policy version the subject consented to (defaults to the client's `defaultVersion`). */
|
|
66
|
+
version?: string;
|
|
67
|
+
/** The opaque host proof to store (validated against `proofValidator`). */
|
|
68
|
+
proof?: TProof;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Construction options for the {@link Consent} client. */
|
|
72
|
+
export interface ConsentOptions<TProof> {
|
|
73
|
+
/**
|
|
74
|
+
* The policy version stamped on a decision when `record` is called without an
|
|
75
|
+
* explicit `version`. Lets a host bump its policy version in one place. Defaults
|
|
76
|
+
* to `"1"`.
|
|
77
|
+
*/
|
|
78
|
+
defaultVersion?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Validates/narrows a stored `proof` to `TProof` at the boundary — applied to
|
|
81
|
+
* the `proof` passed into `record`/`withdraw` (before storage) and the `proof`
|
|
82
|
+
* returned by `history`. Throws on a mismatch. Omit to leave proof unvalidated.
|
|
83
|
+
*/
|
|
84
|
+
proofValidator?: Parser<TProof>;
|
|
85
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as crons from "../crons.js";
|
|
12
|
+
import type * as mutations from "../mutations.js";
|
|
13
|
+
import type * as queries from "../queries.js";
|
|
14
|
+
import type * as validators from "../validators.js";
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
ApiFromModules,
|
|
18
|
+
FilterApi,
|
|
19
|
+
FunctionReference,
|
|
20
|
+
} from "convex/server";
|
|
21
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
22
|
+
|
|
23
|
+
const fullApi: ApiFromModules<{
|
|
24
|
+
crons: typeof crons;
|
|
25
|
+
mutations: typeof mutations;
|
|
26
|
+
queries: typeof queries;
|
|
27
|
+
validators: typeof validators;
|
|
28
|
+
}> = anyApi as any;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
32
|
+
*
|
|
33
|
+
* Usage:
|
|
34
|
+
* ```js
|
|
35
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const api: FilterApi<
|
|
39
|
+
typeof fullApi,
|
|
40
|
+
FunctionReference<any, "public">
|
|
41
|
+
> = anyApi as any;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
45
|
+
*
|
|
46
|
+
* Usage:
|
|
47
|
+
* ```js
|
|
48
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export const internal: FilterApi<
|
|
52
|
+
typeof fullApi,
|
|
53
|
+
FunctionReference<any, "internal">
|
|
54
|
+
> = anyApi as any;
|
|
55
|
+
|
|
56
|
+
export const components = componentsGeneric() as unknown as {};
|