Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/pages/sdk/api/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const constructor = `new MdbaseConnect<Frontmatter>({
})`;

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);
Expand Down Expand Up @@ -114,39 +114,44 @@ const error = `try {
<td>Load, canonicalize, and register the bundled manifest</td>
</tr>
<tr>
<td><code>authorize(operations?)</code></td>
<td>Navigates to approval</td>
<td>Start PKCE and navigate to collection approval</td>
<td><code>authorize(options?)</code></td>
<td>Does not return</td>
<td>Start PKCE; options include operations, collectionId hint, and returnTo</td>
</tr>
<tr>
<td><code>completeAuthorization(callbackUrl?)</code></td>
<td>Connection metadata</td>
<td>Verify state, exchange the code, and retain the grant</td>
<td><code>MdbaseAuthorizationResult</code></td>
<td>Return the bound connection and app-local returnTo value</td>
</tr>
<tr>
<td><code>authorizationCapabilities(required)</code></td>
<td><code>MdbaseAuthorizationCapabilities</code></td>
<td>Compare exact required and granted operations</td>
<td><code>connections()</code></td>
<td><code>MdbaseConnectionInfo[]</code></td>
<td>List every saved collection authorization for this application</td>
</tr>
<tr>
<td><code>hasOperations(required)</code></td>
<td><code>boolean</code></td>
<td>Check an operation subset</td>
<td><code>connection(collectionId)</code></td>
<td><code>MdbaseConnection | null</code></td>
<td>Resolve one permanently collection-bound client</td>
</tr>
<tr>
<td><code>requestOperations(required)</code></td>
<td><code>Promise&lt;void&gt;</code></td>
<td>Request the least-privilege union when the current grant is insufficient</td>
<td><code>onConnectionsChange(listener)</code></td>
<td>Unsubscribe function</td>
<td>Observe additions, removal, refresh, and cross-tab storage changes</td>
</tr>
<tr>
<td><code>disconnect()</code></td>
<td><code>forgetAll()</code></td>
<td><code>void</code></td>
<td>Clear this installation's retained authorization</td>
<td>Forget every locally retained connection for this application</td>
</tr>
</tbody>
</table>

<h2 id="operations">Collection operations</h2>
<p>
The methods below belong to <code>MdbaseConnection</code>, 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.
</p>
<table class="api-table">
<thead>
<tr>
Expand Down Expand Up @@ -208,7 +213,11 @@ const error = `try {
<tr><th>Method</th><th>Purpose</th></tr>
</thead>
<tbody>
<tr><td><code>connection()</code></td><td>Read collection ID, operations, scope, route, and direct status</td></tr>
<tr><td><code>info()</code></td><td>Read collection ID, display name, operations, scope, route, and direct status</td></tr>
<tr><td><code>authorizationCapabilities(required)</code></td><td>Compare required and granted operations for this collection</td></tr>
<tr><td><code>hasOperations(required)</code></td><td>Check an operation subset</td></tr>
<tr><td><code>requestOperations(required, options?)</code></td><td>Request the least-privilege union for this same collection</td></tr>
<tr><td><code>forget()</code></td><td>Forget only this locally retained connection</td></tr>
<tr><td><code>onConnectionChange(listener)</code></td><td>Subscribe and receive an unsubscribe function</td></tr>
<tr><td><code>checkDirectAccess()</code></td><td>Read same-computer availability silently</td></tr>
<tr><td><code>requestDirectAccess()</code></td><td>Prompt from a user gesture where required</td></tr>
Expand Down
36 changes: 22 additions & 14 deletions src/pages/sdk/authorization/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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.
</p>
<p>
One application installation may retain several independent grants. The
manager lists them with <code>connections()</code>; every operation is made
through a <code>MdbaseConnection</code> bound to one collection ID.
</p>

<h2 id="operations">Choose operations by feature</h2>
<table class="api-table">
Expand Down Expand Up @@ -147,9 +154,10 @@ const recover = `try {

<h2 id="disconnect">Disconnecting an application</h2>
<p>
<code>connect.disconnect()</code> 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.
<code>connection.forget()</code> clears only that collection's local
authorization state. <code>manager.forgetAll()</code> 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.
</p>
</DocsLayout>
8 changes: 5 additions & 3 deletions src/pages/sdk/contracts/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -23,8 +23,10 @@ const adapter = `import {
} from "@mdbase/tasknotes";
import { MdbaseConnect } from "@mdbase/connect";

const connect = new MdbaseConnect<TaskFrontmatter>(options);
const tasks = new TasknotesCollection(connect);
const mdbase = new MdbaseConnect<TaskFrontmatter>(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();
Expand Down
18 changes: 13 additions & 5 deletions src/pages/sdk/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export async function listOpenTasks(client: MdbaseCollectionClient<Task>) {
<tbody>
<tr>
<td><code>MdbaseConnect</code></td>
<td>Application entry points, authorization, routing, renewal, notifications, and operations</td>
<td>Application-level manager for saved connections and authorization</td>
<td>Browser APIs or adapters supplied by a native shell</td>
</tr>
<tr>
<td><code>MdbaseConnection</code></td>
<td>One collection's operations, renewal, routing, notifications, and hosted sync</td>
<td>Browser APIs or adapters supplied by a native shell</td>
</tr>
<tr>
Expand All @@ -69,10 +74,13 @@ export async function listOpenTasks(client: MdbaseCollectionClient<Task>) {

<h2 id="application-shape">Application structure</h2>
<p>
Create one <code>MdbaseConnect</code> 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 <code>MdbaseConnect</code> manager at the application boundary.
Resolve the ID in the URL with <code>manager.connection(id)</code>, then
pass that <code>MdbaseConnection</code> 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 <code>MdbaseCollectionClient</code> can also run against the
developer sandbox or another conforming transport.
</p>
<CodeBlock code={seam} language="ts" label="tasks.ts" />
<div class="callout">
Expand Down
4 changes: 2 additions & 2 deletions src/pages/sdk/notifications/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
});`;
Expand All @@ -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: [{
Expand Down
2 changes: 1 addition & 1 deletion src/pages/sdk/offline-sync/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
16 changes: 8 additions & 8 deletions src/pages/sdk/operations/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -38,22 +38,22 @@ 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
}));`;

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,
Expand All @@ -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,
Expand Down
Loading