Skip to content

refactor(server): a folder per route, under server/routes - #326

Closed
Ardnived wants to merge 11 commits into
mainfrom
claude/server-routes-folders
Closed

Ardnived wants to merge 11 commits into
mainfrom
claude/server-routes-folders

Conversation

@Ardnived

@Ardnived Ardnived commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

server/api-routes.ts held all four same-origin JSON routes in one file. Every route now has its own folder, named after its path — /api or not, since the reviewer chose to move /oembed here too. The folder holds the route, its test, and anything only that route uses, so the folder tree lists every route:

server/routes/
  index.ts                     registerRoutes(): registers every route below
  oembed/                      route.ts, route.test.ts     GET  /oembed
                               oembed.ts, oembed.test.ts   the pure builders only it reads
  api/
    index.ts                   registerApiRoutes(): the four below
    _related.ts                relatedAnswer, for the two related-* routes
    _proxy-failure.ts (+test)  upstreamRefusal, reportProxyFailure (used by both proxies)
    _testing.ts                request helpers and fixtures for the route tests
    related-meditations/       route.ts, route.test.ts   GET  /api/related-meditations/:lectureId
    related-lectures/          route.ts, route.test.ts   GET  /api/related-lectures/:meditationId
    submissions/               route.ts, route.test.ts,  POST /api/submissions
                               schema.ts
    live-preview/populate/     route.ts, route.test.ts   POST /api/live-preview/populate
  • No behaviour change. Every handler, helper and constant moved unchanged. The only rewording is in file-level docblocks and in comments that point at other files. entry.ts registers the tree once instead of naming each group, and the routes register in the same order.

  • One file moved to the route that uses it. submissionSchema and SUBMISSION_BODY_LIMIT moved from server/validation.ts to submissions/schema.ts, since no other code uses them. The zod-validation rule's paths: now includes server/routes/**/schema.ts, and the rule says a schema used by only one route goes in that route's folder.

  • Two helpers went to an owner that already existed. parseLocale is isLocale plus a default, so it sits beside both in server/sahajcloud-types.ts. The short JSON Cache-Control had a byte-identical twin in oembed-routes.ts, so both now read JSON_CACHE_CONTROL from the new server/cache-headers.ts.

  • One registration list per level. server/routes/index.ts is the whole tree, api/index.ts the four /api routes. The guard holds each one to the routes below it, so a second list cannot silently drop a route.

  • Tests are split along their existing describe blocks, each next to the route it covers. All 48 existing /api cases and all 17 /oembed cases are kept unchanged, titles included. Each test file now registers only its own route.

  • New guard, tests/guards/route-files.test.ts, beside the repo's other twenty. It checks three things:

    • each route.ts registers only paths that map to its own folder (/api/a/b/:id → api/a/b/route.ts, /oembed → oembed/route.ts);
    • every other file belongs to a route folder, or is a _ file with routes below it to share with;
    • each index.ts registers exactly the routes below it.

    I checked that it fails on each: a misplaced path, a stray file, a _ file with no route below it, and a route an index.ts forgets.

  • Docs: a convention section in server/AGENTS.md, cut to the rule and one pointer at the guard. The references to server/api-routes.ts and server/oembed-routes.ts in prose and comments now point at the new files.

Preview

No visible change: each link should answer exactly as it does on main.

  • /api/related-lectures/142 — the moved related-lectures route: a JSON items list, Cache-Control: public, max-age=300, stale-while-revalidate=1800.
  • /api/related-meditations/150 — the moved related-meditations route, same cache header.
  • /meditations/142 — a meditation whose related-lectures row loads from the first route after hydration.
  • /oembed for /meditations/142 — the other reader of the shared JSON_CACHE_CONTROL; the header must be unchanged: https://claude-server-routes-folders-wemeditate-web.contact-c66.workers.dev/oembed?url=https%3A%2F%2Fclaude-server-routes-folders-wemeditate-web.contact-c66.workers.dev%2Fmeditations%2F142

