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.
| * provider, another company's backend) call INTO an app that has no server. |
| 6 | * |
| 7 | * Every install gets a private, unguessable URL per endpoint. Coline |
| 8 | * receives the request, verifies it server-side with the declared scheme |
| 9 | * (the verification secret is an install secret the app never sees), and |
| 10 | * only then dispatches it to the app's `onRequest` handler — hosted or |
| 11 | * external. `async` endpoints acknowledge with 202 immediately and run the |
| 12 | * handler as a durable, retried job; `sync` endpoints wait for the handler |
| 13 | * and return its response. |
| 14 | */ |
| 15 |
| 16 | export const secretKeySchema = z |
| 17 | .string() |
| 18 | .trim() |
| 19 | .regex(/^[A-Z][A-Z0-9_]*$/, "Secret names are UPPER_SNAKE_CASE.") |
| 20 | .max(120); |
| 21 |
| 22 | export const ENDPOINT_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const; |
| 23 | export const endpointMethodSchema = z.enum(ENDPOINT_METHODS); |
| 24 | export type EndpointMethod = z.infer<typeof endpointMethodSchema>; |
| 25 |
| 26 | const headerNameSchema = z |
| 27 | .string() |
| 28 | .trim() |
| 29 | .toLowerCase() |
| 30 | .regex(/^[a-z0-9-]+$/, "Header names are lowercase letters, digits, and dashes.") |
| 31 | .max(120); |
| 32 |
| 33 | export const endpointVerificationSchema = z.discriminatedUnion("kind", [ |
| 34 | /** No verification beyond the unguessable URL. Review flags this. */ |
| 35 | z.strictObject({ kind: z.literal("none") }), |
| 36 | /** |
| 37 | * A shared token, compared in constant time. Read from `header` |
| 38 | * (default `authorization`, with an optional `Bearer ` prefix) or from |
| 39 | * the `query` parameter when one is named. |
| 40 | */ |
| 41 | z.strictObject({ |
| 42 | kind: z.literal("token"), |
| 43 | secret: secretKeySchema, |
| 44 | header: headerNameSchema.optional(), |
| 45 | query: z.string().trim().min(1).max(120).optional(), |
| 46 | }), |
| 47 | /** Generic HMAC over the raw body (optionally prefixed by a timestamp). */ |
| 48 | z.strictObject({ |
| 49 | kind: z.literal("hmac"), |
| 50 | secret: secretKeySchema, |
| 51 | header: headerNameSchema, |
| 52 | algorithm: z.enum(["sha256", "sha1", "sha512"]).default("sha256"), |
| 53 | encoding: z.enum(["hex", "base64"]).default("hex"), |
| 54 | /** Literal prefix on the header value, e.g. `sha256=`. */ |
| 55 | prefix: z.string().max(40).optional(), |
| 56 | /** When set, the signed payload is `${timestamp}.${body}` and stale timestamps are rejected. */ |
| 57 | timestampHeader: headerNameSchema.optional(), |
| 58 | }), |
| 59 | /** Provider presets with their exact signature formats. */ |
| 60 | z.strictObject({ kind: z.literal("stripe"), secret: secretKeySchema }), |
| 61 | z.strictObject({ kind: z.literal("github"), secret: secretKeySchema }), |
| 62 | z.strictObject({ kind: z.literal("slack"), secret: secretKeySchema }), |
| 63 | z.strictObject({ kind: z.literal("shopify"), secret: secretKeySchema }), |
| 64 | z.strictObject({ kind: z.literal("linear"), secret: secretKeySchema }), |
| 65 | ]); |
| 66 | export type EndpointVerification = z.infer<typeof endpointVerificationSchema>; |
| 67 |
| 68 | export const endpointIdSchema = z |
| 69 | .string() |
| 70 | .trim() |
| 71 | .regex(/^[a-z][a-z0-9-]*$/, "Endpoint ids are lowercase kebab-case.") |
| 72 | .max(80); |
| 73 |
| 74 | export const appEndpointSchema = z.strictObject({ |
| 75 | id: endpointIdSchema, |
| 76 | description: z.string().trim().max(500).optional(), |
| 77 | methods: z.array(endpointMethodSchema).min(1).max(5).default(["POST"]), |
| 78 | verify: endpointVerificationSchema, |
| 79 | /** `async`: 202 now, durable retried handler run. `sync`: wait (≤25s) and relay the response. */ |
| 80 | mode: z.enum(["async", "sync"]).default("async"), |
| 81 | /** Largest accepted request body. */ |
| 82 | maxBodyBytes: z |
| 83 | .number() |
| 84 | .int() |
| 85 | .min(1_024) |
| 86 | .max(5_000_000) |
| 87 | .default(1_000_000), |
| 88 | }); |
| 89 | export type AppEndpoint = z.infer<typeof appEndpointSchema>; |
| 90 | export type AppEndpointInput = z.input<typeof appEndpointSchema>; |
| 91 |
| 92 | export const appEndpointsSchema = z |
| 93 | .array(appEndpointSchema) |
| 94 | .max(20) |
| 95 | .default([]) |
| 96 | .refine( |
| 97 | (endpoints) => new Set(endpoints.map((endpoint) => endpoint.id)).size === endpoints.length, |
| 98 | "Endpoint ids must be unique.", |
| 99 | ); |
| 100 |