@kitcms/plugin-entitlements 0.0.0-stage → 0.1.1
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 +63 -2
- package/package.json +38 -3
- package/src/index.ts +6 -0
- package/src/parse.ts +77 -0
- package/src/plugin.ts +234 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kitcms contributors
|
|
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
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kitcms/plugin-entitlements
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turns a plan (set by your platform's control plane) into add-on gates and usage limits for a kitcms app.
|
|
4
|
+
The plan is read from a Worker variable, never from D1, and nothing in kitcms can edit it.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
// kitcms.config.ts
|
|
10
|
+
import { entitlements } from '@kitcms/plugin-entitlements';
|
|
11
|
+
|
|
12
|
+
export default defineConfig({
|
|
13
|
+
plugins: [
|
|
14
|
+
entitlements({
|
|
15
|
+
addons: { booking: { label: 'Booking', description: 'Table bookings' } },
|
|
16
|
+
manageUrl: 'https://platform.example/plan',
|
|
17
|
+
}),
|
|
18
|
+
],
|
|
19
|
+
// ...
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
// wrangler.jsonc (or set as a secret by the control plane)
|
|
25
|
+
{ "vars": { "KITCMS_ENTITLEMENTS": "{\"plan\":\"starter\",\"addons\":[\"booking\"],\"limits\":{\"documents\":{\"pages\":50},\"uploadBytes\":5000000}}" } }
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## JSON shape
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{ "plan": "starter", "addons": ["booking"], "limits": { "documents": { "pages": 50 }, "uploadBytes": 5000000 } }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- `addons`: names matching `/^[a-z][a-z0-9_-]{0,63}$/`.
|
|
35
|
+
- `limits` is optional. Limits are non-negative integers.
|
|
36
|
+
- Options: `variable` (default `KITCMS_ENTITLEMENTS`), `read` (override how the value is read, for tests), `addons`, `manageUrl`.
|
|
37
|
+
|
|
38
|
+
## Gating features
|
|
39
|
+
|
|
40
|
+
Set `entitlement: '<addon>'` on a collection, global or plugin. When the add-on is off, core denies every operation
|
|
41
|
+
(except `overrideAccess`), hides it in the admin and 404s plugin endpoints.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
collection({ slug: 'bookings', entitlement: 'booking', fields: { /* ... */ } });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Limits
|
|
48
|
+
|
|
49
|
+
- `limits.documents[slug]`: creating a document is refused (403) once the collection holds that many.
|
|
50
|
+
- `limits.uploadBytes`: an upload collection refuses files larger than this.
|
|
51
|
+
- The auth collection is never limited.
|
|
52
|
+
- Limits are `beforeChange` hooks placed before your own. They apply to every create, including `overrideAccess` ones.
|
|
53
|
+
A control-plane migration or seed can skip them with `{ context: { kitcmsEntitlementsBypass: true } }`.
|
|
54
|
+
|
|
55
|
+
## Fail closed
|
|
56
|
+
|
|
57
|
+
A missing variable, invalid JSON, one bad add-on name or a negative limit makes the whole value invalid: plan `unknown`,
|
|
58
|
+
no add-ons, no limits. Gated features turn off and ungated ones keep working. An error while reading counts as invalid.
|
|
59
|
+
The value is parsed once per isolate and re-parsed when it changes.
|
|
60
|
+
|
|
61
|
+
## Admin
|
|
62
|
+
|
|
63
|
+
The plugin adds a read-only "Plan & add-ons" panel (admins only): plan, add-ons (on or locked), document usage and a
|
|
64
|
+
"Manage plan" link. It adds no collections, globals or endpoints.
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kitcms/plugin-entitlements",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/Norden-Tenzin/kitcms.git",
|
|
8
|
+
"directory": "packages/plugin-entitlements"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://kitcms.dev",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/Norden-Tenzin/kitcms/issues"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./src/index.ts",
|
|
19
|
+
"default": "./src/index.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@kitcms/core": "^0.1.1",
|
|
24
|
+
"svelte": "^5"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@kitcms/core": "0.1.1",
|
|
28
|
+
"@sveltejs/kit": "^3.2.0",
|
|
29
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
30
|
+
"svelte": "^5.57.3",
|
|
31
|
+
"svelte-check": "^4.7.8"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"src",
|
|
35
|
+
"!src/**/*.test.ts"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"typecheck": "svelte-check --tsconfig ./tsconfig.json",
|
|
39
|
+
"test": "vitest run"
|
|
40
|
+
}
|
|
6
41
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @kitcms/plugin-entitlements (plan §17.2, Decision 34). Reads the plan from a Worker var set by the
|
|
2
|
+
// platform's control plane. There is deliberately no write path anywhere in this package.
|
|
3
|
+
export { entitlements } from './plugin';
|
|
4
|
+
export type { EntitlementsOptions } from './plugin';
|
|
5
|
+
export { parseEntitlements } from './parse';
|
|
6
|
+
export type { Entitlements, EntitlementLimits } from './parse';
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Pure parsing of the entitlements JSON. Strict and fail-closed: anything malformed yields the
|
|
2
|
+
// "unknown" plan with no add-ons and no limits, so gated features stay off and ungated ones keep working.
|
|
3
|
+
|
|
4
|
+
export interface EntitlementLimits {
|
|
5
|
+
/** Max documents per collection slug. */
|
|
6
|
+
documents: Record<string, number>;
|
|
7
|
+
/** Max size of one uploaded file in bytes, or `null` for no limit. */
|
|
8
|
+
uploadBytes: number | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Entitlements {
|
|
12
|
+
plan: string;
|
|
13
|
+
addons: string[];
|
|
14
|
+
limits: EntitlementLimits;
|
|
15
|
+
/** `false` when the value was missing or malformed (everything gated is off). */
|
|
16
|
+
valid: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ADDON = /^[a-z][a-z0-9_-]{0,63}$/;
|
|
20
|
+
const SLUG = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
|
21
|
+
|
|
22
|
+
const invalid = (): Entitlements => ({
|
|
23
|
+
plan: 'unknown',
|
|
24
|
+
addons: [],
|
|
25
|
+
limits: { documents: {}, uploadBytes: null },
|
|
26
|
+
valid: false,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
|
30
|
+
typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
31
|
+
|
|
32
|
+
const isLimit = (v: unknown): v is number =>
|
|
33
|
+
typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parses the raw variable value (a JSON string or an already-parsed object).
|
|
37
|
+
* Strict: ONE bad add-on name, limit or field makes the whole value invalid (a half-trusted
|
|
38
|
+
* payload is not trusted at all), so a typo can never silently turn something on.
|
|
39
|
+
*/
|
|
40
|
+
export function parseEntitlements(raw: unknown): Entitlements {
|
|
41
|
+
let value = raw;
|
|
42
|
+
if (typeof raw === 'string') {
|
|
43
|
+
try {
|
|
44
|
+
value = JSON.parse(raw);
|
|
45
|
+
} catch {
|
|
46
|
+
return invalid();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!isRecord(value)) return invalid();
|
|
50
|
+
|
|
51
|
+
const { plan, addons, limits } = value;
|
|
52
|
+
if (typeof plan !== 'string' || plan.trim() === '' || plan.length > 64) return invalid();
|
|
53
|
+
if (!Array.isArray(addons) || addons.length > 256) return invalid();
|
|
54
|
+
const names: string[] = [];
|
|
55
|
+
for (const a of addons) {
|
|
56
|
+
if (typeof a !== 'string' || !ADDON.test(a)) return invalid();
|
|
57
|
+
if (!names.includes(a)) names.push(a);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const out: EntitlementLimits = { documents: {}, uploadBytes: null };
|
|
61
|
+
if (limits !== undefined) {
|
|
62
|
+
if (!isRecord(limits)) return invalid();
|
|
63
|
+
const { documents, uploadBytes } = limits;
|
|
64
|
+
if (documents !== undefined) {
|
|
65
|
+
if (!isRecord(documents)) return invalid();
|
|
66
|
+
for (const [slug, n] of Object.entries(documents)) {
|
|
67
|
+
if (!SLUG.test(slug) || !isLimit(n)) return invalid();
|
|
68
|
+
out.documents[slug] = n;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (uploadBytes !== undefined && uploadBytes !== null) {
|
|
72
|
+
if (!isLimit(uploadBytes)) return invalid();
|
|
73
|
+
out.uploadBytes = uploadBytes;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { plan, addons: names, limits: out, valid: true };
|
|
77
|
+
}
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CollectionDef,
|
|
3
|
+
Config,
|
|
4
|
+
EntitlementsProvider,
|
|
5
|
+
KitcmsPlugin,
|
|
6
|
+
PanelBlock,
|
|
7
|
+
} from '@kitcms/core';
|
|
8
|
+
import { ForbiddenError } from '@kitcms/core';
|
|
9
|
+
import { parseEntitlements, type Entitlements } from './parse';
|
|
10
|
+
|
|
11
|
+
export interface EntitlementsOptions {
|
|
12
|
+
/** Worker var/secret holding the JSON. Default 'KITCMS_ENTITLEMENTS'. */
|
|
13
|
+
variable?: string;
|
|
14
|
+
/** Override how the raw value is read (tests). Default: lazy `await import('cloudflare:workers')` -> env[variable]. */
|
|
15
|
+
read?: () => unknown | Promise<unknown>;
|
|
16
|
+
/** Known add-ons, so the panel can show locked ones too: `{ booking: { label: 'Booking', description? } }`. */
|
|
17
|
+
addons?: Record<string, { label: string; description?: string }>;
|
|
18
|
+
/** Shown as a "Manage plan" link in the panel (https only; core drops anything else). */
|
|
19
|
+
manageUrl?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULT_VARIABLE = 'KITCMS_ENTITLEMENTS';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* `req.context.kitcmsEntitlementsBypass = true` skips the document and upload limits for that
|
|
26
|
+
* operation. For control-plane migrations and seeds that must not be refused. Hooks can't tell a
|
|
27
|
+
* system call from a user call, so this explicit flag is the only escape hatch.
|
|
28
|
+
*/
|
|
29
|
+
export const BYPASS_CONTEXT_KEY = 'kitcmsEntitlementsBypass';
|
|
30
|
+
|
|
31
|
+
const fail = (msg: string): never => {
|
|
32
|
+
throw new Error(`entitlements: ${msg}`);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Core's error: the admin, REST, RPC and plugin endpoints map it to 403 with this message.
|
|
36
|
+
function limitError(message: string): Error {
|
|
37
|
+
return new ForbiddenError(message);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Source {
|
|
41
|
+
/** Parsed value for the current raw value (re-read on every call, parse cached per raw string). */
|
|
42
|
+
get(): Promise<Entitlements>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function createSource(options: EntitlementsOptions): Source {
|
|
46
|
+
const variable = options.variable ?? DEFAULT_VARIABLE;
|
|
47
|
+
const read =
|
|
48
|
+
options.read ??
|
|
49
|
+
(async () => {
|
|
50
|
+
// Lazy: this module must also load where `cloudflare:workers` doesn't exist (tests, build tooling).
|
|
51
|
+
const { env } = await import('cloudflare:workers');
|
|
52
|
+
return (env as unknown as Record<string, unknown>)[variable];
|
|
53
|
+
});
|
|
54
|
+
// Cached per isolate, keyed by the raw string so a changed var is re-parsed.
|
|
55
|
+
let cachedKey: string | undefined;
|
|
56
|
+
let cached: Entitlements | undefined;
|
|
57
|
+
return {
|
|
58
|
+
async get() {
|
|
59
|
+
let raw: unknown;
|
|
60
|
+
try {
|
|
61
|
+
raw = await read();
|
|
62
|
+
} catch {
|
|
63
|
+
return parseEntitlements(undefined); // fail closed
|
|
64
|
+
}
|
|
65
|
+
const key = typeof raw === 'string' ? raw : raw === undefined ? '' : safeStringify(raw);
|
|
66
|
+
if (cached && cachedKey === key) return cached;
|
|
67
|
+
cached = parseEntitlements(raw);
|
|
68
|
+
cachedKey = key;
|
|
69
|
+
return cached;
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function safeStringify(v: unknown): string {
|
|
75
|
+
try {
|
|
76
|
+
return JSON.stringify(v) ?? '';
|
|
77
|
+
} catch {
|
|
78
|
+
return '';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Builds the provider core calls for every gated check. Never throws. */
|
|
83
|
+
export function createProvider(options: EntitlementsOptions = {}): EntitlementsProvider & Source {
|
|
84
|
+
const source = createSource(options);
|
|
85
|
+
return {
|
|
86
|
+
get: () => source.get(),
|
|
87
|
+
async has(addon: string): Promise<boolean> {
|
|
88
|
+
try {
|
|
89
|
+
return (await source.get()).addons.includes(addon);
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function formatBytes(n: number): string {
|
|
98
|
+
if (n >= 1_000_000) return `${+(n / 1_000_000).toFixed(1)} MB`;
|
|
99
|
+
if (n >= 1_000) return `${+(n / 1_000).toFixed(1)} KB`;
|
|
100
|
+
return `${n} bytes`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type AnyHook = (args: never) => unknown;
|
|
104
|
+
const toArray = <T>(h: T | T[] | undefined): T[] =>
|
|
105
|
+
h === undefined ? [] : Array.isArray(h) ? h : [h];
|
|
106
|
+
|
|
107
|
+
/** Adds the add-on limit hooks in front of the collection's own `beforeChange` hooks. */
|
|
108
|
+
function withLimits(col: CollectionDef, source: Source): CollectionDef {
|
|
109
|
+
if (col.auth) return col; // never throttle the auth collection (lock-out risk)
|
|
110
|
+
|
|
111
|
+
const hook = async (args: {
|
|
112
|
+
req: {
|
|
113
|
+
context: Record<string, unknown>;
|
|
114
|
+
cms: { count: (slug: never, a: unknown) => Promise<number> };
|
|
115
|
+
};
|
|
116
|
+
operation: string;
|
|
117
|
+
data: Record<string, unknown>;
|
|
118
|
+
}) => {
|
|
119
|
+
if (args.req.context[BYPASS_CONTEXT_KEY] === true) return;
|
|
120
|
+
const { limits } = await source.get();
|
|
121
|
+
if (args.operation === 'create') {
|
|
122
|
+
const max = limits.documents[col.slug];
|
|
123
|
+
if (max !== undefined) {
|
|
124
|
+
const used = await args.req.cms.count(col.slug as never, {
|
|
125
|
+
overrideAccess: true,
|
|
126
|
+
req: args.req,
|
|
127
|
+
});
|
|
128
|
+
if (used >= max)
|
|
129
|
+
throw limitError(`Your plan allows ${max} "${col.slug}" documents. Upgrade to add more.`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (col.upload && limits.uploadBytes !== null) {
|
|
133
|
+
const size = args.data.filesize;
|
|
134
|
+
if (typeof size === 'number' && size > limits.uploadBytes) {
|
|
135
|
+
throw limitError(`Your plan allows files up to ${limits.uploadBytes} bytes.`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const hooks = col.hooks ?? {};
|
|
141
|
+
return {
|
|
142
|
+
...col,
|
|
143
|
+
hooks: {
|
|
144
|
+
...hooks,
|
|
145
|
+
beforeChange: [
|
|
146
|
+
hook as unknown as AnyHook,
|
|
147
|
+
...toArray(hooks.beforeChange as AnyHook | AnyHook[]),
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
} as unknown as CollectionDef;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Entitlements: reads the plan JSON from a Worker var, turns it into `config.entitlements`
|
|
155
|
+
* (add-on gates, enforced by core) and enforces document and upload limits. Read-only: it adds no
|
|
156
|
+
* collections, globals, endpoints or writes, so nothing in kitcms can edit the plan.
|
|
157
|
+
*/
|
|
158
|
+
export function entitlements(options: EntitlementsOptions = {}): KitcmsPlugin {
|
|
159
|
+
const provider = createProvider(options);
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
name: 'entitlements',
|
|
163
|
+
config(config: Config): Config {
|
|
164
|
+
if (config.entitlements !== undefined) {
|
|
165
|
+
fail(
|
|
166
|
+
'`config.entitlements` is already set. Only one entitlements provider may be configured.',
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const collections = (config.collections ?? []) as readonly CollectionDef[];
|
|
170
|
+
return {
|
|
171
|
+
...config,
|
|
172
|
+
entitlements: provider,
|
|
173
|
+
collections: collections.map((c) => withLimits(c, provider)),
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
admin: {
|
|
177
|
+
panels: [
|
|
178
|
+
{
|
|
179
|
+
id: 'plan',
|
|
180
|
+
label: 'Plan & add-ons',
|
|
181
|
+
async load({ cms }) {
|
|
182
|
+
const ent = await provider.get();
|
|
183
|
+
const blocks: PanelBlock[] = [
|
|
184
|
+
{ type: 'heading', text: ent.valid ? `Plan: ${ent.plan}` : 'Plan unknown' },
|
|
185
|
+
];
|
|
186
|
+
if (!ent.valid) {
|
|
187
|
+
blocks.push({
|
|
188
|
+
type: 'text',
|
|
189
|
+
tone: 'warning',
|
|
190
|
+
text: 'No plan information found; paid add-ons are off.',
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const known = options.addons ?? {};
|
|
195
|
+
const ids = [...new Set([...Object.keys(known), ...ent.addons])];
|
|
196
|
+
if (ids.length) {
|
|
197
|
+
blocks.push({ type: 'heading', text: 'Add-ons' });
|
|
198
|
+
blocks.push({
|
|
199
|
+
type: 'list',
|
|
200
|
+
items: ids.map((id) => ({
|
|
201
|
+
label: known[id]?.label ?? id,
|
|
202
|
+
...(known[id]?.description ? { description: known[id].description } : {}),
|
|
203
|
+
status: ent.addons.includes(id) ? ('on' as const) : ('locked' as const),
|
|
204
|
+
})),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const docs = Object.entries(ent.limits.documents);
|
|
209
|
+
if (docs.length || ent.limits.uploadBytes !== null) {
|
|
210
|
+
blocks.push({ type: 'heading', text: 'Limits' });
|
|
211
|
+
for (const [slug, limit] of docs) {
|
|
212
|
+
const used = await cms
|
|
213
|
+
.count(slug as never, { overrideAccess: true } as never)
|
|
214
|
+
.catch(() => 0);
|
|
215
|
+
blocks.push({ type: 'meter', label: slug, used, limit, unit: 'documents' });
|
|
216
|
+
}
|
|
217
|
+
if (ent.limits.uploadBytes !== null) {
|
|
218
|
+
// A per-file cap, not a quota, so there's no usage to meter.
|
|
219
|
+
blocks.push({
|
|
220
|
+
type: 'keyValue',
|
|
221
|
+
items: [{ label: 'Largest upload', value: formatBytes(ent.limits.uploadBytes) }],
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (options.manageUrl)
|
|
227
|
+
blocks.push({ type: 'link', label: 'Manage plan', href: options.manageUrl });
|
|
228
|
+
return { blocks };
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|