Skip to content

Implement method-specific exports for API handlers #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 19, 2025

Adds support for method-specific exports in API handlers, allowing developers to use named exports like export const GET = (req, res) => {...} in addition to the legacy default export pattern.

Changes

Core Implementation

  • Updated adex/runtime/handler.js: Added getMethodHandler() function that implements method-specific dispatch logic with proper fallback behavior
  • Updated adex/src/http.d.ts: Added TypeScript types for APIHandler and APIModule interface supporting both export patterns

Key Features

  • Method-specific exports: Support for GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
  • Case-insensitive matching: GET, get, and Get all work
  • Backward compatibility: Existing export default handlers continue to work
  • Smart fallback: Method-specific exports take precedence, falls back to default export
  • Proper 405 handling: Returns 405 Method Not Allowed with correct Allow header when no handlers exist
  • Preserved behavior: Maintains existing req.params assignment and hook emission

Usage Examples

New method-specific pattern:

// api/users.js
export function GET(req, res) {
  res.json({ users: await getUsers() })
}

export function POST(req, res) {
  const user = await createUser(req.body)
  res.json({ user })
}

export function PUT(req, res) {
  const user = await updateUser(req.params.id, req.body)
  res.json({ user })
}

Legacy pattern (still supported):

// api/legacy.js
export default function handler(req, res) {
  if (req.method === 'GET') {
    res.json({ data: 'legacy handler' })
  } else {
    res.statusCode = 405
    res.end('Method Not Allowed')
  }
}

Mixed pattern:

// api/mixed.js
export function GET(req, res) {
  res.json({ data: 'specific GET handler' })
}

// Fallback for all other methods
export default function handler(req, res) {
  res.statusCode = 405
  res.end('Only GET is supported')
}

Testing

  • Added comprehensive test suites covering all functionality
  • 25 new tests ensuring correct dispatch logic, fallback behavior, and edge cases
  • All existing tests continue to pass, confirming no regressions
  • TypeScript compilation verified

Benefits

  • Cleaner code: No more manual method checking in handlers
  • Better DX: Method-specific exports are more intuitive and easier to maintain
  • Type safety: Full TypeScript support for both patterns
  • Zero breaking changes: Existing code continues to work unchanged
  • Automatic 405 handling: No need to manually implement Method Not Allowed responses

Fixes requirements for method-specific API handler exports with proper fallback behavior and TypeScript support.

This pull request was created as a result of the following prompt from Copilot chat.

Implement method-specific exports for API handlers, allowing named exports like export const GET = (req, res) => {...} in addition to the legacy default export. Update the dispatch logic in adex/runtime/handler.js to:

  1. Check for method-specific exports matching the HTTP method (case-insensitive).
  2. Fall back to module.default if no method-specific export exists.
  3. Return 405 Method Not Allowed if neither exists.
  4. Support GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD.
  5. Maintain req.params assignment and hook emission behavior.

Also:

  • Update TypeScript types in adex/src/http.d.ts to support both patterns.
  • Ensure the solution passes linting and compilation.
  • Do not alter unrelated code.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Support method-specific exports for API handlers Implement method-specific exports for API handlers Jul 19, 2025
@Copilot Copilot AI requested a review from barelyhuman July 19, 2025 08:38
Copilot finished work on behalf of barelyhuman July 19, 2025 08:38
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.

2 participants