All files / examples router-demo.js

0% Statements 0/458
0% Branches 0/1
0% Functions 0/1
0% Lines 0/458

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * Object-based Router Demo for Coherent.js
 * 
 * @fileoverview Demonstrates pure object-oriented routing with:
 * - Declarative nested route objects
 * - Validation and middleware
 * - Error handling
 * - Pure Node.js HTTP server
 * 
 * @author Coherent.js Team
 * @version 1.0.0
 */
 
import { createObjectRouter } from '../src/api/router.js';
import { ApiError } from '../src/api/errors.js';
 
// Sample data stores
const users = [
  { id: 1, name: 'John Doe', email: 'john@example.com' },
  { id: 2, name: 'Jane Smith', email: 'jane@example.com' }
];
 
const posts = [
  { id: 1, title: 'Hello World', content: 'First post', authorId: 1 },
  { id: 2, title: 'API Design', content: 'Object-based routing', authorId: 2 }
];
 
// Validation schemas
const userSchema = {
  type: 'object',
  properties: {
    name: { type: 'string', minLength: 1 },
    email: { type: 'string', format: 'email' }
  },
  required: ['name', 'email']
};
 
const postSchema = {
  type: 'object',
  properties: {
    title: { type: 'string', minLength: 1 },
    content: { type: 'string', minLength: 1 },
    authorId: { type: 'number' }
  },
  required: ['title', 'content', 'authorId']
};
 
/**
 * Authentication middleware - validates authorization header
 * @param {Object} req - Express request object
 * @param {Object} res - Express response object
 * @param {Function} next - Express next function
 * @throws {ApiError} When authorization header is missing
 */
const authMiddleware = (req, res, next) => {
  const token = req.headers.authorization;
  if (!token) {
    throw new ApiError('Authorization required', 401);
  }
  req.user = { id: 1, name: 'John Doe' };
  next();
};
 
/**
 * Logging middleware - logs HTTP method and URL
 * @param {Object} req - Express request object
 * @param {Object} res - Express response object
 * @param {Function} next - Express next function
 */
const logMiddleware = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};
 
/**
 * Comprehensive object-based route definition
 * Demonstrates the full capabilities of declarative routing
 */