Out of scope

  • sahajcloud-client.ts gets its own split later, once the open PRs that edit it have merged.
  • robots.txt, sitemap.xml and manifest.webmanifest (sitemap-routes.ts) stay at server/*-routes.ts: feat(components): a MeditationLibrary for the meditations index block #293 touches that file. It is the one route left outside the tree, and server/AGENTS.md and routes/index.ts both say so.

Heads-up for #321 and #293

Both edit server/api-routes.ts, and that file is gone: #321 edits reportProxyFailure, and #293 the reportedConfigGaps docblock line inside it. That code now lives in server/routes/api/_proxy-failure.ts. Because the file was split rather than renamed, git won't carry either edit across, so each has to be re-applied there after a rebase.

Separately, #321's new reportOnce (server/report-once.ts) does the same job as warnOnce, which is already in server/error-utils.ts on main.

Test plan

  • .claude/skills/pr-prep/check.sh: lint, tsc, 165 files / 1701 tests pass
  • pnpm build: passes
  • vitest run server/routes tests/guards/route-files.test.ts: 8 files, 87 tests
  • /security-review over the whole branch diff

Notes for reviewer

The four adversarial-review threads are all built: the guard moved to tests/guards/, the server/AGENTS.md section cut to its rule, parseLocale rehomed, and the duplicate Cache-Control given one owner. One commit each.

The open question is answered — A, so /oembed moved in this PR. That settled two things worth a look: a non-/api route is a folder at its bare path (oembed/, no prefix segment), and the guard covers it, which is why it is route-files.test.ts now rather than api-route-files.test.ts. sitemap-routes.ts will fall in behind #293 the same way, except that it registers three paths from one file, so it becomes three folders.

🤖 Generated with Claude Code

server/api-routes.ts held all four same-origin JSON routes. Each now lives
in the file its path names, so the folder is the route list:

  routes/api/related-meditations.ts      GET  /api/related-meditations/:lectureId
  routes/api/related-lectures.ts         GET  /api/related-lectures/:meditationId
  routes/api/submissions.ts              POST /api/submissions
  routes/api/live-preview/populate.ts    POST /api/live-preview/populate

What more than one route reads moves to _shared.ts (parseLocale,
relatedAnswer) and _proxy-failure.ts (upstreamRefusal,
reportProxyFailure). index.ts keeps registerApiRoutes, so entry.ts changes
by one import.

No behaviour changes: every handler body is moved verbatim, and every
existing test case is kept, split beside the file it covers. The new
index.test.ts fails a route registered outside the file its path names, a
route file that registers nothing, and a file index.ts does not register.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sydevs-bot

sydevs-bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

🤖 no session running
CI fix 1

recent dispatches
  • 17:47Z resolve-conflicts · attempt 1 · session · done
  • 17:47Z resolve-conflicts · attempt 1 · session
  • 00:36Z address-review · attempt 1 · session · done
  • 00:36Z address-review · attempt 1 · session
  • 00:11Z fix-ci · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 20, 2026
@sydevs-bot sydevs-bot moved this from Pending to Working in SYDevelopers Technical Roadmap Nov 20, 2026
…wn files

Each route is now a folder at its path, holding route.ts, route.test.ts and
anything only it reads:

  related-meditations/route.ts       GET  /api/related-meditations/:lectureId
  related-lectures/route.ts          GET  /api/related-lectures/:meditationId
  submissions/route.ts, schema.ts    POST /api/submissions
  live-preview/populate/route.ts     POST /api/live-preview/populate

submissionSchema and SUBMISSION_BODY_LIMIT move from server/validation.ts
to submissions/schema.ts, since only that route reads them; the
zod-validation rule now also loads for a route's schema.ts. Top-level `_`
files stay for what several routes share.

index.test.ts now maps a path to `<path>/route.ts`, and also fails a file
that sits in no route's folder and is not a shared `_` file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 Adversarial review

A pure move, and I checked it as one: strip comments and imports from the old server/api-routes.ts and from the six new files and the code is identical, bar export on four moved helpers and the two register* calls that became index.ts. All 48 case titles survive. Nothing in the diff is outside what the body claims.

Two things to answer. The convention is half-applied: server/routes/ holds only api/, while /oembed, /robots.txt, /sitemap.xml and /manifest.webmanifest stay at server/*-routes.ts. #293 blocks sitemap-routes.ts, but no open PR touches oembed-routes.ts — so nothing stops it moving here, and two homes for "where a route lives" is worse than either end state. And the heads-up is short by one: #293 also edits server/api-routes.ts, the reportedConfigGaps docblock line inside reportProxyFailure, so it hits the same vanished-file conflict #321 does.

Findings

  • should-fix — index.test.ts is a guard outside tests/guards/, where the other twenty live.
  • should-fix — the new server/AGENTS.md section retells the test's cases.
  • should-fix — parseLocale has a home beside isLocale; _shared names nothing.
  • consider — CACHE_CONTROL has a byte-identical twin in oembed-routes.ts:21.

What was checked

Read whole on the branch: six route files, _shared.ts, _proxy-failure.ts, _testing.ts, index.ts, index.test.ts, server/AGENTS.md, entry.ts, plus api-routes.ts on main. Ladder on each new piece: _shared, _proxy-failure and _testing each have two or more consumers, index.ts is the registration list, and only index.test.ts had a named repo answer. git grep api-routes on the branch: no stale reference. Diffed main against the four open server-touching branches to test the collision claims. No contract surface touched.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread tests/guards/api-route-files.test.ts Resolved
Comment thread server/AGENTS.md Resolved
Comment thread server/routes/api/_shared.ts Resolved
Comment thread server/routes/api/_shared.ts Resolved
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wemeditate-web 19087b4 Commit Preview URL

Branch Preview URL
Nov 21 2026, 12:46 AM

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 20, 2026
@Ardnived Ardnived changed the title refactor(server): one file per /api route, under server/routes/api refactor(server): a folder per /api route, under server/routes/api Nov 20, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wemeditate-design 19087b4 Commit Preview URL

Branch Preview URL
Nov 21 2026, 12:46 AM

@sydevs-bot sydevs-bot moved this from Working to Pending in SYDevelopers Technical Roadmap Nov 20, 2026
@Ardnived

Copy link
Copy Markdown
Contributor Author

@sydevs-bot address

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 20, 2026
@sydevs-bot sydevs-bot moved this from Pending to Working in SYDevelopers Technical Roadmap Nov 20, 2026
sydevs-bot and others added 5 commits November 21, 2026 00:00
index.test.ts was a guard — a prose rule held by a test — sitting inside
the folder it audits, while the other twenty live in tests/guards/ with
shared plumbing in _source-scan.ts and two of them cited by AGENTS.md at
that path. Keeping this one next to its subject made a second convention
for the same kind of check.

It globs ../../server/routes/api/**/*.ts from there and strips that prefix
off each key, so a reported path still reads as the folder does. Checked
that it still fails on a stray file and on a misplaced route path.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXkzgLHTsVGKfpM6Gf
The section retold what the code and the guard already hold: the
path-to-file example, the register signature, and a last sentence listing
the three cases the guard fails on. A check that enforces a rule gets one
pointer, never its cases.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXkzgLHTsVGKfpM6Gf
parseLocale is isLocale plus a default, and both the narrowing and the
'en' it falls back to already live in sahajcloud-types.ts, where the
default is named DEFAULT_LOCALE. It is reusable there by name.

