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 | * - `tree` — tier 1: app logic returns a declarative UI tree; the host |
| 7 | * renders it with its own components on every platform. |
| 8 | * - `react` — tier 2: a Coline-built React bundle runs in the sandboxed |
| 9 | * client runtime. `entry` is the module path within the app |
| 10 | * source. |
| 11 | * - `web` — tier 3: an iframe served from the app's external origin. |
| 12 | * Requires external hosting; excluded from mobile and preview |
| 13 | * composition. |
| 14 | */ |
| 15 |
| 16 | const surfaceEntrySchema = z.string().trim().min(1).max(500); |
| 17 | const surfacePathSchema = z |
| 18 | .string() |
| 19 | .trim() |
| 20 | .min(1) |
| 21 | .max(2000) |
| 22 | .startsWith("/", "Web surface paths must be origin-relative (start with /)."); |
| 23 |
| 24 | export const treeSurfaceSchema = z.object({ |
| 25 | tier: z.literal("tree"), |
| 26 | }); |
| 27 |
| 28 | export const reactSurfaceSchema = z.object({ |
| 29 | tier: z.literal("react"), |
| 30 | entry: surfaceEntrySchema, |
| 31 | }); |
| 32 |
| 33 | export const webSurfaceSchema = z.object({ |
| 34 | tier: z.literal("web"), |
| 35 | path: surfacePathSchema, |
| 36 | }); |
| 37 |
| 38 | export const surfaceVariantSchema = z.discriminatedUnion("tier", [ |
| 39 | treeSurfaceSchema, |
| 40 | reactSurfaceSchema, |
| 41 | webSurfaceSchema, |
| 42 | ]); |
| 43 | export type SurfaceVariant = z.infer<typeof surfaceVariantSchema>; |
| 44 | export type SurfaceTier = SurfaceVariant["tier"]; |
| 45 |
| 46 | /** |
| 47 | * A surface with an optional mobile override (plan §13): universal by |
| 48 | * default; specialize only when the interaction genuinely differs. |
| 49 | */ |
| 50 | export const appSurfaceSchema = z.discriminatedUnion("tier", [ |
| 51 | treeSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 52 | reactSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 53 | webSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 54 | ]); |
| 55 | export type AppSurface = z.infer<typeof appSurfaceSchema>; |
| 56 |
| 57 | /** |
| 58 | * The site surface (SDK 2.1, Coline Sites): the app served as a website on |
| 59 | * its own origin, `{slug}.coline.space` or a custom domain. |
| 60 | * |
| 61 | * The page is a client-rendered React bundle built from `entry`. Coline |
| 62 | * serves an HTML shell per route with the route's `head`, the bundle as a |
| 63 | * content-hashed script, and the files under `assets` at the site root. |
| 64 | * |
| 65 | * Visitors are a principal of their own, never members and never the app. |
| 66 | * They reach data only through collections whose `access` opens them (see |
| 67 | * `collections.ts`), and logic only through the tools listed in `tools`, |
| 68 | * which run at most at the `write` effect. |
| 69 | */ |
| 70 | const SITE_ROUTE_SEGMENT = String.raw`(?:[A-Za-z0-9._~-]+|:[a-z][A-Za-z0-9_]*)`; |
| 71 | export const siteRoutePathSchema = z |
| 72 | .string() |
| 73 | .trim() |
| 74 | .max(300) |
| 75 | .regex( |
| 76 | new RegExp(`^/(?:${SITE_ROUTE_SEGMENT}(?:/${SITE_ROUTE_SEGMENT})*)?(?:/\\*)?$|^/\\*$`), |
| 77 | 'Route paths start with "/", use plain segments or ":params", and may end with "/*".', |
| 78 | ); |
| 79 |
| 80 | const siteImageSchema = z |
| 81 | .string() |
| 82 | .trim() |
| 83 | .max(2000) |
| 84 | .refine( |
| 85 | (value) => value.startsWith("/") || value.startsWith("https://"), |
| 86 | "Images are a path under the site's assets or an https URL.", |
| 87 | ); |
| 88 |
| 89 | export const siteHeadSchema = z.strictObject({ |
| 90 | title: z.string().trim().min(1).max(120).optional(), |
| 91 | description: z.string().trim().max(500).optional(), |
| 92 | /** Link preview image. */ |
| 93 | image: siteImageSchema.optional(), |
| 94 | }); |
| 95 | export type SiteHead = z.infer<typeof siteHeadSchema>; |
| 96 |
| 97 | export const siteRouteSchema = z.strictObject({ |
| 98 | path: siteRoutePathSchema, |
| 99 | head: siteHeadSchema.optional(), |
| 100 | }); |
| 101 | export type SiteRoute = z.infer<typeof siteRouteSchema>; |
| 102 |
| 103 | export const siteSurfaceSchema = z |
| 104 | .strictObject({ |
| 105 | /** Module path of the site's React entry, relative to the app root. */ |
| 106 | entry: surfaceEntrySchema, |
| 107 | /** Directory of static files served at the site root (favicon, images, fonts). */ |
| 108 | assets: surfaceEntrySchema.optional(), |
| 109 | /** Site name and default head for routes that set none. */ |
| 110 | title: z.string().trim().min(1).max(120).optional(), |
| 111 | description: z.string().trim().max(500).optional(), |
| 112 | image: siteImageSchema.optional(), |
| 113 | /** Page language, for the `lang` attribute. */ |
| 114 | lang: z.string().trim().regex(/^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/).default("en"), |
| 115 | routes: z.array(siteRouteSchema).min(1).max(200).default([{ path: "/" }]), |
| 116 | visitors: z |
| 117 | .strictObject({ |
| 118 | /** Offer Sign in with Coline. Off by default: everyone is a guest. */ |
| 119 | signIn: z.boolean().default(false), |
| 120 | }) |
| 121 | .default({ signIn: false }), |
| 122 | /** Tools visitors may run. Each runs at most at the `write` effect. */ |
| 123 | tools: z.array(z.string().trim().min(1).max(160)).max(20).default([]), |
| 124 | }) |
| 125 | .refine( |
| 126 | (site) => new Set(site.routes.map((route) => route.path)).size === site.routes.length, |
| 127 | "Route paths must be unique.", |
| 128 | ); |
| 129 | export type SiteSurface = z.infer<typeof siteSurfaceSchema>; |
| 130 | export type SiteSurfaceInput = z.input<typeof siteSurfaceSchema>; |
| 131 |
| 132 | export const appSurfacesSchema = z |
| 133 | .object({ |
| 134 | home: appSurfaceSchema.optional(), |
| 135 | site: siteSurfaceSchema.optional(), |
| 136 | }) |
| 137 | .default({}); |
| 138 | export type AppSurfaces = z.infer<typeof appSurfacesSchema>; |
| 139 |