A monorepo using pnpm workspaces containing shared TypeScript types and Cloudflare Workers.
ns-cf/
├── packages/
│ └── nerdsec/ # Shared types and utilities
│ ├── src/
│ │ ├── types/ # TypeScript type definitions
│ │ ├── utils/ # Utility functions
│ │ └── index.ts # Main export file
│ ├── package.json
│ └── tsconfig.json
├── workers/
│ └── dev-api/ # Cloudflare Worker API
│ ├── src/
│ │ └── index.ts # Worker entry point
│ ├── package.json
│ ├── tsconfig.json
│ └── wrangler.toml # Cloudflare configuration
├── package.json # Root package.json
├── pnpm-workspace.yaml # Workspace configuration
├── tsconfig.json # Root TypeScript config
└── README.md
- Node.js 18+
- pnpm 8+
- Cloudflare account (for deployment)
-
Install dependencies:
pnpm install
-
Build the shared package:
cd packages/nerdsec pnpm build
The @nerdsec/core package contains shared types and utilities:
- Types: User management, security events, API responses, pagination
- Utils: Response helpers, validation, sanitization, ID generation
To add new types or utilities:
- Edit files in
packages/nerdsec/src/ - Run
pnpm buildto compile TypeScript - The compiled package will be available to workers
-
Start development server:
cd workers/dev-api pnpm dev -
The worker will be available at:
http://localhost:8787
GET /- Health checkGET /health- Detailed health statusGET /users- List users (paginated)POST /users- Create a new userGET /security/events- List security events (paginated)POST /security/events- Create a security event
Create a user:
curl -X POST http://localhost:8787/users \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"name": "Test User",
"role": "user"
}'Get users:
curl http://localhost:8787/users?page=1&limit=10- Build all packages:
pnpm build - Type check all packages:
pnpm type-check - Clean build artifacts:
pnpm clean
-
Login to Cloudflare:
cd workers/dev-api npx wrangler login -
Deploy:
pnpm deploy
The worker supports multiple environments:
development- For local developmentstaging- For staging environmentproduction- For production environment
Deploy to specific environment:
npx wrangler deploy --env staging- Shared Types: Common TypeScript definitions across all projects
- Code Reuse: Utilities and helpers available to all workers
- Consistent APIs: Standardized response formats and error handling
- Type Safety: Full TypeScript support with shared interfaces
The dev-api worker depends on @nerdsec/core using workspace protocol:
{
"dependencies": {
"@nerdsec/core": "workspace:*"
}
}This ensures the worker always uses the local version of the shared package.
-
Create worker directory:
mkdir workers/new-worker cd workers/new-worker -
Create package.json:
{ "name": "@nerdsec/new-worker", "dependencies": { "@nerdsec/core": "workspace:*" } } -
Add to workspace: The worker will be automatically included due to the
workers/*pattern inpnpm-workspace.yaml
From the root directory:
pnpm build- Build all packagespnpm dev- Start development for all packagespnpm type-check- Type check all packagespnpm clean- Clean all build artifacts
- Type-safe APIs using shared TypeScript definitions
- Standardized responses with consistent error handling
- Input validation and sanitization for security
- Pagination support for list endpoints
- CORS enabled for cross-origin requests
- Environment-specific deployments (dev/staging/prod)
- Make changes to shared types in
packages/nerdsec/src/ - Build the package:
pnpm build - Test changes in workers
- Ensure type checking passes:
pnpm type-check
The monorepo structure ensures that changes to shared types are immediately available to all workers while maintaining type safety throughout the codebase.