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 | type ColineFileTypeDefinition, |
| 7 | type ColineFileTypeDefinitionInput, |
| 8 | type ColineAppRegistrationPayload, |
| 9 | } from "./manifest"; |
| 10 |
| 11 | type UnknownPayload = Record<string, unknown>; |
| 12 |
| 13 | export interface HomeRenderContext extends UnknownPayload {} |
| 14 | export interface FileRenderContext extends UnknownPayload { |
| 15 | fileId?: string; |
| 16 | } |
| 17 | export interface IndexSyncContext extends UnknownPayload { |
| 18 | workspaceId?: string; |
| 19 | } |
| 20 |
| 21 | export type HomeRenderHandler = (context: HomeRenderContext) => unknown | Promise<unknown>; |
| 22 | export type FileRenderHandler = (context: FileRenderContext) => unknown | Promise<unknown>; |
| 23 | export type IndexSyncHandler = (context: IndexSyncContext) => unknown | Promise<unknown>; |
| 24 |
| 25 | export interface ColineAppDefinition { |
| 26 | manifest: ColineAppManifest; |
| 27 | fileTypes: ColineFileTypeDefinition[]; |
| 28 | handlers: { |
| 29 | homeRender?: HomeRenderHandler; |
| 30 | fileRender?: FileRenderHandler; |
| 31 | indexSync?: IndexSyncHandler; |
| 32 | }; |
| 33 | } |
| 34 |
| 35 | export class ColineApp { |
| 36 | readonly manifest: ColineAppManifest; |
| 37 | private readonly fileTypes = new Map<string, ColineFileTypeDefinition>(); |
| 38 | private readonly handlers: ColineAppDefinition["handlers"] = {}; |
| 39 |
| 40 | constructor(manifest: ColineAppManifest) { |
| 41 | this.manifest = colineAppManifestSchema.parse(manifest); |
| 42 | } |
| 43 |
| 44 | defineFileType(fileType: ColineFileTypeDefinitionInput): this { |
| 45 | const parsed = colineFileTypeSchema.parse(fileType); |
| 46 | this.fileTypes.set(parsed.typeKey, parsed); |
| 47 | return this; |
| 48 | } |
| 49 |
| 50 | onHomeRender(handler: HomeRenderHandler): this { |
| 51 | this.handlers.homeRender = handler; |
| 52 | return this; |
| 53 | } |
| 54 |
| 55 | onFileRender(handler: FileRenderHandler): this { |
| 56 | this.handlers.fileRender = handler; |
| 57 | return this; |
| 58 | } |
| 59 |
| 60 | onIndexSync(handler: IndexSyncHandler): this { |
| 61 | this.handlers.indexSync = handler; |
| 62 | return this; |
| 63 | } |
| 64 |
| 65 | getDefinition(): ColineAppDefinition { |
| 66 | return { |
| 67 | manifest: this.manifest, |
| 68 | fileTypes: [...this.fileTypes.values()], |
| 69 | handlers: { ...this.handlers }, |
| 70 | }; |
| 71 | } |
| 72 |
| 73 | getRegistrationPayload(version: string): ColineAppRegistrationPayload { |
| 74 | return colineAppRegistrationPayloadSchema.parse({ |
| 75 | version, |
| 76 | manifest: this.manifest, |
| 77 | fileTypes: [...this.fileTypes.values()], |
| 78 | }); |
| 79 | } |
| 80 | } |
| 81 |