@proveanything/smartlinks 1.17.6 → 2.0.0-alpha.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.
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 1.17.6 | Generated: 2026-09-14T10:25:34.491Z
3
+ Version: 2.0.0-alpha.1 | Generated: 2026-09-14T11:30:24.015Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -22,6 +22,7 @@ For detailed guides on specific features:
22
22
  - **[App Configuration Files](app-manifest.md)** - `app.manifest.json` and `app.admin.json` reference — bundles, components, setup questions, import schemas, tunable fields, and metrics
23
23
  - **[Executor Model](executor.md)** - Programmatic JS bundles for AI-driven setup, server-side SEO metadata generation, and LLM content for AI crawlers
24
24
  - **[Server Functions](server-functions.md)** - App-authored server-side "edge functions" (`async (ctx, event) => result`): http/event/cron triggers, the visibility/authority/capabilities security model, and the pre-scoped `ctx` (authority-scoped SDK, capability-gated secrets + fetch)
25
+ - **[Deploying & Registering an App](deploying-apps.md)** - Publish → build → register: the app CDN layout (smartlinks.app), dev/beta/prod channels, channel-scoped deploy keys, the `POST /apps/:appId/releases` registration endpoint + validation, and how to wire it into your build so a bad manifest fails the deploy
25
26
  - **[Realtime](realtime.md)** - Real-time data updates and WebSocket connections
26
27
  - **[iframe Responder](iframe-responder.md)** - iframe integration and cross-origin communication
27
28
  - **[iframe Streaming Parent Changes](iframe-streaming-parent-changes.md)** - Parent-side changes required to support AI streaming in iframe proxy mode