What stays behind is relatedAnswer, for the two related-* routes, so the
file takes their name: _shared said only that something shared it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXkzgLHTsVGKfpM6Gf
oembed-routes.ts and the related-* answer each held a byte-identical
'public, max-age=300, stale-while-revalidate=1800'. The split built the
shared owner for that value and left its twin next door, so both now read
JSON_CACHE_CONTROL from server/cache-headers.ts. The sitemap's longer one
stays where it is.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXkzgLHTsVGKfpM6Gf

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 question for the reviewer, one per thread.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread server/oembed-routes.ts Resolved
@sydevs-bot

Copy link
Copy Markdown
Contributor

Built all four review findings, one commit each, and pushed.

Asked: does /oembed move here now? Nothing blocks it, but moving one of four is still two homes, so the recommendation is the built option.

#293 also edits reportProxyFailure, so the heads-up names both PRs now. origin/main merged clean. Lean gate, pnpm build and a security review all pass.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Nov 21, 2026
submissions/schema.ts ended on a second newline. Lint went red once #313
set --max-warnings 0, which turns prettier/prettier into a failure.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjWhMsP61j5GKkGuVZy
@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 21, 2026
@sydevs-bot sydevs-bot moved this from Working to Awaiting in SYDevelopers Technical Roadmap Nov 21, 2026

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for Ardnived, who wrote it: GitHub refuses an author's own approval. CI is green and the automated review has run. Enable auto-merge when you want it merged: the queue tests it against its base. 🤖 Written by the sydevs autonomous loop — see what this is

