Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/dev-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
63 changes: 61 additions & 2 deletions packages/dev-api/src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/**
* 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
* GET /api/dev-content?path=work:kettle:title
* 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 = {
Expand All @@ -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 {
Expand All @@ -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<string, unknown>,
): { ok: true; opts: Omit<DevApiCreateDraftRequest, "action" | "skin"> } | { 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,
},
};
}
55 changes: 55 additions & 0 deletions packages/dev-api/src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
});
});
18 changes: 11 additions & 7 deletions packages/dev-api/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<string, unknown>;
Expand Down Expand Up @@ -165,13 +165,17 @@ export function createDevContentHandler(opts: DevContentHandlerOptions): DevApiH
}

if (body.action === "create-draft") {
const parsed = parseCreateDraftBody(body as Record<string, unknown>);
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,
Expand Down
3 changes: 3 additions & 0 deletions packages/dev-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading