Lab 15: Capstone — Typed API

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

CAPSTONE: Fully typed Express REST API with Zod schemas, typed middleware stack, Result<T,E> error handling, typed env vars, 100% strict TypeScript.


Step 1: Setup

docker run -it --rm node:20-alpine sh
npm install -g typescript ts-node
mkdir /lab15 && cd /lab15
npm init -y
npm install express zod
npm install --save-dev @types/express @types/node typescript
cat > tsconfig.json << 'EOF'
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  }
}
EOF

Step 2: Typed Environment Variables


Step 3: Result<T, E> Error Handling


Step 4: Zod Schemas (Request/Response)


Step 5: Typed Middleware Stack


Step 6: Typed Service Layer


Step 7: Typed Router


Step 8: Capstone — Runnable API

Run:

📸 Verified Output:


Summary — Full Stack TypeScript Patterns

Layer
Pattern
Library

Environment

Zod schema validation

zod

Error handling

Result<T, E> discriminated union

Pure TS

Request validation

validateBody(ZodSchema) middleware

zod + express

Service layer

Returns Result<T, AppError>

Pure TS

Router

Typed params, body, response

@types/express

Config

as const + z.infer<>

zod

Last updated