@sydevs-bot

Copy link
Copy Markdown
Contributor

Approved, but this thread still waits on you, and the ruleset will not merge until it is resolved: #326 (comment). Reply there, or resolve it.

🤖 sydevs dispatcher

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 21, 2026
@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 21, 2026
The reviewer answered A on the open question: /oembed moves now rather
than waiting for sitemap-routes.ts. So server/routes/ is no longer the
/api tree with a prefix of its own — it is where a route lives, and the
folder is named for the path, /api or not.

/oembed is oembed/route.ts, with its test and the pure builders only it
reads beside it. A new server/routes/index.ts is the one registration
list, so entry.ts registers the tree once; routes/api/index.ts keeps the
four /api routes and the convention it used to restate now lives one
level up.

The guard moves with the convention, from api-route-files to
route-files, and generalises: the route path maps to the folder relative
to server/routes/, a shared `_` file is one the routes below it read at
any depth rather than only at the top, and each index.ts registers every
route below it, which is what keeps a second registration list honest.
Re-checked all four failure modes from there: a stray file, a `_` file
with no route below it, a renamed route path, and a route index.ts
forgets.

sitemap-routes.ts is still outside the tree, blocked by #293.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjVHokFtWLKv2yi9xRp
entry.ts registers the tree through registerRoutes now, so "registered
beside registerApiRoutes" named a call it no longer sits beside.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjVHokFtWLKv2yi9xRp
@sydevs-bot sydevs-bot changed the title refactor(server): a folder per /api route, under server/routes/api refactor(server): a folder per route, under server/routes Nov 21, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor

Built the answer and pushed.

/oembed is server/routes/oembed/route.ts now, with its test and the builders only it reads beside it. A non-/api route is a folder at its bare path, so the tree is "where a route lives", not "the /api tree".

That forced two things option A named. server/routes/index.ts is the one registration list, so entry.ts registers the tree once. And the guard generalised with the convention — tests/guards/route-files.test.ts — which now also holds each index.ts to the routes below it, so a second list cannot drop a route silently. Re-checked all four failure modes.

sitemap-routes.ts is the one route left outside, behind #293. It registers three paths from one file, so it becomes three folders when it comes.

Title and body refreshed. Lean gate (165 files / 1701 tests), pnpm build and a security review all pass.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 21, 2026
@sydevs-bot sydevs-bot moved this from Working to Awaiting in SYDevelopers Technical Roadmap Nov 21, 2026
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 22, 2026
@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 22, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor

Nothing left to resolve — #327 already landed this work

main carries this PR's whole refactor. #327 (5d6cbd9) took these commits and went further.

I merged origin/main in a worktree, took main's side on all 25 conflicted paths, then diffed the result against main. One file was left: server/routes/api/index.ts, 15 lines. The rest is already on main — the server/routes/ tree, tests/guards/route-files.test.ts, the server/AGENTS.md convention, server/cache-headers.ts, submissions/schema.ts, and parseLocale (now in lib/i18n/locales.ts).

That file is a second registration list for /api. main collapsed it into routes/index.ts, which registers all eight routes.

I pushed nothing.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@Ardnived

Copy link
Copy Markdown
Contributor Author

Superseded by #327, which contained this PR's changes and has merged to main.

@Ardnived Ardnived closed this Nov 22, 2026
@sydevs-bot sydevs-bot moved this from Working to Done in SYDevelopers Technical Roadmap Nov 22, 2026
@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants