@usebecky/extension 0.0.0-stage → 0.2.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/README.md +223 -2
- package/dist/accounts.d.ts +85 -0
- package/dist/accounts.js +149 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +9 -0
- package/dist/cli/run.d.ts +25 -0
- package/dist/cli/run.js +330 -0
- package/dist/cli/skill.d.ts +16 -0
- package/dist/cli/skill.js +106 -0
- package/dist/crypto.d.ts +12 -0
- package/dist/crypto.js +56 -0
- package/dist/dev/pages.d.ts +155 -0
- package/dist/dev/pages.js +243 -0
- package/dist/dev/plans.d.ts +45 -0
- package/dist/dev/plans.js +135 -0
- package/dist/dev/samples.d.ts +61 -0
- package/dist/dev/samples.js +191 -0
- package/dist/dev/server.d.ts +120 -0
- package/dist/dev/server.js +1657 -0
- package/dist/dev/tagging.d.ts +16 -0
- package/dist/dev/tagging.js +81 -0
- package/dist/dev/ui-render.d.ts +25 -0
- package/dist/dev/ui-render.js +256 -0
- package/dist/dev/wordmark.d.ts +1 -0
- package/dist/dev/wordmark.js +2 -0
- package/dist/errors.d.ts +98 -0
- package/dist/errors.js +110 -0
- package/dist/events.d.ts +92 -0
- package/dist/events.js +54 -0
- package/dist/extension.d.ts +214 -0
- package/dist/extension.js +497 -0
- package/dist/handlers.d.ts +39 -0
- package/dist/handlers.js +113 -0
- package/dist/identity.d.ts +99 -0
- package/dist/identity.js +134 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +17 -0
- package/dist/jwt.d.ts +62 -0
- package/dist/jwt.js +152 -0
- package/dist/manifest.d.ts +42 -0
- package/dist/manifest.js +785 -0
- package/dist/node.d.ts +10 -0
- package/dist/node.js +56 -0
- package/dist/pkce.d.ts +9 -0
- package/dist/pkce.js +16 -0
- package/dist/stores.d.ts +100 -0
- package/dist/stores.js +159 -0
- package/dist/types.d.ts +346 -0
- package/dist/types.js +70 -0
- package/dist/ui/handler.d.ts +66 -0
- package/dist/ui/handler.js +105 -0
- package/dist/ui/schema.d.ts +423 -0
- package/dist/ui/schema.js +666 -0
- package/dist/webhooks.d.ts +26 -0
- package/dist/webhooks.js +99 -0
- package/package.json +57 -4
- package/skill/usebecky-extension/SKILL.md +181 -0
- package/skill/usebecky-extension/references/accounts.md +125 -0
- package/skill/usebecky-extension/references/dev-server.md +102 -0
- package/skill/usebecky-extension/references/manifest.md +104 -0
- package/skill/usebecky-extension/references/oauth-and-tokens.md +179 -0
- package/skill/usebecky-extension/references/partner-api.md +87 -0
- package/skill/usebecky-extension/references/submit-and-pricing.md +31 -0
- package/skill/usebecky-extension/references/ui.md +172 -0
- package/skill/usebecky-extension/references/webhooks.md +97 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The keys of the settings, in usebecky's order. */
|
|
2
|
+
export declare const TAGGING_KEYS: readonly ["tagging_server_url", "measurement_id", "send_client_id"];
|
|
3
|
+
/** An invalid setting: 400 invalid_settings (with details.field for an unknown key). */
|
|
4
|
+
export declare class TaggingSettingsError extends Error {
|
|
5
|
+
hint?: string;
|
|
6
|
+
field?: string;
|
|
7
|
+
constructor(message: string, hint?: string, field?: string);
|
|
8
|
+
}
|
|
9
|
+
export declare function parseTaggingUrl(raw: unknown): string;
|
|
10
|
+
export declare function parseMeasurementId(raw: unknown): string;
|
|
11
|
+
/** The body of PUT /installation/settings → the settings usebecky stores. Throws TaggingSettingsError. */
|
|
12
|
+
export declare function checkTaggingSettings(body: unknown): {
|
|
13
|
+
tagging_server_url: string;
|
|
14
|
+
measurement_id: string;
|
|
15
|
+
send_client_id: boolean;
|
|
16
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// The server-side tagging settings of PUT /extensions/v1/installation/settings, checked as usebecky checks them
|
|
2
|
+
// (packages/server/src/modules/extensions/adapters: sgtm.ts parseTaggingUrl / parseMeasurementId, index.ts
|
|
3
|
+
// validateForwardingSettings), with the same messages. One difference: usebecky also looks the name up and refuses an
|
|
4
|
+
// address in a private network ("Couldn't reach the address."); the dev server checks the form only.
|
|
5
|
+
import { isIP } from 'node:net';
|
|
6
|
+
/** The keys of the settings, in usebecky's order. */
|
|
7
|
+
export const TAGGING_KEYS = ['tagging_server_url', 'measurement_id', 'send_client_id'];
|
|
8
|
+
const MEASUREMENT_ID = /^G-[A-Z0-9]{4,15}$/;
|
|
9
|
+
const MAX_URL = 300;
|
|
10
|
+
const EXAMPLE = '{"tagging_server_url": "https://abc123.signalhost.io", "measurement_id": "G-AB12CD34", "send_client_id": true}';
|
|
11
|
+
/** An invalid setting: 400 invalid_settings (with details.field for an unknown key). */
|
|
12
|
+
export class TaggingSettingsError extends Error {
|
|
13
|
+
hint;
|
|
14
|
+
field;
|
|
15
|
+
constructor(message, hint, field) {
|
|
16
|
+
super(message);
|
|
17
|
+
if (hint)
|
|
18
|
+
this.hint = hint;
|
|
19
|
+
if (field)
|
|
20
|
+
this.field = field;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function parseTaggingUrl(raw) {
|
|
24
|
+
const bad = (message, hint = 'Use the address from your tagging server, like https://abc123.signalhost.io or https://metrics.example.com.') => new TaggingSettingsError(message, hint);
|
|
25
|
+
if (typeof raw !== 'string' || !raw.trim())
|
|
26
|
+
throw bad('Enter the address of your tagging server.');
|
|
27
|
+
let text = raw.trim();
|
|
28
|
+
if (text.length > MAX_URL)
|
|
29
|
+
throw bad(`The tagging server address is too long (at most ${MAX_URL} characters).`);
|
|
30
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(text))
|
|
31
|
+
text = `https://${text}`;
|
|
32
|
+
let u;
|
|
33
|
+
try {
|
|
34
|
+
u = new URL(text);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
throw bad(`"${raw.trim().slice(0, 80)}" is not a web address.`);
|
|
38
|
+
}
|
|
39
|
+
if (u.protocol !== 'https:')
|
|
40
|
+
throw bad('The tagging server address must start with https://', 'usebecky only sends events over an encrypted connection.');
|
|
41
|
+
if (u.username || u.password)
|
|
42
|
+
throw bad("Don't put a username or password in the tagging server address.");
|
|
43
|
+
if (u.port && u.port !== '443')
|
|
44
|
+
throw bad('Use the tagging server address without a port number.', 'usebecky sends events to the standard HTTPS port (443).');
|
|
45
|
+
const host = u.hostname.replace(/^\[|\]$/g, '');
|
|
46
|
+
if (isIP(host))
|
|
47
|
+
throw bad('Use the tagging server’s name, not an IP address.', 'Like https://abc123.signalhost.io or https://metrics.example.com.');
|
|
48
|
+
if (!host.includes('.') || host.endsWith('.local') || host.endsWith('.internal') || host === 'localhost' || host.endsWith('.localhost')) {
|
|
49
|
+
throw bad(`"${host}" is not a public address.`);
|
|
50
|
+
}
|
|
51
|
+
if (u.search || u.hash)
|
|
52
|
+
throw bad('Use the tagging server address without ?… or #… at the end.');
|
|
53
|
+
const path = u.pathname.replace(/\/g\/collect\/?$/i, '').replace(/\/+$/, '');
|
|
54
|
+
return `https://${u.host}${path}`;
|
|
55
|
+
}
|
|
56
|
+
export function parseMeasurementId(raw) {
|
|
57
|
+
const id = typeof raw === 'string' ? raw.trim().toUpperCase() : '';
|
|
58
|
+
if (!MEASUREMENT_ID.test(id)) {
|
|
59
|
+
throw new TaggingSettingsError('Enter your Google Analytics 4 measurement ID, like G-AB12CD34.', 'Find it in Google Analytics → Admin → Data streams → your web stream.');
|
|
60
|
+
}
|
|
61
|
+
return id;
|
|
62
|
+
}
|
|
63
|
+
/** The body of PUT /installation/settings → the settings usebecky stores. Throws TaggingSettingsError. */
|
|
64
|
+
export function checkTaggingSettings(body) {
|
|
65
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
66
|
+
throw new TaggingSettingsError(`Send the settings as a JSON object, like ${EXAMPLE}.`, 'Use Content-Type: application/json. To stop forwarding, call DELETE /extensions/v1/installation/settings.');
|
|
67
|
+
}
|
|
68
|
+
const known = TAGGING_KEYS;
|
|
69
|
+
for (const k of Object.keys(body))
|
|
70
|
+
if (!known.includes(k))
|
|
71
|
+
throw new TaggingSettingsError(`There is no setting called "${k}".`, `The settings are: ${TAGGING_KEYS.join(', ')}.`, k);
|
|
72
|
+
const b = body;
|
|
73
|
+
const tagging_server_url = parseTaggingUrl(b.tagging_server_url);
|
|
74
|
+
const measurement_id = parseMeasurementId(b.measurement_id);
|
|
75
|
+
const raw = b.send_client_id;
|
|
76
|
+
const send = typeof raw === 'boolean' ? raw : raw == null || raw === '' ? false : typeof raw === 'string' && /^(yes|no|true|false)$/i.test(raw.trim()) ? /^(yes|true)$/i.test(raw.trim()) : null;
|
|
77
|
+
if (send === null) {
|
|
78
|
+
throw new TaggingSettingsError('Turn "Send the visitor’s Google Analytics client ID" on or off.', 'On: purchases join the visitor’s browser session in Google Analytics (only for visitors who agreed). Off: they are counted without it.');
|
|
79
|
+
}
|
|
80
|
+
return { tagging_server_url, measurement_id, send_client_id: send };
|
|
81
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type FieldValue, type UiParams, type View } from '../ui/schema.ts';
|
|
2
|
+
export interface RenderOptions {
|
|
3
|
+
/** The pages' base address: /project/<ref>/x */
|
|
4
|
+
base: string;
|
|
5
|
+
page: string;
|
|
6
|
+
params: UiParams;
|
|
7
|
+
/** A form's problems (an action answered { errors }) and what was typed, to show again. */
|
|
8
|
+
errors?: Record<string, string>;
|
|
9
|
+
values?: Record<string, FieldValue>;
|
|
10
|
+
/** Where actions come back to (the extension's page for the settings view); default the page itself. */
|
|
11
|
+
back?: string;
|
|
12
|
+
/** A plan button's address: the extension's page with its plan dialog on `plan`. */
|
|
13
|
+
planHref?: (plan: string) => string;
|
|
14
|
+
}
|
|
15
|
+
export declare const UI_CSS = "\n.ub-layout{display:grid;grid-template-columns:220px minmax(0,1fr);gap:28px;max-width:1120px;margin:0 auto;padding:28px 20px 64px}\n.ub-layout main{max-width:none;margin:0;padding:0}\n.ub-nav{display:flex;flex-direction:column;gap:2px}.ub-nav .group{font-size:12px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;color:var(--muted);margin:14px 0 4px}\n.ub-nav a{display:flex;align-items:center;gap:8px;padding:7px 10px;border-radius:4px;color:var(--ink-2);text-decoration:none;font-weight:600;font-size:15px}.ub-nav a:hover{background:var(--sunken)}\n.ub-nav a[aria-current=page]{background:var(--accent-soft);color:var(--ink)}.ub-nav img{width:18px;height:18px;border-radius:3px}\n@media (max-width:760px){.ub-layout{grid-template-columns:minmax(0,1fr);gap:16px;padding:20px 16px 48px}.ub-nav{flex-direction:row;flex-wrap:wrap}.ub-nav .group{display:none}}\n.ub-head{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:flex-end;gap:12px;margin-bottom:22px}.ub-head h1{font-size:30px;font-weight:800;letter-spacing:-.03em;line-height:1.1;margin:0}\n.ub-by{display:flex;align-items:center;gap:6px;font-size:13.5px;color:var(--muted);margin-bottom:6px}.ub-by img{width:16px;height:16px;border-radius:3px}\n.ub-blocks{display:flex;flex-direction:column;gap:18px}.ub-row{display:flex;flex-wrap:wrap;gap:8px;align-items:center}\n.ub-panel{background:var(--surface);border:1px solid var(--line);border-radius:6px}.ub-panel>header{padding:14px 18px;border-bottom:1px solid var(--line)}.ub-panel>header h2{margin:0;font-size:17px}.ub-panel>header p{margin:2px 0 0;color:var(--muted);font-size:15px}.ub-panel>.ub-blocks{padding:18px}\n.ub-cols{display:grid;gap:18px;grid-template-columns:repeat(var(--n),minmax(0,1fr))}@media (max-width:760px){.ub-cols{grid-template-columns:1fr}}\n.ub-stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));background:var(--surface);border:1px solid var(--line);border-radius:6px;overflow:hidden}\n.ub-stat{padding:14px 16px;border-left:1px solid var(--line);border-top:1px solid var(--line);margin:-1px 0 0 -1px}.ub-stat .l{font-size:14px;color:var(--muted)}.ub-stat .v{font-size:26px;font-weight:700;letter-spacing:-.02em;font-variant-numeric:tabular-nums}.ub-stat .d{font-size:13px;font-weight:600;margin-left:6px}.ub-stat .x{font-size:13.5px;color:var(--muted)}\n.up{color:var(--mint)}.down{color:var(--danger)}.flat{color:var(--muted)}\n.ub-tabs{display:flex;gap:4px;border-bottom:1px solid var(--line);overflow-x:auto}.ub-tabs a{padding:9px 12px;border-bottom:2px solid transparent;margin-bottom:-1px;color:var(--muted);font-weight:600;text-decoration:none;white-space:nowrap}.ub-tabs a[aria-current=page]{border-color:var(--accent);color:var(--ink);font-weight:700}\n.ub-table-wrap{overflow-x:auto;border:1px solid var(--line);border-radius:6px;background:var(--surface)}\ntable.ub{width:100%;border-collapse:collapse;font-size:15px}table.ub th{text-align:left;font-weight:600;font-size:14px;color:var(--muted);background:var(--sunken);padding:9px 14px;border-bottom:1px solid var(--line);white-space:nowrap}table.ub td{padding:11px 14px;border-bottom:1px solid var(--line);vertical-align:middle}table.ub tr:last-child td{border-bottom:none}\ntable.ub .r{text-align:right;font-variant-numeric:tabular-nums}table.ub th a{color:inherit;text-decoration:none}table.ub td.acts{white-space:nowrap;text-align:right}table.ub .button{padding:5px 12px;font-size:14px}\n.ub-toolbar{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin-bottom:10px}.ub-toolbar input[type=search]{flex:1;min-width:180px;font:inherit;font-size:15px;padding:9px 10px;border-radius:6px;border:1px solid var(--line-strong);background:var(--surface);color:var(--ink)}\n.ub-pager{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:8px;margin-top:10px;font-size:14px;color:var(--muted)}\n.ub-bars-head{display:flex;justify-content:space-between;gap:10px;font-size:13.5px;color:var(--muted)}.ub-bars{list-style:none;margin:0;padding:0}.ub-bars li{padding:7px 0}.ub-bars .t{display:flex;justify-content:space-between;gap:10px}.ub-bars .t b{font-variant-numeric:tabular-nums}.ub-bars .m{height:6px;background:var(--sunken);border-radius:2px;margin-top:5px;overflow:hidden}.ub-bars .m span{display:block;height:100%;background:#3352f5;border-radius:2px}\n.ub-kv{display:grid;grid-template-columns:minmax(120px,max-content) 1fr;gap:8px 18px;margin:0}.ub-kv dt{color:var(--muted)}.ub-kv dd{margin:0;word-break:break-word}\n.ub-empty{text-align:center;padding:34px 18px;border:1px dashed var(--line-strong);border-radius:6px}.ub-empty h3{margin:0 0 6px;font-size:17px}\n.ub-chart svg{display:block;width:100%;height:auto}.ub-chart text{fill:var(--muted);font-size:13px}.ub-legend{display:flex;flex-wrap:wrap;gap:12px;font-size:13.5px;color:var(--muted);margin-top:6px}.ub-legend i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:5px;vertical-align:-1px}\n.ub-form .field{margin:0 0 16px}.ub-form textarea,.ub-form input[type=number],.ub-form input[type=email]{font:inherit;font-size:15px;padding:9px 10px;border-radius:6px;border:1px solid var(--line-strong);background:var(--surface);color:var(--ink);width:100%}\n.ub-form .err{color:var(--danger);font-size:14px;margin:6px 0 0}.ub-form .bad input,.ub-form .bad textarea,.ub-form .bad select{border-color:var(--danger)}\n.note.info{background:var(--accent-soft)}.note.warning{background:#fdf0dc}.note.danger{background:var(--danger-soft)}@media (prefers-color-scheme:dark){.note.warning{background:#33270f}}\n.ub-text.muted{color:var(--muted)}h2.ub-h{margin:6px 0 0}\n";
|
|
16
|
+
/** The view's blocks (and the forms its buttons submit). */
|
|
17
|
+
export declare function renderBlocks(view: View, o: RenderOptions): string;
|
|
18
|
+
/** The view's title row (title, description, buttons) and blocks. */
|
|
19
|
+
export declare function renderView(view: View, o: RenderOptions & {
|
|
20
|
+
fallbackTitle: string;
|
|
21
|
+
by?: {
|
|
22
|
+
name: string;
|
|
23
|
+
logoUrl: string | null;
|
|
24
|
+
};
|
|
25
|
+
}): string;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The dev server draws your pages (listing `ui`) the way usebecky's dashboard does, close enough to build them: the same
|
|
2
|
+
// blocks, the same look (square controls, orange primary button with aubergine text, light and dark, fine at 390 px),
|
|
3
|
+
// plain HTML. Links carry the page's params in the address; buttons and forms POST to the dev server, which sends your
|
|
4
|
+
// ui.url the same `ui.action` request usebecky sends; a plan button opens the extension's page with the plan picked. A
|
|
5
|
+
// view with refresh_seconds is wrapped in an element the page's one small script asks for again (pages.ts
|
|
6
|
+
// REFRESH_SCRIPT, the dashboard's rules). The real dashboard draws them with its own components (charts, menus,
|
|
7
|
+
// toasts); what a page may hold is the same (src/ui/schema.ts).
|
|
8
|
+
import { escapeHtml as h } from "../handlers.js";
|
|
9
|
+
import { inlineTokens } from "../ui/schema.js";
|
|
10
|
+
const SERIES = ['#3352f5', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300'];
|
|
11
|
+
/** A value the object holds itself (never one every object inherits, like "constructor"). */
|
|
12
|
+
const own = (o, k) => (o && Object.hasOwn(o, k) ? o[k] : undefined);
|
|
13
|
+
export const UI_CSS = `
|
|
14
|
+
.ub-layout{display:grid;grid-template-columns:220px minmax(0,1fr);gap:28px;max-width:1120px;margin:0 auto;padding:28px 20px 64px}
|
|
15
|
+
.ub-layout main{max-width:none;margin:0;padding:0}
|
|
16
|
+
.ub-nav{display:flex;flex-direction:column;gap:2px}.ub-nav .group{font-size:12px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;color:var(--muted);margin:14px 0 4px}
|
|
17
|
+
.ub-nav a{display:flex;align-items:center;gap:8px;padding:7px 10px;border-radius:4px;color:var(--ink-2);text-decoration:none;font-weight:600;font-size:15px}.ub-nav a:hover{background:var(--sunken)}
|
|
18
|
+
.ub-nav a[aria-current=page]{background:var(--accent-soft);color:var(--ink)}.ub-nav img{width:18px;height:18px;border-radius:3px}
|
|
19
|
+
@media (max-width:760px){.ub-layout{grid-template-columns:minmax(0,1fr);gap:16px;padding:20px 16px 48px}.ub-nav{flex-direction:row;flex-wrap:wrap}.ub-nav .group{display:none}}
|
|
20
|
+
.ub-head{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:flex-end;gap:12px;margin-bottom:22px}.ub-head h1{font-size:30px;font-weight:800;letter-spacing:-.03em;line-height:1.1;margin:0}
|
|
21
|
+
.ub-by{display:flex;align-items:center;gap:6px;font-size:13.5px;color:var(--muted);margin-bottom:6px}.ub-by img{width:16px;height:16px;border-radius:3px}
|
|
22
|
+
.ub-blocks{display:flex;flex-direction:column;gap:18px}.ub-row{display:flex;flex-wrap:wrap;gap:8px;align-items:center}
|
|
23
|
+
.ub-panel{background:var(--surface);border:1px solid var(--line);border-radius:6px}.ub-panel>header{padding:14px 18px;border-bottom:1px solid var(--line)}.ub-panel>header h2{margin:0;font-size:17px}.ub-panel>header p{margin:2px 0 0;color:var(--muted);font-size:15px}.ub-panel>.ub-blocks{padding:18px}
|
|
24
|
+
.ub-cols{display:grid;gap:18px;grid-template-columns:repeat(var(--n),minmax(0,1fr))}@media (max-width:760px){.ub-cols{grid-template-columns:1fr}}
|
|
25
|
+
.ub-stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));background:var(--surface);border:1px solid var(--line);border-radius:6px;overflow:hidden}
|
|
26
|
+
.ub-stat{padding:14px 16px;border-left:1px solid var(--line);border-top:1px solid var(--line);margin:-1px 0 0 -1px}.ub-stat .l{font-size:14px;color:var(--muted)}.ub-stat .v{font-size:26px;font-weight:700;letter-spacing:-.02em;font-variant-numeric:tabular-nums}.ub-stat .d{font-size:13px;font-weight:600;margin-left:6px}.ub-stat .x{font-size:13.5px;color:var(--muted)}
|
|
27
|
+
.up{color:var(--mint)}.down{color:var(--danger)}.flat{color:var(--muted)}
|
|
28
|
+
.ub-tabs{display:flex;gap:4px;border-bottom:1px solid var(--line);overflow-x:auto}.ub-tabs a{padding:9px 12px;border-bottom:2px solid transparent;margin-bottom:-1px;color:var(--muted);font-weight:600;text-decoration:none;white-space:nowrap}.ub-tabs a[aria-current=page]{border-color:var(--accent);color:var(--ink);font-weight:700}
|
|
29
|
+
.ub-table-wrap{overflow-x:auto;border:1px solid var(--line);border-radius:6px;background:var(--surface)}
|
|
30
|
+
table.ub{width:100%;border-collapse:collapse;font-size:15px}table.ub th{text-align:left;font-weight:600;font-size:14px;color:var(--muted);background:var(--sunken);padding:9px 14px;border-bottom:1px solid var(--line);white-space:nowrap}table.ub td{padding:11px 14px;border-bottom:1px solid var(--line);vertical-align:middle}table.ub tr:last-child td{border-bottom:none}
|
|
31
|
+
table.ub .r{text-align:right;font-variant-numeric:tabular-nums}table.ub th a{color:inherit;text-decoration:none}table.ub td.acts{white-space:nowrap;text-align:right}table.ub .button{padding:5px 12px;font-size:14px}
|
|
32
|
+
.ub-toolbar{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin-bottom:10px}.ub-toolbar input[type=search]{flex:1;min-width:180px;font:inherit;font-size:15px;padding:9px 10px;border-radius:6px;border:1px solid var(--line-strong);background:var(--surface);color:var(--ink)}
|
|
33
|
+
.ub-pager{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:8px;margin-top:10px;font-size:14px;color:var(--muted)}
|
|
34
|
+
.ub-bars-head{display:flex;justify-content:space-between;gap:10px;font-size:13.5px;color:var(--muted)}.ub-bars{list-style:none;margin:0;padding:0}.ub-bars li{padding:7px 0}.ub-bars .t{display:flex;justify-content:space-between;gap:10px}.ub-bars .t b{font-variant-numeric:tabular-nums}.ub-bars .m{height:6px;background:var(--sunken);border-radius:2px;margin-top:5px;overflow:hidden}.ub-bars .m span{display:block;height:100%;background:#3352f5;border-radius:2px}
|
|
35
|
+
.ub-kv{display:grid;grid-template-columns:minmax(120px,max-content) 1fr;gap:8px 18px;margin:0}.ub-kv dt{color:var(--muted)}.ub-kv dd{margin:0;word-break:break-word}
|
|
36
|
+
.ub-empty{text-align:center;padding:34px 18px;border:1px dashed var(--line-strong);border-radius:6px}.ub-empty h3{margin:0 0 6px;font-size:17px}
|
|
37
|
+
.ub-chart svg{display:block;width:100%;height:auto}.ub-chart text{fill:var(--muted);font-size:13px}.ub-legend{display:flex;flex-wrap:wrap;gap:12px;font-size:13.5px;color:var(--muted);margin-top:6px}.ub-legend i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:5px;vertical-align:-1px}
|
|
38
|
+
.ub-form .field{margin:0 0 16px}.ub-form textarea,.ub-form input[type=number],.ub-form input[type=email]{font:inherit;font-size:15px;padding:9px 10px;border-radius:6px;border:1px solid var(--line-strong);background:var(--surface);color:var(--ink);width:100%}
|
|
39
|
+
.ub-form .err{color:var(--danger);font-size:14px;margin:6px 0 0}.ub-form .bad input,.ub-form .bad textarea,.ub-form .bad select{border-color:var(--danger)}
|
|
40
|
+
.note.info{background:var(--accent-soft)}.note.warning{background:#fdf0dc}.note.danger{background:var(--danger-soft)}@media (prefers-color-scheme:dark){.note.warning{background:#33270f}}
|
|
41
|
+
.ub-text.muted{color:var(--muted)}h2.ub-h{margin:6px 0 0}
|
|
42
|
+
`;
|
|
43
|
+
const qs = (params) => {
|
|
44
|
+
const s = new URLSearchParams(Object.entries(params).filter(([, v]) => v !== '')).toString();
|
|
45
|
+
return s ? `?${s}` : '';
|
|
46
|
+
};
|
|
47
|
+
const pageHref = (c, page, params = {}) => `${c.base}/${encodeURIComponent(page)}${qs(params)}`;
|
|
48
|
+
const withParams = (c, over) => {
|
|
49
|
+
const next = { ...c.params };
|
|
50
|
+
for (const [k, v] of Object.entries(over)) {
|
|
51
|
+
if (v === null)
|
|
52
|
+
delete next[k];
|
|
53
|
+
else
|
|
54
|
+
next[k] = v;
|
|
55
|
+
}
|
|
56
|
+
return pageHref(c, c.page, next);
|
|
57
|
+
};
|
|
58
|
+
const variantClass = (v) => (v === 'primary' ? 'button primary' : v === 'danger' ? 'button danger' : 'button');
|
|
59
|
+
function inline(s) {
|
|
60
|
+
return inlineTokens(s)
|
|
61
|
+
.map((t) => (t.t === 'bold' ? `<strong>${h(t.v)}</strong>` : t.t === 'code' ? `<code>${h(t.v)}</code>` : t.t === 'link' ? `<a href="${h(t.href)}" target="_blank" rel="noopener noreferrer">${h(t.v)}</a>` : h(t.v)))
|
|
62
|
+
.join('');
|
|
63
|
+
}
|
|
64
|
+
/** A detached form (never nested in another one), and the attributes a button needs to submit it. */
|
|
65
|
+
function actionForm(c, fields) {
|
|
66
|
+
const id = `ub-f${++c.n}`;
|
|
67
|
+
const hidden = { ...fields, _params: JSON.stringify(c.params), ...(c.back ? { _back: c.back } : {}) };
|
|
68
|
+
c.forms.push(`<form id="${id}" method="post" action="${h(pageHref(c, c.page))}" hidden>${Object.entries(hidden).map(([k, v]) => `<input type="hidden" name="${h(k)}" value="${h(v)}">`).join('')}</form>`);
|
|
69
|
+
return id;
|
|
70
|
+
}
|
|
71
|
+
function button(c, b, row) {
|
|
72
|
+
const cls = variantClass(b.variant);
|
|
73
|
+
if ('url' in b)
|
|
74
|
+
return `<a class="${cls}" href="${h(b.url)}" target="_blank" rel="noopener noreferrer">${h(b.label)}</a>`;
|
|
75
|
+
if ('page' in b)
|
|
76
|
+
return `<a class="${cls}" href="${h(pageHref(c, b.page, b.params ?? {}))}">${h(b.label)}</a>`;
|
|
77
|
+
if ('plan' in b)
|
|
78
|
+
return `<a class="${cls}" href="${h(c.planHref?.(b.plan) ?? '#')}">${h(b.label)}</a>`;
|
|
79
|
+
const id = actionForm(c, { _action: b.action, ...((b.row ?? row) ? { _row: (b.row ?? row) } : {}) });
|
|
80
|
+
const confirm = b.confirm ? ` onclick="return confirm(this.dataset.confirm)" data-confirm="${h(b.confirm)}"` : '';
|
|
81
|
+
return `<button class="${cls}" type="submit" form="${id}"${confirm}>${h(b.label)}</button>`;
|
|
82
|
+
}
|
|
83
|
+
function change(v, good = 'up') {
|
|
84
|
+
if (v === undefined)
|
|
85
|
+
return '';
|
|
86
|
+
const flat = Math.abs(v) < 0.5;
|
|
87
|
+
const cls = flat ? 'flat' : (v > 0) === (good === 'up') ? 'up' : 'down';
|
|
88
|
+
return `<span class="d ${cls}">${flat ? '±0%' : `${v > 0 ? '▲' : '▼'} ${Math.abs(v).toFixed(Math.abs(v) < 10 ? 1 : 0)}%`}</span>`;
|
|
89
|
+
}
|
|
90
|
+
function cell(c, v) {
|
|
91
|
+
if (v === null)
|
|
92
|
+
return '<span class="muted">—</span>';
|
|
93
|
+
if (typeof v === 'number')
|
|
94
|
+
return h(v.toLocaleString('en'));
|
|
95
|
+
if (typeof v === 'string')
|
|
96
|
+
return h(v);
|
|
97
|
+
if ('tone' in v)
|
|
98
|
+
return `<span class="badge${v.tone === 'success' ? ' ok' : v.tone === 'danger' ? ' bad' : ''}">${h(v.text)}</span>`;
|
|
99
|
+
if ('url' in v)
|
|
100
|
+
return `<a href="${h(v.url)}" target="_blank" rel="noopener noreferrer">${h(v.text)}</a>`;
|
|
101
|
+
return `<a href="${h(pageHref(c, v.page, v.params ?? {}))}">${h(v.text)}</a>`;
|
|
102
|
+
}
|
|
103
|
+
function chart(b) {
|
|
104
|
+
const W = 480;
|
|
105
|
+
const H = 200;
|
|
106
|
+
const pad = { l: 40, r: 8, t: 12, b: 24 };
|
|
107
|
+
const all = b.series.flatMap((s) => s.values.filter((v) => v !== null));
|
|
108
|
+
const max = Math.max(1e-9, ...all, 0);
|
|
109
|
+
const n = b.labels.length;
|
|
110
|
+
const x = (i) => pad.l + (n === 1 ? (W - pad.l - pad.r) / 2 : (i / (n - 1)) * (W - pad.l - pad.r));
|
|
111
|
+
const y = (v) => pad.t + (1 - v / max) * (H - pad.t - pad.b);
|
|
112
|
+
const fmt = (v) => (b.format === 'percent' ? `${v.toLocaleString('en')}%` : b.format === 'currency' ? new Intl.NumberFormat('en', { style: 'currency', currency: b.currency ?? 'USD', maximumFractionDigits: 0 }).format(v) : v.toLocaleString('en'));
|
|
113
|
+
let marks = '';
|
|
114
|
+
if (b.kind === 'line') {
|
|
115
|
+
b.series.forEach((s, k) => {
|
|
116
|
+
const pts = s.values.map((v, i) => (v === null ? null : `${x(i).toFixed(1)},${y(v).toFixed(1)}`));
|
|
117
|
+
const runs = [[]];
|
|
118
|
+
pts.forEach((p) => (p ? runs[runs.length - 1].push(p) : runs.push([])));
|
|
119
|
+
for (const r of runs)
|
|
120
|
+
if (r.length)
|
|
121
|
+
marks += `<polyline fill="none" stroke="${SERIES[k]}" stroke-width="2" points="${r.join(' ')}"/>`;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const slot = (W - pad.l - pad.r) / n;
|
|
126
|
+
const bw = Math.max(2, (slot * 0.7) / b.series.length);
|
|
127
|
+
b.series.forEach((s, k) => s.values.forEach((v, i) => {
|
|
128
|
+
if (v === null)
|
|
129
|
+
return;
|
|
130
|
+
const left = pad.l + i * slot + slot * 0.15 + k * bw;
|
|
131
|
+
marks += `<rect x="${left.toFixed(1)}" y="${y(v).toFixed(1)}" width="${bw.toFixed(1)}" height="${(H - pad.b - y(v)).toFixed(1)}" fill="${SERIES[k]}"><title>${h(`${s.name}, ${b.labels[i]}: ${fmt(v)}`)}</title></rect>`;
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
const axis = `<line x1="${pad.l}" x2="${W - pad.r}" y1="${H - pad.b}" y2="${H - pad.b}" stroke="currentColor" opacity=".2"/><text x="${pad.l - 6}" y="${pad.t + 10}" text-anchor="end">${h(fmt(max))}</text><text x="${pad.l - 6}" y="${H - pad.b}" text-anchor="end">0</text><text x="${pad.l}" y="${H - 6}">${h(b.labels[0])}</text>${n > 1 ? `<text x="${W - pad.r}" y="${H - 6}" text-anchor="end">${h(b.labels[n - 1])}</text>` : ''}`;
|
|
135
|
+
const legend = `<div class="ub-legend">${b.series.map((s, k) => `<span><i style="background:${SERIES[k]}"></i>${h(s.name)}</span>`).join('')}</div>`;
|
|
136
|
+
return `<figure class="ub-chart" style="margin:0">${b.title ? `<h3 style="margin:0 0 8px;font-size:16px">${h(b.title)}</h3>` : ''}<svg viewBox="0 0 ${W} ${H}" role="img" aria-label="${h(b.title ?? 'Chart')}">${axis}${marks}</svg>${legend}</figure>`;
|
|
137
|
+
}
|
|
138
|
+
function table(c, b) {
|
|
139
|
+
const keep = (omit) => Object.entries(c.params).filter(([k]) => !omit.includes(k)).map(([k, v]) => `<input type="hidden" name="${h(k)}" value="${h(v)}">`).join('');
|
|
140
|
+
const searchParam = b.search ? (b.search.param ?? 'q') : null;
|
|
141
|
+
const filterParams = (b.filters ?? []).map((f) => f.param);
|
|
142
|
+
const toolbar = searchParam || b.filters?.length
|
|
143
|
+
? `<form class="ub-toolbar" method="get" action="${h(pageHref(c, c.page))}">${keep([...(searchParam ? [searchParam] : []), ...filterParams, 'cursor'])}${searchParam ? `<input type="search" name="${h(searchParam)}" value="${h(own(c.params, searchParam) ?? '')}" placeholder="${h(b.search?.placeholder ?? 'Search')}" aria-label="Search">` : ''}${(b.filters ?? [])
|
|
144
|
+
.map((f) => `<select name="${h(f.param)}" aria-label="${h(f.label)}"><option value="">${h(f.label)}: all</option>${f.options.map((o) => `<option value="${h(o.value)}"${own(c.params, f.param) === o.value ? ' selected' : ''}>${h(o.label)}</option>`).join('')}</select>`)
|
|
145
|
+
.join('')}<button type="submit">Apply</button></form>`
|
|
146
|
+
: '';
|
|
147
|
+
const bulkId = b.bulk?.length ? actionForm(c, { _bulk: '1' }) : null;
|
|
148
|
+
const sortKey = b.sort ? b.sort.column : null;
|
|
149
|
+
const head = `<tr>${bulkId ? '<th><span class="muted small">Pick</span></th>' : ''}${b.columns
|
|
150
|
+
.map((col) => {
|
|
151
|
+
const label = h(col.label);
|
|
152
|
+
const cls = col.align === 'right' ? ' class="r"' : '';
|
|
153
|
+
if (!col.sortable)
|
|
154
|
+
return `<th${cls}>${label}</th>`;
|
|
155
|
+
const dir = sortKey === col.key ? b.sort.direction : null;
|
|
156
|
+
const next = dir === 'asc' ? `-${col.key}` : col.key;
|
|
157
|
+
return `<th${cls}><a href="${h(withParams(c, { sort: next, cursor: null }))}">${label}${dir ? (dir === 'asc' ? ' ↑' : ' ↓') : ''}</a></th>`;
|
|
158
|
+
})
|
|
159
|
+
.join('')}${b.rows.some((r) => r.actions?.length) ? '<th><span class="muted small">Actions</span></th>' : ''}</tr>`;
|
|
160
|
+
const hasActs = b.rows.some((r) => r.actions?.length);
|
|
161
|
+
const rows = b.rows.length
|
|
162
|
+
? b.rows
|
|
163
|
+
.map((r) => `<tr>${bulkId ? `<td><input type="checkbox" name="_rows" value="${h(r.id)}" form="${bulkId}" aria-label="Pick"></td>` : ''}${b.columns.map((col) => `<td${col.align === 'right' ? ' class="r"' : ''}>${cell(c, own(r.cells, col.key) ?? null)}</td>`).join('')}${hasActs ? `<td class="acts">${(r.actions ?? []).map((a) => button(c, a, r.id)).join(' ')}</td>` : ''}</tr>`)
|
|
164
|
+
.join('')
|
|
165
|
+
: `<tr><td colspan="${b.columns.length + (bulkId ? 1 : 0) + (hasActs ? 1 : 0)}" class="muted" style="text-align:center;padding:28px">${h(b.empty ?? 'Nothing here yet.')}</td></tr>`;
|
|
166
|
+
const bulk = bulkId ? `<div class="ub-row" style="margin-bottom:10px"><span class="muted small">With the picked rows:</span>${b.bulk.map((a) => `<button class="${variantClass(a.variant)}" type="submit" form="${bulkId}" name="_action" value="${h(a.action)}"${a.confirm ? ` onclick="return confirm(this.dataset.confirm)" data-confirm="${h(a.confirm)}"` : ''}>${h(a.label)}</button>`).join('')}</div>` : '';
|
|
167
|
+
const pager = b.next_cursor || b.prev_cursor || b.total !== undefined
|
|
168
|
+
? `<div class="ub-pager"><span>${b.total !== undefined ? `${h(b.total.toLocaleString('en'))} in total` : ''}</span><span class="ub-row">${b.prev_cursor ? `<a class="button" href="${h(withParams(c, { cursor: b.prev_cursor }))}">Previous</a>` : ''}${b.next_cursor ? `<a class="button" href="${h(withParams(c, { cursor: b.next_cursor }))}">Next</a>` : ''}</span></div>`
|
|
169
|
+
: '';
|
|
170
|
+
return `<div>${toolbar}${bulk}<div class="ub-table-wrap"><table class="ub"><thead>${head}</thead><tbody>${rows}</tbody></table></div>${pager}</div>`;
|
|
171
|
+
}
|
|
172
|
+
function field(c, f, values) {
|
|
173
|
+
const err = own(c.errors, f.key);
|
|
174
|
+
const v = own(values, f.key);
|
|
175
|
+
const name = `f.${f.key}`;
|
|
176
|
+
const req = f.required ? ' required' : '';
|
|
177
|
+
const help = f.help ? `<p class="help">${h(f.help)}</p>` : '';
|
|
178
|
+
const msg = err ? `<p class="err">${h(err)}</p>` : '';
|
|
179
|
+
const ph = f.placeholder ? ` placeholder="${h(f.placeholder)}"` : '';
|
|
180
|
+
const max = f.max_length ? ` maxlength="${f.max_length}"` : '';
|
|
181
|
+
let input;
|
|
182
|
+
if (f.type === 'checkbox') {
|
|
183
|
+
return `<div class="field${err ? ' bad' : ''}"><label class="check"><input type="checkbox" name="${h(name)}" value="true"${v === true ? ' checked' : ''}> ${h(f.label)}</label>${help}${msg}</div>`;
|
|
184
|
+
}
|
|
185
|
+
if (f.type === 'textarea')
|
|
186
|
+
input = `<textarea id="${h(name)}" name="${h(name)}" rows="4"${ph}${max}${req}>${h(v == null ? '' : String(v))}</textarea>`;
|
|
187
|
+
else if (f.type === 'select')
|
|
188
|
+
input = `<select id="${h(name)}" name="${h(name)}"${req}><option value=""></option>${(f.options ?? []).map((o) => `<option value="${h(o.value)}"${v === o.value ? ' selected' : ''}>${h(o.label)}</option>`).join('')}</select>`;
|
|
189
|
+
else {
|
|
190
|
+
const type = f.type === 'number' ? 'number' : f.type === 'email' ? 'email' : f.type === 'url' ? 'url' : f.type === 'password' ? 'password' : 'text';
|
|
191
|
+
const range = f.type === 'number' ? `${f.min !== undefined ? ` min="${f.min}"` : ''}${f.max !== undefined ? ` max="${f.max}"` : ''} step="any"` : '';
|
|
192
|
+
input = `<input id="${h(name)}" type="${type}" name="${h(name)}" value="${h(v == null || f.type === 'password' ? '' : String(v))}"${ph}${max}${range}${req}>`;
|
|
193
|
+
}
|
|
194
|
+
return `<div class="field${err ? ' bad' : ''}"><label for="${h(name)}">${h(f.label)}</label>${input}${help}${msg}</div>`;
|
|
195
|
+
}
|
|
196
|
+
function block(c, b) {
|
|
197
|
+
switch (b.type) {
|
|
198
|
+
case 'text':
|
|
199
|
+
return `<p class="ub-text${b.tone === 'muted' ? ' muted' : ''}" style="margin:0">${inline(b.text)}</p>`;
|
|
200
|
+
case 'heading':
|
|
201
|
+
return `<h2 class="ub-h">${h(b.text)}</h2>`;
|
|
202
|
+
case 'notice':
|
|
203
|
+
return `<div class="note ${b.tone === 'success' ? '' : b.tone}" style="margin:0">${b.title ? `<strong>${h(b.title)}</strong><br>` : ''}${inline(b.text)}${b.action ? `<div style="margin-top:10px">${button(c, b.action)}</div>` : ''}</div>`;
|
|
204
|
+
case 'stats':
|
|
205
|
+
return `<div class="ub-stats">${b.items.map((s) => `<div class="ub-stat"><div class="l">${h(s.label)}</div><div><span class="v">${h(s.value)}</span>${change(s.change, s.good)}</div>${s.hint ? `<div class="x">${h(s.hint)}</div>` : ''}</div>`).join('')}</div>`;
|
|
206
|
+
case 'chart':
|
|
207
|
+
return chart(b);
|
|
208
|
+
case 'bars': {
|
|
209
|
+
const max = Math.max(1e-9, ...b.items.map((i) => i.value));
|
|
210
|
+
const head = b.label_header || b.value_header ? `<div class="ub-bars-head"><span>${h(b.label_header ?? '')}</span><span>${h(b.value_header ?? '')}</span></div>` : '';
|
|
211
|
+
return `<div>${b.title ? `<h3 style="margin:0 0 8px;font-size:16px">${h(b.title)}</h3>` : ''}${head}<ul class="ub-bars">${b.items.map((i) => `<li><div class="t"><span>${h(i.label)}</span><b>${h(i.display ?? i.value.toLocaleString('en'))}</b></div><div class="m"><span style="width:${Math.max(1.5, (i.value / max) * 100).toFixed(1)}%"></span></div></li>`).join('')}</ul></div>`;
|
|
212
|
+
}
|
|
213
|
+
case 'table':
|
|
214
|
+
return table(c, b);
|
|
215
|
+
case 'list':
|
|
216
|
+
return `<dl class="ub-kv">${b.items.map((i) => `<dt>${h(i.label)}</dt><dd>${i.copy ? `<code>${h(i.value)}</code>` : h(i.value) || '<span class="muted">—</span>'}</dd>`).join('')}</dl>`;
|
|
217
|
+
case 'form': {
|
|
218
|
+
const values = { ...(b.values ?? {}), ...(c.values ?? {}) };
|
|
219
|
+
const types = Object.fromEntries(b.fields.map((f) => [f.key, f.type]));
|
|
220
|
+
const hidden = { _action: b.action, _params: JSON.stringify(c.params), _types: JSON.stringify(types), ...(c.back ? { _back: c.back } : {}) };
|
|
221
|
+
return `<form class="ub-form" method="post" action="${h(pageHref(c, c.page))}">${Object.entries(hidden).map(([k, v]) => `<input type="hidden" name="${h(k)}" value="${h(v)}">`).join('')}${b.fields.map((f) => field(c, f, values)).join('')}<button class="button primary" type="submit">${h(b.submit)}</button></form>`;
|
|
222
|
+
}
|
|
223
|
+
case 'buttons':
|
|
224
|
+
return `<div class="ub-row">${b.items.map((x) => button(c, x)).join('')}</div>`;
|
|
225
|
+
case 'code':
|
|
226
|
+
return `<pre style="margin:0">${h(b.text)}</pre>`;
|
|
227
|
+
case 'empty':
|
|
228
|
+
return `<div class="ub-empty"><h3>${h(b.title)}</h3>${b.text ? `<p class="muted" style="margin:0 0 12px">${h(b.text)}</p>` : ''}${b.action ? button(c, b.action) : ''}</div>`;
|
|
229
|
+
case 'tabs':
|
|
230
|
+
return `<nav class="ub-tabs">${b.items.map((t) => `<a href="${h(withParams(c, { [b.param]: t.id, cursor: null }))}"${t.id === b.active ? ' aria-current="page"' : ''}>${h(t.label)}</a>`).join('')}</nav>`;
|
|
231
|
+
case 'section':
|
|
232
|
+
return `<section class="ub-panel">${b.title || b.description ? `<header>${b.title ? `<h2>${h(b.title)}</h2>` : ''}${b.description ? `<p>${h(b.description)}</p>` : ''}</header>` : ''}<div class="ub-blocks">${b.blocks.map((x) => block(c, x)).join('')}</div></section>`;
|
|
233
|
+
case 'columns':
|
|
234
|
+
return `<div class="ub-cols" style="--n:${b.columns.length}">${b.columns.map((col) => `<div class="ub-blocks">${col.map((x) => block(c, x)).join('')}</div>`).join('')}</div>`;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** The element a refreshing view lives in: the page's script swaps it for the same element of the page asked for again
|
|
238
|
+
* (and stops when the new one has no data-ub-refresh). */
|
|
239
|
+
function wrap(view, page, inner) {
|
|
240
|
+
const refresh = view.refresh_seconds ? ` data-ub-refresh="${view.refresh_seconds}"` : '';
|
|
241
|
+
return `<div id="ub-view-${h(page)}"${refresh}>${inner}</div>`;
|
|
242
|
+
}
|
|
243
|
+
/** The view's blocks (and the forms its buttons submit). */
|
|
244
|
+
export function renderBlocks(view, o) {
|
|
245
|
+
const c = { ...o, forms: [], n: 0 };
|
|
246
|
+
const out = `<div class="ub-blocks">${view.blocks.map((b) => block(c, b)).join('')}</div>`;
|
|
247
|
+
return wrap(view, o.page, out + c.forms.join(''));
|
|
248
|
+
}
|
|
249
|
+
/** The view's title row (title, description, buttons) and blocks. */
|
|
250
|
+
export function renderView(view, o) {
|
|
251
|
+
const c = { ...o, forms: [], n: 0 };
|
|
252
|
+
const by = o.by ? `<div class="ub-by">${o.by.logoUrl ? `<img src="${h(o.by.logoUrl)}" alt="">` : ''}${h(o.by.name)}</div>` : '';
|
|
253
|
+
const head = `<div class="ub-head"><div>${by}<h1>${h(view.title ?? o.fallbackTitle)}</h1>${view.description ? `<p class="muted" style="margin:6px 0 0">${h(view.description)}</p>` : ''}</div>${view.actions?.length ? `<div class="ub-row">${view.actions.map((b) => button(c, b)).join('')}</div>` : ''}</div>`;
|
|
254
|
+
const body = `<div class="ub-blocks">${view.blocks.map((b) => block(c, b)).join('')}</div>`;
|
|
255
|
+
return wrap(view, o.page, head + body + c.forms.join(''));
|
|
256
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const WORDMARK_SVG = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"69 242 4145 974\" width=\"162\" height=\"38\" aria-hidden=\"true\" focusable=\"false\"><g transform=\"translate(40,1020) scale(1,-1)\"><path transform=\"translate(0,0)\" d=\"M217 -14Q163 -14 128.0 1.5Q93 17 72.5 44.0Q52 71 43.5 107.5Q35 144 35 186V490H216V217Q216 184 228.0 173.5Q240 163 260 163Q273 163 286.0 166.5Q299 170 314 179Q322 184 331 191V490H513V0H380L363 49Q360 47 358 45Q327 19 291.5 2.5Q256 -14 217 -14Z\" fill=\"#FFFFFF\"/><path transform=\"translate(538,0)\" d=\"M239 -18Q175 -18 122.0 4.0Q69 26 19 81L107 183Q145 149 181.0 135.0Q217 121 251 121Q265 121 275.5 125.5Q286 130 286 137Q286 145 278.5 150.5Q271 156 248.5 162.0Q226 168 182 179Q100 199 64.0 239.0Q28 279 28 337Q28 384 53.0 423.0Q78 462 125.0 485.5Q172 509 236 509Q313 509 365.5 481.5Q418 454 455 414L355 321Q328 343 294.0 354.5Q260 366 222 366Q204 366 193.5 362.0Q183 358 183 350Q183 343 200.0 337.0Q217 331 272 317Q365 295 407.0 255.0Q449 215 449 147Q449 99 423.0 61.5Q397 24 350.0 3.0Q303 -18 239 -18Z\" fill=\"#FFFFFF\"/><path transform=\"translate(985,0)\" d=\"M264 -9Q189 -9 133.0 22.0Q77 53 46.0 110.0Q15 167 15 245Q15 320 46.0 378.5Q77 437 133.0 470.5Q189 504 264 504Q342 504 397.5 468.5Q453 433 483.0 375.0Q513 317 513 248Q513 230 512.0 215.0Q511 200 509 192H175Q181 165 201 149Q229 125 279 125Q317 125 340.5 139.5Q364 154 380 176L497 115Q467 59 405.5 25.0Q344 -9 264 -9ZM177 309H354Q349 328 336 342Q313 366 266 366Q223 366 198 342Q183 328 177 309Z\" fill=\"#FFFFFF\"/><g transform=\"translate(1495,0)\"><path d=\"M332 -14Q270 -14 223 17Q206 28 191 44L177 0H43V681H224V478Q269 504 330 504Q395 504 446.5 470.5Q498 437 527.5 378.5Q557 320 557 245Q557 171 528.0 112.0Q499 53 448.5 19.5Q398 -14 332 -14ZM295.0 163.0Q327 163 350.0 186.5Q373 210 373.0 245.0Q373 280 350.0 304.0Q327 328 295.0 328.0Q263 328 239.0 304.0Q215 280 215.0 245.0Q215 210 239.0 186.5Q263 163 295.0 163.0Z\" fill=\"#FF6A2C\"/><circle cx=\"294\" cy=\"245\" r=\"80\" fill=\"#FFFFFF\"/><circle cx=\"322\" cy=\"252\" r=\"46\" fill=\"#17090F\"/><circle cx=\"338\" cy=\"270\" r=\"14\" fill=\"#FFFFFF\"/><path d=\"M226 280 C 232 220, 288 340, 318 300 C 316 350, 328 390, 338 430 C 344 370, 398 350, 428 310 C 426 360, 436 390, 446 430 C 452 370, 510 326, 540 286 C 538 336, 538 370, 548 410 C 560 250, 576 170, 600 80 C 640 220, 650 430, 610 560 C 560 720, 430 772, 290 772 C 130 772, 14 700, -2 540 C -12 430, -6 280, 20 170 C 110 190, 200 220, 226 280 Z\" fill=\"#FFE34F\"/><g fill=\"none\" stroke=\"#EDB812\" stroke-width=\"16\" stroke-linecap=\"round\"><path d=\"M338 440 C 328 520, 298 590, 258 640\"/><path d=\"M446 440 C 436 520, 406 590, 366 640\"/><path d=\"M588 200 C 600 300, 600 380, 580 460\"/><path d=\"M70 260 C 60 360, 70 460, 100 530\"/></g><path d=\"M150 640 C 220 712, 330 726, 430 690\" fill=\"none\" stroke=\"#FFF6C4\" stroke-width=\"30\" stroke-linecap=\"round\"/><path d=\"M392 122 Q 450 84 506 134\" fill=\"none\" stroke=\"#2C001E\" stroke-width=\"34\" stroke-linecap=\"round\"/></g><path transform=\"translate(2049,0)\" d=\"M264 -9Q189 -9 133.0 22.0Q77 53 46.0 110.0Q15 167 15 245Q15 320 46.0 378.5Q77 437 133.0 470.5Q189 504 264 504Q342 504 397.5 468.5Q453 433 483.0 375.0Q513 317 513 248Q513 230 512.0 215.0Q511 200 509 192H175Q181 165 201 149Q229 125 279 125Q317 125 340.5 139.5Q364 154 380 176L497 115Q467 59 405.5 25.0Q344 -9 264 -9ZM177 309H354Q349 328 336 342Q313 366 266 366Q223 366 198 342Q183 328 177 309Z\" fill=\"#FF6A2C\"/><path transform=\"translate(2559,0)\" d=\"M269 -14Q194 -14 136.5 19.5Q79 53 47.0 112.0Q15 171 15 246Q15 320 47.0 378.5Q79 437 136.5 470.5Q194 504 269 504Q358 504 420.0 462.5Q482 421 508 350L347 277Q335 300 319.0 314.0Q303 328 272 328Q240 328 217.5 305.5Q195 283 195 246Q195 208 217.5 186.0Q240 164 272 164Q303 164 319.0 177.5Q335 191 347 213L508 141Q482 71 419.0 28.5Q356 -14 269 -14Z\" fill=\"#FF6A2C\"/><path transform=\"translate(3053,0)\" d=\"M43 681H224V337L356 490H554L358 263L561 0H353L224 168V0H43Z\" fill=\"#FF6A2C\"/><path transform=\"translate(3587,0)\" d=\"M116 -190 203 18 4 490H185L293 234L400 490H581L297 -190Z\" fill=\"#FF6A2C\"/></g></svg>";
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// usebecky's wordmark for the aubergine bar (apps/dashboard/public/brand/usebecky-wordmark-dark.svg).
|
|
2
|
+
export const WORDMARK_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="69 242 4145 974" width="162" height="38" aria-hidden="true" focusable="false"><g transform="translate(40,1020) scale(1,-1)"><path transform="translate(0,0)" d="M217 -14Q163 -14 128.0 1.5Q93 17 72.5 44.0Q52 71 43.5 107.5Q35 144 35 186V490H216V217Q216 184 228.0 173.5Q240 163 260 163Q273 163 286.0 166.5Q299 170 314 179Q322 184 331 191V490H513V0H380L363 49Q360 47 358 45Q327 19 291.5 2.5Q256 -14 217 -14Z" fill="#FFFFFF"/><path transform="translate(538,0)" d="M239 -18Q175 -18 122.0 4.0Q69 26 19 81L107 183Q145 149 181.0 135.0Q217 121 251 121Q265 121 275.5 125.5Q286 130 286 137Q286 145 278.5 150.5Q271 156 248.5 162.0Q226 168 182 179Q100 199 64.0 239.0Q28 279 28 337Q28 384 53.0 423.0Q78 462 125.0 485.5Q172 509 236 509Q313 509 365.5 481.5Q418 454 455 414L355 321Q328 343 294.0 354.5Q260 366 222 366Q204 366 193.5 362.0Q183 358 183 350Q183 343 200.0 337.0Q217 331 272 317Q365 295 407.0 255.0Q449 215 449 147Q449 99 423.0 61.5Q397 24 350.0 3.0Q303 -18 239 -18Z" fill="#FFFFFF"/><path transform="translate(985,0)" d="M264 -9Q189 -9 133.0 22.0Q77 53 46.0 110.0Q15 167 15 245Q15 320 46.0 378.5Q77 437 133.0 470.5Q189 504 264 504Q342 504 397.5 468.5Q453 433 483.0 375.0Q513 317 513 248Q513 230 512.0 215.0Q511 200 509 192H175Q181 165 201 149Q229 125 279 125Q317 125 340.5 139.5Q364 154 380 176L497 115Q467 59 405.5 25.0Q344 -9 264 -9ZM177 309H354Q349 328 336 342Q313 366 266 366Q223 366 198 342Q183 328 177 309Z" fill="#FFFFFF"/><g transform="translate(1495,0)"><path d="M332 -14Q270 -14 223 17Q206 28 191 44L177 0H43V681H224V478Q269 504 330 504Q395 504 446.5 470.5Q498 437 527.5 378.5Q557 320 557 245Q557 171 528.0 112.0Q499 53 448.5 19.5Q398 -14 332 -14ZM295.0 163.0Q327 163 350.0 186.5Q373 210 373.0 245.0Q373 280 350.0 304.0Q327 328 295.0 328.0Q263 328 239.0 304.0Q215 280 215.0 245.0Q215 210 239.0 186.5Q263 163 295.0 163.0Z" fill="#FF6A2C"/><circle cx="294" cy="245" r="80" fill="#FFFFFF"/><circle cx="322" cy="252" r="46" fill="#17090F"/><circle cx="338" cy="270" r="14" fill="#FFFFFF"/><path d="M226 280 C 232 220, 288 340, 318 300 C 316 350, 328 390, 338 430 C 344 370, 398 350, 428 310 C 426 360, 436 390, 446 430 C 452 370, 510 326, 540 286 C 538 336, 538 370, 548 410 C 560 250, 576 170, 600 80 C 640 220, 650 430, 610 560 C 560 720, 430 772, 290 772 C 130 772, 14 700, -2 540 C -12 430, -6 280, 20 170 C 110 190, 200 220, 226 280 Z" fill="#FFE34F"/><g fill="none" stroke="#EDB812" stroke-width="16" stroke-linecap="round"><path d="M338 440 C 328 520, 298 590, 258 640"/><path d="M446 440 C 436 520, 406 590, 366 640"/><path d="M588 200 C 600 300, 600 380, 580 460"/><path d="M70 260 C 60 360, 70 460, 100 530"/></g><path d="M150 640 C 220 712, 330 726, 430 690" fill="none" stroke="#FFF6C4" stroke-width="30" stroke-linecap="round"/><path d="M392 122 Q 450 84 506 134" fill="none" stroke="#2C001E" stroke-width="34" stroke-linecap="round"/></g><path transform="translate(2049,0)" d="M264 -9Q189 -9 133.0 22.0Q77 53 46.0 110.0Q15 167 15 245Q15 320 46.0 378.5Q77 437 133.0 470.5Q189 504 264 504Q342 504 397.5 468.5Q453 433 483.0 375.0Q513 317 513 248Q513 230 512.0 215.0Q511 200 509 192H175Q181 165 201 149Q229 125 279 125Q317 125 340.5 139.5Q364 154 380 176L497 115Q467 59 405.5 25.0Q344 -9 264 -9ZM177 309H354Q349 328 336 342Q313 366 266 366Q223 366 198 342Q183 328 177 309Z" fill="#FF6A2C"/><path transform="translate(2559,0)" d="M269 -14Q194 -14 136.5 19.5Q79 53 47.0 112.0Q15 171 15 246Q15 320 47.0 378.5Q79 437 136.5 470.5Q194 504 269 504Q358 504 420.0 462.5Q482 421 508 350L347 277Q335 300 319.0 314.0Q303 328 272 328Q240 328 217.5 305.5Q195 283 195 246Q195 208 217.5 186.0Q240 164 272 164Q303 164 319.0 177.5Q335 191 347 213L508 141Q482 71 419.0 28.5Q356 -14 269 -14Z" fill="#FF6A2C"/><path transform="translate(3053,0)" d="M43 681H224V337L356 490H554L358 263L561 0H353L224 168V0H43Z" fill="#FF6A2C"/><path transform="translate(3587,0)" d="M116 -190 203 18 4 490H185L293 234L400 490H581L297 -190Z" fill="#FF6A2C"/></g></svg>';
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface ExtensionErrorInit {
|
|
2
|
+
code: string;
|
|
3
|
+
message: string;
|
|
4
|
+
hint?: string;
|
|
5
|
+
status?: number;
|
|
6
|
+
/** usebecky's `error.details`, e.g. `{ required_scope: 'analytics:read' }` for insufficient_scope. */
|
|
7
|
+
details?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export declare class ExtensionError extends Error {
|
|
10
|
+
code: string;
|
|
11
|
+
status: number;
|
|
12
|
+
hint?: string;
|
|
13
|
+
details?: Record<string, unknown>;
|
|
14
|
+
constructor(init: ExtensionErrorInit);
|
|
15
|
+
toJSON(): ExtensionErrorInit;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The OAuth callback could not finish: the customer pressed Cancel (`access_denied`), the link was old
|
|
19
|
+
* or opened twice (`invalid_state`), or a parameter was missing (`invalid_request`). Show a friendly page
|
|
20
|
+
* with a link back to the install route.
|
|
21
|
+
*/
|
|
22
|
+
export declare class OAuthCallbackError extends ExtensionError {
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* usebecky's token endpoint refused a code or refresh token (`invalid_grant`, `invalid_client`, ...).
|
|
26
|
+
* `invalid_client` means the client id or secret is wrong: check your environment variables.
|
|
27
|
+
* `extension_suspended` (during an install) means usebecky suspended your extension. A refused refresh never
|
|
28
|
+
* arrives as a TokenError: the kit turns `invalid_grant` and `extension_suspended` into InstallationRevokedError.
|
|
29
|
+
*/
|
|
30
|
+
export declare class TokenError extends ExtensionError {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The install is gone, and the kit already deleted its stored tokens. Stop working for this project until the customer
|
|
34
|
+
* connects it again. `code`:
|
|
35
|
+
* - `installation_revoked`: the customer removed the extension, or its tokens were revoked.
|
|
36
|
+
* - `not_connected`: no tokens are stored for this project.
|
|
37
|
+
* - `extension_suspended`: usebecky's team suspended your extension, which revokes every token for good. Check your
|
|
38
|
+
* email from usebecky; once the suspension ends, each customer connects the extension again.
|
|
39
|
+
*/
|
|
40
|
+
export declare class InstallationRevokedError extends ExtensionError {
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The codes a partner API call can fail with. Any other string is possible too (usebecky may add codes).
|
|
44
|
+
*
|
|
45
|
+
* - `insufficient_scope` (403): the install didn't allow this; `details.required_scope` names the scope.
|
|
46
|
+
* - `installation_inactive` (403): the install waits for payment or was disconnected.
|
|
47
|
+
* - `extension_suspended` (403): usebecky's team suspended your extension: every token is revoked for good and
|
|
48
|
+
* refreshes are refused. usebecky emails you why. From ext.api() it arrives as InstallationRevokedError (the kit
|
|
49
|
+
* deletes the dead tokens); only the install lookup in handleCallback() throws it as a PartnerApiError.
|
|
50
|
+
* - `plan_required` (403): an analytics range longer than the project's plan keeps.
|
|
51
|
+
* - `rate_limited` (429), `invalid_range` (400), `project_paused` (402), `project_unavailable` (503),
|
|
52
|
+
* `invalid_token` (401), `route_not_found` (404), `network_error` (usebecky was not reached).
|
|
53
|
+
*/
|
|
54
|
+
export type PartnerApiErrorCode = 'insufficient_scope' | 'capability_required' | 'invalid_settings' | 'installation_inactive' | 'extension_suspended' | 'plan_required' | 'rate_limited' | 'invalid_range' | 'project_paused' | 'project_unavailable' | 'invalid_token' | 'route_not_found' | 'network_error' | (string & {});
|
|
55
|
+
/** What to do next, for usebecky's codes whose answer may come without a hint of its own. */
|
|
56
|
+
export declare const PARTNER_ERROR_HINTS: Readonly<Record<string, string>>;
|
|
57
|
+
/** The kit's hint for a code, if it has one (never something inherited, like "toString"). */
|
|
58
|
+
export declare function partnerErrorHint(code: string): string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* A partner API call failed. `code` is one of PartnerApiErrorCode, e.g. `insufficient_scope` (403,
|
|
61
|
+
* `details.required_scope`), `capability_required` (403, `details.required_capability`), `invalid_settings` (400),
|
|
62
|
+
* `installation_inactive` (403), `rate_limited` (429), `plan_required` (403), `invalid_range` (400),
|
|
63
|
+
* `project_unavailable` (503), `project_paused` (402), `route_not_found`, `network_error`...
|
|
64
|
+
*/
|
|
65
|
+
export declare class PartnerApiError extends ExtensionError {
|
|
66
|
+
code: PartnerApiErrorCode;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A webhook request did not come from usebecky, or was changed or replayed: `missing_signature`,
|
|
70
|
+
* `invalid_signature`, `stale_signature` or `invalid_payload`. Answer 400 and do nothing.
|
|
71
|
+
*/
|
|
72
|
+
export declare class WebhookVerificationError extends ExtensionError {
|
|
73
|
+
}
|
|
74
|
+
/** Wrong options passed to the kit (a missing client id, an http redirect URI in production...). */
|
|
75
|
+
export declare class ConfigError extends ExtensionError {
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* An id_token or launch token from usebecky was refused: `invalid_token` (not a JWT), `wrong_token_type` (an id_token
|
|
79
|
+
* where a launch token belongs, or the other way round), `unknown_key`, `invalid_signature`, `invalid_issuer`,
|
|
80
|
+
* `invalid_audience` (issued to another extension), `expired_token`, `token_not_yet_valid`, `invalid_nonce`,
|
|
81
|
+
* `replayed_token` (a launch link opened twice) or `jwks_unavailable` (usebecky's keys could not be loaded). Sign
|
|
82
|
+
* nobody in; for a launch, send the person back to usebecky to open your service again.
|
|
83
|
+
*/
|
|
84
|
+
export declare class TokenVerificationError extends ExtensionError {
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A provision request (usebecky creating a customer's account at your service) was refused: `missing_signature`,
|
|
88
|
+
* `invalid_signature`, `stale_signature`, `invalid_payload` or `idempotency_mismatch`. Answer 400 and create nothing.
|
|
89
|
+
*/
|
|
90
|
+
export declare class ProvisionRequestError extends ExtensionError {
|
|
91
|
+
}
|
|
92
|
+
/** Pull `{ error: { code, message, hint } }` (usebecky) or `{ error, error_description }` (OAuth) out of a body. */
|
|
93
|
+
export declare function readErrorBody(body: unknown): {
|
|
94
|
+
code?: string;
|
|
95
|
+
message?: string;
|
|
96
|
+
hint?: string;
|
|
97
|
+
details?: Record<string, unknown>;
|
|
98
|
+
};
|