From c9011f0bf38054d6547970a346e2b20a2ca329b9 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 12 Nov 2026 02:01:17 +0000 Subject: [PATCH] feat(dev-api): add create-draft to contract with handler tests Extend DevApiActions and document request/response types for create-draft. Validate draft POST bodies in parseCreateDraftBody; add four handler tests. Co-authored-by: Charles Berret --- packages/dev-api/README.md | 8 ++++ packages/dev-api/src/contract.ts | 63 ++++++++++++++++++++++++++++- packages/dev-api/src/editor.test.ts | 55 +++++++++++++++++++++++++ packages/dev-api/src/handler.ts | 18 +++++---- packages/dev-api/src/index.ts | 3 ++ 5 files changed, 138 insertions(+), 9 deletions(-) diff --git a/packages/dev-api/README.md b/packages/dev-api/README.md index cd4b6ab..7f8f6e5 100644 --- a/packages/dev-api/README.md +++ b/packages/dev-api/README.md @@ -9,3 +9,11 @@ import { createDevContentHandler } from "@shellside/dev-api"; ``` Gates: `isDev()` + localhost Host check. See `docs/ARE_YOU_INSANE.md`. + +## Actions (`DevApiActions`) + +Scalar writes use `{ path, value? }`. Named actions include `add-link`, `remove-link`, +look-history helpers, and **`create-draft`** — POST `{ action: "create-draft", draftTitle, +draftKind? }` to allocate a new work markdown file or YAML row. Request/response shapes +live in `src/contract.ts` (`DevApiCreateDraftRequest`, `DevApiCreateDraftResponse`); +`createDevContentHandler` implements them. diff --git a/packages/dev-api/src/contract.ts b/packages/dev-api/src/contract.ts index 256e161..5c9f4ea 100644 --- a/packages/dev-api/src/contract.ts +++ b/packages/dev-api/src/contract.ts @@ -1,6 +1,9 @@ /** * DEV-only content + look API contract. - * Full implementation remains site-side until host policy + content root are injected. + * + * HTTP behavior is implemented by `createDevContentHandler` in this package. + * The site injects dev gating, a `createContentEditor` instance, and optional + * look handlers; it mounts the returned `{ GET, POST }` on a dev-only route. * * Typical routes (Astro): * GET /api/dev-content?meta=1 @@ -8,8 +11,11 @@ * GET /api/dev-content?lookHistory=1 * POST /api/dev-content { path, value?, style?, … } * POST /api/dev-content { action: "look-history-restore", lookHistoryId } + * POST /api/dev-content { action: "create-draft", draftKind?, draftTitle, … } */ +import type { CreateDraftResult, DraftKind } from "./content-editor.js"; + export type DevApiSkin = "portfolio" | "console"; export type DevApiFieldStyle = { @@ -25,7 +31,28 @@ export type DevApiActions = | "add-link" | "remove-link" | "look-history-list" - | "look-history-restore"; + | "look-history-restore" + | "create-draft"; + +/** POST body for `action: "create-draft"`. */ +export type DevApiCreateDraftRequest = { + action: "create-draft"; + skin?: DevApiSkin; + /** Defaults to `"work"`. */ + draftKind?: DraftKind; + draftTitle?: string; + workStatus?: "current" | "complete"; + writingType?: string; + recordSection?: string; +}; + +/** Successful POST response for `create-draft`. */ +export type DevApiCreateDraftResponse = CreateDraftResult & { + ok: true; + action: "create-draft"; + note: string; + reload: true; +}; /** Minimum host policy for v1 */ export function isLoopbackHost(hostHeader: string): boolean { @@ -37,4 +64,36 @@ export const DEV_API_NOTES = ` - Prefer loopback hosts until real auth - Style writes are role-scoped (path → look role) - Look history snapshots full field-styles.yaml +- create-draft allocates a new work file or YAML row (visibility draft) ` as const; + +const DRAFT_KINDS: readonly DraftKind[] = ["work", "writing", "record"]; + +export function parseCreateDraftBody( + body: Record, +): { ok: true; opts: Omit } | { ok: false; error: string; status: number } { + if (body.draftTitle != null && typeof body.draftTitle !== "string") { + return { ok: false, error: "invalid_shape", status: 400 }; + } + if (body.draftKind != null && typeof body.draftKind !== "string") { + return { ok: false, error: "invalid_shape", status: 400 }; + } + const title = String(body.draftTitle ?? ""); + if (/[\\/]|\.\./.test(title)) { + return { ok: false, error: "path_escape", status: 400 }; + } + const kind = (body.draftKind ?? "work") as string; + if (!DRAFT_KINDS.includes(kind as DraftKind)) { + return { ok: false, error: "invalid_shape", status: 400 }; + } + return { + ok: true, + opts: { + draftKind: kind as DraftKind, + draftTitle: title, + workStatus: body.workStatus as DevApiCreateDraftRequest["workStatus"], + writingType: typeof body.writingType === "string" ? body.writingType : undefined, + recordSection: typeof body.recordSection === "string" ? body.recordSection : undefined, + }, + }; +} diff --git a/packages/dev-api/src/editor.test.ts b/packages/dev-api/src/editor.test.ts index f4c8cc5..d70e816 100644 --- a/packages/dev-api/src/editor.test.ts +++ b/packages/dev-api/src/editor.test.ts @@ -316,4 +316,59 @@ describe("createDevContentHandler", () => { assert.equal((await h.POST({ request: req({ path: "work:kettle:title" }) })).status, 400); assert.equal((await h.POST({ request: req({ path: "work:kettle:title", value: "x".repeat(20001) }) })).status, 400); }); + + describe("POST create-draft", () => { + it("creates a valid work draft", async () => { + const h = makeHandler(true); + const res = await h.POST({ + request: req({ + action: "create-draft", + draftKind: "work", + draftTitle: "Fresh Project", + workStatus: "current", + }), + }); + const body = await res.json(); + assert.equal(res.status, 200); + assert.equal(body.action, "create-draft"); + assert.equal(body.id, "fresh-project"); + assert.equal(body.editPath, "work:fresh-project:title"); + assert.equal(body.reload, true); + const abs = path.join(root, "work", "fresh-project.md"); + assert.match(await fs.readFile(abs, "utf8"), /visibility: draft/); + }); + + it("rejects invalid shape", async () => { + const h = makeHandler(true); + let res = await h.POST({ request: req({ action: "create-draft", draftKind: "profile" }) }); + assert.equal((await res.json()).error, "invalid_shape"); + assert.equal(res.status, 400); + res = await h.POST({ request: req({ action: "create-draft", draftTitle: 42 }) }); + assert.equal(res.status, 400); + assert.equal((await res.json()).error, "invalid_shape"); + }); + + it("rejects path escape in draftTitle", async () => { + const h = makeHandler(true); + const res = await h.POST({ + request: req({ action: "create-draft", draftKind: "work", draftTitle: "../../outside" }), + }); + const body = await res.json(); + assert.equal(res.status, 400); + assert.equal(body.error, "path_escape"); + await assert.rejects(fs.access(path.join(root, "outside.md"))); + }); + + it("allocates a new id when the slug already exists", async () => { + const h = makeHandler(true); + const res = await h.POST({ + request: req({ action: "create-draft", draftKind: "work", draftTitle: "Kettle" }), + }); + const body = await res.json(); + assert.equal(res.status, 200); + assert.equal(body.id, "kettle-2"); + assert.equal(body.editPath, "work:kettle-2:title"); + await fs.access(path.join(root, "work", "kettle-2.md")); + }); + }); }); diff --git a/packages/dev-api/src/handler.ts b/packages/dev-api/src/handler.ts index a50d8f2..e247dcf 100644 --- a/packages/dev-api/src/handler.ts +++ b/packages/dev-api/src/handler.ts @@ -11,7 +11,7 @@ * `handler.GET({ url, request })`. */ import { parseContentPath } from "@shellside/edit-protocol"; -import { isLoopbackHost } from "./contract.js"; +import { isLoopbackHost, parseCreateDraftBody, type DevApiActions } from "./contract.js"; import type { ContentEditor, CreateDraftOpts, LinkRecord } from "./content-editor.js"; /** What the site supplies for the look half of the desk. */ @@ -58,7 +58,7 @@ export type DevPostBody = { skin?: string; tags?: string[]; link?: LinkRecord; - action?: "add-link" | "remove-link" | "look-history-list" | "look-history-restore" | "create-draft"; + action?: DevApiActions; lookHistoryId?: string; tokens?: unknown; palette?: Record; @@ -165,13 +165,17 @@ export function createDevContentHandler(opts: DevContentHandlerOptions): DevApiH } if (body.action === "create-draft") { + const parsed = parseCreateDraftBody(body as Record); + if (!parsed.ok) { + return json({ error: parsed.error }, parsed.status); + } try { const result = await opts.editor.createDraft({ - kind: (body.draftKind || "work") as CreateDraftOpts["kind"], - title: String(body.draftTitle || ""), - workStatus: body.workStatus, - writingType: body.writingType, - recordSection: body.recordSection, + kind: parsed.opts.draftKind ?? "work", + title: String(parsed.opts.draftTitle ?? ""), + workStatus: parsed.opts.workStatus, + writingType: parsed.opts.writingType, + recordSection: parsed.opts.recordSection, }); return json({ ok: true, diff --git a/packages/dev-api/src/index.ts b/packages/dev-api/src/index.ts index c75437b..969e71a 100644 --- a/packages/dev-api/src/index.ts +++ b/packages/dev-api/src/index.ts @@ -1,9 +1,12 @@ export { isLoopbackHost, DEV_API_NOTES, + parseCreateDraftBody, type DevApiSkin, type DevApiFieldStyle, type DevApiActions, + type DevApiCreateDraftRequest, + type DevApiCreateDraftResponse, } from "./contract.js"; export { createContentEditor,