todo-service
Hono/oRPC service implementing todoContract against todo-domain. It is the boilerplate's worked example (contract, domain, service and todo-ui wired end to end), with health, Scalar docs, OpenAPI, RPC and REST transports, workspace-scoped authorization, and the todo domain repository.
API
createTodoServiceApp: builds the service app;loadTodoServiceConfig: reads its config.createTodoRateLimitMiddleware: rate limiting (it resolves the client IP from the proxy headers).serviceOpenApiSchemas: schemas merged into the OpenAPI document; typeTodoServiceDomains.
Endpoints
Every procedure takes workspaceId and goes through createAuthenticateMiddleware and then createRequireIntent (isRoleAllowed from workspace-model), in src/lib/create-app.ts. No credential is a 401; a user who is not a member is a 403 not_a_member; a role without the procedure's intent (todo:read, todo:create, todo:complete, todo:delete) is a 403 insufficient_role. delete loads the todo first: an id that is not in the workspace is RESOURCE_NOT_FOUND, and a member deleting another member's todo is USER_UNAUTHORIZED insufficient_role; admins and owners delete any.
createTodoServiceApp takes authProvider and resolveRole as parameters. main.ts passes createIamSessionAuthProvider (forwards the caller's session cookie or bearer token to IAM GET /api/auth/get-session, with an AbortSignal timeout) and a resolveRole backed by createWorkspaceDomainRepository(...).findMemberRole on the shared Mongo connection (read-only). The browser cookie reaches todo-service only when todo-ui, IAM and todo-service are on one site (localhost ports count), and corsOrigins (credentialed CORS for /rpc and /api) lists the todo-ui origins. A test passes createDevAuthProvider and a fake resolveRole, so no IAM is needed.
delete is recorded as todo.deleted (createAuditMiddleware, placed after authenticate and before the intent guard, so a refused delete is a denied row and a missing todo a failed one). Reading the trail is audit.list on iam-service.
list is cached (.meta({ cache: ["todo"] })); create, complete, and delete invalidate that cache (.meta({ invalidatesCache: ["todo"] })) after a successful write (see createCacheMiddleware in cache), attached in src/lib/create-app.ts after the intent guard, so a cached list is never served to a caller who may not read it. The cache key includes the input (so workspaceId); the tag is shared, so a write in one workspace drops every workspace's cached list (correct, a little eager).
create is rate limited per client IP (.meta({ rateLimit: { rules: [{ by: "ip", limit: 30, windowMs: 60_000 }] } })). A refused call returns 429 RATE_LIMIT_EXCEEDED with a Retry-After header. The client address is the last x-forwarded-for entry (TRUSTED_PROXY_HOPS in createClientIpRateLimitMiddleware, @gentlemans-stack/server-setup/client-ip-rate-limit). The limiter is rate-limit backed by Redis, in its own namespace so cache invalidation never clears rate-limit windows.
Extend this example
- Add a procedure: give its contract an
intentand aworkspaceIdinput, then implement it on the guarded implementer insrc/lib/create-app.ts. - Register any additional shared OpenAPI schemas in
src/lib/openapi-schemas.ts.
Configuration
Add these values to the repository root .env.
MONGO_URI: MongoDB connection URI, shared by every service.TODO_SERVICE_REDIS_URI: Redis connection URI, used for response caching and rate-limit windows.TODO_SERVICE_CACHE_TTL_MS: default cache entry TTL in milliseconds; defaults to30000.TODO_SERVICE_API_ADDRESS: externally advertised service address.TODO_SERVICE_API_HOST: listener host; defaults to0.0.0.0.TODO_SERVICE_API_PORT: listener port; defaults to33002.TODO_SERVICE_IAM_URL: IAM base URL used to verify sessions; defaults tohttp://localhost:33001.TODO_SERVICE_IAM_SESSION_CACHE_TTL_MS: how long a verified session is trusted without asking IAM (capped at 300000); defaults to60000.TODO_SERVICE_IAM_TIMEOUT_MS: timeout of the IAM session call; defaults to3000.TODO_SERVICE_TRUSTED_ORIGINS: comma-separated exact todo-ui origins allowed to call with cookies, no wildcards; defaults tohttp://localhost:4200.LOG_LEVELandNODE_ENV: shared logging/runtime configuration.
Commands
pnpm nx run todo-service:typecheck # type-check the service and its dependencies
pnpm nx run todo-service:test # run the service tests
pnpm nx run todo-service:serve # run locally
pnpm nx run todo-service:build # create the deployable bundle
pnpm nx run todo-service:prune # create the deployment manifest and workspace modules
pnpm nx run todo-service:sync-deploy-deps # check the bundle dependencies against the deploy manifest
pnpm nx run todo-service:docker:build # build the container image
pnpm nx run todo-service:docker:run # run the container image