@@ -1722,6 +1723,7 @@ interface AppFunctionDef {
1722
1723
  trigger: AppFunctionTrigger;
1723
1724
  visibility?: AppFunctionVisibility;
1724
1725
  authority?: AppFunctionAuthority;
1726
+ elevated?: boolean;
1725
1727
  capabilities?: string[];
1726
1728
  apiVersion?: string;
1727
1729
  handler?: string;
@@ -0,0 +1,193 @@
1
+ # Deploying & registering an app
2
+
3
+ > **Preview — SmartLinks SDK 2.0.0-alpha.** Part of the installable-app platform being built
4
+ > toward 2.0.0 stable. These APIs may change before then. Published under the npm `next` tag;
5
+ > `latest` remains 1.x.
6
+
7
+ A SmartLinks app is a bundle (widgets, containers, and — new — [server functions](server-functions.md))
8
+ described by an `app.manifest.json`. Deploying an app has two halves:
9
+
10
+ 1. **Publish the bundles** to the SmartLinks app CDN.
11
+ 2. **Register the release** — tell the SmartLinks backend a new version exists, hand it the
12
+ manifest, and let it validate everything. Registration is what makes the app (and its
13
+ server functions) known, installable, and testable.
14
+
15
+ This guide covers registration and how to wire it into your build.
16
+
17
+ ---
18
+
19
+ ## Two "wheres": the API host vs the bundle CDN
20
+
21
+ Deploying touches two different locations — keep them distinct:
22
+
23
+ 1. **The install endpoint (API host)** — *where you register*. This is a SmartLinks
24
+ **API v1** endpoint, and its **host is the environment** you're installing into (see
25
+ below). This is the one that varies per deployment.
26
+ 2. **The bundle CDN** — *where your files are served from*. **Today always `smartlinks.app`**:
27
+
28
+ ```
29
+ https://smartlinks.app/apps/{appId}/{version}/app.manifest.json
30
+ https://smartlinks.app/apps/{appId}/{version}/widgets-<hash>.umd.js
31
+ https://smartlinks.app/apps/{appId}/{version}/functions.umd.js
32
+ ```
33
+
34
+ That base — `https://smartlinks.app/apps/{appId}/{version}` — is your **`bundleBaseUrl`**,
35
+ which you pass at registration. Always pass it explicitly so nothing breaks if the CDN
36
+ location changes later.
37
+
38
+ ## Environments & the app registry
39
+
40
+ Historically an app was **stateless everywhere**: every environment fetched the same CDN
41
+ manifest on the fly, so apps were agnostic to shards, clients, and VPCs. Registration changes
42
+ that — an app is now **installed into an environment's registry**, which is **global to that
43
+ environment (not per-collection / not per-shard)**.
44
+
45
+ An **environment** is one SmartLinks deployment: the main SaaS, or a client's isolated (VPC)
46
+ instance. Each owns its own app registry, so:
47
+
48
+ - The **install endpoint's host is the environment** — `POST https://<that-env's-api>/api/v1/apps/…`.
49
+ Installing into a client's VPC means calling *their* API with *their* deploy key.
50
+ - **Rolling out to several environments = the same call, once per target** (different API host
51
+ + key each). The registration script below takes the API base as a parameter for exactly this.
52
+
53
+ > Enabling an app on a specific **collection** (and consenting to its capabilities) is a
54
+ > separate, per-collection step layered on top of the environment's registry.
55
+
56
+ ---
57
+
58
+ ## Channels
59
+
60
+ A release is registered on one **channel**, matching how you deploy:
61
+
62
+ | Channel | Deployed from | Typical use |
63
+ |---|---|---|
64
+ | `dev` | **Lovable "Publish"** | Live development / testing |
65
+ | `beta` | your pipeline | Staging / preview |
66
+ | `prod` | your **Cloud Build** toolset (from git) | Production |
67
+
68
+ A collection chooses which channel it follows, so you can point a test collection at `dev`
69
+ and exercise a build before it reaches `prod`.
70
+
71
+ ---
72
+
73
+ ## Deploy keys
74
+
75
+ Registration is authenticated by a **deploy key** — not a user login — scoped to the
76
+ channels it may write. Security comes from the *scope*, so a key that leaks can only affect
77
+ what it was allowed to touch.
78
+
79
+ | Key | Lives in | May write |
80
+ |---|---|---|
81
+ | **Dev key** | your app's (private) source / build env | `dev` channel only |
82
+ | **Prod master key** | your Cloud Build toolset only — never in app source | all channels |
83
+ | **Per-app key** *(future)* | a client's build | one specific app |
84
+
85
+ Because a Lovable build has no secret store, the **dev key lives in your app source** — which
86
+ is safe precisely because it can only ever write the `dev` channel. The **prod key stays in
87
+ Cloud Build**. Present the key in the `x-smartlinks-deploy-key` header.
88
+
89
+ ---
90
+
91
+ ## The registration endpoint
92
+
93
+ ```
94
+ POST https://<smartlinks-api>/api/v1/apps/{appId}/releases
95
+ x-smartlinks-deploy-key: <your deploy key>
96
+ Content-Type: application/json
97
+ ```
98
+
99
+ ```jsonc
100
+ {
101
+ "channel": "dev", // dev | beta | prod
102
+ "version": "1.2.3", // from your manifest meta.version
103
+ "build": { "at": "2026-09-14T10:00:00Z", "gitHash": "abc1234", "builder": "lovable" },
104
+ "manifest": { /* your full app.manifest.json, including the functions block */ },
105
+ "bundleBaseUrl": "https://smartlinks.app/apps/currys/1.2.3"
106
+ }
107
+ ```
108
+
109
+ ### Responses
110
+
111
+ | Status | Meaning |
112
+ |---|---|
113
+ | `200` | Registered. Body: `{ ok: true, appId, channel, version, functions: [names], registeredAt }` |
114
+ | `422` | Validation failed — **the build should fail**. Body: `{ ok: false, errors: [...] }` |
115
+ | `401` | Missing/invalid deploy key |
116
+ | `403` | Key not permitted for that channel (e.g. a dev key targeting `prod`) — `CHANNEL_FORBIDDEN` |
117
+
118
+ Each validation error is `{ code, message, path }`, e.g.:
119
+
120
+ ```json
121
+ { "ok": false, "errors": [
122
+ { "code": "AUTHORITY_INVALID", "message": "invalid authority \"root\"", "path": "functions.definitions[0].authority" },
123
+ { "code": "CAPABILITY_INVALID", "message": "invalid capability \"bogus\"", "path": "functions.definitions[0].capabilities[0]" }
124
+ ]}
125
+ ```
126
+
127
+ ### What gets validated
128
+
129
+ - `manifest.meta.appId` must match the `{appId}` in the URL.
130
+ - The **`functions` block** is validated with the *same* rules the runtime enforces
131
+ (names, triggers, `visibility`/`authority`, the capability grammar, duplicate names) — so
132
+ a malformed server function is caught **at deploy time**, not at runtime. See
133
+ [server-functions.md](server-functions.md).
134
+
135
+ ---
136
+
137
+ ## Wiring it into your build
138
+
139
+ Registration is the **last step of your build** — after bundles are built and hashed. Run a
140
+ small script that reads your built manifest and POSTs it, and **exits non-zero on failure**
141
+ so a bad install fails the publish.
142
+
143
+ ```js
144
+ // scripts/register-release.mjs — run as the build's postbuild step
145
+ import { readFileSync } from 'node:fs'
146
+ import { execSync } from 'node:child_process'
147
+
148
+ const API = process.env.SMARTLINKS_API || 'https://api.smartlinks.app'
149
+ const KEY = process.env.SMARTLINKS_DEPLOY_KEY // dev key (in source/env) or prod key (Cloud Build)
150
+ const CHANNEL = process.env.SMARTLINKS_CHANNEL || 'dev' // 'prod' in Cloud Build
151
+
152
+ const manifest = JSON.parse(readFileSync('dist/app.manifest.json', 'utf8'))
153
+ const appId = manifest.meta.appId
154
+ const version = manifest.meta.version
155
+ const gitHash = (() => { try { return execSync('git rev-parse --short HEAD').toString().trim() } catch { return null } })()
156
+
157
+ const res = await fetch(`${API}/api/v1/apps/${appId}/releases`, {
158
+ method: 'POST',
159
+ headers: { 'content-type': 'application/json', 'x-smartlinks-deploy-key': KEY },
160
+ body: JSON.stringify({
161
+ channel: CHANNEL,
162
+ version,
163
+ build: { at: new Date().toISOString(), gitHash, builder: CHANNEL === 'dev' ? 'lovable' : 'cloudbuild' },
164
+ manifest,
165
+ bundleBaseUrl: `https://smartlinks.app/apps/${appId}/${version}`,
166
+ }),
167
+ })
168
+
169
+ const body = await res.json().catch(() => ({}))
170
+ if (!res.ok || !body.ok) {
171
+ console.error(`❌ SmartLinks registration failed (${res.status}):`)
172
+ for (const e of body.errors || []) console.error(` • ${e.path}: ${e.message}`)
173
+ process.exit(1) // fail the build
174
+ }
175
+ console.log(`✅ Registered ${appId}@${version} on "${CHANNEL}" — functions: ${(body.functions || []).join(', ') || 'none'}`)
176
+ ```
177
+
178
+ Wire it after your bundle build/hash step, e.g.:
179
+
180
+ ```jsonc
181
+ // package.json
182
+ "scripts": {
183
+ "build": "vite build && … && node scripts/hash-bundles.mjs",
184
+ "postbuild": "node scripts/register-release.mjs"
185
+ }
186
+ ```
187
+
188
+ - **Dev (Lovable "Publish")** runs `build` → `postbuild` with the **dev key** and
189
+ `SMARTLINKS_CHANNEL=dev`.
190
+ - **Prod (Cloud Build)** runs the same with the **prod key** and `SMARTLINKS_CHANNEL=prod`.
191
+
192
+ That's it: hitting Publish now validates and registers your app — and a broken manifest or
193
+ function stops the deploy with an actionable error instead of shipping.
@@ -1,5 +1,9 @@
1
1
  # Server functions ("edge functions")
2
2
 
3
+ > **Preview — SmartLinks SDK 2.0.0-alpha.** Part of the installable-app platform being built
4
+ > toward 2.0.0 stable (author → register → install → run → test). These APIs may change before
5
+ > then. Published under the npm `next` tag; `latest` remains 1.x.
6
+
3
7
  A **server function** is arbitrary server-side JavaScript your app deploys directly into
4
8
  SmartLinks. It runs on the SmartLinks servers — with access to the full SDK, to your app's
5
9
  secrets, and to outbound network — so you can do things a browser app can't: validate and
@@ -38,6 +42,7 @@ in a bundle alongside your widgets/containers:
38
42
  "trigger": { "type": "http", "methods": ["POST"] },
39
43
  "visibility": "public", // WHO may call it
40
44
  "authority": "collection", // WHOSE authority it runs as
45
+ "elevated": true, // required ack for public + collection
41
46
  "capabilities": ["sl:records:write", "network:api.recaptcha.net"],
42
47
  "apiVersion": "2026-09"
43
48
  },
@@ -104,6 +109,10 @@ functions have no external caller, so they always run as `collection`.
104
109
  > secrets, even though it's "elevated"). But **validating the request is still your job**:
105
110
  > check the payload, rate-limit using `ctx.caller`, guard against replay. Treat the function
106
111
  > body as a trust boundary.
112
+ >
113
+ > Because it's the sharp edge, a `public` + `collection` function must **explicitly opt in**
114
+ > with `elevated: true` in its declaration — a conscious acknowledgment that you're exposing
115
+ > collection authority to public callers. Without it, install/validation fails.
107
116
 
108
117
  ---
109
118
 
@@ -219,6 +228,69 @@ at admin level, attributed to the signed-in admin. The public surface resolves a
219
228
  token is present (→ `owner`) and treats its absence as anonymous (→ `public`); a
220
229
  `collection`-authority function runs elevated regardless.
221
230
 
231
+ ## Testing & preview
232
+
233
+ You don't have to deploy to find out whether a function works. There are three levels of
234
+ fidelity — use them in order.
235
+
236
+ ### 1. Local harness (fast, offline)
237
+
238
+ `@proveanything/smartlinks/testing` builds a `ctx` that **enforces the declared capability
239
+ envelope**, so a function fails locally the same way it would in production — the common
240
+ "I forgot to declare `sl:records:write`" bug is caught before you deploy, not after.
241
+
242
+ ```ts
243
+ import { createFunctionTestContext } from '@proveanything/smartlinks/testing'
244
+ import manifest from '../public/app.manifest.json'
245
+ import { submitCompetitionEntry } from '../src/functions'
246
+
247
+ const def = manifest.functions.definitions.find(d => d.name === 'submitCompetitionEntry')
248
+
249
+ const ctx = createFunctionTestContext({
250
+ def, // capabilities enforced come from the manifest itself
251
+ caller: { userId: 'tester' },
252
+ secrets: { 'recaptcha-secret': 'test-value' }, // fixtures — real secrets are server-only
253
+ })
254
+
255
+ const res = await submitCompetitionEntry(ctx, { method: 'POST', body: { email: 'a@b.com', answer: '42' } })
256
+ // ctx.sl.appRecords.create(...) throws CapabilityError unless `def` declares sl:records:write
257
+ ```
258
+
259
+ Pass the **`def`** (not a hand-typed capability list) so "tested" can't drift from
260
+ "declared". By default `ctx.sl` methods return a stub result (pure unit test — no network);
261
+ inject `sl` to delegate to your live SDK for real reads/writes:
262
+
263
+ ```ts
264
+ const ctx = createFunctionTestContext({
265
+ def,
266
+ sl: { appRecords: { create: (fields) => mySdk.app.records.create(fields) } },
267
+ })
268
+ ```
269
+
270
+ ### 2. Deployed test mode (high fidelity, safe)
271
+
272
+ Register to the `dev` channel and invoke on the real server — real secrets, real data —
273
+ without a live run *(coming next)*: a test invocation is forced to `caller` authority,
274
+ side-effecting writes are dry-run, and the traffic is logged separately from live metrics.
275
+
276
+ ### 3. Live
277
+
278
+ Point a real collection at the channel and invoke for real.
279
+
280
+ ### What differs across the three
281
+
282
+ | | Capabilities | `ctx.sl` | Secrets | Authority | Writes |
283
+ |---|---|---|---|---|---|
284
+ | **Local harness** | Enforced (from `def`) | Stub, or your injected SDK | Fixtures you pass | Informational | Whatever your impl does |
285
+ | **Deployed test** | Enforced | Real (test-scoped) | Real | Forced to `caller` | Dry-run |
286
+ | **Live** | Enforced | Real | Real | As declared | Real |
287
+
288
+ ### Recommended CI pattern
289
+
290
+ 1. **Unit** — run each handler through `createFunctionTestContext` (no network); assert
291
+ behaviour *and* that capabilities are sufficient (an under-declared capability throws).
292
+ 2. **Post-deploy smoke** — after registering to `dev`, hit each function once in test mode.
293
+
222
294
  ## Where functions run (and why it doesn't change how you write them)
223
295
 
224
296
  SmartLinks runs first-party (trusted) functions **in-process** and untrusted third-party
package/dist/openapi.yaml CHANGED
@@ -17281,6 +17281,8 @@ components:
17281
17281
  $ref: "#/components/schemas/AppFunctionVisibility"
17282
17282
  authority:
17283
17283
  $ref: "#/components/schemas/AppFunctionAuthority"
17284
+ elevated:
17285
+ type: boolean
17284
17286
  capabilities:
17285
17287
  type: array
17286
17288
  items:
@@ -0,0 +1,64 @@
1
+ import type { AppFunctionDef, ServerFunctionContext } from '../types/appManifest';
2
+ export declare class CapabilityError extends Error {
3
+ capability: string;
4
+ code: string;
5
+ constructor(capability: string, message?: string);
6
+ }
7
+ /** The surface `ctx.sl` exposes — mirrors the server facade. Provide the methods your test needs. */
8
+ export interface TestSlImpl {
9
+ appRecords?: {
10
+ create?(fields: any): any;
11
+ update?(id: string, fields: any): any;
12
+ upsert?(fields: any): any;
13
+ delete?(id: string): any;
14
+ get?(id: string): any;
15
+ query?(params: any): any;
16
+ listTypes?(): any;
17
+ };
18
+ products?: {
19
+ get?(id: string, opts?: any): any;
20
+ query?(body?: any, opts?: any): any;
21
+ create?(data: any, opts?: any): any;
22
+ update?(id: string, data: any, opts?: any): any;
23
+ };
24
+ attestations?: {
25
+ create?(fields: any): any;
26
+ };
27
+ }
28
+ export interface TestCaller {
29
+ userId?: string | null;
30
+ anonymous?: boolean;
31
+ origin?: string | null;
32
+ ip?: string | null;
33
+ }
34
+ export interface CreateFunctionTestContextOptions {
35
+ /** The manifest declaration under test — its `capabilities` are the enforced envelope. */
36
+ def: Pick<AppFunctionDef, 'capabilities' | 'trigger' | 'visibility' | 'authority'>;
37
+ collectionId?: string;
38
+ appId?: string;
39
+ caller?: TestCaller;
40
+ /** Fixture secrets, keyed by ref. Real sealed secrets are server-only and never available locally. */
41
+ secrets?: Record<string, string>;
42
+ /** Backing impl for ctx.sl. Omit for pure unit tests (methods return a stub echo). */
43
+ sl?: TestSlImpl;
44
+ /** Backing fetch (defaults to global fetch). Still gated by the `network` capability. */
45
+ fetch?: typeof fetch;
46
+ }
47
+ export interface FunctionTestContext extends ServerFunctionContext {
48
+ /** Captured log lines (also written via ctx.log). */
49
+ logs: Array<{
50
+ at: string;
51
+ message: string;
52
+ data?: Record<string, any>;
53
+ }>;
54
+ }
55
+ /**
56
+ * Build a capability-enforcing test ctx for a server function. Run your handler with it:
57
+ *
58
+ * const ctx = createFunctionTestContext({ def, caller: { userId: 'me' }, secrets: { k: 'v' } })
59
+ * const result = await myHandler(ctx, { method: 'POST', body: { … } })
60
+ *
61
+ * A ctx.sl / ctx.secrets / ctx.fetch call not covered by `def.capabilities` throws
62
+ * CapabilityError — exactly as it would in production.
63
+ */
64
+ export declare function createFunctionTestContext(opts: CreateFunctionTestContextOptions): FunctionTestContext;
@@ -0,0 +1,145 @@
1
+ // src/testing/index.ts
2
+ //
3
+ // Local test harness for SmartLinks server functions — importable as
4
+ // `@proveanything/smartlinks/testing`. It builds a `ctx` that matches the runtime
5
+ // contract AND enforces the declared capability envelope, so a function fails locally
6
+ // the same way it would after deploy — no "deploy and pray".
7
+ //
8
+ // Fidelity (documented in docs/server-functions.md "Testing & preview"):
9
+ // - Capabilities are enforced EXACTLY as declared in the manifest `def` — pass the def
10
+ // itself so "tested" can't drift from "declared".
11
+ // - `ctx.sl` delegates to an impl you inject: your live SDK for real reads/writes, or
12
+ // omit it for pure unit tests (methods return a stub result instead of hitting the
13
+ // network — control flow + capability enforcement still run).
14
+ // - `ctx.secrets` are FIXTURES you provide; real sealed secrets are server-only and
15
+ // never resolvable locally.
16
+ // - `ctx.fetch` is gated by the `network` capability just like production.
17
+ export class CapabilityError extends Error {
18
+ constructor(capability, message) {
19
+ super(message || `capability not granted: ${capability}`);
20
+ this.code = 'CAPABILITY_DENIED';
21
+ this.name = 'CapabilityError';
22
+ this.capability = capability;
23
+ }
24
+ }
25
+ // The SAME grammar the server enforces (prove server/services/functions/validate.js).
26
+ function parseCapabilities(list = []) {
27
+ const sl = new Set();
28
+ const secrets = new Set();
29
+ const networkHosts = new Set();
30
+ let networkAll = false;
31
+ for (const raw of list || []) {
32
+ const cap = String(raw || '').trim();
33
+ if (!cap)
34
+ continue;
35
+ if (cap === 'network') {
36
+ networkAll = true;
37
+ continue;
38
+ }
39
+ if (cap.startsWith('network:')) {
40
+ networkHosts.add(cap.slice(8).toLowerCase());
41
+ continue;
42
+ }
43
+ if (cap.startsWith('secrets:')) {
44
+ secrets.add(cap.slice(8));
45
+ continue;
46
+ }
47
+ if (cap.startsWith('sl:')) {
48
+ sl.add(cap.slice(3));
49
+ continue;
50
+ }
51
+ }
52
+ return { sl, secrets, networkAll, networkHosts };
53
+ }
54
+ function allowsSl(p, resource, op) {
55
+ if (p.sl.has(`${resource}:${op}`))
56
+ return true;
57
+ if (op === 'read' && p.sl.has(`${resource}:write`))
58
+ return true; // write implies read
59
+ return false;
60
+ }
61
+ const stub = (method, args) => ({ __stub: true, method, args });
62
+ /**
63
+ * Build a capability-enforcing test ctx for a server function. Run your handler with it:
64
+ *
65
+ * const ctx = createFunctionTestContext({ def, caller: { userId: 'me' }, secrets: { k: 'v' } })
66
+ * const result = await myHandler(ctx, { method: 'POST', body: { … } })
67
+ *
68
+ * A ctx.sl / ctx.secrets / ctx.fetch call not covered by `def.capabilities` throws
69
+ * CapabilityError — exactly as it would in production.
70
+ */
71
+ export function createFunctionTestContext(opts) {
72
+ var _a, _b, _c, _d;
73
+ const def = opts.def || {};
74
+ const parsed = parseCapabilities(def.capabilities || []);
75
+ const impl = opts.sl || {};
76
+ const logs = [];
77
+ const gated = (resource, op, fn, name) => async (...args) => {
78
+ if (!allowsSl(parsed, resource, op))
79
+ throw new CapabilityError(`sl:${resource}:${op}`);
80
+ return fn ? fn(...args) : stub(`${resource}.${name}`, args);
81
+ };
82
+ const ar = impl.appRecords || {};
83
+ const pr = impl.products || {};
84
+ const at = impl.attestations || {};
85
+ const sl = {
86
+ appRecords: {
87
+ create: gated('records', 'write', ar.create && ar.create.bind(ar), 'create'),
88
+ update: gated('records', 'write', ar.update && ar.update.bind(ar), 'update'),
89
+ upsert: gated('records', 'write', ar.upsert && ar.upsert.bind(ar), 'upsert'),
90
+ delete: gated('records', 'write', ar.delete && ar.delete.bind(ar), 'delete'),
91
+ get: gated('records', 'read', ar.get && ar.get.bind(ar), 'get'),
92
+ query: gated('records', 'read', ar.query && ar.query.bind(ar), 'query'),
93
+ listTypes: gated('records', 'read', ar.listTypes && ar.listTypes.bind(ar), 'listTypes'),
94
+ },
95
+ products: {
96
+ get: gated('products', 'read', pr.get && pr.get.bind(pr), 'get'),
97
+ query: gated('products', 'read', pr.query && pr.query.bind(pr), 'query'),
98
+ create: gated('products', 'write', pr.create && pr.create.bind(pr), 'create'),
99
+ update: gated('products', 'write', pr.update && pr.update.bind(pr), 'update'),
100
+ },
101
+ attestations: {
102
+ create: gated('attestations', 'write', at.create && at.create.bind(at), 'create'),
103
+ },
104
+ };
105
+ const secretsMap = opts.secrets || {};
106
+ const baseFetch = opts.fetch || (typeof fetch !== 'undefined' ? fetch : undefined);
107
+ const via = (def.trigger && def.trigger.type) || 'http';
108
+ const caller = opts.caller || {};
109
+ return {
110
+ collectionId: opts.collectionId || 'test-collection',
111
+ appId: opts.appId || 'test-app',
112
+ sl,
113
+ secrets: {
114
+ async get(ref) {
115
+ if (!parsed.secrets.has(ref))
116
+ throw new CapabilityError(`secrets:${ref}`);
117
+ return Object.prototype.hasOwnProperty.call(secretsMap, ref) ? secretsMap[ref] : null;
118
+ },
119
+ },
120
+ caller: {
121
+ userId: (_a = caller.userId) !== null && _a !== void 0 ? _a : null,
122
+ anonymous: (_b = caller.anonymous) !== null && _b !== void 0 ? _b : !caller.userId,
123
+ origin: (_c = caller.origin) !== null && _c !== void 0 ? _c : null,
124
+ ip: (_d = caller.ip) !== null && _d !== void 0 ? _d : null,
125
+ via,
126
+ },
127
+ fetch: (async (input, init) => {
128
+ let host = '';
129
+ try {
130
+ host = new URL(typeof input === 'string' ? input : input.url).host;
131
+ }
132
+ catch ( /* bad URL → denied */_a) { /* bad URL → denied */ }
133
+ const allowed = parsed.networkAll || (!!host && parsed.networkHosts.has(host.toLowerCase()));
134
+ if (!allowed)
135
+ throw new CapabilityError(host ? `network:${host}` : 'network');
136
+ if (!baseFetch)
137
+ throw new Error('fetch is not available in this environment; pass opts.fetch');
138
+ return baseFetch(input, init);
139
+ }),
140
+ log: (message, data) => {
141
+ logs.push(Object.assign({ at: new Date().toISOString(), message: String(message) }, (data ? { data } : {})));
142
+ },
143
+ logs,
144
+ };
145
+ }
@@ -244,6 +244,13 @@ export interface AppFunctionDef {
244
244
  * `event`/`cron` functions have no caller and always run as `collection`.
245
245
  */
246
246
  authority?: AppFunctionAuthority;
247
+ /**
248
+ * Required acknowledgment for the sharp edge: a `public` + `collection` function is
249
+ * publicly callable AND runs with elevated collection authority. Set `elevated: true`
250
+ * to confirm you intend that and accept responsibility for validating requests —
251
+ * without it, install/validation fails. Ignored for any other visibility/authority combo.
252
+ */
253
+ elevated?: boolean;
247
254
  /**
248
255
  * Least-privilege capabilities this function needs, surfaced at install for
249
256
  * consent and capped at runtime — even for `collection`-authority functions.
@@ -1,6 +1,6 @@
1
1
  # Smartlinks API Summary
2
2
 
3
- Version: 1.17.6 | Generated: 2026-09-14T10:25:34.491Z
3
+ Version: 2.0.0-alpha.1 | Generated: 2026-09-14T11:30:24.015Z
4
4
 
5
5
  This is a concise summary of all available API functions and types.
6
6
 
@@ -22,6 +22,7 @@ For detailed guides on specific features:
22
22
  - **[App Configuration Files](app-manifest.md)** - `app.manifest.json` and `app.admin.json` reference — bundles, components, setup questions, import schemas, tunable fields, and metrics
23
23
  - **[Executor Model](executor.md)** - Programmatic JS bundles for AI-driven setup, server-side SEO metadata generation, and LLM content for AI crawlers
24
24
  - **[Server Functions](server-functions.md)** - App-authored server-side "edge functions" (`async (ctx, event) => result`): http/event/cron triggers, the visibility/authority/capabilities security model, and the pre-scoped `ctx` (authority-scoped SDK, capability-gated secrets + fetch)
25
+ - **[Deploying & Registering an App](deploying-apps.md)** - Publish → build → register: the app CDN layout (smartlinks.app), dev/beta/prod channels, channel-scoped deploy keys, the `POST /apps/:appId/releases` registration endpoint + validation, and how to wire it into your build so a bad manifest fails the deploy
25
26
  - **[Realtime](realtime.md)** - Real-time data updates and WebSocket connections
26
27
  - **[iframe Responder](iframe-responder.md)** - iframe integration and cross-origin communication
27
28
  - **[iframe Streaming Parent Changes](iframe-streaming-parent-changes.md)** - Parent-side changes required to support AI streaming in iframe proxy mode
@@ -1722,6 +1723,7 @@ interface AppFunctionDef {
1722
1723
  trigger: AppFunctionTrigger;
1723
1724
  visibility?: AppFunctionVisibility;
1724
1725
  authority?: AppFunctionAuthority;
1726
+ elevated?: boolean;
1725
1727
  capabilities?: string[];
1726
1728
  apiVersion?: string;
1727
1729
  handler?: string;
@@ -0,0 +1,193 @@
1
+ # Deploying & registering an app
2
+
3
+ > **Preview — SmartLinks SDK 2.0.0-alpha.** Part of the installable-app platform being built
4
+ > toward 2.0.0 stable. These APIs may change before then. Published under the npm `next` tag;
5
+ > `latest` remains 1.x.
6
+
7
+ A SmartLinks app is a bundle (widgets, containers, and — new — [server functions](server-functions.md))
8
+ described by an `app.manifest.json`. Deploying an app has two halves:
9
+
10
+ 1. **Publish the bundles** to the SmartLinks app CDN.
11
+ 2. **Register the release** — tell the SmartLinks backend a new version exists, hand it the
12
+ manifest, and let it validate everything. Registration is what makes the app (and its
13
+ server functions) known, installable, and testable.
14
+
15
+ This guide covers registration and how to wire it into your build.
16
+
17
+ ---
18
+
19
+ ## Two "wheres": the API host vs the bundle CDN
20
+
21
+ Deploying touches two different locations — keep them distinct:
22
+
23
+ 1. **The install endpoint (API host)** — *where you register*. This is a SmartLinks
24
+ **API v1** endpoint, and its **host is the environment** you're installing into (see
25
+ below). This is the one that varies per deployment.
26
+ 2. **The bundle CDN** — *where your files are served from*. **Today always `smartlinks.app`**:
27
+
28
+ ```
29
+ https://smartlinks.app/apps/{appId}/{version}/app.manifest.json
30
+ https://smartlinks.app/apps/{appId}/{version}/widgets-<hash>.umd.js
31
+ https://smartlinks.app/apps/{appId}/{version}/functions.umd.js
32
+ ```
33
+
34
+ That base — `https://smartlinks.app/apps/{appId}/{version}` — is your **`bundleBaseUrl`**,
35
+ which you pass at registration. Always pass it explicitly so nothing breaks if the CDN
36
+ location changes later.
37
+
38
+ ## Environments & the app registry
39
+
40
+ Historically an app was **stateless everywhere**: every environment fetched the same CDN
41
+ manifest on the fly, so apps were agnostic to shards, clients, and VPCs. Registration changes
42
+ that — an app is now **installed into an environment's registry**, which is **global to that
43
+ environment (not per-collection / not per-shard)**.
44
+
45
+ An **environment** is one SmartLinks deployment: the main SaaS, or a client's isolated (VPC)
46
+ instance. Each owns its own app registry, so:
47
+
48
+ - The **install endpoint's host is the environment** — `POST https://<that-env's-api>/api/v1/apps/…`.
49
+ Installing into a client's VPC means calling *their* API with *their* deploy key.
50
+ - **Rolling out to several environments = the same call, once per target** (different API host
51
+ + key each). The registration script below takes the API base as a parameter for exactly this.
52
+
53
+ > Enabling an app on a specific **collection** (and consenting to its capabilities) is a
54
+ > separate, per-collection step layered on top of the environment's registry.
55
+
56
+ ---
57
+
58
+ ## Channels
59
+
60
+ A release is registered on one **channel**, matching how you deploy:
61
+
62
+ | Channel | Deployed from | Typical use |
63
+ |---|---|---|
64
+ | `dev` | **Lovable "Publish"** | Live development / testing |
65
+ | `beta` | your pipeline | Staging / preview |
66
+ | `prod` | your **Cloud Build** toolset (from git) | Production |
67
+
68
+ A collection chooses which channel it follows, so you can point a test collection at `dev`
69
+ and exercise a build before it reaches `prod`.
70
+
71
+ ---
72
+
73
+ ## Deploy keys
74
+
75
+ Registration is authenticated by a **deploy key** — not a user login — scoped to the
76
+ channels it may write. Security comes from the *scope*, so a key that leaks can only affect
77
+ what it was allowed to touch.
78
+
79
+ | Key | Lives in | May write |
80
+ |---|---|---|
81
+ | **Dev key** | your app's (private) source / build env | `dev` channel only |
82
+ | **Prod master key** | your Cloud Build toolset only — never in app source | all channels |
83
+ | **Per-app key** *(future)* | a client's build | one specific app |
84
+
85
+ Because a Lovable build has no secret store, the **dev key lives in your app source** — which
86
+ is safe precisely because it can only ever write the `dev` channel. The **prod key stays in
87
+ Cloud Build**. Present the key in the `x-smartlinks-deploy-key` header.
88
+
89
+ ---
90
+
91
+ ## The registration endpoint
92
+
93
+ ```
94
+ POST https://<smartlinks-api>/api/v1/apps/{appId}/releases
95
+ x-smartlinks-deploy-key: <your deploy key>
96
+ Content-Type: application/json
97
+ ```
98
+
99
+ ```jsonc
100
+ {
101
+ "channel": "dev", // dev | beta | prod
102
+ "version": "1.2.3", // from your manifest meta.version
103
+ "build": { "at": "2026-09-14T10:00:00Z", "gitHash": "abc1234", "builder": "lovable" },
104
+ "manifest": { /* your full app.manifest.json, including the functions block */ },
105
+ "bundleBaseUrl": "https://smartlinks.app/apps/currys/1.2.3"
106
+ }
107
+ ```
108
+
109
+ ### Responses
110
+
111
+ | Status | Meaning |
112
+ |---|---|
113
+ | `200` | Registered. Body: `{ ok: true, appId, channel, version, functions: [names], registeredAt }` |
114
+ | `422` | Validation failed — **the build should fail**. Body: `{ ok: false, errors: [...] }` |
115
+ | `401` | Missing/invalid deploy key |
116
+ | `403` | Key not permitted for that channel (e.g. a dev key targeting `prod`) — `CHANNEL_FORBIDDEN` |
117
+
118
+ Each validation error is `{ code, message, path }`, e.g.:
119
+
120
+ ```json
121
+ { "ok": false, "errors": [
122
+ { "code": "AUTHORITY_INVALID", "message": "invalid authority \"root\"", "path": "functions.definitions[0].authority" },
123
+ { "code": "CAPABILITY_INVALID", "message": "invalid capability \"bogus\"", "path": "functions.definitions[0].capabilities[0]" }
124
+ ]}
125
+ ```
126
+
127
+ ### What gets validated
128
+
129
+ - `manifest.meta.appId` must match the `{appId}` in the URL.
130
+ - The **`functions` block** is validated with the *same* rules the runtime enforces
131
+ (names, triggers, `visibility`/`authority`, the capability grammar, duplicate names) — so
132
+ a malformed server function is caught **at deploy time**, not at runtime. See
133
+ [server-functions.md](server-functions.md).
134
+
135
+ ---
136
+
137
+ ## Wiring it into your build
138
+
139
+ Registration is the **last step of your build** — after bundles are built and hashed. Run a
140
+ small script that reads your built manifest and POSTs it, and **exits non-zero on failure**
141
+ so a bad install fails the publish.
142
+
143
+ ```js
144
+ // scripts/register-release.mjs — run as the build's postbuild step
145
+ import { readFileSync } from 'node:fs'
146
+ import { execSync } from 'node:child_process'
147
+
148
+ const API = process.env.SMARTLINKS_API || 'https://api.smartlinks.app'
149
+ const KEY = process.env.SMARTLINKS_DEPLOY_KEY // dev key (in source/env) or prod key (Cloud Build)
150
+ const CHANNEL = process.env.SMARTLINKS_CHANNEL || 'dev' // 'prod' in Cloud Build
151
+
152
+ const manifest = JSON.parse(readFileSync('dist/app.manifest.json', 'utf8'))
153
+ const appId = manifest.meta.appId
154
+ const version = manifest.meta.version
155
+ const gitHash = (() => { try { return execSync('git rev-parse --short HEAD').toString().trim() } catch { return null } })()
156
+
157
+ const res = await fetch(`${API}/api/v1/apps/${appId}/releases`, {
158
+ method: 'POST',
159
+ headers: { 'content-type': 'application/json', 'x-smartlinks-deploy-key': KEY },
160
+ body: JSON.stringify({
161
+ channel: CHANNEL,
162
+ version,
163
+ build: { at: new Date().toISOString(), gitHash, builder: CHANNEL === 'dev' ? 'lovable' : 'cloudbuild' },
164
+ manifest,
165
+ bundleBaseUrl: `https://smartlinks.app/apps/${appId}/${version}`,
166
+ }),
167
+ })
168
+
169
+ const body = await res.json().catch(() => ({}))
170
+ if (!res.ok || !body.ok) {
171
+ console.error(`❌ SmartLinks registration failed (${res.status}):`)
172
+ for (const e of body.errors || []) console.error(` • ${e.path}: ${e.message}`)
173
+ process.exit(1) // fail the build
174
+ }
175
+ console.log(`✅ Registered ${appId}@${version} on "${CHANNEL}" — functions: ${(body.functions || []).join(', ') || 'none'}`)
176
+ ```
177
+
178
+ Wire it after your bundle build/hash step, e.g.:
179
+
180
+ ```jsonc
181
+ // package.json
182
+ "scripts": {
183
+ "build": "vite build && … && node scripts/hash-bundles.mjs",
184
+ "postbuild": "node scripts/register-release.mjs"
185
+ }
186
+ ```
187
+
188
+ - **Dev (Lovable "Publish")** runs `build` → `postbuild` with the **dev key** and
189
+ `SMARTLINKS_CHANNEL=dev`.
190
+ - **Prod (Cloud Build)** runs the same with the **prod key** and `SMARTLINKS_CHANNEL=prod`.
191
+
192
+ That's it: hitting Publish now validates and registers your app — and a broken manifest or
193
+ function stops the deploy with an actionable error instead of shipping.
@@ -1,5 +1,9 @@
1
1
  # Server functions ("edge functions")
2
2
 
3
+ > **Preview — SmartLinks SDK 2.0.0-alpha.** Part of the installable-app platform being built
4
+ > toward 2.0.0 stable (author → register → install → run → test). These APIs may change before
5
+ > then. Published under the npm `next` tag; `latest` remains 1.x.
6
+
3
7
  A **server function** is arbitrary server-side JavaScript your app deploys directly into
4
8
  SmartLinks. It runs on the SmartLinks servers — with access to the full SDK, to your app's
5
9
  secrets, and to outbound network — so you can do things a browser app can't: validate and
@@ -38,6 +42,7 @@ in a bundle alongside your widgets/containers:
38
42
  "trigger": { "type": "http", "methods": ["POST"] },
39
43
  "visibility": "public", // WHO may call it
40
44
  "authority": "collection", // WHOSE authority it runs as
45
+ "elevated": true, // required ack for public + collection
41
46
  "capabilities": ["sl:records:write", "network:api.recaptcha.net"],
42
47
  "apiVersion": "2026-09"
43
48
  },
@@ -104,6 +109,10 @@ functions have no external caller, so they always run as `collection`.
104
109
  > secrets, even though it's "elevated"). But **validating the request is still your job**:
105
110
  > check the payload, rate-limit using `ctx.caller`, guard against replay. Treat the function
106
111
  > body as a trust boundary.
112
+ >
113
+ > Because it's the sharp edge, a `public` + `collection` function must **explicitly opt in**
114
+ > with `elevated: true` in its declaration — a conscious acknowledgment that you're exposing
115
+ > collection authority to public callers. Without it, install/validation fails.
107
116
 
108
117
  ---
109
118
 
@@ -219,6 +228,69 @@ at admin level, attributed to the signed-in admin. The public surface resolves a
219
228
  token is present (→ `owner`) and treats its absence as anonymous (→ `public`); a
220
229
  `collection`-authority function runs elevated regardless.
221
230
 
231
+ ## Testing & preview
232
+
233
+ You don't have to deploy to find out whether a function works. There are three levels of
234
+ fidelity — use them in order.
235
+
236
+ ### 1. Local harness (fast, offline)
237
+
238
+ `@proveanything/smartlinks/testing` builds a `ctx` that **enforces the declared capability
239
+ envelope**, so a function fails locally the same way it would in production — the common
240
+ "I forgot to declare `sl:records:write`" bug is caught before you deploy, not after.
241
+
242
+ ```ts
243
+ import { createFunctionTestContext } from '@proveanything/smartlinks/testing'
244
+ import manifest from '../public/app.manifest.json'
245
+ import { submitCompetitionEntry } from '../src/functions'
246
+
247
+ const def = manifest.functions.definitions.find(d => d.name === 'submitCompetitionEntry')
248
+
249
+ const ctx = createFunctionTestContext({
250
+ def, // capabilities enforced come from the manifest itself
251
+ caller: { userId: 'tester' },
252
+ secrets: { 'recaptcha-secret': 'test-value' }, // fixtures — real secrets are server-only
253
+ })
254
+
255
+ const res = await submitCompetitionEntry(ctx, { method: 'POST', body: { email: 'a@b.com', answer: '42' } })
256
+ // ctx.sl.appRecords.create(...) throws CapabilityError unless `def` declares sl:records:write
257
+ ```
258
+
259
+ Pass the **`def`** (not a hand-typed capability list) so "tested" can't drift from
260
+ "declared". By default `ctx.sl` methods return a stub result (pure unit test — no network);
261
+ inject `sl` to delegate to your live SDK for real reads/writes:
262
+
263
+ ```ts
264
+ const ctx = createFunctionTestContext({
265
+ def,
266
+ sl: { appRecords: { create: (fields) => mySdk.app.records.create(fields) } },
267
+ })
268
+ ```
269
+
270
+ ### 2. Deployed test mode (high fidelity, safe)
271
+
272
+ Register to the `dev` channel and invoke on the real server — real secrets, real data —
273
+ without a live run *(coming next)*: a test invocation is forced to `caller` authority,
274
+ side-effecting writes are dry-run, and the traffic is logged separately from live metrics.
275
+
276
+ ### 3. Live
277
+
278
+ Point a real collection at the channel and invoke for real.
279
+
280
+ ### What differs across the three
281
+
282
+ | | Capabilities | `ctx.sl` | Secrets | Authority | Writes |
283
+ |---|---|---|---|---|---|
284
+ | **Local harness** | Enforced (from `def`) | Stub, or your injected SDK | Fixtures you pass | Informational | Whatever your impl does |
285
+ | **Deployed test** | Enforced | Real (test-scoped) | Real | Forced to `caller` | Dry-run |
286
+ | **Live** | Enforced | Real | Real | As declared | Real |
287
+
288
+ ### Recommended CI pattern
289
+
290
+ 1. **Unit** — run each handler through `createFunctionTestContext` (no network); assert
291
+ behaviour *and* that capabilities are sufficient (an under-declared capability throws).
292
+ 2. **Post-deploy smoke** — after registering to `dev`, hit each function once in test mode.
293
+
222
294
  ## Where functions run (and why it doesn't change how you write them)
223
295
 
224
296
  SmartLinks runs first-party (trusted) functions **in-process** and untrusted third-party
package/openapi.yaml CHANGED
@@ -17281,6 +17281,8 @@ components:
17281
17281
  $ref: "#/components/schemas/AppFunctionVisibility"
17282
17282
  authority:
17283
17283
  $ref: "#/components/schemas/AppFunctionAuthority"
17284
+ elevated:
17285
+ type: boolean
17284
17286
  capabilities:
17285
17287
  type: array
17286
17288
  items:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proveanything/smartlinks",
3
- "version": "1.17.6",
3
+ "version": "2.0.0-alpha.1",
4
4
  "description": "Official JavaScript/TypeScript SDK for the Smartlinks API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,6 +8,10 @@
8
8
  ".": {
9
9
  "types": "./dist/index.d.ts",
10
10
  "default": "./dist/index.js"
11
+ },
12
+ "./testing": {
13
+ "types": "./dist/testing/index.d.ts",
14
+ "default": "./dist/testing/index.js"
11
15
  }
12
16
  },
13
17
  "files": [
@@ -37,7 +41,8 @@
37
41
  "author": "Glenn Shoosmith",
38
42
  "license": "MIT",
39
43
  "publishConfig": {
40
- "access": "public"
44
+ "access": "public",
45
+ "tag": "next"
41
46
  },
42
47
  "dependencies": {
43
48
  "cross-fetch": "^3.1.5"