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.
| 4 | import { webhooks } from "./webhooks"; |
| 5 | import { actions } from "./ui"; |
| 6 | import { |
| 7 | parseSignedColineRequest, |
| 8 | colineHostedRenderHomeRequestSchema, |
| 9 | colineHostedRenderFileRequestSchema, |
| 10 | colineHostedActionRequestSchema, |
| 11 | hostedActionResults, |
| 12 | type ColineHostedActionResponse, |
| 13 | } from "./hosted-runtime"; |
| 14 |
| 15 | // --------------------------------------------------------------------------- |
| 16 | // Types |
| 17 | // --------------------------------------------------------------------------- |
| 18 |
| 19 | type WebhookHandler = (event: WebhookEvent) => void | Promise<void>; |
| 20 | type ActionHandler = ( |
| 21 | action: { type: string; [key: string]: unknown }, |
| 22 | context: { |
| 23 | workspace: { id: string; slug: string; name: string }; |
| 24 | app: { appInstallId: string; appId: string; appKey: string; name: string }; |
| 25 | actor: { userId: string; email: string } | null; |
| 26 | file: { id: string; typeKey: string; title?: string | undefined } | null; |
| 27 | }, |
| 28 | ) => ColineHostedActionResponse | Promise<ColineHostedActionResponse>; |
| 29 |
| 30 | export interface CreateHandlerOptions { |
| 31 | /** |
| 32 | * The ColineApp instance that defines your app's manifest, file types, |
| 33 | * and render handlers. |
| 34 | */ |
| 35 | app: ColineApp; |
| 36 |
| 37 | /** |
| 38 | * Your app's delivery secret from the Coline developer console. |
| 39 | * Used to verify that incoming requests are genuinely from Coline. |
| 40 | */ |
| 41 | secret: string; |
| 42 |
| 43 | /** |
| 44 | * Handle incoming webhook events (messages, tasks, notes, etc.). |
| 45 | * Optional — only needed if your app subscribes to webhooks. |
| 46 | */ |
| 47 | onWebhook?: WebhookHandler | undefined; |
| 48 |
| 49 | /** |
| 50 | * Handle UI action callbacks (button clicks, form submits, etc.). |
| 51 | * Optional — defaults to returning `{ type: "ok" }`. |
| 52 | */ |
| 53 | onAction?: ActionHandler | undefined; |
| 54 | } |
| 55 |
| 56 | // --------------------------------------------------------------------------- |
| 57 | // Route matching |
| 58 | // --------------------------------------------------------------------------- |
| 59 |
| 60 | const ROUTES = { |
| 61 | manifest: "/coline/manifest", |
| 62 | renderHome: "/coline/render/home", |
| 63 | renderFile: "/coline/render/file", |
| 64 | actions: "/coline/actions", |
| 65 | events: "/coline/events", |
| 66 | } as const; |
| 67 |
| 68 | function matchRoute(pathname: string): keyof typeof ROUTES | null { |
| 69 | // Normalize trailing slashes |
| 70 | const normalized = pathname.replace(/\/+$/, ""); |
| 71 | for (const [key, path] of Object.entries(ROUTES)) { |
| 72 | if (normalized === path) return key as keyof typeof ROUTES; |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 |
| 77 | // --------------------------------------------------------------------------- |
| 78 | // Main handler factory |
| 79 | // --------------------------------------------------------------------------- |
| 80 |
| 81 | /** |
| 82 | * Creates a single `(request: Request) => Promise<Response>` handler that |
| 83 | * routes all Coline platform requests to the correct handler. |
| 84 | * |
| 85 | * Mount this on your HTTP server and point all `/coline/*` traffic to it. |
| 86 | * |
| 87 | * @example |
| 88 | * ```ts |
| 89 | * // === Hono === |
| 90 | * import { Hono } from "hono"; |
| 91 | * import { createHandler } from "@colineapp/sdk"; |
| 92 | * |
| 93 | * const handler = createHandler({ |
| 94 | * app: myColineApp, |
| 95 | * secret: process.env.COLINE_DELIVERY_SECRET!, |
| 96 | * onWebhook: (event) => { |
| 97 | * console.log("Webhook:", event.type, event.data); |
| 98 | * }, |
| 99 | * }); |
| 100 | * |
| 101 | * const server = new Hono(); |
| 102 | * server.all("/coline/*", (c) => handler(c.req.raw)); |
| 103 | * export default server; |
| 104 | * ``` |
| 105 | * |
| 106 | * @example |
| 107 | * ```ts |
| 108 | * // === Next.js App Router (app/coline/[...path]/route.ts) === |
| 109 | * import { createHandler } from "@colineapp/sdk"; |
| 110 | * |
| 111 | * const handler = createHandler({ |
| 112 | * app: myColineApp, |
| 113 | * secret: process.env.COLINE_DELIVERY_SECRET!, |
| 114 | * }); |
| 115 | * |
| 116 | * export const GET = handler; |
| 117 | * export const POST = handler; |
| 118 | * ``` |
| 119 | * |
| 120 | * @example |
| 121 | * ```ts |
| 122 | * // === Cloudflare Workers / Bun / Deno === |
| 123 | * import { createHandler } from "@colineapp/sdk"; |
| 124 | * |
| 125 | * const handler = createHandler({ |
| 126 | * app: myColineApp, |
| 127 | * secret: env.COLINE_DELIVERY_SECRET, |
| 128 | * }); |
| 129 | * |
| 130 | * export default { fetch: handler }; |
| 131 | * ``` |
| 132 | */ |
| 133 | export function createHandler( |
| 134 | options: CreateHandlerOptions, |
| 135 | ): (request: Request) => Promise<Response> { |
| 136 | const { app, secret, onWebhook, onAction } = options; |
| 137 | const definition = app.getDefinition(); |
| 138 |
| 139 | return async function handle(request: Request): Promise<Response> { |
| 140 | const url = new URL(request.url); |
| 141 | const route = matchRoute(url.pathname); |
| 142 |
| 143 | if (!route) { |
| 144 | return new Response( |
| 145 | JSON.stringify({ error: "Not a Coline route" }), |
| 146 | { status: 404, headers: { "Content-Type": "application/json" } }, |
| 147 | ); |
| 148 | } |
| 149 |
| 150 | // GET /coline/manifest — no auth required |
| 151 | if (route === "manifest") { |
| 152 | if (request.method === "GET" || request.method === "HEAD") { |
| 153 | return app.handleManifestRequest(); |
| 154 | } |
| 155 | return new Response(null, { status: 405 }); |
| 156 | } |
| 157 |
| 158 | // All other routes require POST + signature verification |
| 159 | if (request.method !== "POST") { |
| 160 | return new Response(null, { status: 405 }); |
| 161 | } |
| 162 |
| 163 | try { |
| 164 | switch (route) { |
| 165 | case "renderHome": { |
| 166 | if (!definition.handlers.homeRender) { |
| 167 | return jsonResponse({ error: "Home render not implemented" }, 501); |
| 168 | } |
| 169 | const { data } = await parseSignedColineRequest({ |
| 170 | request, |
| 171 | secret, |
| 172 | schema: colineHostedRenderHomeRequestSchema, |
| 173 | }); |
| 174 | const homeCtx: HomeRenderContext = { |
| 175 | workspace: { |
| 176 | workspaceId: data.workspace.id, |
| 177 | workspaceSlug: data.workspace.slug, |
| 178 | workspaceName: data.workspace.name, |
| 179 | }, |
| 180 | app: { appKey: data.app.appKey, appInstallId: data.app.appInstallId }, |
| 181 | actor: data.actor ?? null, |
| 182 | routes: actions, |
| 183 | }; |
| 184 | const result = await definition.handlers.homeRender(homeCtx); |
| 185 | return Response.json(result); |
| 186 | } |
| 187 |
| 188 | case "renderFile": { |
| 189 | if (!definition.handlers.fileRender) { |
| 190 | return jsonResponse({ error: "File render not implemented" }, 501); |
| 191 | } |
| 192 | const { data } = await parseSignedColineRequest({ |
| 193 | request, |
| 194 | secret, |
| 195 | schema: colineHostedRenderFileRequestSchema, |
| 196 | }); |
| 197 | const fileCtx: FileRenderContext = { |
| 198 | workspace: { |
| 199 | workspaceId: data.workspace.id, |
| 200 | workspaceSlug: data.workspace.slug, |
| 201 | workspaceName: data.workspace.name, |
| 202 | }, |
| 203 | app: { appKey: data.app.appKey, appInstallId: data.app.appInstallId }, |
| 204 | actor: data.actor ?? null, |
| 205 | routes: actions, |
| 206 | file: data.file, |
| 207 | document: data.document, |
| 208 | }; |
| 209 | const result = await definition.handlers.fileRender(fileCtx); |
| 210 | return Response.json(result); |
| 211 | } |
| 212 |
| 213 | case "actions": { |
| 214 | const { data } = await parseSignedColineRequest({ |
| 215 | request, |
| 216 | secret, |
| 217 | schema: colineHostedActionRequestSchema, |
| 218 | }); |
| 219 | if (onAction) { |
| 220 | const result = await onAction( |
| 221 | data.action as { type: string;[key: string]: unknown }, |
| 222 | { |
| 223 | workspace: data.workspace, |
| 224 | app: data.app, |
| 225 | actor: data.actor ?? null, |
| 226 | file: data.file ?? null, |
| 227 | }, |
| 228 | ); |
| 229 | return Response.json(result); |
| 230 | } |
| 231 | return Response.json(hostedActionResults.ok()); |
| 232 | } |
| 233 |
| 234 | case "events": { |
| 235 | const body = await request.text(); |
| 236 | const headers = Object.fromEntries(request.headers.entries()); |
| 237 | const event = await webhooks.constructEvent(body, headers, secret); |
| 238 | if (onWebhook) { |
| 239 | await onWebhook(event); |
| 240 | } |
| 241 | return jsonResponse({ received: true }, 200); |
| 242 | } |
| 243 |
| 244 | default: |
| 245 | return jsonResponse({ error: "Unknown route" }, 404); |
| 246 | } |
| 247 | } catch (err) { |
| 248 | const message = err instanceof Error ? err.message : "Internal error"; |
| 249 | return jsonResponse({ error: message }, 400); |
| 250 | } |
| 251 | }; |
| 252 | } |
| 253 |
| 254 | function jsonResponse(body: unknown, status: number): Response { |
| 255 | return new Response(JSON.stringify(body), { |
| 256 | status, |
| 257 | headers: { "Content-Type": "application/json" }, |
| 258 | }); |
| 259 | } |
| 260 |