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.
| COLINE_DELIVERY_HEADER, |
| 5 | COLINE_SIGNATURE_HEADER, |
| 6 | COLINE_TIMESTAMP_HEADER, |
| 7 | verifyAppRequestSignature, |
| 8 | } from "./signing"; |
| 9 |
| 10 | export const colineHostedWorkspaceSchema = z.object({ |
| 11 | id: z.string().min(1), |
| 12 | slug: z.string().min(1), |
| 13 | name: z.string().min(1), |
| 14 | }); |
| 15 |
| 16 | export const colineHostedAppSchema = z.object({ |
| 17 | appInstallId: z.string().min(1), |
| 18 | appId: z.string().min(1), |
| 19 | appKey: z.string().min(1), |
| 20 | name: z.string().min(1), |
| 21 | }); |
| 22 |
| 23 | export const colineHostedActorSchema = z.object({ |
| 24 | userId: z.string().min(1), |
| 25 | email: z.string().email(), |
| 26 | handle: z.string().nullable().optional(), |
| 27 | firstName: z.string().nullable().optional(), |
| 28 | lastName: z.string().nullable().optional(), |
| 29 | }); |
| 30 |
| 31 | export const colineHostedFileSchema = z.object({ |
| 32 | id: z.string().min(1), |
| 33 | typeKey: z.string().min(1), |
| 34 | title: z.string().optional(), |
| 35 | }); |
| 36 |
| 37 | export const colineHostedRenderHomeRequestSchema = z.object({ |
| 38 | workspace: colineHostedWorkspaceSchema, |
| 39 | app: colineHostedAppSchema, |
| 40 | actor: colineHostedActorSchema.nullable().optional(), |
| 41 | }); |
| 42 |
| 43 | export const colineHostedRenderFileRequestSchema = z.object({ |
| 44 | workspace: colineHostedWorkspaceSchema, |
| 45 | app: colineHostedAppSchema, |
| 46 | actor: colineHostedActorSchema.nullable().optional(), |
| 47 | file: colineHostedFileSchema, |
| 48 | document: z.record(z.string(), z.unknown()), |
| 49 | }); |
| 50 |
| 51 | export const colineHostedActionRequestSchema = z.object({ |
| 52 | workspace: colineHostedWorkspaceSchema, |
| 53 | app: colineHostedAppSchema, |
| 54 | actor: colineHostedActorSchema.nullable().optional(), |
| 55 | action: colineUiActionSchema, |
| 56 | file: colineHostedFileSchema.nullable().optional(), |
| 57 | }); |
| 58 |
| 59 | export const colineHostedActionResponseSchema = z.union([ |
| 60 | z.object({ type: z.literal("ok") }), |
| 61 | z.object({ type: z.literal("refresh") }), |
| 62 | z.object({ |
| 63 | type: z.literal("navigate"), |
| 64 | href: z.string().min(1), |
| 65 | }), |
| 66 | z.object({ |
| 67 | type: z.literal("open_file"), |
| 68 | fileId: z.string().min(1), |
| 69 | }), |
| 70 | ]); |
| 71 |
| 72 | export type ColineHostedWorkspace = z.infer<typeof colineHostedWorkspaceSchema>; |
| 73 | export type ColineHostedApp = z.infer<typeof colineHostedAppSchema>; |
| 74 | export type ColineHostedActor = z.infer<typeof colineHostedActorSchema>; |
| 75 | export type ColineHostedFile = z.infer<typeof colineHostedFileSchema>; |
| 76 | export type ColineHostedRenderHomeRequest = z.infer< |
| 77 | typeof colineHostedRenderHomeRequestSchema |
| 78 | >; |
| 79 | export type ColineHostedRenderFileRequest = z.infer< |
| 80 | typeof colineHostedRenderFileRequestSchema |
| 81 | >; |
| 82 | export type ColineHostedActionRequest = z.infer< |
| 83 | typeof colineHostedActionRequestSchema |
| 84 | >; |
| 85 | export type ColineHostedActionResponse = z.infer< |
| 86 | typeof colineHostedActionResponseSchema |
| 87 | >; |
| 88 |
| 89 | export async function parseSignedColineRequest<T>(params: { |
| 90 | request: Request; |
| 91 | secret: string; |
| 92 | schema: z.ZodType<T>; |
| 93 | }): Promise<{ |
| 94 | deliveryId: string; |
| 95 | timestamp: string; |
| 96 | data: T; |
| 97 | }> { |
| 98 | const signature = params.request.headers.get(COLINE_SIGNATURE_HEADER); |
| 99 | const timestamp = params.request.headers.get(COLINE_TIMESTAMP_HEADER); |
| 100 | const deliveryId = params.request.headers.get(COLINE_DELIVERY_HEADER); |
| 101 | const body = await params.request.text(); |
| 102 |
| 103 | if (!signature || !timestamp || !deliveryId) { |
| 104 | throw new Error("Missing required Coline signing headers."); |
| 105 | } |
| 106 |
| 107 | const isValid = await verifyAppRequestSignature({ |
| 108 | secret: params.secret, |
| 109 | timestamp, |
| 110 | body, |
| 111 | signature, |
| 112 | }); |
| 113 |
| 114 | if (!isValid) { |
| 115 | throw new Error("Invalid Coline request signature."); |
| 116 | } |
| 117 |
| 118 | const parsedBody = JSON.parse(body) as unknown; |
| 119 |
| 120 | return { |
| 121 | deliveryId, |
| 122 | timestamp, |
| 123 | data: params.schema.parse(parsedBody), |
| 124 | }; |
| 125 | } |
| 126 |
| 127 | export const hostedActionResults = { |
| 128 | ok(): ColineHostedActionResponse { |
| 129 | return { type: "ok" }; |
| 130 | }, |
| 131 | refresh(): ColineHostedActionResponse { |
| 132 | return { type: "refresh" }; |
| 133 | }, |
| 134 | navigate(href: string): ColineHostedActionResponse { |
| 135 | return colineHostedActionResponseSchema.parse({ |
| 136 | type: "navigate", |
| 137 | href, |
| 138 | }); |
| 139 | }, |
| 140 | openFile(fileId: string): ColineHostedActionResponse { |
| 141 | return colineHostedActionResponseSchema.parse({ |
| 142 | type: "open_file", |
| 143 | fileId, |
| 144 | }); |
| 145 | }, |
| 146 | }; |
| 147 |