Skip to content

Conversation

@malekelkssas
Copy link

Add Next.js App Router API Routes Support

This PR adds support for Next.js App Router API routes, making it easy for Next.js developers to list and inspect their API endpoints. The implementation follows Next.js's App Router conventions and provides a seamless experience for viewing API routes.

Changes

  • Added Next.js App Router support (13+)
  • Added automatic HTTP method detection from route handlers
  • Added support for dynamic routes ([param] -> :param)
  • Moved framework names to constants.js for better maintainability
  • Updated documentation with Next.js examples and usage
  • Added Next.js test suite with fixtures

Implementation Details

  • Direct API directory scanning (app/api)
  • Intelligent method detection from route handlers (GET, POST, PUT, DELETE, PATCH)
  • Support for TypeScript and JavaScript route files
  • Maintained existing grouping and filtering features
  • Framework names are now referenced from constants.js for consistency

Usage

# List all API routes
route-list app/api

# Group routes by path
route-list app/api --group

# Filter by HTTP methods
route-list app/api --methods GET,POST

# Filter by path
route-list app/api --include-paths /users

Why This Implementation

I chose this approach because it aligns with how I structure my Next.js API routes. The implementation:

  1. Points directly to the api directory for clarity
  2. Scans route files to detect actual HTTP methods
  3. Preserves the existing CLI interface and options
  4. Maintains compatibility with other frameworks

Testing

  • Added dedicated Next.js test suite
  • Created test fixtures matching real-world API patterns
  • Tests both method detection and path handling
  • Verified compatibility with existing features
> npm run test

> route-list@1.3.5 test
> node --test

✔ Express Routes (2.6805ms)
✔ Fastify Routes (4.7625ms)
✔ D:\github\route-list\test\fixtures\next-app\app\about\page.js (107.3965ms)     
✔ D:\github\route-list\test\fixtures\next-app\app\api\users\route.js (126.9189ms)
✔ D:\github\route-list\test\fixtures\next-app\app\page.js (124.4004ms)
✔ D:\github\route-list\test\fixtures\next-app\app\users\[id]\page.js (124.8508ms)
✔ D:\github\route-list\test\fixtures\next-app\app\users\page.js (125.8635ms)     
✔ Hapi Routes (2.1555ms)
✔ Koa Routes (2.1131ms)
✔ Next.js Routes (5.5734ms)      
✔ getAppWorkingDirPath (2.8925ms)
✔ getFrameworkName (1.9406ms)    
ℹ tests 12
ℹ suites 0
ℹ pass 12
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 610.1453

Documentation

  • Updated README with Next.js examples
  • Added framework-specific notes
  • Included directory structure examples
  • Added programmatic usage examples

Notes

  • Only supports App Router API routes (13+)
  • Framework names are now referenced from constants.js
  • Automatically detects HTTP methods from route handlers
  • Maintains backward compatibility

Screenshots

This is a real example using one of my active projects

>node D:\github\route-list\src\cli.js ./app/api --group

  POST          /ai/batches.........................................................................................
  GET|HEAD      /ai/batches/:batchId................................................................................
  POST          /ai/batches/:batchId................................................................................
  GET|HEAD      /ai/calls...........................................................................................
  GET|HEAD      /ai/calls/:callId...................................................................................
  GET|HEAD      /ai/inbound/:phoneNumber............................................................................
  POST          /ai/inbound/:phoneNumber............................................................................
  GET|HEAD      /ai/voices..........................................................................................
  POST          /ai/voices/:voiceId/sample..........................................................................

  POST          /calls..............................................................................................

  GET|HEAD      /health.............................................................................................

  GET|HEAD      /integrations/google-calendar.......................................................................
  POST          /integrations/google-calendar.......................................................................
  GET|HEAD      /integrations/google-calendar/auth-url..............................................................
  GET|HEAD      /integrations/google-calendar/callback..............................................................
  POST          /integrations/google-calendar/disconnect............................................................

  GET|HEAD      /notifications......................................................................................
  PUT           /notifications......................................................................................

  GET|HEAD      /outbound...........................................................................................
  POST          /outbound...........................................................................................

  GET|HEAD      /phone-number.......................................................................................

  GET|HEAD      /prompts............................................................................................
  POST          /prompts............................................................................................
  DELETE        /prompts/:id........................................................................................

  Listed 24 HTTP routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant