Bouncer functions are missing types for server
Hello!
I found this module which I really like for my needs, but I ran into an issue with types of the bouncer functions on the server side. For example the authorize method. The method is auto-imported, but the code imported is for the client part of the application and I can't force it to the correct type where event is the first argument. I also tried to import it manually from the module itself but also with no luck.
The file on the screenshot is nested in the server/ directory.
Sample recreation on StackBlitz, https://stackblitz.com/edit/github-bgkk3g6m?file=server%2Fapi%2Fhello.ts
Could you please help me on that?
Thank you
I temporarily fixed it with patch-package module by appending Event (eg. authorizeEvent) to the bouncer functions.
nuxt-authorization+0.3.2.patch
diff --git a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
index ab46b57..585a9b2 100644
--- a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
+++ b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
@@ -3,12 +3,12 @@ import type { BouncerAbility, BouncerArgs } from '../../../utils/index.js';
/**
* Allows a user to perform an action based on their role and the data.
*/
-export declare function allows<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
+export declare function allowsEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
/**
* Denies a user to perform an action based on their role and the data.
*/
-export declare function denies<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
+export declare function deniesEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
/**
* Throws an error if the user is not allowed to perform an action.
*/
-export declare function authorize<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<void>;
+export declare function authorizeEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<void>;
diff --git a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
index 680999c..d4fbb8d 100644
--- a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
+++ b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
@@ -1,14 +1,14 @@
import { allows as _allows, denies as _denies, authorize as _authorize, AuthorizationError } from "../../../utils";
import { createError } from "#imports";
-export async function allows(event, ability, ...args) {
+export async function allowsEvent(event, ability, ...args) {
const user = await event.context.$authorization.resolveServerUser();
return _allows(ability, user, ...args);
}
-export async function denies(event, ability, ...args) {
+export async function deniesEvent(event, ability, ...args) {
const user = await event.context.$authorization.resolveServerUser();
return _denies(ability, user, ...args);
}
-export async function authorize(event, ability, ...args) {
+export async function authorizeEvent(event, ability, ...args) {
try {
const user = await event.context.$authorization.resolveServerUser();
await _authorize(ability, user, ...args);
Hey 👋,
I'm sorry about it but I'm unable to understand why it does not work. 🙁
I believe TypeScript is confused between src/runtime/utils/bouncer.ts#authorize() and src/runtime/server/utils/bouncer.ts#authorize() which signatures are not the same.
Maybe this will be fixed with Nuxt v4 new directory structure.
Same problem, I think @huafu is right.
I found a temporal solution importing the function with the entire path:
import { denies } from "~~/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer";
Daniel could give the solution
https://github.com/nuxt/nuxt/issues/29263#issuecomment-3009000635