Skip to content

Rewrite API on Express 5 + TypeScript with layered architecture - #2

Merged
vadim9999 merged 1 commit into
mainfrom
feat/enterprise-express
Nov 11, 2026
Merged

vadim9999 merged 1 commit into
mainfrom
feat/enterprise-express

Conversation

@vadim9999

@vadim9999 vadim9999 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What & why

The server was a single app.js on lowdb with no validation, inconsistent error handling and a few bugs. This rewrites it as a typed, layered Express 5 service that is easier to test, extend and run in production.

Architecture

helmet → cors → request-id → pino-http → json → static
  → router (zod validation) → controller (HTTP only) → service (business rules)
  → TemplateStore (interface) → JsonFileStore (db.json)
  → not-found → error-handler
  • Dependencies are wired in src/routes.ts; the store can be swapped (SQLite/Postgres) by implementing TemplateStore.
  • JsonFileStore keeps the existing db.json format, writes atomically (temp file + rename) and serializes concurrent updates.
  • One error shape everywhere: { error: { code, message, details?, requestId } } — 400 / 404 / 409 / 413 / 422 / 500 (500 never leaks internals).
  • Env config validated with zod at startup (.env.example), structured pino logs with X-Request-Id, graceful shutdown on SIGTERM/SIGINT.

⚠️ Breaking: new REST routes

v1 v2
GET /template-tree GET /tree
POST /save-template-tree PUT /tree
POST /add-node-tree POST /tree/nodes
POST /update-node-tree PATCH /tree/nodes/:key
POST /delete-node-tree DELETE /tree/nodes/:key
POST /generate (string) POST /generate → { url }
GET /all-elements GET /elements
POST /add-element POST /elements
PUT /element PATCH /elements/:key
DELETE /delete-element/:key DELETE /elements/:key

Must be merged together with the frontend PR: LLazyEmail/email-template-editor-reactjs#32.

Bug fixes

  • /generate no longer sends the response twice
  • Update/delete of a missing node returns 404 instead of 200; duplicate keys return 409
  • addNode no longer crashes when the parent has no children
  • An empty optional value is stored as "" instead of rendering "undefined" in HTML
  • $& / $$ in HTML are inserted literally during generation

Tooling

TypeScript (strict), Vitest + Supertest, ESLint, Prettier, GitHub Actions CI (lint, format, typecheck, test, build, docker build), multi-stage non-root Dockerfile with /health healthcheck, dev docker-compose.yml.

Testing

  • 52 unit + HTTP integration tests pass; lint, prettier, typecheck and build are clean
  • /generate output on the current db.json is byte-identical to the old server's output (golden fixture in test/fixtures)
  • The frontend API client from feat/rest-api was run against the new server: all 10 operations work and data round-trips unchanged
  • Docker image build not verified locally (no Docker in the dev environment) — covered by the CI docker job

- Layers: router (zod validation) -> controller -> service -> TemplateStore
- JsonFileStore replaces lowdb: atomic writes, serialized updates, same db.json format
- Unified JSON error format with request id; 400/404/409/413/422/500
- REST routes: /tree, /tree/nodes/:key, /elements/:key, /generate -> { url }
- Env config validated with zod, pino logging, helmet, CORS, graceful shutdown
- Fixes: /generate double response, 200 on missing nodes, addNode without
  children, "undefined" values, $-patterns expanded during generation
- Vitest + Supertest suite (52 tests), ESLint, Prettier, CI, multi-stage Dockerfile

BREAKING CHANGE: routes renamed, see README "Migrating from v1 routes".
@vadim9999
vadim9999 merged commit 0e7a9f0 into main Nov 11, 2026
2 checks passed
@vadim9999
vadim9999 deleted the feat/enterprise-express branch November 11, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant