The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 6 | } from "./types"; |
| 7 |
| 8 | /** |
| 9 | * The HTTP transport for `coline.*` (plan §8): capability calls become |
| 10 | * `POST { operation, args }` against the invocation's capability endpoint, |
| 11 | * authorized by the invocation token. Used by external app backends and by |
| 12 | * the hosted Dynamic Worker bootstrap — one wire contract for both. |
| 13 | * |
| 14 | * Client-side interaction capabilities (pickers, navigation, realtime) do |
| 15 | * not exist in server contexts and reject immediately with |
| 16 | * `capability_unavailable`. |
| 17 | */ |
| 18 |
| 19 | export interface CapabilityEndpoint { |
| 20 | url: string; |
| 21 | token: string; |
| 22 | fetchImpl?: typeof fetch | undefined; |
| 23 | } |
| 24 |
| 25 | export class ColineCapabilityCallError extends Error { |
| 26 | readonly code: string; |
| 27 | readonly operation: string; |
| 28 |
| 29 | constructor(operation: string, code: string, message: string) { |
| 30 | super(message); |
| 31 | this.name = "ColineCapabilityCallError"; |
| 32 | this.code = code; |
| 33 | this.operation = operation; |
| 34 | } |
| 35 | } |
| 36 |
| 37 | async function callCapability<T>( |
| 38 | endpoint: CapabilityEndpoint, |
| 39 | operation: string, |
| 40 | args: unknown[], |
| 41 | ): Promise<T> { |
| 42 | const fetchImpl = endpoint.fetchImpl ?? fetch; |
| 43 | let response: Response; |
| 44 | try { |
| 45 | response = await fetchImpl(endpoint.url, { |
| 46 | method: "POST", |
| 47 | headers: { |
| 48 | "content-type": "application/json", |
| 49 | authorization: `Bearer ${endpoint.token}`, |
| 50 | }, |
| 51 | body: JSON.stringify({ operation, args }), |
| 52 | }); |
| 53 | } catch (error) { |
| 54 | throw new ColineCapabilityCallError( |
| 55 | operation, |
| 56 | "network_error", |
| 57 | `Capability call failed to reach Coline: ${error instanceof Error ? error.message : String(error)}`, |
| 58 | ); |
| 59 | } |
| 60 |
| 61 | let parsed: unknown; |
| 62 | try { |
| 63 | parsed = await response.json(); |
| 64 | } catch { |
| 65 | throw new ColineCapabilityCallError( |
| 66 | operation, |
| 67 | "invalid_response", |
| 68 | `Capability endpoint returned a non-JSON response (${response.status}).`, |
| 69 | ); |
| 70 | } |
| 71 |
| 72 | const record = parsed as { data?: unknown; error?: { code?: string; message?: string } }; |
| 73 | if (record.error) { |
| 74 | throw new ColineCapabilityCallError( |
| 75 | operation, |
| 76 | record.error.code ?? "capability_error", |
| 77 | record.error.message ?? "Capability call failed.", |
| 78 | ); |
| 79 | } |
| 80 | if (!response.ok) { |
| 81 | throw new ColineCapabilityCallError( |
| 82 | operation, |
| 83 | "capability_error", |
| 84 | `Capability endpoint returned ${response.status}.`, |
| 85 | ); |
| 86 | } |
| 87 | return record.data as T; |
| 88 | } |
| 89 |
| 90 | function unavailable(operation: string): never { |
| 91 | throw new ColineCapabilityCallError( |
| 92 | operation, |
| 93 | "capability_unavailable", |
| 94 | `"${operation}" is a client-context capability and is not available to server-side app logic.`, |
| 95 | ); |
| 96 | } |
| 97 |
| 98 | function createHttpCollection<TData>( |
| 99 | endpoint: CapabilityEndpoint, |
| 100 | name: string, |
| 101 | ): ColineCollection<TData> { |
| 102 | return { |
| 103 | insert: (data) => |
| 104 | callCapability<ColineRecordEnvelope<TData>>(endpoint, "storage.records.insert", [name, data]), |
| 105 | get: (recordId) => |
| 106 | callCapability<ColineRecordEnvelope<TData> | null>(endpoint, "storage.records.get", [name, recordId]), |
| 107 | update: (recordId, data) => |
| 108 | callCapability<ColineRecordEnvelope<TData>>(endpoint, "storage.records.update", [name, recordId, data]), |
| 109 | delete: (recordId) => |
| 110 | callCapability<void>(endpoint, "storage.records.delete", [name, recordId]), |
| 111 | query: (query?: ColineRecordQuery<TData>) => |
| 112 | callCapability<{ |
| 113 | records: Array<ColineRecordEnvelope<TData>>; |
| 114 | nextCursor: string | null; |
| 115 | }>(endpoint, "storage.records.query", [name, query ?? {}]), |
| 116 | }; |
| 117 | } |
| 118 |
| 119 | export function createHttpCapabilities(endpoint: CapabilityEndpoint): ColineCapabilities { |
| 120 | return { |
| 121 | workspace: { |
| 122 | get: () => callCapability(endpoint, "workspace.get", []), |
| 123 | }, |
| 124 | members: { |
| 125 | list: (input) => callCapability(endpoint, "members.list", [input ?? {}]), |
| 126 | get: (userId) => callCapability(endpoint, "members.get", [userId]), |
| 127 | }, |
| 128 | files: { |
| 129 | list: (input) => callCapability(endpoint, "files.list", [input ?? {}]), |
| 130 | get: (fileId) => callCapability(endpoint, "files.get", [fileId]), |
| 131 | create: (input) => callCapability(endpoint, "files.create", [input]), |
| 132 | update: (fileId, input) => callCapability(endpoint, "files.update", [fileId, input]), |
| 133 | trash: (fileId) => callCapability(endpoint, "files.trash", [fileId]), |
| 134 | getDocument: (fileId) => callCapability(endpoint, "files.getDocument", [fileId]), |
| 135 | updateDocument: (fileId, document) => |
| 136 | callCapability(endpoint, "files.updateDocument", [fileId, document]), |
| 137 | }, |
| 138 | drives: { |
| 139 | getAppDrive: () => callCapability(endpoint, "drives.getAppDrive", []), |
| 140 | }, |
| 141 | storage: { |
| 142 | kv: { |
| 143 | get: (key) => callCapability(endpoint, "storage.kv.get", [key]), |
| 144 | set: (key, value) => callCapability(endpoint, "storage.kv.set", [key, value]), |
| 145 | delete: (key) => callCapability(endpoint, "storage.kv.delete", [key]), |
| 146 | list: (input) => callCapability(endpoint, "storage.kv.list", [input ?? {}]), |
| 147 | }, |
| 148 | collection: <TData>(name: string) => createHttpCollection<TData>(endpoint, name), |
| 149 | }, |
| 150 | search: { |
| 151 | query: (input) => callCapability(endpoint, "search.query", [input]), |
| 152 | index: { |
| 153 | upsert: (input) => callCapability(endpoint, "search.index.upsert", [input]), |
| 154 | delete: (input) => callCapability(endpoint, "search.index.delete", [input]), |
| 155 | }, |
| 156 | }, |
| 157 | notifications: { |
| 158 | create: (input) => callCapability(endpoint, "notifications.create", [input]), |
| 159 | }, |
| 160 | ai: { |
| 161 | generate: (prompt, options) => |
| 162 | callCapability(endpoint, "ai.generate", [prompt, options ?? {}]), |
| 163 | }, |
| 164 | net: { |
| 165 | fetch: (input) => callCapability(endpoint, "net.fetch", [input]), |
| 166 | }, |
| 167 | pickers: { |
| 168 | file: () => unavailable("pickers.file"), |
| 169 | member: () => unavailable("pickers.member"), |
| 170 | confirm: () => unavailable("pickers.confirm"), |
| 171 | }, |
| 172 | navigation: { |
| 173 | openFile: () => unavailable("navigation.openFile"), |
| 174 | openAppHome: () => unavailable("navigation.openAppHome"), |
| 175 | navigate: () => unavailable("navigation.navigate"), |
| 176 | openReference: () => unavailable("navigation.openReference"), |
| 177 | }, |
| 178 | realtime: { |
| 179 | // Server-side logic cannot hold a socket; it publishes, clients |
| 180 | // subscribe (tier-2 via the bridge). |
| 181 | subscribe: () => unavailable("realtime.subscribe"), |
| 182 | publish: (event, data) => |
| 183 | callCapability(endpoint, "realtime.publish", [{ event, data }]) as Promise<{ |
| 184 | delivered: boolean; |
| 185 | }>, |
| 186 | }, |
| 187 | events: { |
| 188 | emit: (event, data) => |
| 189 | callCapability(endpoint, "events.emit", [{ event, data }]) as Promise<{ |
| 190 | emitted: boolean; |
| 191 | }>, |
| 192 | }, |
| 193 | }; |
| 194 | } |
| 195 |