diff --git a/src/pages/sdk/api/index.astro b/src/pages/sdk/api/index.astro index 6077697..d0cf7b3 100644 --- a/src/pages/sdk/api/index.astro +++ b/src/pages/sdk/api/index.astro @@ -15,7 +15,7 @@ const constructor = `new MdbaseConnect({ })`; const error = `try { - const result = unwrapOperation(await connect.update(input)); + const result = unwrapOperation(await connection.update(input)); } catch (error) { if (error instanceof MdbaseOperationValidationError) { renderDiagnostics(error.diagnostics); @@ -114,39 +114,44 @@ const error = `try { Load, canonicalize, and register the bundled manifest - authorize(operations?) - Navigates to approval - Start PKCE and navigate to collection approval + authorize(options?) + Does not return + Start PKCE; options include operations, collectionId hint, and returnTo completeAuthorization(callbackUrl?) - Connection metadata - Verify state, exchange the code, and retain the grant + MdbaseAuthorizationResult + Return the bound connection and app-local returnTo value - authorizationCapabilities(required) - MdbaseAuthorizationCapabilities - Compare exact required and granted operations + connections() + MdbaseConnectionInfo[] + List every saved collection authorization for this application - hasOperations(required) - boolean - Check an operation subset + connection(collectionId) + MdbaseConnection | null + Resolve one permanently collection-bound client - requestOperations(required) - Promise<void> - Request the least-privilege union when the current grant is insufficient + onConnectionsChange(listener) + Unsubscribe function + Observe additions, removal, refresh, and cross-tab storage changes - disconnect() + forgetAll() void - Clear this installation's retained authorization + Forget every locally retained connection for this application

Collection operations

+

+ The methods below belong to MdbaseConnection, not the manager. + A repository that holds one connection cannot accidentally read or write a + different collection when the user changes another part of the UI. +

@@ -208,7 +213,11 @@ const error = `try { - + + + + + diff --git a/src/pages/sdk/authorization/index.astro b/src/pages/sdk/authorization/index.astro index c453542..f3f7cdf 100644 --- a/src/pages/sdk/authorization/index.astro +++ b/src/pages/sdk/authorization/index.astro @@ -3,40 +3,42 @@ import DocsLayout from "../../../components/DocsLayout.astro"; import CodeBlock from "../../../components/CodeBlock.astro"; const stepUp = `const required = ["read", "query", "update"] as const; -const capability = connect.authorizationCapabilities([...required]); +const capability = connection.authorizationCapabilities([...required]); if (!capability.sufficient) { - await connect.requestOperations([...required]); + await connection.requestOperations([...required], { + returnTo: location.pathname + location.search + }); }`; -const listen = `const stop = connect.onConnectionChange((connection) => { - if (!connection) { +const listen = `const stop = connection.onConnectionChange((info) => { + if (!info) { renderDisconnected(); return; } renderConnection({ - collectionId: connection.collectionId, - route: connection.route, - operations: connection.operations + collectionId: info.collectionId, + route: info.route, + operations: info.operations }); }); // Call stop() when the owning UI is destroyed.`; const recover = `try { - await connect.update(input); + await connection.update(input); } catch (error) { if (error instanceof MdbaseConnectError) { switch (error.recovery) { case "reauthorize": - await connect.requestOperations(["update"]); + await connection.requestOperations(["update"]); break; case "refresh": await refreshVisibleState(); break; case "resolve_outcome": - await connect.resumePendingMutation(input); + await connection.resumePendingMutation(input); break; } } @@ -61,6 +63,11 @@ const recover = `try { one collection, one scope, and an exact operation set. The collection authority checks the grant again for every request.

+

+ One application installation may retain several independent grants. The + manager lists them with connections(); every operation is made + through a MdbaseConnection bound to one collection ID. +

Choose operations by feature

MethodPurpose
connection()Read collection ID, operations, scope, route, and direct status
info()Read collection ID, display name, operations, scope, route, and direct status
authorizationCapabilities(required)Compare required and granted operations for this collection
hasOperations(required)Check an operation subset
requestOperations(required, options?)Request the least-privilege union for this same collection
forget()Forget only this locally retained connection
onConnectionChange(listener)Subscribe and receive an unsubscribe function
checkDirectAccess()Read same-computer availability silently
requestDirectAccess()Prompt from a user gesture where required
@@ -147,9 +154,10 @@ const recover = `try {

Disconnecting an application

- connect.disconnect() clears this application's local - authorization state. Grant revocation remains under user control at the - collection authority. Provide a clear path to the Connect controller for - reviewing, narrowing, pausing, or revoking a grant. + connection.forget() clears only that collection's local + authorization state. manager.forgetAll() clears the saved set. + Neither replaces user-controlled revocation at the + collection authority. Provide a clear path to the Connect controller when a + user needs to review, narrow, pause, or revoke a grant.

diff --git a/src/pages/sdk/contracts/index.astro b/src/pages/sdk/contracts/index.astro index 4525f51..3c9ff2a 100644 --- a/src/pages/sdk/contracts/index.astro +++ b/src/pages/sdk/contracts/index.astro @@ -2,7 +2,7 @@ import DocsLayout from "../../../components/DocsLayout.astro"; import CodeBlock from "../../../components/CodeBlock.astro"; -const generic = `const description = await connect.describe(); +const generic = `const description = await connection.describe(); const taskContract = description.contracts.find( (contract) => @@ -23,8 +23,10 @@ const adapter = `import { } from "@mdbase/tasknotes"; import { MdbaseConnect } from "@mdbase/connect"; -const connect = new MdbaseConnect(options); -const tasks = new TasknotesCollection(connect); +const mdbase = new MdbaseConnect(options); +const connection = mdbase.connection(collectionId); +if (!connection) throw new Error("Choose a collection."); +const tasks = new TasknotesCollection(connection); await tasks.describe(); const open = await tasks.list(); diff --git a/src/pages/sdk/index.astro b/src/pages/sdk/index.astro index 4d4cfef..9ade993 100644 --- a/src/pages/sdk/index.astro +++ b/src/pages/sdk/index.astro @@ -46,7 +46,12 @@ export async function listOpenTasks(client: MdbaseCollectionClient) { - + + + + + + @@ -69,10 +74,13 @@ export async function listOpenTasks(client: MdbaseCollectionClient) {

Application structure

- Create one MdbaseConnect instance at the application boundary. - Pass its collection-client behavior into feature code and keep OAuth state - at that boundary. The same feature code can then run against Connect, the - developer sandbox, or a future conforming transport. + Create one MdbaseConnect manager at the application boundary. + Resolve the ID in the URL with manager.connection(id), then + pass that MdbaseConnection into feature code while keeping + OAuth and saved-connection state at the boundary. A bound connection never + changes collections underneath a repository, and feature code written + against MdbaseCollectionClient can also run against the + developer sandbox or another conforming transport.

diff --git a/src/pages/sdk/notifications/index.astro b/src/pages/sdk/notifications/index.astro index beecb31..7d87892 100644 --- a/src/pages/sdk/notifications/index.astro +++ b/src/pages/sdk/notifications/index.astro @@ -28,7 +28,7 @@ const browser = `const worker = await navigator.serviceWorker.register( "/service-worker.js" ); -await connect.registerNotifications({ +await connection.registerNotifications({ serviceWorker: worker, criteria: ["task.changed"] });`; @@ -44,7 +44,7 @@ self.addEventListener("push", (event) => { ); });`; -const timers = `await connect.reconcileTimers({ +const timers = `await connection.reconcileTimers({ namespace: "task-reminders", criterion_id: "task.reminder", timers: [{ diff --git a/src/pages/sdk/offline-sync/index.astro b/src/pages/sdk/offline-sync/index.astro index 8fb6108..68c6772 100644 --- a/src/pages/sdk/offline-sync/index.astro +++ b/src/pages/sdk/offline-sync/index.astro @@ -8,7 +8,7 @@ const createReplica = `import { type ReplicaData } from "@mdbase/connect-sync"; -const hosted = connect.hostedSync(); +const hosted = connection.hostedSync(); if (!hosted) { throw new Error("Authorize a hosted collection first."); } diff --git a/src/pages/sdk/operations/index.astro b/src/pages/sdk/operations/index.astro index 3fdffbd..4f6e6fa 100644 --- a/src/pages/sdk/operations/index.astro +++ b/src/pages/sdk/operations/index.astro @@ -2,11 +2,11 @@ import DocsLayout from "../../../components/DocsLayout.astro"; import CodeBlock from "../../../components/CodeBlock.astro"; -const readQuery = `const record = unwrapOperation(await connect.read({ +const readQuery = `const record = unwrapOperation(await connection.read({ path: "tasks/release.md" })); -const open = unwrapOperation(await connect.query({ +const open = unwrapOperation(await connection.query({ types: ["task"], where: 'status == "open" && due != null', order_by: [ @@ -15,7 +15,7 @@ const open = unwrapOperation(await connect.query({ limit: 100 }));`; -const pageQuery = `for await (const page of connect.queryPages( +const pageQuery = `for await (const page of connection.queryPages( { types: ["task"], where: 'status != "done"' }, { firstPageSize: 100, @@ -27,7 +27,7 @@ const pageQuery = `for await (const page of connect.queryPages( appendRows(page.results); }`; -const createUpdate = `const created = unwrapOperation(await connect.create({ +const createUpdate = `const created = unwrapOperation(await connection.create({ path: "tasks/release.md", type: "task", frontmatter: { @@ -38,7 +38,7 @@ const createUpdate = `const created = unwrapOperation(await connect.create({ body: "Release checklist." })); -unwrapOperation(await connect.update({ +unwrapOperation(await connection.update({ path: created.path, patch: { status: "done" }, if_revision: created.revision @@ -46,14 +46,14 @@ unwrapOperation(await connect.update({ const watch = `const controller = new AbortController(); -for await (const change of connect.watch({ +for await (const change of connection.watch({ signal: controller.signal, onStatus: (status) => renderWatchStatus(status) })) { applyChange(change); }`; -const preflight = `const preview = unwrapOperation(await connect.preflightRename({ +const preflight = `const preview = unwrapOperation(await connection.preflightRename({ from: "tasks/release.md", to: "archive/release.md", update_refs: true, @@ -62,7 +62,7 @@ const preflight = `const preview = unwrapOperation(await connect.preflightRename showImpact(preview.references_affected, preview.warnings); -await connect.renameWithProgress({ +await connection.renameWithProgress({ from: preview.from, to: preview.to, update_refs: true, diff --git a/src/pages/sdk/quickstart/index.astro b/src/pages/sdk/quickstart/index.astro index 3a25709..9403c34 100644 --- a/src/pages/sdk/quickstart/index.astro +++ b/src/pages/sdk/quickstart/index.astro @@ -29,31 +29,41 @@ export interface Workout extends JsonObject { completed?: boolean; } -export const connect = new MdbaseConnect({ +export const mdbase = new MdbaseConnect({ serverUrl: "https://connect.mdbase.dev", manifest: new URL("/.well-known/mdbase-app.json", location.origin).href, redirectUri: new URL("/auth/mdbase/callback", location.origin).href });`; -const connectButton = `import { connect } from "./connect"; +const connectButton = `import { mdbase } from "./connect"; export async function connectCollection() { - await connect.authorize(["describe", "read", "query", "update"]); - // Authorization continues on the registered callback route. + await mdbase.authorize({ + operations: ["describe", "read", "query", "update"], + returnTo: location.pathname + location.search + }); + // authorize() navigates to Connect and does not return. }`; -const callback = `import { connect } from "./connect"; +const callback = `import { mdbase } from "./connect"; export async function completeMdbaseCallback() { - const connection = await connect.completeAuthorization(location.href); - history.replaceState({}, "", "/"); - return connection; + const { connection, returnTo = "/" } = + await mdbase.completeAuthorization(location.href); + const destination = new URL(returnTo, location.origin); + destination.searchParams.set("collection", connection.collectionId); + history.replaceState({}, "", destination); + return connection; // Permanently bound to this collection. }`; -const query = `import { connect } from "./connect"; +const query = `import { mdbase } from "./connect"; import { unwrapOperation } from "@mdbase/connect"; -const page = unwrapOperation(await connect.query({ +const collectionId = new URL(location.href).searchParams.get("collection"); +const collection = collectionId ? mdbase.connection(collectionId) : null; +if (!collection) throw new Error("Choose a collection."); + +const page = unwrapOperation(await collection.query({ types: ["workout"], where: 'completed != true', order_by: [{ field: "title", direction: "asc" }], @@ -62,7 +72,7 @@ const page = unwrapOperation(await connect.query({ const first = page.results[0]; if (first) { - unwrapOperation(await connect.update({ + unwrapOperation(await collection.update({ path: first.path, patch: { completed: true }, if_revision: first.revision @@ -121,9 +131,10 @@ const validate = `mdbase-connect-dev validate-manifest \\

3. Create one Connect client

- Keep this instance stable. It owns persisted authorization state, refresh - rotation, the non-extractable relay key, route selection, and connection - change notifications. + Keep this manager stable. It owns the set of saved connections and returns + a separate collection-bound client for each one. Feature and repository + code should receive that bound client, never ambient “current collection” + state.

4. Start authorization from a user action

@@ -138,7 +149,7 @@ const validate = `mdbase-connect-dev validate-manifest \\

Call this once on the exact callback route listed in the manifest. PKCE is mandatory. On success, the client stores the grant-bound authorization and - emits the active connection. + returns the newly approved collection-bound connection.

6. Query and update with a revision

@@ -148,6 +159,16 @@ const validate = `mdbase-connect-dev validate-manifest \\ {" "}if_revision when changing the record so a concurrent edit cannot be silently overwritten.

+
+ Use a stable collection ID in app URLs. +

+ A query parameter such as ?collection=<id> works on + static hosts, survives refreshes, and makes a view bookmarkable. Treat an + explicit ID as authoritative: if it is not saved, show the chooser or + reconnect that ID instead of silently opening another collection. Names + are display text and can change. +

+
Resolve domain fields from the contract descriptor.

diff --git a/src/pages/sdk/routing/index.astro b/src/pages/sdk/routing/index.astro index bad3a3f..a1479f5 100644 --- a/src/pages/sdk/routing/index.astro +++ b/src/pages/sdk/routing/index.astro @@ -2,20 +2,20 @@ import DocsLayout from "../../../components/DocsLayout.astro"; import CodeBlock from "../../../components/CodeBlock.astro"; -const direct = `const status = await connect.checkDirectAccess(); +const direct = `const status = await connection.checkDirectAccess(); if (status === "permission_required") { directAccessButton.onclick = async () => { - await connect.requestDirectAccess(); + await connection.requestDirectAccess(); }; } -connect.onConnectionChange((connection) => { +connection.onConnectionChange((info) => { // "direct", "relay", or "hosted" - renderRoute(connection?.route); + renderRoute(info?.route); });`; -const options = `const connect = new MdbaseConnect({ +const options = `const mdbase = new MdbaseConnect({ serverUrl: "https://connect.mdbase.dev", manifest, redirectUri, @@ -44,6 +44,11 @@ const options = `const connect = new MdbaseConnect({ relay. The user chooses the collection authority. The SDK selects the network route for each request.

+

+ Routing state belongs to a collection-bound MdbaseConnection. + Different saved collections can be hosted, direct, or relayed at the same + time. +

Request same-computer browser access from a user gesture

MdbaseConnectApplication entry points, authorization, routing, renewal, notifications, and operationsApplication-level manager for saved connections and authorizationBrowser APIs or adapters supplied by a native shell
MdbaseConnectionOne collection's operations, renewal, routing, notifications, and hosted sync Browser APIs or adapters supplied by a native shell