servcraft 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/.dockerignore +45 -0
  2. package/.env.example +46 -0
  3. package/.husky/commit-msg +1 -0
  4. package/.husky/pre-commit +1 -0
  5. package/.prettierignore +4 -0
  6. package/.prettierrc +11 -0
  7. package/Dockerfile +76 -0
  8. package/Dockerfile.dev +31 -0
  9. package/README.md +232 -0
  10. package/commitlint.config.js +24 -0
  11. package/dist/cli/index.cjs +3968 -0
  12. package/dist/cli/index.cjs.map +1 -0
  13. package/dist/cli/index.d.cts +1 -0
  14. package/dist/cli/index.d.ts +1 -0
  15. package/dist/cli/index.js +3945 -0
  16. package/dist/cli/index.js.map +1 -0
  17. package/dist/index.cjs +2458 -0
  18. package/dist/index.cjs.map +1 -0
  19. package/dist/index.d.cts +828 -0
  20. package/dist/index.d.ts +828 -0
  21. package/dist/index.js +2332 -0
  22. package/dist/index.js.map +1 -0
  23. package/docker-compose.prod.yml +118 -0
  24. package/docker-compose.yml +147 -0
  25. package/eslint.config.js +27 -0
  26. package/npm-cache/_cacache/content-v2/sha512/1c/d0/03440d500a0487621aad1d6402978340698976602046db8e24fa03c01ee6c022c69b0582f969042d9442ee876ac35c038e960dd427d1e622fa24b8eb7dba +0 -0
  27. package/npm-cache/_cacache/content-v2/sha512/42/55/28b493ca491833e5aab0e9c3108d29ab3f36c248ca88f45d4630674fce9130959e56ae308797ac2b6328fa7f09a610b9550ed09cb971d039876d293fc69d +0 -0
  28. package/npm-cache/_cacache/content-v2/sha512/e0/12/f360dc9315ee5f17844a0c8c233ee6bf7c30837c4a02ea0d56c61c7f7ab21c0e958e50ed2c57c59f983c762b93056778c9009b2398ffc26def0183999b13 +0 -0
  29. package/npm-cache/_cacache/content-v2/sha512/ed/b0/fae1161902898f4c913c67d7f6cdf6be0665aec3b389b9c4f4f0a101ca1da59badf1b59c4e0030f5223023b8d63cfe501c46a32c20c895d4fb3f11ca2232 +0 -0
  30. package/npm-cache/_cacache/index-v5/58/94/c2cba79e0f16b4c10e95a87e32255741149e8222cc314a476aab67c39cc0 +5 -0
  31. package/npm-cache/_update-notifier-last-checked +0 -0
  32. package/package.json +112 -0
  33. package/prisma/schema.prisma +157 -0
  34. package/src/cli/commands/add-module.ts +422 -0
  35. package/src/cli/commands/db.ts +137 -0
  36. package/src/cli/commands/docs.ts +16 -0
  37. package/src/cli/commands/generate.ts +459 -0
  38. package/src/cli/commands/init.ts +640 -0
  39. package/src/cli/index.ts +32 -0
  40. package/src/cli/templates/controller.ts +67 -0
  41. package/src/cli/templates/dynamic-prisma.ts +89 -0
  42. package/src/cli/templates/dynamic-schemas.ts +232 -0
  43. package/src/cli/templates/dynamic-types.ts +60 -0
  44. package/src/cli/templates/module-index.ts +33 -0
  45. package/src/cli/templates/prisma-model.ts +17 -0
  46. package/src/cli/templates/repository.ts +104 -0
  47. package/src/cli/templates/routes.ts +70 -0
  48. package/src/cli/templates/schemas.ts +26 -0
  49. package/src/cli/templates/service.ts +58 -0
  50. package/src/cli/templates/types.ts +27 -0
  51. package/src/cli/utils/docs-generator.ts +47 -0
  52. package/src/cli/utils/field-parser.ts +315 -0
  53. package/src/cli/utils/helpers.ts +89 -0
  54. package/src/config/env.ts +80 -0
  55. package/src/config/index.ts +97 -0
  56. package/src/core/index.ts +5 -0
  57. package/src/core/logger.ts +43 -0
  58. package/src/core/server.ts +132 -0
  59. package/src/database/index.ts +7 -0
  60. package/src/database/prisma.ts +54 -0
  61. package/src/database/seed.ts +59 -0
  62. package/src/index.ts +63 -0
  63. package/src/middleware/error-handler.ts +73 -0
  64. package/src/middleware/index.ts +3 -0
  65. package/src/middleware/security.ts +116 -0
  66. package/src/modules/audit/audit.service.ts +192 -0
  67. package/src/modules/audit/index.ts +2 -0
  68. package/src/modules/audit/types.ts +37 -0
  69. package/src/modules/auth/auth.controller.ts +182 -0
  70. package/src/modules/auth/auth.middleware.ts +87 -0
  71. package/src/modules/auth/auth.routes.ts +123 -0
  72. package/src/modules/auth/auth.service.ts +142 -0
  73. package/src/modules/auth/index.ts +49 -0
  74. package/src/modules/auth/schemas.ts +52 -0
  75. package/src/modules/auth/types.ts +69 -0
  76. package/src/modules/email/email.service.ts +212 -0
  77. package/src/modules/email/index.ts +10 -0
  78. package/src/modules/email/templates.ts +213 -0
  79. package/src/modules/email/types.ts +57 -0
  80. package/src/modules/swagger/index.ts +3 -0
  81. package/src/modules/swagger/schema-builder.ts +263 -0
  82. package/src/modules/swagger/swagger.service.ts +169 -0
  83. package/src/modules/swagger/types.ts +68 -0
  84. package/src/modules/user/index.ts +30 -0
  85. package/src/modules/user/schemas.ts +49 -0
  86. package/src/modules/user/types.ts +78 -0
  87. package/src/modules/user/user.controller.ts +139 -0
  88. package/src/modules/user/user.repository.ts +156 -0
  89. package/src/modules/user/user.routes.ts +199 -0
  90. package/src/modules/user/user.service.ts +145 -0
  91. package/src/modules/validation/index.ts +18 -0
  92. package/src/modules/validation/validator.ts +104 -0
  93. package/src/types/common.ts +61 -0
  94. package/src/types/index.ts +10 -0
  95. package/src/utils/errors.ts +66 -0
  96. package/src/utils/index.ts +33 -0
  97. package/src/utils/pagination.ts +38 -0
  98. package/src/utils/response.ts +63 -0
  99. package/tests/integration/auth.test.ts +59 -0
  100. package/tests/setup.ts +17 -0
  101. package/tests/unit/modules/validation.test.ts +88 -0
  102. package/tests/unit/utils/errors.test.ts +113 -0
  103. package/tests/unit/utils/pagination.test.ts +82 -0
  104. package/tsconfig.json +33 -0
  105. package/tsup.config.ts +14 -0
  106. package/vitest.config.ts +34 -0
package/.dockerignore ADDED
@@ -0,0 +1,45 @@
1
+ # Dependencies
2
+ node_modules
3
+
4
+ # Build output
5
+ dist
6
+
7
+ # Git
8
+ .git
9
+ .gitignore
10
+
11
+ # IDE
12
+ .vscode
13
+ .idea
14
+
15
+ # Logs
16
+ logs
17
+ *.log
18
+
19
+ # Environment files
20
+ .env
21
+ .env.local
22
+ .env.*.local
23
+
24
+ # Test coverage
25
+ coverage
26
+
27
+ # Docker files (not needed inside container)
28
+ Dockerfile*
29
+ docker-compose*
30
+ .dockerignore
31
+
32
+ # Documentation
33
+ *.md
34
+ docs
35
+
36
+ # Tests
37
+ tests
38
+ **/*.test.ts
39
+ **/*.spec.ts
40
+
41
+ # Misc
42
+ .DS_Store
43
+ Thumbs.db
44
+ *.swp
45
+ *.swo
package/.env.example ADDED
@@ -0,0 +1,46 @@
1
+ # =================================
2
+ # Server Configuration
3
+ # =================================
4
+ NODE_ENV=development
5
+ PORT=3000
6
+ HOST=0.0.0.0
7
+
8
+ # =================================
9
+ # Database
10
+ # =================================
11
+ DATABASE_URL="postgresql://user:password@localhost:5432/servcraft?schema=public"
12
+ # DATABASE_URL="mysql://user:password@localhost:3306/servcraft"
13
+ # DATABASE_URL="file:./dev.db"
14
+
15
+ # =================================
16
+ # JWT Configuration
17
+ # =================================
18
+ JWT_SECRET=your-super-secret-key-min-32-characters-long
19
+ JWT_ACCESS_EXPIRES_IN=15m
20
+ JWT_REFRESH_EXPIRES_IN=7d
21
+
22
+ # =================================
23
+ # Security
24
+ # =================================
25
+ CORS_ORIGIN=http://localhost:3000,http://localhost:5173
26
+ RATE_LIMIT_MAX=100
27
+ RATE_LIMIT_WINDOW_MS=60000
28
+
29
+ # =================================
30
+ # Email (SMTP)
31
+ # =================================
32
+ SMTP_HOST=smtp.example.com
33
+ SMTP_PORT=587
34
+ SMTP_USER=your-email@example.com
35
+ SMTP_PASS=your-email-password
36
+ SMTP_FROM="Servcraft <noreply@example.com>"
37
+
38
+ # =================================
39
+ # Redis (Optional)
40
+ # =================================
41
+ REDIS_URL=redis://localhost:6379
42
+
43
+ # =================================
44
+ # Logging
45
+ # =================================
46
+ LOG_LEVEL=info
@@ -0,0 +1 @@
1
+ npx --no -- commitlint --edit "$1"
@@ -0,0 +1 @@
1
+ npx lint-staged
@@ -0,0 +1,4 @@
1
+ dist
2
+ node_modules
3
+ coverage
4
+ *.md
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "semi": true,
3
+ "trailingComma": "es5",
4
+ "singleQuote": true,
5
+ "printWidth": 100,
6
+ "tabWidth": 2,
7
+ "useTabs": false,
8
+ "bracketSpacing": true,
9
+ "arrowParens": "always",
10
+ "endOfLine": "lf"
11
+ }
package/Dockerfile ADDED
@@ -0,0 +1,76 @@
1
+ # ==========================================
2
+ # Servcraft - Production Dockerfile
3
+ # Multi-stage build for optimized image size
4
+ # ==========================================
5
+
6
+ # Stage 1: Dependencies
7
+ FROM node:20-alpine AS deps
8
+ WORKDIR /app
9
+
10
+ # Install dependencies needed for native modules
11
+ RUN apk add --no-cache libc6-compat
12
+
13
+ # Copy package files
14
+ COPY package*.json ./
15
+ COPY prisma ./prisma/
16
+
17
+ # Install dependencies
18
+ RUN npm ci --only=production && npm cache clean --force
19
+
20
+ # Generate Prisma client
21
+ RUN npx prisma generate
22
+
23
+ # Stage 2: Builder
24
+ FROM node:20-alpine AS builder
25
+ WORKDIR /app
26
+
27
+ COPY package*.json ./
28
+ COPY tsconfig.json ./
29
+ COPY tsup.config.ts ./
30
+ COPY prisma ./prisma/
31
+
32
+ # Install all dependencies (including dev)
33
+ RUN npm ci
34
+
35
+ # Copy source code
36
+ COPY src ./src
37
+
38
+ # Generate Prisma client
39
+ RUN npx prisma generate
40
+
41
+ # Build the application
42
+ RUN npm run build
43
+
44
+ # Stage 3: Production
45
+ FROM node:20-alpine AS production
46
+ WORKDIR /app
47
+
48
+ # Add non-root user for security
49
+ RUN addgroup --system --gid 1001 nodejs
50
+ RUN adduser --system --uid 1001 servcraft
51
+
52
+ # Set environment
53
+ ENV NODE_ENV=production
54
+ ENV PORT=3000
55
+
56
+ # Copy necessary files from previous stages
57
+ COPY --from=deps /app/node_modules ./node_modules
58
+ COPY --from=builder /app/dist ./dist
59
+ COPY --from=builder /app/prisma ./prisma
60
+ COPY package*.json ./
61
+
62
+ # Set ownership
63
+ RUN chown -R servcraft:nodejs /app
64
+
65
+ # Switch to non-root user
66
+ USER servcraft
67
+
68
+ # Expose port
69
+ EXPOSE 3000
70
+
71
+ # Health check
72
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
73
+ CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
74
+
75
+ # Start the application
76
+ CMD ["node", "dist/index.js"]
package/Dockerfile.dev ADDED
@@ -0,0 +1,31 @@
1
+ # ==========================================
2
+ # Servcraft - Development Dockerfile
3
+ # ==========================================
4
+
5
+ FROM node:20-alpine
6
+
7
+ WORKDIR /app
8
+
9
+ # Install dependencies needed for native modules
10
+ RUN apk add --no-cache libc6-compat
11
+
12
+ # Copy package files
13
+ COPY package*.json ./
14
+
15
+ # Install all dependencies
16
+ RUN npm install
17
+
18
+ # Copy prisma schema
19
+ COPY prisma ./prisma/
20
+
21
+ # Generate Prisma client
22
+ RUN npx prisma generate
23
+
24
+ # Copy source code
25
+ COPY . .
26
+
27
+ # Expose port
28
+ EXPOSE 3000
29
+
30
+ # Start in development mode with hot reload
31
+ CMD ["npm", "run", "dev"]
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # Servcraft
2
+
3
+ A modular, production-ready Node.js backend framework built with TypeScript, Fastify, and Prisma.
4
+
5
+ ## Features
6
+
7
+ - **Core Server**: Fastify with graceful shutdown, health checks
8
+ - **Authentication**: JWT access/refresh tokens, RBAC
9
+ - **User Management**: Full CRUD with roles & permissions
10
+ - **Validation**: Zod/Joi/Yup support
11
+ - **Database**: Prisma ORM (PostgreSQL, MySQL, SQLite)
12
+ - **Email**: SMTP with Handlebars templates
13
+ - **Security**: Helmet, CORS, Rate limiting
14
+ - **Logging**: Pino structured logs + Audit trail
15
+ - **Docker**: Ready for containerization
16
+ - **CLI**: Generate modules, controllers, services
17
+
18
+ ## Quick Start
19
+
20
+ ### Create a new project
21
+
22
+ ```bash
23
+ npx servcraft init my-app
24
+ cd my-app
25
+ npm run dev
26
+ ```
27
+
28
+ ### Interactive setup
29
+
30
+ ```bash
31
+ npx servcraft init
32
+ ```
33
+
34
+ You'll be prompted to choose:
35
+ - Project name
36
+ - Language (TypeScript/JavaScript)
37
+ - Database (PostgreSQL, MySQL, SQLite, MongoDB)
38
+ - Validation library (Zod, Joi, Yup)
39
+ - Features (Auth, Users, Email, etc.)
40
+
41
+ ## CLI Commands
42
+
43
+ ### Initialize project
44
+
45
+ ```bash
46
+ servcraft init [name] # Create new project
47
+ servcraft init --yes # Use defaults
48
+ servcraft init --js # Use JavaScript
49
+ servcraft init --db postgresql # Specify database
50
+ ```
51
+
52
+ ### Generate resources
53
+
54
+ ```bash
55
+ # Generate complete module
56
+ servcraft generate module product
57
+ servcraft g m product --prisma # Include Prisma model
58
+
59
+ # Generate individual files
60
+ servcraft generate controller user
61
+ servcraft generate service order
62
+ servcraft generate repository item
63
+ servcraft generate schema post
64
+ servcraft generate routes comment
65
+
66
+ # Aliases
67
+ servcraft g c user # controller
68
+ servcraft g s order # service
69
+ servcraft g r item # repository
70
+ servcraft g v post # schema/validator
71
+ ```
72
+
73
+ ### Add pre-built modules
74
+
75
+ ```bash
76
+ servcraft add auth # Authentication module
77
+ servcraft add users # User management
78
+ servcraft add email # Email service
79
+ servcraft add audit # Audit logging
80
+ servcraft add cache # Redis cache
81
+ servcraft add upload # File uploads
82
+ servcraft add --list # Show all modules
83
+ ```
84
+
85
+ ### Database commands
86
+
87
+ ```bash
88
+ servcraft db migrate # Run migrations
89
+ servcraft db push # Push schema
90
+ servcraft db generate # Generate client
91
+ servcraft db studio # Open Prisma Studio
92
+ servcraft db seed # Seed database
93
+ servcraft db reset # Reset database
94
+ ```
95
+
96
+ ## Project Structure
97
+
98
+ ```
99
+ my-app/
100
+ ├── src/
101
+ │ ├── core/ # Server, logger
102
+ │ ├── config/ # Environment config
103
+ │ ├── modules/
104
+ │ │ ├── auth/ # Authentication
105
+ │ │ ├── user/ # User management
106
+ │ │ ├── email/ # Email service
107
+ │ │ └── [your-modules]/
108
+ │ ├── middleware/ # Security, error handling
109
+ │ ├── utils/ # Helpers, errors
110
+ │ ├── types/ # Type definitions
111
+ │ └── index.ts # Entry point
112
+ ├── prisma/
113
+ │ └── schema.prisma # Database schema
114
+ ├── tests/
115
+ │ ├── unit/
116
+ │ └── integration/
117
+ ├── docker-compose.yml
118
+ └── package.json
119
+ ```
120
+
121
+ ## Module Architecture
122
+
123
+ Each module follows the Controller/Service/Repository pattern:
124
+
125
+ ```
126
+ modules/product/
127
+ ├── product.types.ts # Interfaces & types
128
+ ├── product.schemas.ts # Validation schemas
129
+ ├── product.repository.ts # Data access layer
130
+ ├── product.service.ts # Business logic
131
+ ├── product.controller.ts # HTTP handlers
132
+ ├── product.routes.ts # Route definitions
133
+ └── index.ts # Module exports
134
+ ```
135
+
136
+ ## Environment Variables
137
+
138
+ ```env
139
+ # Server
140
+ NODE_ENV=development
141
+ PORT=3000
142
+ HOST=0.0.0.0
143
+
144
+ # Database
145
+ DATABASE_URL="postgresql://user:pass@localhost:5432/db"
146
+ DATABASE_PROVIDER=postgresql
147
+
148
+ # JWT
149
+ JWT_SECRET=your-secret-key-min-32-chars
150
+ JWT_ACCESS_EXPIRES_IN=15m
151
+ JWT_REFRESH_EXPIRES_IN=7d
152
+
153
+ # Security
154
+ CORS_ORIGIN=http://localhost:3000
155
+ RATE_LIMIT_MAX=100
156
+
157
+ # Email
158
+ SMTP_HOST=smtp.example.com
159
+ SMTP_PORT=587
160
+ SMTP_USER=user
161
+ SMTP_PASS=pass
162
+
163
+ # Logging
164
+ LOG_LEVEL=info
165
+ ```
166
+
167
+ ## API Endpoints
168
+
169
+ ### Authentication
170
+
171
+ ```
172
+ POST /auth/register Register new user
173
+ POST /auth/login Login
174
+ POST /auth/refresh Refresh tokens
175
+ POST /auth/logout Logout
176
+ GET /auth/me Get current user
177
+ POST /auth/change-password
178
+ ```
179
+
180
+ ### Users (Admin)
181
+
182
+ ```
183
+ GET /users List users
184
+ GET /users/:id Get user
185
+ PATCH /users/:id Update user
186
+ DELETE /users/:id Delete user
187
+ POST /users/:id/suspend
188
+ POST /users/:id/ban
189
+ POST /users/:id/activate
190
+ ```
191
+
192
+ ### Health
193
+
194
+ ```
195
+ GET /health Health check
196
+ GET /ready Readiness check
197
+ ```
198
+
199
+ ## Docker
200
+
201
+ ### Development
202
+
203
+ ```bash
204
+ docker-compose up -d
205
+ ```
206
+
207
+ ### Production
208
+
209
+ ```bash
210
+ docker-compose -f docker-compose.prod.yml up -d
211
+ ```
212
+
213
+ ## Scripts
214
+
215
+ ```bash
216
+ npm run dev # Development with hot reload
217
+ npm run build # Build for production
218
+ npm run start # Start production server
219
+ npm run test # Run tests
220
+ npm run test:coverage # Test with coverage
221
+ npm run lint # Lint code
222
+ npm run format # Format code
223
+ npm run typecheck # Type check
224
+ npm run db:migrate # Run migrations
225
+ npm run db:push # Push schema
226
+ npm run db:studio # Open Prisma Studio
227
+ npm run db:seed # Seed database
228
+ ```
229
+
230
+ ## License
231
+
232
+ MIT
@@ -0,0 +1,24 @@
1
+ export default {
2
+ extends: ['@commitlint/config-conventional'],
3
+ rules: {
4
+ 'type-enum': [
5
+ 2,
6
+ 'always',
7
+ [
8
+ 'feat', // New feature
9
+ 'fix', // Bug fix
10
+ 'docs', // Documentation
11
+ 'style', // Formatting, missing semicolons, etc.
12
+ 'refactor', // Code refactoring
13
+ 'perf', // Performance improvements
14
+ 'test', // Adding tests
15
+ 'chore', // Maintenance tasks
16
+ 'ci', // CI/CD changes
17
+ 'build', // Build system changes
18
+ 'revert', // Revert previous commit
19
+ ],
20
+ ],
21
+ 'subject-case': [2, 'always', 'lower-case'],
22
+ 'subject-max-length': [2, 'always', 72],
23
+ },
24
+ };