const apiDefinition = {
 
  // Root endpoint - GET /
  get: {
    path: '/',
    handler: (req, res) => ({ 
      message: 'Welcome to Coherent.js Object-based Router!',
      timestamp: new Date().toISOString(),
      features: [
        'Declarative route definitions',
        'Nested route structures',
        'Built-in validation',
        'Middleware support',
        'Express/Fastify integration'
      ]
    })
  },
 
  // Simple utility endpoints
  ping: {
    get: {
      handler: () => ({ message: 'pong', timestamp: Date.now() })
    }
  },
 
  time: {
    get: {
      handler: () => ({ 
        time: new Date().toISOString(),
        timestamp: Date.now(),
        timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
      })
    }
  },
 
  echo: {
    post: {
      handler: (req, res) => ({
        echo: req.body,
        method: req.method,
        path: req.url,
        headers: req.headers
      })
    }
  },
 
  // Error handling demonstration endpoints
  error: {
    // Test generic error
    generic: {
      get: {
        handler: () => {
          throw new Error('This is a generic error for testing');
        }
      }
    },
 
    // Test API error with custom status
    api: {
      get: {
        handler: () => {
          throw new ApiError('This is a custom API error', 422);
        }
      }
    },
 
    // Test validation error (will be caught by validation middleware)
    validation: {
      post: {
        validation: {
          type: 'object',
          properties: {
            required_field: { type: 'string', minLength: 5 }
          },
          required: ['required_field']
        },
        handler: () => ({
          message: 'This should not be reached due to validation error'
        })
      }
    },
 
    // Test async error
    async: {
      get: {
        handler: async () => {
          await new Promise(resolve => setTimeout(resolve, 100));
          throw new ApiError('Async operation failed', 503);
        }
      }
    },
 
    // Test error handling disabled for a specific route
    unhandled: {
      get: {
        errorHandling: false,
        handler: () => {
          throw new Error('This error will not be caught by middleware');
        }
      }
    }
  },
 
  // API namespace with comprehensive features
  api: {
    // Health and status endpoints
    health: {
      get: {
        handler: (req, res) => ({ 
          status: 'healthy',
          uptime: process.uptime(),
          memory: process.memoryUsage(),
          timestamp: new Date().toISOString()
        })
      }
    },
 
    status: {
      get: {
        handler: () => ({ 
          status: 'running', 
          version: '1.0.0',
          environment: process.env.NODE_ENV || 'development'
        })
      }
    },
 
    // Users resource with full CRUD operations
    users: {
      // GET /api/users - List all users
      get: {
        handler: (req, res) => ({ 
          users,
          total: users.length,
          timestamp: new Date().toISOString()
        })
      },
 
      // POST /api/users - Create user
      post: {
        validation: userSchema,
        handler: (req, res) => {
          const { name, email } = req.body;
          
          if (users.some(u => u.email === email)) {
            throw new ApiError('User with this email already exists', 409);
          }
          
          const newUser = {
            id: Math.max(0, ...users.map(u => u.id)) + 1,
            name,
            email,
            created: new Date().toISOString()
          };
          
          users.push(newUser);
          res.status(201);
          return { user: newUser, message: 'User created successfully' };
        }
      },
 
      // User by ID operations
      ':id': {
        // GET /api/users/:id - Get specific user
        get: {
          handler: (req, res) => {
            const userId = parseInt(req.params.id);
            const user = users.find(u => u.id === userId);
            
            if (!user) {
              throw new ApiError('User not found', 404);
            }
            
            return { user };
          }
        },
 
        // PUT /api/users/:id - Update user (requires auth)
        put: {
          validation: userSchema,
          middleware: [authMiddleware],
          handler: (req, res) => {
            const userId = parseInt(req.params.id);
            const userIndex = users.findIndex(u => u.id === userId);
            
            if (userIndex === -1) {
              throw new ApiError('User not found', 404);
            }
            
            const updatedUser = { 
              ...users[userIndex], 
              ...req.body,
              updated: new Date().toISOString()
            };
            users[userIndex] = updatedUser;
            
            return { user: updatedUser, message: 'User updated successfully' };
          }
        },
 
        // DELETE /api/users/:id - Delete user (requires auth)
        delete: {
          middleware: [authMiddleware],
          handler: (req, res) => {
            const userId = parseInt(req.params.id);
            const userIndex = users.findIndex(u => u.id === userId);
            
            if (userIndex === -1) {
              throw new ApiError('User not found', 404);
            }
            
            const deletedUser = users.splice(userIndex, 1)[0];
            return { message: 'User deleted successfully', user: deletedUser };
          }
        },
 
        // User's posts - nested resource
        posts: {
          get: {
            handler: (req, res) => {
              const userId = parseInt(req.params.id);
              const userPosts = posts.filter(p => p.authorId === userId);
              return { 
                userId,
                posts: userPosts,
                total: userPosts.length
              };
            }
          }
        }
      }
    },
 
    // Posts resource with full CRUD operations
    posts: {
      // GET /api/posts - List all posts
      get: {
        handler: (req, res) => ({ 
          posts,
          total: posts.length,
          timestamp: new Date().toISOString()
        })
      },
 
      // POST /api/posts - Create post (requires auth)
      post: {
        validation: postSchema,
        middleware: [authMiddleware],
        handler: (req, res) => {
          const { title, content, authorId } = req.body;
          
          const newPost = {
            id: Math.max(0, ...posts.map(p => p.id)) + 1,
            title,
            content,
            authorId,
            created: new Date().toISOString()
          };
          
          posts.push(newPost);
          res.status(201);
          return { post: newPost, message: 'Post created successfully' };
        }
      },
 
      // Post by ID operations
      ':id': {
        get: {
          handler: (req, res) => {
            const postId = parseInt(req.params.id);
            const post = posts.find(p => p.id === postId);
            
            if (!post) {
              throw new ApiError('Post not found', 404);
            }
            
            return { post };
          }
        },
 
        put: {
          validation: postSchema,
          middleware: [authMiddleware],
          handler: (req, res) => {
            const postId = parseInt(req.params.id);
            const postIndex = posts.findIndex(p => p.id === postId);
            
            if (postIndex === -1) {
              throw new ApiError('Post not found', 404);
            }
            
            const updatedPost = { 
              ...posts[postIndex], 
              ...req.body,
              updated: new Date().toISOString()
            };
            posts[postIndex] = updatedPost;
            
            return { post: updatedPost, message: 'Post updated successfully' };
          }
        },
 
        delete: {
          middleware: [authMiddleware],
          handler: (req, res) => {
            const postId = parseInt(req.params.id);
            const postIndex = posts.findIndex(p => p.id === postId);
            
            if (postIndex === -1) {
              throw new ApiError('Post not found', 404);
            }
            
            const deletedPost = posts.splice(postIndex, 1)[0];
            return { message: 'Post deleted successfully', post: deletedPost };
          }
        }
      }
    },
 
    // Calculator utilities
    calc: {
      add: {
        post: {
          handler: (req, res) => {
            const { a, b } = req.body;
            const numA = Number(a);
            const numB = Number(b);
            return {
              operation: 'add',
              a: numA,
              b: numB,
              result: numA + numB,
              timestamp: new Date().toISOString()
            };
          }
        }
      },
 
      multiply: {
        post: {
          handler: (req, res) => {
            const { a, b } = req.body;
            const numA = Number(a);
            const numB = Number(b);
            return {
              operation: 'multiply',
              a: numA,
              b: numB,
              result: numA * numB,
              timestamp: new Date().toISOString()
            };
          }
        }
      },
 
      divide: {
        post: {
          handler: (req, res) => {
            const { a, b } = req.body;
            const numA = Number(a);
            const numB = Number(b);
            
            if (numB === 0) {
              throw new ApiError('Division by zero is not allowed', 400);
            }
            
            return {
              operation: 'divide',
              a: numA,
              b: numB,
              result: numA / numB,
              timestamp: new Date().toISOString()
            };
          }
        }
      }
    },
 
    // Admin section (requires authentication)
    admin: {
      dashboard: {
        middleware: [authMiddleware],
        get: {
          handler: () => ({ 
            message: 'Admin dashboard',
            stats: {
              totalUsers: users.length,
              totalPosts: posts.length,
              serverUptime: process.uptime()
            }
          })
        }
      },
 
      settings: {
        get: {
          middleware: [authMiddleware],
          handler: () => ({ 
            settings: {
              environment: process.env.NODE_ENV || 'development',
              version: '1.0.0',
              features: ['object-routing', 'validation', 'middleware']
            }
          })
        },
 
        post: {
          middleware: [authMiddleware],
          validation: {
            type: 'object',
            properties: {
              setting: { type: 'string' },
              value: { type: 'string' }
            },
            required: ['setting', 'value']
          },
          handler: (req, res) => ({ 
            message: 'Settings updated', 
            data: req.body,
            timestamp: new Date().toISOString()
          })
        }
      }
    },
    
    // Add version endpoint to the object definition
    version: {
      get: {
        handler: (req, res) => ({
          version: '1.0.0',
          framework: 'Coherent.js',
          routing: 'object-based'
        })
      }
    }
  }
};
 
// Create the main router
const router = createObjectRouter(apiDefinition);
 
// Create pure Node.js HTTP server if running directly
if (import.meta.url === `file://${process.argv[1]}`) {
  const server = router.createServer();
  
  const PORT = process.env.PORT || 3000;
  
  server.listen(PORT, () => {
    console.log(`🚀 Comprehensive Object-based API server running on port ${PORT}`);
    console.log('\n📋 Available endpoints:');
    console.log('  GET  /                    - Welcome message');
    console.log('  GET  /ping                - Simple ping');
    console.log('  GET  /time                - Current time');
    console.log('  POST /echo                - Echo request');
    console.log('  GET  /error/generic       - Test generic error handling');
    console.log('  GET  /error/api           - Test API error handling');
    console.log('  POST /error/validation    - Test validation error handling');
    console.log('  GET  /error/async         - Test async error handling');
    console.log('  GET  /error/unhandled     - Test unhandled error (no middleware)');
    console.log('  GET  /api/health          - Health check');
    console.log('  GET  /api/status          - Server status');
    console.log('  GET  /api/users           - List users');
    console.log('  POST /api/users           - Create user');
    console.log('  GET  /api/users/:id       - Get user by ID');
    console.log('  PUT  /api/users/:id       - Update user (auth required)');
    console.log('  DELETE /api/users/:id     - Delete user (auth required)');
    console.log('  GET  /api/users/:id/posts - Get user posts');
    console.log('  GET  /api/posts           - List posts');
    console.log('  POST /api/posts           - Create post (auth required)');
    console.log('  GET  /api/posts/:id       - Get post by ID');
    console.log('  PUT  /api/posts/:id       - Update post (auth required)');
    console.log('  DELETE /api/posts/:id     - Delete post (auth required)');
    console.log('  POST /api/calc/add        - Add two numbers');
    console.log('  POST /api/calc/multiply   - Multiply two numbers');
    console.log('  POST /api/calc/divide     - Divide two numbers');
    console.log('  GET  /api/admin/dashboard - Admin dashboard (auth required)');
    console.log('  GET  /api/admin/settings  - Admin settings (auth required)');
    console.log('  POST /api/admin/settings  - Update settings (auth required)');
    console.log('  GET  /version             - Framework version');
    
    console.log('\n🧪 Test commands:');
    console.log(`  curl http://localhost:${PORT}/`);
    console.log(`  curl http://localhost:${PORT}/api/health`);
    console.log(`  curl http://localhost:${PORT}/api/users`);
    console.log(`  curl -X POST http://localhost:${PORT}/api/calc/add -H "Content-Type: application/json" -d '{"a":5,"b":3}'`);
    console.log(`  curl -X POST http://localhost:${PORT}/api/users -H "Content-Type: application/json" -d '{"name":"Alice","email":"alice@example.com"}'`);
    console.log(`  curl -H "Authorization: Bearer token" http://localhost:${PORT}/api/admin/dashboard`);
    
    console.log('\n✨ Features demonstrated:');
    console.log('  • Declarative object-based route definitions');
    console.log('  • Nested route structures matching URL hierarchy');
    console.log('  • Built-in JSON Schema validation');
    console.log('  • Route-specific and global middleware');
    console.log('  • Authentication and authorization');
    console.log('  • Full CRUD operations');
    console.log('  • Error handling with custom ApiError');
    console.log('  • Pure Node.js HTTP server');
    console.log('  • Zero external dependencies');
  });
}
 
export default router;