@patricio0312rev/skillset 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 (115) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE +21 -0
  3. package/README.md +176 -0
  4. package/bin/cli.js +37 -0
  5. package/package.json +55 -0
  6. package/src/commands/init.js +301 -0
  7. package/src/index.js +168 -0
  8. package/src/lib/config.js +200 -0
  9. package/src/lib/generator.js +166 -0
  10. package/src/utils/display.js +95 -0
  11. package/src/utils/readme.js +196 -0
  12. package/src/utils/tool-specific.js +233 -0
  13. package/templates/ai-engineering/agent-orchestration-planner/ SKILL.md +266 -0
  14. package/templates/ai-engineering/cost-latency-optimizer/ SKILL.md +270 -0
  15. package/templates/ai-engineering/doc-to-vector-dataset-generator/ SKILL.md +239 -0
  16. package/templates/ai-engineering/evaluation-harness/ SKILL.md +219 -0
  17. package/templates/ai-engineering/guardrails-safety-filter-builder/ SKILL.md +226 -0
  18. package/templates/ai-engineering/llm-debugger/ SKILL.md +283 -0
  19. package/templates/ai-engineering/prompt-regression-tester/ SKILL.md +216 -0
  20. package/templates/ai-engineering/prompt-template-builder/ SKILL.md +393 -0
  21. package/templates/ai-engineering/rag-pipeline-builder/ SKILL.md +244 -0
  22. package/templates/ai-engineering/tool-function-schema-designer/ SKILL.md +219 -0
  23. package/templates/architecture/adr-writer/ SKILL.md +250 -0
  24. package/templates/architecture/api-versioning-deprecation-planner/ SKILL.md +331 -0
  25. package/templates/architecture/domain-model-boundaries-mapper/ SKILL.md +300 -0
  26. package/templates/architecture/migration-planner/ SKILL.md +376 -0
  27. package/templates/architecture/performance-budget-setter/ SKILL.md +318 -0
  28. package/templates/architecture/reliability-strategy-builder/ SKILL.md +286 -0
  29. package/templates/architecture/rfc-generator/ SKILL.md +362 -0
  30. package/templates/architecture/scalability-playbook/ SKILL.md +279 -0
  31. package/templates/architecture/system-design-generator/ SKILL.md +339 -0
  32. package/templates/architecture/tech-debt-prioritizer/ SKILL.md +329 -0
  33. package/templates/backend/api-contract-normalizer/ SKILL.md +487 -0
  34. package/templates/backend/api-endpoint-generator/ SKILL.md +415 -0
  35. package/templates/backend/auth-module-builder/ SKILL.md +99 -0
  36. package/templates/backend/background-jobs-designer/ SKILL.md +166 -0
  37. package/templates/backend/caching-strategist/ SKILL.md +190 -0
  38. package/templates/backend/error-handling-standardizer/ SKILL.md +174 -0
  39. package/templates/backend/rate-limiting-abuse-protection/ SKILL.md +147 -0
  40. package/templates/backend/rbac-permissions-builder/ SKILL.md +158 -0
  41. package/templates/backend/service-layer-extractor/ SKILL.md +269 -0
  42. package/templates/backend/webhook-receiver-hardener/ SKILL.md +211 -0
  43. package/templates/ci-cd/artifact-sbom-publisher/ SKILL.md +236 -0
  44. package/templates/ci-cd/caching-strategy-optimizer/ SKILL.md +195 -0
  45. package/templates/ci-cd/deployment-checklist-generator/ SKILL.md +381 -0
  46. package/templates/ci-cd/github-actions-pipeline-creator/ SKILL.md +348 -0
  47. package/templates/ci-cd/monorepo-ci-optimizer/ SKILL.md +298 -0
  48. package/templates/ci-cd/preview-environments-builder/ SKILL.md +187 -0
  49. package/templates/ci-cd/quality-gates-enforcer/ SKILL.md +342 -0
  50. package/templates/ci-cd/release-automation-builder/ SKILL.md +281 -0
  51. package/templates/ci-cd/rollback-workflow-builder/ SKILL.md +372 -0
  52. package/templates/ci-cd/secrets-env-manager/ SKILL.md +242 -0
  53. package/templates/db-management/backup-restore-runbook-generator/ SKILL.md +505 -0
  54. package/templates/db-management/data-integrity-auditor/ SKILL.md +505 -0
  55. package/templates/db-management/data-retention-archiving-planner/ SKILL.md +430 -0
  56. package/templates/db-management/data-seeding-fixtures-builder/ SKILL.md +375 -0
  57. package/templates/db-management/db-performance-watchlist/ SKILL.md +425 -0
  58. package/templates/db-management/etl-sync-job-builder/ SKILL.md +457 -0
  59. package/templates/db-management/multi-tenant-safety-checker/ SKILL.md +398 -0
  60. package/templates/db-management/prisma-migration-assistant/ SKILL.md +379 -0
  61. package/templates/db-management/schema-consistency-checker/ SKILL.md +440 -0
  62. package/templates/db-management/sql-query-optimizer/ SKILL.md +324 -0
  63. package/templates/foundation/changelog-writer/ SKILL.md +431 -0
  64. package/templates/foundation/code-formatter-installer/ SKILL.md +320 -0
  65. package/templates/foundation/codebase-summarizer/ SKILL.md +360 -0
  66. package/templates/foundation/dependency-doctor/ SKILL.md +163 -0
  67. package/templates/foundation/dev-environment-bootstrapper/ SKILL.md +259 -0
  68. package/templates/foundation/dev-onboarding-builder/ SKILL.md +556 -0
  69. package/templates/foundation/docs-starter-kit/ SKILL.md +574 -0
  70. package/templates/foundation/explaining-code/SKILL.md +13 -0
  71. package/templates/foundation/git-hygiene-enforcer/ SKILL.md +455 -0
  72. package/templates/foundation/project-scaffolder/ SKILL.md +65 -0
  73. package/templates/foundation/project-scaffolder/references/templates.md +126 -0
  74. package/templates/foundation/repo-structure-linter/ SKILL.md +0 -0
  75. package/templates/foundation/repo-structure-linter/references/conventions.md +98 -0
  76. package/templates/frontend/animation-micro-interaction-pack/ SKILL.md +41 -0
  77. package/templates/frontend/component-scaffold-generator/ SKILL.md +562 -0
  78. package/templates/frontend/design-to-component-translator/ SKILL.md +547 -0
  79. package/templates/frontend/form-wizard-builder/ SKILL.md +553 -0
  80. package/templates/frontend/frontend-refactor-planner/ SKILL.md +37 -0
  81. package/templates/frontend/i18n-frontend-implementer/ SKILL.md +44 -0
  82. package/templates/frontend/modal-drawer-system/ SKILL.md +377 -0
  83. package/templates/frontend/page-layout-builder/ SKILL.md +630 -0
  84. package/templates/frontend/state-ux-flow-builder/ SKILL.md +23 -0
  85. package/templates/frontend/table-builder/ SKILL.md +350 -0
  86. package/templates/performance/alerting-dashboard-builder/ SKILL.md +162 -0
  87. package/templates/performance/backend-latency-profiler-helper/ SKILL.md +108 -0
  88. package/templates/performance/caching-cdn-strategy-planner/ SKILL.md +150 -0
  89. package/templates/performance/capacity-planning-helper/ SKILL.md +242 -0
  90. package/templates/performance/core-web-vitals-tuner/ SKILL.md +126 -0
  91. package/templates/performance/incident-runbook-generator/ SKILL.md +162 -0
  92. package/templates/performance/load-test-scenario-builder/ SKILL.md +256 -0
  93. package/templates/performance/observability-setup/ SKILL.md +232 -0
  94. package/templates/performance/postmortem-writer/ SKILL.md +203 -0
  95. package/templates/performance/structured-logging-standardizer/ SKILL.md +122 -0
  96. package/templates/security/auth-security-reviewer/ SKILL.md +428 -0
  97. package/templates/security/dependency-vulnerability-triage/ SKILL.md +495 -0
  98. package/templates/security/input-validation-sanitization-auditor/ SKILL.md +76 -0
  99. package/templates/security/pii-redaction-logging-policy-builder/ SKILL.md +65 -0
  100. package/templates/security/rbac-policy-tester/ SKILL.md +80 -0
  101. package/templates/security/secrets-scanner/ SKILL.md +462 -0
  102. package/templates/security/secure-headers-csp-builder/ SKILL.md +404 -0
  103. package/templates/security/security-incident-playbook-generator/ SKILL.md +76 -0
  104. package/templates/security/security-pr-checklist-skill/ SKILL.md +62 -0
  105. package/templates/security/threat-model-generator/ SKILL.md +394 -0
  106. package/templates/testing/contract-testing-builder/ SKILL.md +492 -0
  107. package/templates/testing/coverage-strategist/ SKILL.md +436 -0
  108. package/templates/testing/e2e-test-builder/ SKILL.md +382 -0
  109. package/templates/testing/flaky-test-detective/ SKILL.md +416 -0
  110. package/templates/testing/integration-test-builder/ SKILL.md +525 -0
  111. package/templates/testing/mocking-assistant/ SKILL.md +383 -0
  112. package/templates/testing/snapshot-test-refactorer/ SKILL.md +375 -0
  113. package/templates/testing/test-data-factory-builder/ SKILL.md +449 -0
  114. package/templates/testing/test-reporting-triage-skill/ SKILL.md +469 -0
  115. package/templates/testing/unit-test-generator/ SKILL.md +548 -0
