import { IDecorator, Meta } from "oly"; import { olyApiKeys, use } from "oly-api"; import { hasRole } from "../middlewares/hasRole"; export class AuthDecorator implements IDecorator { public constructor(private roles: string | string[] = []) { } public asMethod(target: object, propertyKey: string, i: TypedPropertyDescriptor) { const roles = typeof this.roles === "string" ? [this.roles] : this.roles; use(hasRole(...roles))(target, propertyKey); Meta.of({key: olyApiKeys.router, target, propertyKey}).set({ roles, }); } } /** * Sugar of @use + hasRole(...roles) middleware. * * ```ts * * class Api { * * @auth // check only auth * @get("/") * authOnly() {} * * @auth("ADMIN") // check data.roles[] * @get("/admin") * adminOnly() {} * } * ``` */ export const auth = Meta.decorator(AuthDecorator);