ReviewOS

stacks/stx

feat(server): discover file-based API endpoints and generate client response types

#1967
Closed glennmichael123 opened this 10 days ago ·
10 days ago

Problem

stx supports manual apiRoutes and an external apiRouter, but does not offer a unified file-based endpoint convention with response-type inference for client fetching.

For standalone apps, a handler file such as server/api/users.get.ts should be routable without a second manual route declaration, and clients should be able to infer its response shape.

Existing foundation

Reviewed at 7d93c4c: config types and production-server.ts.

Scope and acceptance criteria

  • Define an endpoint directory convention with HTTP-method suffixes and dynamic parameters.
  • Discover handlers during development and package them for production.
  • Generate a route/response type map for a typed client-fetch API.
  • Preserve manual apiRoutes and apiRouter integrations with documented precedence.
  • Diagnose ambiguous routes and unsupported methods.
  • Keep server-only implementation code out of browser bundles.
  • Test the same endpoint contract through development and production serving.
  • Document how standalone stx and Stacks-hosted applications opt in.

Use existing Stacks/Bun router facilities where appropriate; this should not introduce a second backend framework.

Prior art: Nuxt server directory and typed data fetching.

10 days ago

Implemented in 8fbde39. Opt-in serverApi discovery supports method suffixes, index/dynamic/catch-all paths, ambiguity diagnostics, shared dev/production dispatch, and content-addressed Bun server bundles. Development sees endpoint/helper edits; production works without original endpoint sources. Generated api-types.d.ts powers the browser-safe apiFetch client with checked methods, params and JSON response types. Existing apiRouter/apiRoutes retain precedence. Endpoint source URLs and accidental client imports (including cached bundles) are blocked. Added dev/app/single-file/production HTTP tests, TypeScript negative tests and browser-bundle isolation checks. Local full suite: 13,146 pass; final endpoint/bundler checks: 56 pass. CI, including build, typecheck, lint, full tests, coverage, integration and publish, is green: https://github.com/stacksjs/stx/actions/runs/36009675909 . Usage and deployment limits: docs/features/server-api.md.

Sign in to comment on this issue.