Skip to content

Is it possbile to change or access decorator variables? #538

@fendyk

Description

@fendyk

Let's say I have a list function like this:

@Get()
async list() {
    await db.users.findMany();
}

And now, I need to add a header with 'X-total-Count'. How would I approach this using a decorator? For example, I currently use the following:

@Get()
async list(@Res() res: NextApiResponse) {
    const items = await db.user.findMany();
    setTotalCountHeader(res, items.length) // translates to: res.setHeader('X-Total-Count', amount)
    return items;
}

This is a workaround and it works as aspected, but could I use it with a decorator? The only thing I can think of is the following:

@Get()
@SetHeader('X-Total-Count', '2') // Value needs to be pulled from somewhere in the function below
async list(@Res() res: NextApiResponse) {
    const items = await db.user.findMany();
    return items;
}

Is this just not possbile with decorators? Let me know, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions