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