Skip to content

Add TypeScript generic support for Request and Response types #292

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 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 23, 2025

This PR adds comprehensive TypeScript generic support to lambda-api's Request and Response types, enabling developers to create strongly-typed APIs with better compile-time safety.

Key Changes

Generic Type Definitions

  • Request<TParams, TQuery, TBody> - Generic Request class with customizable params, query, and body types
  • Response - Generic Response class with customizable response body type
  • Middleware<TReq, TRes> - Generic middleware type
  • HandlerFunction<TReq, TRes> - Generic handler function type
  • ErrorHandlingMiddleware<TReq, TRes> - Generic error handling middleware type
  • FinallyFunction<TReq, TRes> - Generic finally function type

Usage Example

import { Request, Response, HandlerFunction, Middleware } from 'lambda-api';

// Define strongly typed interfaces
interface UserParams { userId: string; }
interface UserQuery { include?: 'profile' | 'settings'; }
interface UserBody { name: string; email: string; }
interface ApiResponse { success: boolean; data: any; }

// Create typed request/response aliases
type UserRequest = Request<UserParams, UserQuery, UserBody>;
type UserResponse = Response<ApiResponse>;

// Fully type-safe handler
const getUserHandler: HandlerFunction<UserRequest, UserResponse> = (req, res) => {
  // req.params.userId is typed as string
  // req.query.include is typed as 'profile' | 'settings' | undefined
  // req.body.name and req.body.email are typed as string
  // res.json() expects ApiResponse shape
  res.json({ success: true, data: { user: req.body.name } });
};

// Type-safe middleware
const authMiddleware: Middleware<UserRequest, UserResponse> = (req, res, next) => {
  if (!req.headers.authorization) {
    res.status(401).json({ success: false, data: null });
    return;
  }
  next();
};

// Usage with API routes
api.get('/users/:userId', authMiddleware, getUserHandler);

Benefits

  • Type Safety - Catch type errors at compile time instead of runtime
  • IntelliSense - Better IDE support with autocomplete and error detection
  • Documentation - Types serve as living documentation of API contracts
  • Refactoring - Safer code changes with compiler assistance
  • Team Collaboration - Clearer contracts between API consumers and producers

Backwards Compatibility

All existing lambda-api code continues to work without changes. The generic types have sensible defaults:

  • Request defaults to Request<{[key: string]: string | undefined}, {[key: string]: string | undefined}, any>
  • Response defaults to Response<any>
  • All middleware and handler types maintain their current behavior

Developers can gradually adopt the new generic types at their own pace.

Testing

  • ✅ All existing unit tests pass (478 tests)
  • ✅ Comprehensive TypeScript type tests added
  • ✅ Code formatting and linting verified
  • ✅ Real-world examples compile successfully
  • ✅ Backwards compatibility validated

Fixes #244.


💡 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] [Feature request] Typescript - Support generic request / response types Add TypeScript generic support for Request and Response types Aug 23, 2025
@Copilot Copilot AI requested a review from naorpeled August 23, 2025 17:00
Copilot finished work on behalf of naorpeled August 23, 2025 17:00
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.

[Feature request] Typescript - Support generic request / response types
2 participants