@@ -0,0 +1,415 @@
1
+ ---
2
+ name: api-contract-normalizer
3
+ description: Unifies API response patterns across endpoints including pagination format, error structure, status codes, response envelopes, and versioning strategy. Provides contract documentation, shared TypeScript types, middleware utilities, and migration plan. Use when standardizing "API contracts", "response formats", "API conventions", or "API consistency".
4
+ ---
5
+
6
+ # API Contract Normalizer
7
+
8
+ Standardize API contracts across all endpoints for consistency and developer experience.
9
+
10
+ ## Core Workflow
11
+
12
+ 1. **Audit existing APIs**: Document current inconsistencies
13
+ 2. **Define standards**: Response format, pagination, errors, status codes
14
+ 3. **Create shared types**: TypeScript interfaces for all contracts
15
+ 4. **Build middleware**: Normalize responses automatically
16
+ 5. **Document contract**: OpenAPI spec with examples
17
+ 6. **Migration plan**: Phased rollout strategy
18
+ 7. **Versioning**: API version strategy
19
+
20
+ ## Standard Response Envelope
21
+
22
+ ```typescript
23
+ // types/api-contract.ts
24
+ export interface ApiResponse<T = unknown> {
25
+ success: boolean;
26
+ data?: T;
27
+ error?: ApiError;
28
+ meta?: ResponseMeta;
29
+ }
30
+
31
+ export interface ApiError {
32
+ code: string;
33
+ message: string;
34
+ details?: Record<string, string[] | string>;
35
+ trace_id?: string;
36
+ }
37
+
38
+ export interface ResponseMeta {
39
+ timestamp: string;
40
+ request_id: string;
41
+ version: string;
42
+ }
43
+
44
+ export interface PaginatedResponse<T> extends ApiResponse<T[]> {
45
+ meta: ResponseMeta & PaginationMeta;
46
+ }
47
+
48
+ export interface PaginationMeta {
49
+ page: number;
50
+ limit: number;
51
+ total: number;
52
+ total_pages: number;
53
+ has_next: boolean;
54
+ has_prev: boolean;
55
+ }
56
+ ```
57
+
58
+ ## Pagination Standards
59
+
60
+ ```typescript
61
+ // Standard pagination query params
62
+ interface PaginationQuery {
63
+ page: number; // 1-indexed, default: 1
64
+ limit: number; // default: 10, max: 100
65
+ sort_by?: string; // field name
66
+ sort_order?: 'asc' | 'desc'; // default: 'desc'
67
+ }
68
+
69
+ // Standard pagination response
70
+ {
71
+ "success": true,
72
+ "data": [...],
73
+ "meta": {
74
+ "page": 1,
75
+ "limit": 10,
76
+ "total": 156,
77
+ "total_pages": 16,
78
+ "has_next": true,
79
+ "has_prev": false
80
+ }
81
+ }
82
+
83
+ // Cursor-based pagination (for large datasets)
84
+ interface CursorPaginationQuery {
85
+ cursor?: string;
86
+ limit: number;
87
+ }
88
+
89
+ interface CursorPaginationMeta {
90
+ next_cursor?: string;
91
+ prev_cursor?: string;
92
+ has_more: boolean;
93
+ }
94
+ ```
95
+
96
+ ## Error Standards
97
+
98
+ ```typescript
99
+ // Error taxonomy
100
+ export enum ErrorCode {
101
+ // Client errors (4xx)
102
+ VALIDATION_ERROR = 'VALIDATION_ERROR',
103
+ UNAUTHORIZED = 'UNAUTHORIZED',
104
+ FORBIDDEN = 'FORBIDDEN',
105
+ NOT_FOUND = 'NOT_FOUND',
106
+ CONFLICT = 'CONFLICT',
107
+ RATE_LIMIT_EXCEEDED = 'RATE_LIMIT_EXCEEDED',
108
+
109
+ // Server errors (5xx)
110
+ INTERNAL_ERROR = 'INTERNAL_ERROR',
111
+ SERVICE_UNAVAILABLE = 'SERVICE_UNAVAILABLE',
112
+ TIMEOUT = 'TIMEOUT',
113
+ }
114
+
115
+ // Error to HTTP status mapping
116
+ export const ERROR_STATUS_MAP: Record<ErrorCode, number> = {
117
+ VALIDATION_ERROR: 400,
118
+ UNAUTHORIZED: 401,
119
+ FORBIDDEN: 403,
120
+ NOT_FOUND: 404,
121
+ CONFLICT: 409,
122
+ RATE_LIMIT_EXCEEDED: 429,
123
+ INTERNAL_ERROR: 500,
124
+ SERVICE_UNAVAILABLE: 503,
125
+ TIMEOUT: 504,
126
+ };
127
+
128
+ // Standard error responses
129
+ {
130
+ "success": false,
131
+ "error": {
132
+ "code": "VALIDATION_ERROR",
133
+ "message": "Invalid request data",
134
+ "details": {
135
+ "email": ["Invalid email format"],
136
+ "age": ["Must be at least 18"]
137
+ },
138
+ "trace_id": "abc123"
139
+ }
140
+ }
141
+ ```
142
+
143
+ ## Response Normalization Middleware
144
+
145
+ ```typescript
146
+ // middleware/normalize-response.ts
147
+ import { Request, Response, NextFunction } from "express";
148
+
149
+ export function normalizeResponse() {
150
+ return (req: Request, res: Response, next: NextFunction) => {
151
+ const originalJson = res.json.bind(res);
152
+
153
+ res.json = function (data: any) {
154
+ // Already normalized
155
+ if (data.success !== undefined) {
156
+ return originalJson(data);
157
+ }
158
+
159
+ // Normalize success response
160
+ const normalized: ApiResponse = {
161
+ success: true,
162
+ data,
163
+ meta: {
164
+ timestamp: new Date().toISOString(),
165
+ request_id: req.id,
166
+ version: "v1",
167
+ },
168
+ };
169
+
170
+ return originalJson(normalized);
171
+ };
172
+
173
+ next();
174
+ };
175
+ }
176
+
177
+ // Error normalization middleware
178
+ export function normalizeError() {
179
+ return (err: Error, req: Request, res: Response, next: NextFunction) => {
180
+ const error: ApiError = {
181
+ code: err.name || "INTERNAL_ERROR",
182
+ message: err.message || "An unexpected error occurred",
183
+ trace_id: req.id,
184
+ };
185
+
186
+ if (err instanceof ValidationError) {
187
+ error.details = err.details;
188
+ }
189
+
190
+ const statusCode = ERROR_STATUS_MAP[error.code] || 500;
191
+
192
+ res.status(statusCode).json({
193
+ success: false,
194
+ error,
195
+ meta: {
196
+ timestamp: new Date().toISOString(),
197
+ request_id: req.id,
198
+ version: "v1",
199
+ },
200
+ });
201
+ };
202
+ }
203
+ ```
204
+
205
+ ## Status Code Standards
206
+
207
+ ```typescript
208
+ // Standard status codes by operation
209
+ const STATUS_CODES = {
210
+ // Success
211
+ OK: 200, // GET, PUT, PATCH success
212
+ CREATED: 201, // POST success
213
+ NO_CONTENT: 204, // DELETE success
214
+
215
+ // Client errors
216
+ BAD_REQUEST: 400, // Validation errors
217
+ UNAUTHORIZED: 401, // Missing/invalid auth
218
+ FORBIDDEN: 403, // Insufficient permissions
219
+ NOT_FOUND: 404, // Resource not found
220
+ CONFLICT: 409, // Duplicate/conflict
221
+ UNPROCESSABLE: 422, // Semantic errors
222
+ TOO_MANY_REQUESTS: 429, // Rate limit
223
+
224
+ // Server errors
225
+ INTERNAL_ERROR: 500, // Unexpected errors
226
+ SERVICE_UNAVAILABLE: 503, // Temporarily down
227
+ GATEWAY_TIMEOUT: 504, // Upstream timeout
228
+ };
229
+ ```
230
+
231
+ ## Versioning Strategy
232
+
233
+ ```typescript
234
+ // URL versioning (recommended)
235
+ /api/v1/users
236
+ /api/v2/users
237
+
238
+ // Header versioning
239
+ Accept: application/vnd.api.v1+json
240
+
241
+ // Query param versioning (not recommended)
242
+ /api/users?version=1
243
+
244
+ // Version middleware
245
+ export function apiVersion(version: string) {
246
+ return (req: Request, res: Response, next: NextFunction) => {
247
+ req.apiVersion = version;
248
+ res.setHeader('X-API-Version', version);
249
+ next();
250
+ };
251
+ }
252
+
253
+ // Route versioning
254
+ app.use('/api/v1', apiVersion('v1'), v1Router);
255
+ app.use('/api/v2', apiVersion('v2'), v2Router);
256
+ ```
257
+
258
+ ## Migration Strategy
259
+
260
+ ```markdown
261
+ # API Contract Migration Plan
262
+
263
+ ## Phase 1: Add Normalization (Week 1-2)
264
+
265
+ - [ ] Deploy normalization middleware
266
+ - [ ] Run alongside existing responses
267
+ - [ ] Monitor for issues
268
+ - [ ] No breaking changes yet
269
+
270
+ ## Phase 2: Deprecation Notice (Week 3-4)
271
+
272
+ - [ ] Add deprecation headers
273
+ - [ ] Update documentation
274
+ - [ ] Notify API consumers
275
+ - [ ] Provide migration guide
276
+
277
+ ## Phase 3: Dual Format Support (Week 5-8)
278
+
279
+ - [ ] Support both old and new formats
280
+ - [ ] Add ?format=v2 query param
281
+ - [ ] Track adoption metrics
282
+ - [ ] Help consumers migrate
283
+
284
+ ## Phase 4: Switch Default (Week 9-10)
285
+
286
+ - [ ] New format becomes default
287
+ - [ ] Old format requires ?format=v1
288
+ - [ ] Final migration reminders
289
+ - [ ] Extended support period
290
+
291
+ ## Phase 5: Remove Old Format (Week 12+)
292
+
293
+ - [ ] Remove old format support
294
+ - [ ] Clean up legacy code
295
+ - [ ] Update all documentation
296
+ - [ ] Celebrate consistency! 🎉
297
+ ```
298
+
299
+ ## Contract Documentation
300
+
301
+ ```yaml
302
+ # openapi.yaml
303
+ openapi: 3.0.0
304
+ info:
305
+ title: Standardized API
306
+ version: 1.0.0
307
+ description: All endpoints follow this contract
308
+
309
+ components:
310
+ schemas:
311
+ ApiResponse:
312
+ type: object
313
+ required: [success]
314
+ properties:
315
+ success:
316
+ type: boolean
317
+ data:
318
+ type: object
319
+ error:
320
+ $ref: "#/components/schemas/ApiError"
321
+ meta:
322
+ $ref: "#/components/schemas/ResponseMeta"
323
+
324
+ ApiError:
325
+ type: object
326
+ required: [code, message]
327
+ properties:
328
+ code:
329
+ type: string
330
+ enum: [VALIDATION_ERROR, UNAUTHORIZED, ...]
331
+ message:
332
+ type: string
333
+ details:
334
+ type: object
335
+ additionalProperties: true
336
+ trace_id:
337
+ type: string
338
+
339
+ PaginationMeta:
340
+ type: object
341
+ required: [page, limit, total, total_pages]
342
+ properties:
343
+ page: { type: integer }
344
+ limit: { type: integer }
345
+ total: { type: integer }
346
+ total_pages: { type: integer }
347
+ has_next: { type: boolean }
348
+ has_prev: { type: boolean }
349
+ ```
350
+
351
+ ## Shared Utilities
352
+
353
+ ```typescript
354
+ // utils/api-response.ts
355
+ export class ApiResponseBuilder {
356
+ static success<T>(data: T, meta?: Partial<ResponseMeta>): ApiResponse<T> {
357
+ return {
358
+ success: true,
359
+ data,
360
+ meta: {
361
+ timestamp: new Date().toISOString(),
362
+ ...meta,
363
+ },
364
+ };
365
+ }
366
+
367
+ static paginated<T>(
368
+ data: T[],
369
+ pagination: PaginationMeta
370
+ ): PaginatedResponse<T> {
371
+ return {
372
+ success: true,
373
+ data,
374
+ meta: {
375
+ timestamp: new Date().toISOString(),
376
+ ...pagination,
377
+ },
378
+ };
379
+ }
380
+
381
+ static error(code: ErrorCode, message: string, details?: any): ApiResponse {
382
+ return {
383
+ success: false,
384
+ error: { code, message, details },
385
+ meta: {
386
+ timestamp: new Date().toISOString(),
387
+ },
388
+ };
389
+ }
390
+ }
391
+ ```
392
+
393
+ ## Best Practices
394
+
395
+ 1. **Consistent envelope**: All responses use same structure
396
+ 2. **Type safety**: Shared types across frontend/backend
397
+ 3. **Clear errors**: Descriptive codes and messages
398
+ 4. **Standard pagination**: Same format for all lists
399
+ 5. **Versioning**: Plan for API evolution
400
+ 6. **Documentation**: OpenAPI spec as source of truth
401
+ 7. **Gradual migration**: Don't break existing clients
402
+ 8. **Monitoring**: Track adoption and errors
403
+
404
+ ## Output Checklist
405
+
406
+ - [ ] Standard response envelope defined
407
+ - [ ] Error taxonomy documented
408
+ - [ ] Pagination format standardized
409
+ - [ ] Status code mapping
410
+ - [ ] Normalization middleware
411
+ - [ ] Shared TypeScript types
412
+ - [ ] Versioning strategy
413
+ - [ ] OpenAPI specification
414
+ - [ ] Migration plan with phases
415
+ - [ ] Consumer communication plan
@@ -0,0 +1,99 @@
1
+ ---
2
+ name: auth-module-builder
3
+ description: Implements secure authentication patterns including login/registration, session management, JWT tokens, password hashing, cookie settings, and CSRF protection. Provides auth routes, middleware, security configurations, and threat model documentation. Use when building "authentication", "login system", "JWT auth", or "session management".
4
+ ---
5
+
6
+ # Auth Module Builder
7
+
8
+ Implement secure, production-ready authentication systems.
9
+
10
+ ## Core Components
11
+
12
+ **Routes**: POST /login, /register, /logout, /refresh, /forgot-password
13
+ **Middleware**: authenticate, requireAuth, optionalAuth
14
+ **Security**: bcrypt hashing, JWT signing, secure cookies, CSRF tokens
15
+ **Session**: Redis/DB storage, expiration, refresh tokens
16
+ **Threats**: Document common attacks and mitigations
17
+
18
+ ## JWT Pattern
19
+
20
+ ```typescript
21
+ // Generate tokens
22
+ const accessToken = jwt.sign(
23
+ { userId: user.id, email: user.email },
24
+ process.env.JWT_SECRET,
25
+ { expiresIn: "15m" }
26
+ );
27
+
28
+ const refreshToken = jwt.sign(
29
+ { userId: user.id, type: "refresh" },
30
+ process.env.JWT_REFRESH_SECRET,
31
+ { expiresIn: "7d" }
32
+ );
33
+
34
+ // Verify middleware
35
+ export const authenticate = async (req, res, next) => {
36
+ const token = req.headers.authorization?.split(" ")[1];
37
+ if (!token) return res.status(401).json({ error: "No token" });
38
+
39
+ try {
40
+ const decoded = jwt.verify(token, process.env.JWT_SECRET);
41
+ req.user = await User.findById(decoded.userId);
42
+ next();
43
+ } catch (err) {
44
+ res.status(401).json({ error: "Invalid token" });
45
+ }
46
+ };
47
+ ```
48
+
49
+ ## Session Pattern
50
+
51
+ ```typescript
52
+ // Express session with Redis
53
+ app.use(
54
+ session({
55
+ store: new RedisStore({ client: redisClient }),
56
+ secret: process.env.SESSION_SECRET,
57
+ resave: false,
58
+ saveUninitialized: false,
59
+ cookie: {
60
+ secure: process.env.NODE_ENV === "production",
61
+ httpOnly: true,
62
+ maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
63
+ sameSite: "lax",
64
+ },
65
+ })
66
+ );
67
+ ```
68
+
69
+ ## Password Security
70
+
71
+ ```typescript
72
+ import bcrypt from "bcrypt";
73
+
74
+ // Hash password
75
+ const hashedPassword = await bcrypt.hash(password, 10);
76
+
77
+ // Verify password
78
+ const isValid = await bcrypt.compare(password, user.hashedPassword);
79
+ ```
80
+
81
+ ## Security Checklist
82
+
83
+ - [ ] Passwords hashed with bcrypt (cost ≥10)
84
+ - [ ] JWT secrets from environment, rotated regularly
85
+ - [ ] HTTPS only in production
86
+ - [ ] httpOnly, secure cookies
87
+ - [ ] CSRF protection enabled
88
+ - [ ] Rate limiting on auth routes
89
+ - [ ] Account lockout after failed attempts
90
+ - [ ] Password reset tokens expire
91
+ - [ ] Email verification for new accounts
92
+
93
+ ## Threat Model
94
+
95
+ **Brute Force**: Rate limit + account lockout
96
+ **Token Theft**: Short expiry, httpOnly cookies, HTTPS only
97
+ **CSRF**: SameSite cookies + CSRF tokens
98
+ **Session Fixation**: Regenerate session ID on login
99
+ **XSS**: Sanitize inputs, CSP headers
@@ -0,0 +1,166 @@
1
+ ---
2
+ name: background-jobs-designer
3
+ description: Designs background job processing systems with queue integration (BullMQ/Celery), job definitions, retry policies, exponential backoff, idempotent execution, and monitoring hooks. Use when implementing "background jobs", "task queues", "async processing", or "job workers".
4
+ ---
5
+
6
+ # Background Jobs Designer
7
+
8
+ Design reliable background job processing with retries and monitoring.
9
+
10
+ ## Queue Integration
11
+
12
+ **BullMQ (Node.js)**:
13
+
14
+ ```typescript
15
+ import { Queue, Worker } from "bullmq";
16
+
17
+ const emailQueue = new Queue("email", {
18
+ connection: { host: "localhost", port: 6379 },
19
+ });
20
+
21
+ // Add job
22
+ await emailQueue.add(
23
+ "send-welcome",
24
+ {
25
+ userId: "123",
26
+ email: "user@example.com",
27
+ },
28
+ {
29
+ attempts: 3,
30
+ backoff: { type: "exponential", delay: 2000 },
31
+ }
32
+ );
33
+ ```
34
+
35
+ **Celery (Python)**:
36
+
37
+ ```python
38
+ from celery import Celery
39
+
40
+ app = Celery('tasks', broker='redis://localhost:6379')
41
+
42
+ @app.task(bind=True, max_retries=3)
43
+ def send_email(self, user_id, email):
44
+ try:
45
+ # Send email
46
+ pass
47
+ except Exception as exc:
48
+ raise self.retry(exc=exc, countdown=60)
49
+ ```
50
+
51
+ ## Job Definitions
52
+
53
+ ```typescript
54
+ export interface Job {
55
+ id: string;
56
+ type: string;
57
+ payload: unknown;
58
+ attempts: number;
59
+ maxAttempts: number;
60
+ createdAt: Date;
61
+ processedAt?: Date;
62
+ failedAt?: Date;
63
+ error?: string;
64
+ }
65
+
66
+ export const JOB_TYPES = {
67
+ SEND_EMAIL: "send-email",
68
+ PROCESS_PAYMENT: "process-payment",
69
+ GENERATE_REPORT: "generate-report",
70
+ SYNC_DATA: "sync-data",
71
+ } as const;
72
+ ```
73
+
74
+ ## Retry Strategy
75
+
76
+ ```typescript
77
+ // Exponential backoff
78
+ const RETRY_CONFIG = {
79
+ maxAttempts: 5,
80
+ delays: [
81
+ 1000, // 1 second
82
+ 5000, // 5 seconds
83
+ 30000, // 30 seconds
84
+ 300000, // 5 minutes
85
+ 1800000, // 30 minutes
86
+ ],
87
+ };
88
+
89
+ // Worker with retry
90
+ const worker = new Worker("email", async (job) => {
91
+ try {
92
+ await sendEmail(job.data);
93
+ } catch (error) {
94
+ if (job.attemptsMade < RETRY_CONFIG.maxAttempts) {
95
+ throw error; // Will retry
96
+ }
97
+ await handleFailedJob(job, error);
98
+ }
99
+ });
100
+ ```
101
+
102
+ ## Idempotent Jobs
103
+
104
+ ```typescript
105
+ // Track processed jobs
106
+ export const processJob = async (job: Job) => {
107
+ // Check if already processed
108
+ const processed = await db.query(
109
+ "SELECT 1 FROM processed_jobs WHERE job_id = $1",
110
+ [job.id]
111
+ );
112
+
113
+ if (processed.rows.length > 0) {
114
+ console.log("Job already processed");
115
+ return; // Idempotent
116
+ }
117
+
118
+ await db.transaction(async (trx) => {
119
+ // Mark as processed
120
+ await trx("processed_jobs").insert({ job_id: job.id });
121
+
122
+ // Do work
123
+ await performWork(job, trx);
124
+ });
125
+ };
126
+ ```
127
+
128
+ ## Monitoring
129
+
130
+ ```typescript
131
+ // Job events
132
+ worker.on("completed", (job) => {
133
+ metrics.increment("jobs.completed", { type: job.name });
134
+ });
135
+
136
+ worker.on("failed", (job, err) => {
137
+ metrics.increment("jobs.failed", { type: job.name });
138
+ logger.error("Job failed", { jobId: job.id, error: err });
139
+ });
140
+
141
+ worker.on("stalled", (jobId) => {
142
+ metrics.increment("jobs.stalled");
143
+ logger.warn("Job stalled", { jobId });
144
+ });
145
+ ```
146
+
147
+ ## Best Practices
148
+
149
+ - Jobs should be idempotent
150
+ - Use exponential backoff for retries
151
+ - Set reasonable timeouts
152
+ - Monitor queue depth
153
+ - Dead letter queue for failed jobs
154
+ - Log job start/completion
155
+ - Graceful shutdown handling
156
+
157
+ ## Output Checklist
158
+
159
+ - [ ] Queue setup (Redis/RabbitMQ)
160
+ - [ ] Job type definitions
161
+ - [ ] Retry policy with backoff
162
+ - [ ] Idempotency tracking
163
+ - [ ] Error handling
164
+ - [ ] Monitoring/metrics
165
+ - [ ] Dead letter queue
166
+ - [ ] Graceful shutdown