UNPKG

@voilajsx/appkit

Version:

Minimal and framework agnostic Node.js toolkit designed for AI agentic backend development

507 lines (395 loc) • 14 kB
# AppKit CLI Documentation ## Overview The AppKit CLI is a powerful command-line interface for creating production-ready TypeScript backend applications with Feature-Based Component Architecture (FBCA). It provides auto-discovery routing, complete authentication systems, secure environment configuration, database integration, and follows modern development best practices. ## Installation ```bash npm install -g @voilajsx/appkit ``` ## Quick Start ```bash # Create a new app appkit generate app myproject cd myproject # Add authentication system appkit generate feature user # Add custom features appkit generate feature product appkit generate feature order # Start development npm run dev:api ``` ## Commands Overview | Command | Description | When to Use | Security Features | |---------|-------------|-------------|-------------------| | `generate app [name]` | Create complete backend app | Starting new project | Random secure keys, GitHub-safe | | `generate feature <name>` | Add custom CRUD feature | Need basic API endpoints | Follows security patterns | | `generate feature user` | Add authentication system | Need user management/auth | JWT, role-based access, 9-role hierarchy | ## Commands Reference ### šŸš€ `appkit generate app [name]` Creates a complete TypeScript backend application with modern architecture. **Usage:** ```bash # Create in current directory appkit generate app # Create in new directory appkit generate app myproject ``` **What it creates:** ``` myproject/ ā”œā”€ā”€ package.json # Dependencies & scripts ā”œā”€ā”€ tsconfig.json # TypeScript configuration ā”œā”€ā”€ .env # Secure environment variables ā”œā”€ā”€ prisma/ │ └── schema.prisma # Database schema └── src/api/ ā”œā”€ā”€ server.ts # Main server ā”œā”€ā”€ lib/ │ └── api-router.ts # Auto-discovery router └── features/ ā”œā”€ā”€ welcome/ # Default welcome feature ā”œā”€ā”€ sample/ # Example CRUD feature └── new/ # Example feature ``` **✨ Features:** - āœ… **Secure by default** - Random 44-char auth secrets, 26-char API keys - āœ… **GitHub-safe** - No hardcoded credentials - āœ… **TypeScript ready** - Full type safety with proper configuration - āœ… **Auto-discovery** - Routes automatically discovered and mounted - āœ… **Production-ready** - Environment separation, logging, error handling - āœ… **Database ready** - Prisma integration with SQLite default **Generated .env file:** ```bash DATABASE_URL="file:./dev.db" VOILA_FRONTEND_KEY="voila_abc123..." # 26 characters VOILA_AUTH_SECRET="auth_xyz789..." # 44 characters DEFAULT_USER_PASSWORD="pwd9x7k4m2" # 10 alphanumeric ``` ### šŸ”§ `appkit generate feature <name>` Creates a custom feature with complete CRUD operations. **Usage:** ```bash appkit generate feature product appkit generate feature order appkit generate feature blog ``` **What it creates:** ``` src/api/features/product/ ā”œā”€ā”€ product.route.ts # Express routes with CRUD endpoints ā”œā”€ā”€ product.service.ts # Business logic with validation ā”œā”€ā”€ product.types.ts # TypeScript interfaces ā”œā”€ā”€ product.model.ts # Database model └── product.http # HTTP test file ``` **Generated endpoints:** - `GET /api/product` - Get all records - `POST /api/product` - Create new record - `GET /api/product/:id` - Get record by ID - `PUT /api/product/:id` - Update record - `DELETE /api/product/:id` - Delete record **When to use:** - āœ… Need custom business logic - āœ… Want specific data models - āœ… Building domain-specific APIs - āœ… Rapid prototyping ### šŸ” `appkit generate feature user` Creates a complete authentication and user management system. **Usage:** ```bash appkit generate feature user ``` **What it creates:** ``` src/api/features/user/ ā”œā”€ā”€ user.route.ts # Authentication routes ā”œā”€ā”€ user.service.ts # Auth business logic ā”œā”€ā”€ user.types.ts # User interfaces ā”œā”€ā”€ user.model.ts # User database model └── user.http # Complete test suite prisma/seeding/ └── user.seed.js # Seed data for all roles ``` **šŸ” Authentication Features:** - āœ… **JWT Authentication** - Secure token-based auth - āœ… **9-Role Hierarchy** - user.basic → admin.system - āœ… **Password Security** - bcrypt hashing, random generation - āœ… **Role-based Access** - Route protection by role/level - āœ… **Complete User Management** - CRUD for admin users - āœ… **Password Reset** - Forgot/reset password flow - āœ… **Account Management** - Profile updates, password changes **Generated endpoints:** ``` # Authentication POST /api/user/register # Create new user POST /api/user/login # User login POST /api/user/forgot-password # Request password reset POST /api/user/reset-password # Reset with token # Profile Management GET /api/user/profile # Get user profile PUT /api/user/profile # Update profile POST /api/user/change-password # Change password # Admin Only GET /api/user/all # Get all users GET /api/user/list # Get users (moderator+) PUT /api/user/list/:id # Update user DELETE /api/user/list/:id # Delete user ``` **šŸ›”ļø Role Hierarchy:** ``` User Roles (Level 1-3): ā”œā”€ā”€ user.basic # Basic user permissions ā”œā”€ā”€ user.pro # Pro user features └── user.max # Maximum user features Moderator Roles (Level 4-6): ā”œā”€ā”€ moderator.review # Can review content ā”œā”€ā”€ moderator.approve # Can approve/reject └── moderator.manage # Can manage users Admin Roles (Level 7-9): ā”œā”€ā”€ admin.tenant # Tenant administration ā”œā”€ā”€ admin.org # Organization administration └── admin.system # System administration ``` **When to use:** - āœ… Need user accounts and authentication - āœ… Building multi-user applications - āœ… Need role-based access control - āœ… Want production-ready auth system - āœ… Building SaaS or enterprise apps ## Decision Guide ### When to use `generate app` - šŸŽÆ **Starting a new project** - šŸŽÆ **Need complete backend foundation** - šŸŽÆ **Want security best practices built-in** - šŸŽÆ **Building production applications** ### When to use `generate feature <name>` - šŸŽÆ **Adding business-specific functionality** - šŸŽÆ **Need custom data models** - šŸŽÆ **Building domain APIs (products, orders, etc.)** - šŸŽÆ **Rapid prototyping** ### When to use `generate feature user` - šŸŽÆ **Need user authentication** - šŸŽÆ **Building multi-user applications** - šŸŽÆ **Need role-based permissions** - šŸŽÆ **Want enterprise-grade user management** ## Architecture ### Feature-Based Component Architecture (FBCA) Each feature is self-contained and follows consistent patterns: ``` src/api/features/<feature>/ ā”œā”€ā”€ <feature>.route.ts # HTTP endpoints & middleware ā”œā”€ā”€ <feature>.service.ts # Business logic & validation ā”œā”€ā”€ <feature>.types.ts # TypeScript interfaces ā”œā”€ā”€ <feature>.model.ts # Database schema └── <feature>.http # API testing ``` ### Auto-Discovery System - šŸ” **Automatic route detection** - Scans `/features` directory - šŸš€ **Zero configuration** - Routes auto-mount at `/api/{feature}` - šŸ”„ **Hot-reload friendly** - Changes reflected immediately - šŸ“‹ **Discovery endpoint** - `GET /api` lists all features ### Security Architecture **Environment Security:** - šŸ” **Random key generation** - Unique per project - 🚫 **No hardcoded secrets** - GitHub-safe repositories - šŸ›”ļø **Environment separation** - Dev/prod configurations - šŸ”‘ **Secure defaults** - Strong random passwords **Authentication Security:** - šŸŽ« **JWT tokens** - Stateless authentication - šŸ”’ **Password hashing** - bcrypt with salt rounds - šŸ›”ļø **Role-based access** - Granular permissions - šŸ” **Token validation** - Middleware protection ## Development Workflow ### 1. Project Setup ```bash # Create project appkit generate app myapp cd myapp # Install dependencies (auto-done) npm install # Start development npm run dev:api ``` ### 2. Add Authentication ```bash # Add complete auth system appkit generate feature user # Seed test users node prisma/seeding/user.seed.js # Test login curl -X POST http://localhost:3000/api/user/login \ -H "Content-Type: application/json" \ -d '{"email":"admin.system@myapp.com","password":"YOUR_DEFAULT_PASSWORD"}' ``` ### 3. Add Custom Features ```bash # Add business features appkit generate feature product appkit generate feature order appkit generate feature customer ``` ### 4. Test & Deploy ```bash # Run tests npm test # Build for production npm run build # Start production npm run start:prod ``` ## Environment Configuration ### Auto-Generated .env ```bash # Server Configuration PORT=3000 NODE_ENV=development # Database Configuration DATABASE_URL="file:./dev.db" # Application Configuration APP_NAME=myapp API_VERSION=1.0.0 # Security (Auto-generated, GitHub-safe) VOILA_FRONTEND_KEY=voila_abc123def456... # 26 chars VOILA_AUTH_SECRET=auth_xyz789abc123... # 44 chars DEFAULT_USER_PASSWORD=pwd9x7k4m2 # 10 chars # Logging LOG_LEVEL=info ``` ### Production Environment ```bash # Override for production NODE_ENV=production PORT=8080 DATABASE_URL="postgresql://user:pass@host:5432/db" LOG_LEVEL=warn ``` ## Available Scripts | Script | Description | When to Use | |--------|-------------|-------------| | `npm run dev:api` | Development server with hot-reload | During development | | `npm run build` | Build TypeScript to JavaScript | Before deployment | | `npm run start:prod` | Start production server | Production deployment | | `npm run start` | Start without build | Quick production start | ## Testing ### HTTP Test Files Each feature includes a `.http` file for easy testing: ```http ### Test login POST http://localhost:3000/api/user/login Content-Type: application/json { "email": "admin.system@myapp.com", "password": "YOUR_DEFAULT_PASSWORD" } ### Test protected endpoint GET http://localhost:3000/api/user/profile Authorization: Bearer YOUR_JWT_TOKEN ``` ### Integration Testing ```bash # Test endpoints with curl curl http://localhost:3000/health curl http://localhost:3000/api curl http://localhost:3000/api/user/test ``` ## AppKit Integration ### Database ```typescript import { databaseClass } from '@voilajsx/appkit/database'; const db = await databaseClass.get(); const users = await db.user.findMany(); ``` ### Authentication ```typescript import { authClass } from '@voilajsx/appkit/auth'; const auth = authClass.get(); const token = auth.generateLoginToken({ userId, role, level }); ``` ### Error Handling ```typescript import { errorClass } from '@voilajsx/appkit/error'; const error = errorClass.get(); throw error.badRequest('Invalid input'); throw error.notFound('User not found'); ``` ### Logging ```typescript import { loggerClass } from '@voilajsx/appkit/logger'; const logger = loggerClass.get('feature-name'); logger.info('Request processed', { userId }); ``` ## Best Practices ### Security - āœ… **Use environment variables** for all secrets - āœ… **Never commit .env files** to version control - āœ… **Use role-based access control** for sensitive endpoints - āœ… **Validate all inputs** in service layers - āœ… **Use JWT tokens** for stateless authentication ### Code Organization - āœ… **Follow FBCA patterns** - keep features self-contained - āœ… **Use TypeScript interfaces** for type safety - āœ… **Implement proper error handling** with AppKit error classes - āœ… **Add comprehensive logging** for debugging - āœ… **Write HTTP tests** for all endpoints ### Database - āœ… **Use AppKit database class** for consistency - āœ… **Include tenant_id fields** for multi-tenancy - āœ… **Transform dates to ISO strings** in responses - āœ… **Implement input validation** before database operations ## Troubleshooting ### Common Issues **Feature not discovered:** - āœ… Check file naming: `{feature}.route.ts` - āœ… Ensure default export: `export default router` - āœ… Verify directory structure: `features/{feature}/` **Authentication errors:** - āœ… Check `VOILA_AUTH_SECRET` in .env - āœ… Verify password in seeding - āœ… Test with correct user credentials **Database connection issues:** - āœ… Verify `DATABASE_URL` in .env - āœ… Run `npx prisma generate` - āœ… Run `npx prisma db push` **TypeScript compilation errors:** - āœ… Check import paths use `.js` extensions - āœ… Verify `moduleResolution: "bundler"` in tsconfig - āœ… Ensure proper AppKit module imports ## Examples ### Complete User Management Flow ```bash # 1. Create app with auth appkit generate app userapp cd userapp appkit generate feature user # 2. Seed test users node prisma/seeding/user.seed.js # 3. Test authentication curl -X POST http://localhost:3000/api/user/login \ -H "Content-Type: application/json" \ -d '{"email":"admin.system@userapp.com","password":"YOUR_DEFAULT_PASSWORD"}' # 4. Use JWT token for protected endpoints curl -H "Authorization: Bearer YOUR_JWT_TOKEN" \ http://localhost:3000/api/user/profile ``` ### E-commerce API Example ```bash # Create e-commerce backend appkit generate app shopapi cd shopapi # Add authentication appkit generate feature user # Add business features appkit generate feature product appkit generate feature order appkit generate feature customer appkit generate feature category # Result: Complete e-commerce API with auth # GET /api/user/* - User management # GET /api/product/* - Product catalog # GET /api/order/* - Order management # GET /api/customer/* - Customer data # GET /api/category/* - Product categories ``` --- **AppKit CLI - Production-ready backend applications in minutes, not hours.** šŸš€ **Generated with AppKit CLI v3.0 - Enhanced with Authentication, Security, and GitHub-safe Repositories**