lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.32k stars 481 forks source link

Custom parameters in controller #1607

Open SwagiWagi opened 3 months ago

SwagiWagi commented 3 months ago

Sorting

I want to be able to have middlewares add custom parameters to my controller, for example:

const addCustomParamMiddleware = (req: express.Request, res: express.Response, next: nextFuncType) => {
  req.locals.tsoaCustomParams = {
    requestId: req.headers["x-request-id"]
  }

  next();
}

@middlwares(addCustomParamMiddleware)
@Get("{screenName}/details")
  public async details(@Path() screenName: string, @CustomParam("requestId") requestId: string): Promise<any> {
    //logic
  }
}

app.use(addCustomParamMiddleware);
registerRoutes(app);
github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

SwagiWagi commented 1 month ago

up

WoH commented 1 month ago

If you are using decorators, you can use https://tsoa-community.github.io/reference/functions/tsoa.Inject.html

and do something like fn(@Inject() @MyDecorator() injectedArg)

Otherwise you can set it in the Middleware and use @Request

JasonGore commented 1 month ago

Are there any examples of how to implement a custom decorator like @MyDecorator? I've been searching but haven't found one yet.

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

WoH commented 2 weeks ago

Are there any examples of how to implement a custom decorator like @MyDecorator? I've been searching but haven't found one yet.

As in: How to write/use decorators?

https://www.typescriptlang.org/docs/handbook/decorators.html