Repository navigation
Rewrite API on Express 5 + TypeScript with layered architecture - #2
Merged
Merged
Conversation
- 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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
The server was a single
app.json 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
src/routes.ts; the store can be swapped (SQLite/Postgres) by implementingTemplateStore.JsonFileStorekeeps the existingdb.jsonformat, writes atomically (temp file + rename) and serializes concurrent updates.{ error: { code, message, details?, requestId } }— 400 / 404 / 409 / 413 / 422 / 500 (500 never leaks internals)..env.example), structured pino logs withX-Request-Id, graceful shutdown on SIGTERM/SIGINT.GET /template-treeGET /treePOST /save-template-treePUT /treePOST /add-node-treePOST /tree/nodesPOST /update-node-treePATCH /tree/nodes/:keyPOST /delete-node-treeDELETE /tree/nodes/:keyPOST /generate(string)POST /generate→{ url }GET /all-elementsGET /elementsPOST /add-elementPOST /elementsPUT /elementPATCH /elements/:keyDELETE /delete-element/:keyDELETE /elements/:keyMust be merged together with the frontend PR: LLazyEmail/email-template-editor-reactjs#32.
Bug fixes
/generateno longer sends the response twiceaddNodeno longer crashes when the parent has nochildren""instead of rendering"undefined"in HTML$&/$$in HTML are inserted literally during generationTooling
TypeScript (strict), Vitest + Supertest, ESLint, Prettier, GitHub Actions CI (lint, format, typecheck, test, build, docker build), multi-stage non-root Dockerfile with
/healthhealthcheck, devdocker-compose.yml.Testing
/generateoutput on the currentdb.jsonis byte-identical to the old server's output (golden fixture intest/fixtures)feat/rest-apiwas run against the new server: all 10 operations work and data round-trips unchangeddockerjob