Skip to content

Add support for direct middleware functions in endpoint definitions #33

@GroophyLifefor

Description

@GroophyLifefor

Currently, the middlewares export in endpoint files only accepts string identifiers, but it would be more flexible to allow direct middleware functions as well.

Current behavior

export const middlewares = ['dataValidation']; // Only string identifiers work

Proposed behavior

export const middlewares = [
  'dataValidation',            // String identifier
  customMiddlewareFunction,    // Direct middleware function
  async (ctx, next) => {...}   // Inline middleware
];

Implementation Notes

  • Update middleware type definition to union of string | Middleware
  • Modify middleware loader to handle both types
  • Add type checking for valid middleware functions
  • Update documentation with new examples

Breaking Changes

None - this is a backwards compatible enhancement

Example Use Case

const loggerMiddleware: Middleware = async (ctx, next) => {
  console.log(`${ctx.req.method} ${ctx.req.url}`);
  await next();
};

export const middlewares = ['dataValidation', loggerMiddleware];

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions