Hi, firstly I really like this library, however I can't figure out how to use a string enum in a Security decorator.
I have the following code:
enum rights {
READ_FILE: "read_file",
WRITE_FILE: "write_file",
// ...
@Security("jwt", [rights.READ_FILE])
// ...
In the generated routes.ts this is what is generated:
// ...
authenticateMiddleware([{ "jwt": [null] }]),
// ...
I investigated a little bit, but only found, that in https://github.com/lukeautry/tsoa/blob/v2.3.8/src/metadataGeneration/security.ts#L13 there is no text property on the node. I'm not sure if this is more a Typescript problem than a problem with this library. Is there anything I can do about it?
We had same issue on our project. Unfortunately, we found no other way than using strings.
I have the same issuer
I've found solution with string literals.
You can define your own Security decorator: (because tsoa uses just names of decorators)
export interface SecurityProfile {
jwt: Privileges[];
}
export type Privileges = 'campaign_create' | 'campaign_update' | 'campaign_read'
| 'campaign_deactivate';
export function Security(name: SecurityProfile): Function {
return () => {
return;
};
}
so in colntrollers
import { Security } from '../../infrastructure/Security';
@Security({ jwt: ['campaign_create'] })
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
Reopening at @OrKoN鈥檚 request
It would be nice to be able to use an actual enum vs a string type. When using an enum such as:
export enum AuthType {
ApiKey = 'apiKey',
Bearer = 'bearer',
}
...
@Security(AuthType.Bearer);
I get the following error:
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
at __values (/Users/cfitzn/Development/armstrong/node_modules/tsoa/dist/metadataGeneration/security.js:3:46)
at Object.getSecurities (/Users/cfitzn/Development/armstrong/node_modules/tsoa/dist/metadataGeneration/security.js:31:60)
...
Converting to a bare string type works.
On a related note, this same issue prevents using code which needs to be evaluated as a parameter to a decorator. I was trying to not copy http response strings all over the code in my @Response decorators by doing something like:
@Response(statusCode[ErrorCode.NotFound],errorMessage[ErrorCode.NotFound])
@Patch('{id}'
public async update() {
It looks like the decorators are being parsed out of the code using the typescript compiler and the code is not actually being executed.
Maybe more work can be done here to determine if the value is an expression or not and further evaluated it until a more primitive value type is achieved:
It looks like https://github.com/lukeautry/tsoa/pull/648 may fix this issue by checking all decorators for PropertyAccessExpression?
@aldenquimby I will cover the enum case in my PR :smile:
Hi! Is this available for version 3.x?
Regards.