nuxt-authorization
Version:
Authorization module for managing permissions on the Nuxt app and the Nitro server.
91 lines (86 loc) • 3.88 kB
text/typescript
/**
* Represents a possible response from the executed ability. This allows for more flexibility in the response and the error thrown if the user is not authorized.
*/
type AuthorizationResponse = {
statusCode?: number;
message?: string;
authorized: boolean;
};
/**
* Represents the response of an executed ability.
*/
type AuthorizerResponse = boolean | AuthorizationResponse | Promise<boolean | AuthorizationResponse>;
/**
* Represents the authorizer function that will be executed to determine if a user is authorized to perform an action.
*/
type BouncerAuthorizer<User extends Record<string, any>> = (user: User, ...args: any[]) => AuthorizerResponse;
/**
* Represents an ability that can be used by a bouncer.
*/
type BouncerAbility<User extends Record<string, any>> = {
original: BouncerAuthorizer<User>;
execute: (user: User | null, ...args: any[]) => AuthorizerResponse;
allowGuest: boolean;
};
/**
* Represents the arguments that will be passed to the authorizer function.
*/
type BouncerArgs<Ability extends BouncerAbility<any>> = Ability extends {
original: (user: any, ...args: infer Args) => AuthorizerResponse;
} ? Args : never;
/**
* Normalize the ability execution result to an authorization response.
*/
declare function normalizeAuthorizationResponse(result: boolean | AuthorizationResponse): AuthorizationResponse;
/**
* Check if a user can perform an action.
*/
declare function allows<Ability extends BouncerAbility<any>, User extends Record<string, any> = Ability extends BouncerAbility<infer U> ? U : never>(ability: Ability, user: User | null, ...args: BouncerArgs<Ability>): Promise<boolean>;
/**
* Check if a user cannot perform an action.
*/
declare function denies<Ability extends BouncerAbility<any>, User extends Record<string, any> = Ability extends BouncerAbility<infer U> ? U : never>(ability: Ability, user: User | null, ...args: BouncerArgs<Ability>): Promise<boolean>;
/**
* Check if a user can perform an action and throws an error if not.
*/
declare function authorize<Ability extends BouncerAbility<any>, User extends Record<string, any> = Ability extends BouncerAbility<infer U> ? U : never>(ability: Ability, user: User | null, ...args: BouncerArgs<Ability>): Promise<void>;
/**
* @credit https://github.com/adonisjs/bouncer
*/
type AuthorizerToAbility<Authorizer> = Authorizer extends (user: infer User, ...args: infer Args) => AuthorizerResponse ? {
allowGuest: boolean;
original: Authorizer;
execute(user: User | null, ...args: Args): AuthorizerResponse;
} : never;
/**
* Define an ability that you can use to check if a user can perform an action.
* @credit https://github.com/adonisjs/bouncer
*/
declare function defineAbility<Authorizer extends BouncerAuthorizer<any>>(authorizer: Authorizer): AuthorizerToAbility<Authorizer>;
declare function defineAbility<Authorizer extends BouncerAuthorizer<any>>(options: {
allowGuest: boolean;
}, authorizer: Authorizer): AuthorizerToAbility<Authorizer>;
/**
* Helper to allow a user to perform an action.
*/
declare function allow(): AuthorizationResponse;
/**
* Helper to deny a user to perform an action.
*/
declare function deny(options?: {
statusCode?: number;
message?: string;
}): AuthorizationResponse;
/**
* Return a new AuthorizationError that can be thrown.
*/
declare const createAuthorizationError: (message?: string, statusCode?: number) => AuthorizationError;
/**
* Authorization error.
*/
declare class AuthorizationError extends Error {
statusCode: number;
constructor(message: string, statusCode: number);
}
export { AuthorizationError, allow, allows, authorize, createAuthorizationError, defineAbility, denies, deny, normalizeAuthorizationResponse };
export type { AuthorizationResponse, AuthorizerResponse, AuthorizerToAbility, BouncerAbility, BouncerArgs, BouncerAuthorizer };