@anievo-id/rbac-evolution-nuxt 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Server-side enforcement for Nitro / h3 event handlers. Runs on the server only.
5
5
  */
6
- import type { DefaultRbacTypes, PermissionName, Rbac, RbacSnapshot, RbacTypeConfig, RoleName, Subject, SubjectRbac, TeamId } from '@anievo-id/rbac-evolution';
6
+ import type { CheckGuard, DefaultGuardName, DefaultRbacTypes, GuardOptions, GuardRequirementArgs, PermissionName, Rbac, RbacSnapshot, RbacTypeConfig, RequirementGuard, RoleName, ScopedGuardName, Subject, SubjectRbac, TeamId } from '@anievo-id/rbac-evolution';
7
7
  import { type PermissionRequirementOptions, type RequirementArgs, type RequirementKind, type RequirementNames, type RoleRequirementOptions } from '@anievo-id/rbac-evolution';
8
8
  export type { PermissionRequirementOptions, RequirementKind, RequirementNames, RoleRequirementOptions } from '@anievo-id/rbac-evolution';
9
9
  type MaybePromise<T> = T | Promise<T>;
@@ -11,13 +11,16 @@ type MaybePromise<T> = T | Promise<T>;
11
11
  export interface RbacEvent {
12
12
  context: object;
13
13
  }
14
- export interface RbacServerOptions<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {
14
+ export interface RbacServerOptions<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes, G extends string = string> {
15
15
  /** The RBAC engine created with `createRbac(...)`. Its typed names flow into the `require*` / `can*` helpers. */
16
16
  rbac: Rbac<T>;
17
17
  /** Resolves the authenticated subject for this request (e.g. from a session). `null` / `undefined` for guests. */
18
18
  getSubject: (event: E) => MaybePromise<Subject | null | undefined>;
19
- /** Guard name (e.g. `web` or `api`). Defaults to the engine's `defaultGuard`. */
20
- guard?: string | undefined;
19
+ /**
20
+ * Guard name (e.g. `web` or `api`). Defaults to the engine's `defaultGuard`. With per-guard names, it must be one
21
+ * of the typed guards (checked by `createRbacServer()`), and the helpers take its names.
22
+ */
23
+ guard?: G | undefined;
21
24
  /** Resolves the current team (only when the engine was created with `teams: true`). */
22
25
  team?: (event: E) => MaybePromise<TeamId | undefined>;
23
26
  /** Include the required roles/permissions in the 403 body (`required`). Default: `false`. */
@@ -100,6 +103,42 @@ export interface RbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeC
100
103
  /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means "a or b". */
101
104
  canAll<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(event: E, ...args: A & RequirementArgs<A, PermissionName<T>, RequireGuardOptions>): Promise<SubjectRbac<T>>;
102
105
  }
106
+ /**
107
+ * The helpers of `createRbacServer()` when the engine has per-guard names (`RbacTypeConfig['guards']`): like
108
+ * {@link RbacServer}, but the names are those of the guard each call checks in, its `{ guard }` option or else `G`
109
+ * (the `guard` of `createRbacServer()`), and the subject it returns is typed for that guard.
110
+ * `requirePermission(event, 'orders.read', { guard: 'api' })` takes the `api` names.
111
+ */
112
+ export interface ScopedRbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes, G extends string = DefaultGuardName<T>> {
113
+ /**
114
+ * Resolves the subject once per request (cached per helper instance) and stores it on
115
+ * `event.context.rbac` (a `SubjectRbac`, or `null` for guests) unless another instance did
116
+ * first. The Nuxt plugin reads it to hydrate the client.
117
+ */
118
+ resolve(event: E): Promise<SubjectRbac<T, G> | null>;
119
+ /** The current subject's `{ roles, permissions }`, or `null` for guests. */
120
+ snapshot(event: E): Promise<RbacSnapshot | null>;
121
+ /**
122
+ * Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`.
123
+ * With `{ guard: 'api' }` the check (and the returned subject) is in that guard.
124
+ */
125
+ requirePermission<const A extends readonly (RequirementNames | RequirePermissionOptions)[]>(event: E, ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequirePermissionOptions>): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>;
126
+ /** Like Express `requireRole`: any of the roles, `{ all: true }` for all, `{ guard: 'api' }` (spatie's `role:admin,api`). */
127
+ requireRole<const A extends readonly (RequirementNames | RequireRoleOptions)[]>(event: E, ...args: A & GuardRequirementArgs<A, T, 'role', G, RequireRoleOptions>): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>;
128
+ /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */
129
+ requireRoleOrPermission<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(event: E, ...args: A & GuardRequirementArgs<A, T, 'role_or_permission', G, RequireGuardOptions>): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>;
130
+ /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */
131
+ canAny<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(event: E, ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequireGuardOptions>): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>;
132
+ /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means "a or b". */
133
+ canAll<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(event: E, ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequireGuardOptions>): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>;
134
+ }
135
+ /**
136
+ * What `createRbacServer()` returns: {@link RbacServer} for an engine without per-guard names (as in 1.1), and
137
+ * {@link ScopedRbacServer} for one with `guards`.
138
+ */
139
+ export type RbacServerFor<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes, G extends string = DefaultGuardName<T>> = T extends {
140
+ guards: object;
141
+ } ? ScopedRbacServer<E, T, G> : RbacServer<E, T>;
103
142
  /**
104
143
  * Creates request helpers for Nitro event handlers.
105
144
  *
@@ -121,5 +160,7 @@ export interface RbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeC
121
160
  *
122
161
  * With a typed engine (`createRbac<AppTypes>(...)`), names are checked: `requirePermission(event, 'typo')` is a type error.
123
162
  */
124
- export declare function createRbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes>(options: RbacServerOptions<E, T>): RbacServer<E, T>;
163
+ export declare function createRbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes, G extends string = DefaultGuardName<T>>(options: RbacServerOptions<E, T, G> & (T extends {
164
+ guards: object;
165
+ } ? GuardOptions<CheckGuard<T, G>> : unknown)): RbacServerFor<E, T, ScopedGuardName<T, G>>;
125
166
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAChB,cAAc,EACd,IAAI,EACJ,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,OAAO,EACP,WAAW,EACX,MAAM,EACP,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAEL,KAAK,4BAA4B,EAEjC,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EAIrB,KAAK,sBAAsB,EAI5B,MAAM,2BAA2B,CAAA;AAElC,YAAY,EAAE,4BAA4B,EAAE,eAAe,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAA;AAExI,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAErC,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,EAAE,CAAC,SAAS,cAAc,GAAG,gBAAgB;IAC7G,iHAAiH;IACjH,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;IACb,kHAAkH;IACl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enforcement for Nitro / h3 event handlers. Runs on the server only.\n */\n\nimport type {\n DefaultRbacTypes,\n PermissionName,\n Rbac,\n RbacSnapshot,\n RbacTypeConfig,\n RoleName,\n Subject,\n SubjectRbac,\n TeamId,\n} from '@anievo-id/rbac-evolution'\nimport {\n permissionRequirement,\n type PermissionRequirementOptions,\n type RbacRequirement,\n type RequirementArgs,\n type RequirementKind,\n type RequirementNames,\n REQUIREMENT_MESSAGES,\n roleOrPermissionRequirement,\n roleRequirement,\n type RoleRequirementOptions,\n satisfiesRequirement,\n splitRequirementArgs,\n UNAUTHENTICATED_MESSAGE,\n} from '@anievo-id/rbac-evolution'\n\nexport type { PermissionRequirementOptions, RequirementKind, RequirementNames, RoleRequirementOptions } from '@anievo-id/rbac-evolution'\n\ntype MaybePromise<T> = T | Promise<T>\n\n/** The part of an h3 `H3Event` this helper uses. */\nexport interface RbacEvent {\n context: object\n}\n\nexport interface RbacServerOptions<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {\n /** The RBAC engine created with `createRbac(...)`. Its typed names flow into the `require*` / `can*` helpers. */\n rbac: Rbac<T>\n /** Resolves the authenticated subject for this request (e.g. from a session). `null` / `undefined` for guests. */\n getSubject: (event: E) => MaybePromise<Subject | null | undefined>\n /** Guard name (e.g. `web` or `api`). Defaults to the engine's `defaultGuard`. */\n guard?: string | undefined\n /** Resolves the current team (only when the engine was created with `teams: true`). */\n team?: (event: E) => MaybePromise<TeamId | undefined>\n /** Include the required roles/permissions in the 403 body (`required`). Default: `false`. */\n exposeRequirements?: boolean | undefined\n}\n\n/** The options every `require*` / `can*` helper takes (last argument). */\nexport interface RequireGuardOptions {\n /**\n * Check in this guard instead of the server's (`subject.withScope({ guard })`), like spatie's\n * `role:admin,api`. Default: the `guard` of `createRbacServer()`.\n */\n guard?: string | undefined\n}\n\n/** Options of `requirePermission()`: `{ any }` and `{ guard }`. */\nexport interface RequirePermissionOptions extends PermissionRequirementOptions, RequireGuardOptions {}\n\n/** Options of `requireRole()`: `{ all }` and `{ guard }`. */\nexport interface RequireRoleOptions extends RoleRequirementOptions, RequireGuardOptions {}\n\n/** The JSON body of a failed check — identical to `@anievo-id/rbac-evolution-express`. */\nexport type RbacErrorBody =\n | { error: 'UNAUTHENTICATED'; message: string }\n | { error: 'FORBIDDEN'; reason: RequirementKind; message: string; required?: string[][] }\n\n/**\n * Thrown by the `require*` helpers. h3 turns it into an error response with this `statusCode`\n * and the Express-style body under `data`.\n *\n * It is shaped like h3's `H3Error` (including the `__h3_error__` marker h3 checks), so h3 / Nitro\n * treat it as an expected HTTP error instead of logging it as an unhandled exception —\n * without this package depending on h3.\n */\nexport class RbacHttpError extends Error {\n static readonly __h3_error__ = true\n readonly statusCode: 401 | 403\n readonly statusMessage: string\n readonly data: RbacErrorBody\n readonly fatal = false\n readonly unhandled = false\n\n constructor(data: RbacErrorBody) {\n super(data.message)\n this.name = 'RbacHttpError'\n this.statusCode = data.error === 'UNAUTHENTICATED' ? 401 : 403\n this.statusMessage = data.error === 'UNAUTHENTICATED' ? 'Unauthorized' : 'Forbidden'\n this.data = data\n }\n\n /** Alias of `statusCode`. */\n get status(): 401 | 403 {\n return this.statusCode\n }\n\n toJSON() {\n return { message: this.message, statusCode: this.statusCode, statusMessage: this.statusMessage, data: this.data }\n }\n}\n\n/**\n * The helpers returned by `createRbacServer()`. `T` comes from the `rbac` option: with typed names, every\n * name (including each part of a `'a|b'` list) must be one of them; untyped, any string is accepted.\n * A call without names, or with options that are not the last argument, is a type error.\n */\nexport interface RbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {\n /**\n * Resolves the subject once per request (cached per helper instance) and stores it on\n * `event.context.rbac` (a `SubjectRbac`, or `null` for guests) unless another instance did\n * first. The Nuxt plugin reads it to hydrate the client.\n */\n resolve(event: E): Promise<SubjectRbac<T> | null>\n /** The current subject's `{ roles, permissions }`, or `null` for guests. */\n snapshot(event: E): Promise<RbacSnapshot | null>\n /**\n * Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`.\n * With `{ guard: 'api' }` the check (and the returned subject) is in that guard.\n */\n requirePermission<const A extends readonly (RequirementNames | RequirePermissionOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequirePermissionOptions>\n ): Promise<SubjectRbac<T>>\n /** Like Express `requireRole`: any of the roles, `{ all: true }` for all, `{ guard: 'api' }` (spatie's `role:admin,api`). */\n requireRole<const A extends readonly (RequirementNames | RequireRoleOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, RoleName<T>, RequireRoleOptions>\n ): Promise<SubjectRbac<T>>\n /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */\n requireRoleOrPermission<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T> | RoleName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */\n canAny<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means \"a or b\". */\n canAll<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n}\n\n/** The untyped implementation behind `RbacServer` (plain `string` names, spread-friendly). */\ninterface UntypedRbacServer<E extends RbacEvent> {\n resolve(event: E): Promise<SubjectRbac | null>\n snapshot(event: E): Promise<RbacSnapshot | null>\n requirePermission(event: E, ...args: (RequirementNames | RequirePermissionOptions)[]): Promise<SubjectRbac>\n requireRole(event: E, ...args: (RequirementNames | RequireRoleOptions)[]): Promise<SubjectRbac>\n requireRoleOrPermission(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n canAny(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n canAll(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n}\n\nconst CONTEXT_KEY = 'rbac'\n\n/**\n * Creates request helpers for Nitro event handlers.\n *\n * ```ts\n * // server/utils/rbac.ts\n * export const rbacServer = createRbacServer({\n * rbac,\n * getSubject: async (event) => {\n * const user = await getUserFromSession(event)\n * return user ? { type: 'user', id: user.id } : null\n * },\n * })\n *\n * // server/api/articles.post.ts\n * export default defineEventHandler(async (event) => {\n * await rbacServer.requirePermission(event, 'articles.create')\n * })\n * ```\n *\n * With a typed engine (`createRbac<AppTypes>(...)`), names are checked: `requirePermission(event, 'typo')` is a type error.\n */\nexport function createRbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes>(\n options: RbacServerOptions<E, T>,\n): RbacServer<E, T> {\n return buildRbacServer(options as unknown as RbacServerOptions<E>) as unknown as RbacServer<E, T>\n}\n\nfunction buildRbacServer<E extends RbacEvent>(options: RbacServerOptions<E>): UntypedRbacServer<E> {\n const { rbac, getSubject, guard, team, exposeRequirements = false } = options\n // Per instance, so a second server (e.g. guard 'api') never reuses another guard's subject.\n const resolved = new WeakMap<object, SubjectRbac | null>()\n\n async function resolve(event: E): Promise<SubjectRbac | null> {\n const context = event.context as Record<string, unknown>\n if (resolved.has(context)) return resolved.get(context)!\n\n const subject = await getSubject(event)\n let result: SubjectRbac | null = null\n if (subject) {\n const teamId = team ? await team(event) : undefined\n result = rbac.for(subject, {\n ...(guard !== undefined && { guard }),\n ...(teamId !== undefined && { team: teamId }),\n })\n }\n resolved.set(context, result)\n // The client plugin hydrates from `event.context.rbac`: the first instance to resolve fills it.\n if (context[CONTEXT_KEY] === undefined) context[CONTEXT_KEY] = result\n return result\n }\n\n // Async so an empty requirement (TypeError) rejects the returned promise instead of throwing synchronously.\n async function enforce(event: E, build: () => RbacRequirement, scope: RequireGuardOptions = {}): Promise<SubjectRbac> {\n const requirement = build()\n const resolvedSubject = await resolve(event)\n if (resolvedSubject === null) throw new RbacHttpError({ error: 'UNAUTHENTICATED', message: UNAUTHENTICATED_MESSAGE })\n const subject = scope.guard === undefined ? resolvedSubject : resolvedSubject.withScope({ guard: scope.guard })\n if (!(await satisfiesRequirement(subject, requirement))) {\n throw new RbacHttpError({\n error: 'FORBIDDEN',\n reason: requirement.kind,\n message: REQUIREMENT_MESSAGES[requirement.kind],\n ...(exposeRequirements && { required: requirement.requirements }),\n })\n }\n return subject\n }\n\n return {\n resolve,\n async snapshot(event) {\n const subject = await resolve(event)\n return subject ? subject.snapshot() : null\n },\n requirePermission(event, ...args) {\n const { names, options } = splitRequirementArgs<RequirePermissionOptions>(args)\n return enforce(event, () => permissionRequirement(names, options), options)\n },\n requireRole(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireRoleOptions>(args)\n return enforce(event, () => roleRequirement(names, options), options)\n },\n requireRoleOrPermission(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => roleOrPermissionRequirement(names), options)\n },\n canAny(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => permissionRequirement(names, { any: true }), options)\n },\n canAll(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => permissionRequirement(names), options)\n },\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/server.ts"],"names":["options"],"mappings":";;;AAgGO,IAAM,aAAA,GAAN,cAA4B,KAAA,CAAM;AAAA,EACvC,OAAgB,YAAA,GAAe,IAAA;AAAA,EACtB,UAAA;AAAA,EACA,aAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA,GAAQ,KAAA;AAAA,EACR,SAAA,GAAY,KAAA;AAAA,EAErB,YAAY,IAAA,EAAqB;AAC/B,IAAA,KAAA,CAAM,KAAK,OAAO,CAAA;AAClB,IAAA,IAAA,CAAK,IAAA,GAAO,eAAA;AACZ,IA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AAE,KAAA,EAAO,OAAA,EAAAA,QAAAA,EAAQ,GAAI,qBAA0C,IAAI,CAAA;AACzE,MAAA,OAAO,QAAQ,KAAA,EAAO,MAAM,qBAAA,CAAsB,KAAK,GAAGA,QAAO,CAAA;AAAA,IACnE;AAAA,GACF;AACF","file":"server.js","sourcesContent":["/**\n * @anievo-id/rbac-evolution-nuxt/server\n *\n * Server-side enforcement for Nitro / h3 event handlers. Runs on the server only.\n */\n\nimport type {\n CheckGuard,\n DefaultGuardName,\n DefaultRbacTypes,\n GuardOptions,\n GuardRequirementArgs,\n PermissionName,\n Rbac,\n RbacSnapshot,\n RbacTypeConfig,\n RequirementGuard,\n RoleName,\n ScopedGuardName,\n Subject,\n SubjectRbac,\n TeamId,\n} from '@anievo-id/rbac-evolution'\nimport {\n permissionRequirement,\n type PermissionRequirementOptions,\n type RbacRequirement,\n type RequirementArgs,\n type RequirementKind,\n type RequirementNames,\n REQUIREMENT_MESSAGES,\n roleOrPermissionRequirement,\n roleRequirement,\n type RoleRequirementOptions,\n satisfiesRequirement,\n splitRequirementArgs,\n UNAUTHENTICATED_MESSAGE,\n} from '@anievo-id/rbac-evolution'\n\nexport type { PermissionRequirementOptions, RequirementKind, RequirementNames, RoleRequirementOptions } from '@anievo-id/rbac-evolution'\n\ntype MaybePromise<T> = T | Promise<T>\n\n/** The part of an h3 `H3Event` this helper uses. */\nexport interface RbacEvent {\n context: object\n}\n\nexport interface RbacServerOptions<\n E extends RbacEvent = RbacEvent,\n T extends RbacTypeConfig = DefaultRbacTypes,\n G extends string = string,\n> {\n /** The RBAC engine created with `createRbac(...)`. Its typed names flow into the `require*` / `can*` helpers. */\n rbac: Rbac<T>\n /** Resolves the authenticated subject for this request (e.g. from a session). `null` / `undefined` for guests. */\n getSubject: (event: E) => MaybePromise<Subject | null | undefined>\n /**\n * Guard name (e.g. `web` or `api`). Defaults to the engine's `defaultGuard`. With per-guard names, it must be one\n * of the typed guards (checked by `createRbacServer()`), and the helpers take its names.\n */\n guard?: G | undefined\n /** Resolves the current team (only when the engine was created with `teams: true`). */\n team?: (event: E) => MaybePromise<TeamId | undefined>\n /** Include the required roles/permissions in the 403 body (`required`). Default: `false`. */\n exposeRequirements?: boolean | undefined\n}\n\n/** The options every `require*` / `can*` helper takes (last argument). */\nexport interface RequireGuardOptions {\n /**\n * Check in this guard instead of the server's (`subject.withScope({ guard })`), like spatie's\n * `role:admin,api`. Default: the `guard` of `createRbacServer()`.\n */\n guard?: string | undefined\n}\n\n/** Options of `requirePermission()`: `{ any }` and `{ guard }`. */\nexport interface RequirePermissionOptions extends PermissionRequirementOptions, RequireGuardOptions {}\n\n/** Options of `requireRole()`: `{ all }` and `{ guard }`. */\nexport interface RequireRoleOptions extends RoleRequirementOptions, RequireGuardOptions {}\n\n/** The JSON body of a failed check — identical to `@anievo-id/rbac-evolution-express`. */\nexport type RbacErrorBody =\n | { error: 'UNAUTHENTICATED'; message: string }\n | { error: 'FORBIDDEN'; reason: RequirementKind; message: string; required?: string[][] }\n\n/**\n * Thrown by the `require*` helpers. h3 turns it into an error response with this `statusCode`\n * and the Express-style body under `data`.\n *\n * It is shaped like h3's `H3Error` (including the `__h3_error__` marker h3 checks), so h3 / Nitro\n * treat it as an expected HTTP error instead of logging it as an unhandled exception —\n * without this package depending on h3.\n */\nexport class RbacHttpError extends Error {\n static readonly __h3_error__ = true\n readonly statusCode: 401 | 403\n readonly statusMessage: string\n readonly data: RbacErrorBody\n readonly fatal = false\n readonly unhandled = false\n\n constructor(data: RbacErrorBody) {\n super(data.message)\n this.name = 'RbacHttpError'\n this.statusCode = data.error === 'UNAUTHENTICATED' ? 401 : 403\n this.statusMessage = data.error === 'UNAUTHENTICATED' ? 'Unauthorized' : 'Forbidden'\n this.data = data\n }\n\n /** Alias of `statusCode`. */\n get status(): 401 | 403 {\n return this.statusCode\n }\n\n toJSON() {\n return { message: this.message, statusCode: this.statusCode, statusMessage: this.statusMessage, data: this.data }\n }\n}\n\n/**\n * The helpers returned by `createRbacServer()`. `T` comes from the `rbac` option: with typed names, every\n * name (including each part of a `'a|b'` list) must be one of them; untyped, any string is accepted.\n * A call without names, or with options that are not the last argument, is a type error.\n */\nexport interface RbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {\n /**\n * Resolves the subject once per request (cached per helper instance) and stores it on\n * `event.context.rbac` (a `SubjectRbac`, or `null` for guests) unless another instance did\n * first. The Nuxt plugin reads it to hydrate the client.\n */\n resolve(event: E): Promise<SubjectRbac<T> | null>\n /** The current subject's `{ roles, permissions }`, or `null` for guests. */\n snapshot(event: E): Promise<RbacSnapshot | null>\n /**\n * Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`.\n * With `{ guard: 'api' }` the check (and the returned subject) is in that guard.\n */\n requirePermission<const A extends readonly (RequirementNames | RequirePermissionOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequirePermissionOptions>\n ): Promise<SubjectRbac<T>>\n /** Like Express `requireRole`: any of the roles, `{ all: true }` for all, `{ guard: 'api' }` (spatie's `role:admin,api`). */\n requireRole<const A extends readonly (RequirementNames | RequireRoleOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, RoleName<T>, RequireRoleOptions>\n ): Promise<SubjectRbac<T>>\n /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */\n requireRoleOrPermission<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T> | RoleName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */\n canAny<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means \"a or b\". */\n canAll<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, RequireGuardOptions>\n ): Promise<SubjectRbac<T>>\n}\n\n/**\n * The helpers of `createRbacServer()` when the engine has per-guard names (`RbacTypeConfig['guards']`): like\n * {@link RbacServer}, but the names are those of the guard each call checks in, its `{ guard }` option or else `G`\n * (the `guard` of `createRbacServer()`), and the subject it returns is typed for that guard.\n * `requirePermission(event, 'orders.read', { guard: 'api' })` takes the `api` names.\n */\nexport interface ScopedRbacServer<\n E extends RbacEvent = RbacEvent,\n T extends RbacTypeConfig = DefaultRbacTypes,\n G extends string = DefaultGuardName<T>,\n> {\n /**\n * Resolves the subject once per request (cached per helper instance) and stores it on\n * `event.context.rbac` (a `SubjectRbac`, or `null` for guests) unless another instance did\n * first. The Nuxt plugin reads it to hydrate the client.\n */\n resolve(event: E): Promise<SubjectRbac<T, G> | null>\n /** The current subject's `{ roles, permissions }`, or `null` for guests. */\n snapshot(event: E): Promise<RbacSnapshot | null>\n /**\n * Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`.\n * With `{ guard: 'api' }` the check (and the returned subject) is in that guard.\n */\n requirePermission<const A extends readonly (RequirementNames | RequirePermissionOptions)[]>(\n event: E,\n ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequirePermissionOptions>\n ): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>\n /** Like Express `requireRole`: any of the roles, `{ all: true }` for all, `{ guard: 'api' }` (spatie's `role:admin,api`). */\n requireRole<const A extends readonly (RequirementNames | RequireRoleOptions)[]>(\n event: E,\n ...args: A & GuardRequirementArgs<A, T, 'role', G, RequireRoleOptions>\n ): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>\n /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */\n requireRoleOrPermission<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & GuardRequirementArgs<A, T, 'role_or_permission', G, RequireGuardOptions>\n ): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>\n /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */\n canAny<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequireGuardOptions>\n ): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>\n /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means \"a or b\". */\n canAll<const A extends readonly (RequirementNames | RequireGuardOptions)[]>(\n event: E,\n ...args: A & GuardRequirementArgs<A, T, 'permission', G, RequireGuardOptions>\n ): Promise<SubjectRbac<T, ScopedGuardName<T, RequirementGuard<A, G>>>>\n}\n\n/**\n * What `createRbacServer()` returns: {@link RbacServer} for an engine without per-guard names (as in 1.1), and\n * {@link ScopedRbacServer} for one with `guards`.\n */\nexport type RbacServerFor<\n E extends RbacEvent = RbacEvent,\n T extends RbacTypeConfig = DefaultRbacTypes,\n G extends string = DefaultGuardName<T>,\n> = T extends { guards: object } ? ScopedRbacServer<E, T, G> : RbacServer<E, T>\n\n/** The untyped implementation behind `RbacServer` (plain `string` names, spread-friendly). */\ninterface UntypedRbacServer<E extends RbacEvent> {\n resolve(event: E): Promise<SubjectRbac | null>\n snapshot(event: E): Promise<RbacSnapshot | null>\n requirePermission(event: E, ...args: (RequirementNames | RequirePermissionOptions)[]): Promise<SubjectRbac>\n requireRole(event: E, ...args: (RequirementNames | RequireRoleOptions)[]): Promise<SubjectRbac>\n requireRoleOrPermission(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n canAny(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n canAll(event: E, ...args: (RequirementNames | RequireGuardOptions)[]): Promise<SubjectRbac>\n}\n\nconst CONTEXT_KEY = 'rbac'\n\n/**\n * Creates request helpers for Nitro event handlers.\n *\n * ```ts\n * // server/utils/rbac.ts\n * export const rbacServer = createRbacServer({\n * rbac,\n * getSubject: async (event) => {\n * const user = await getUserFromSession(event)\n * return user ? { type: 'user', id: user.id } : null\n * },\n * })\n *\n * // server/api/articles.post.ts\n * export default defineEventHandler(async (event) => {\n * await rbacServer.requirePermission(event, 'articles.create')\n * })\n * ```\n *\n * With a typed engine (`createRbac<AppTypes>(...)`), names are checked: `requirePermission(event, 'typo')` is a type error.\n */\nexport function createRbacServer<\n E extends RbacEvent = RbacEvent,\n T extends RbacTypeConfig = DefaultRbacTypes,\n G extends string = DefaultGuardName<T>,\n>(\n options: RbacServerOptions<E, T, G> & (T extends { guards: object } ? GuardOptions<CheckGuard<T, G>> : unknown),\n): RbacServerFor<E, T, ScopedGuardName<T, G>> {\n return buildRbacServer(options as unknown as RbacServerOptions<E>) as unknown as RbacServerFor<E, T, ScopedGuardName<T, G>>\n}\n\nfunction buildRbacServer<E extends RbacEvent>(options: RbacServerOptions<E>): UntypedRbacServer<E> {\n const { rbac, getSubject, guard, team, exposeRequirements = false } = options\n // Per instance, so a second server (e.g. guard 'api') never reuses another guard's subject.\n const resolved = new WeakMap<object, SubjectRbac | null>()\n\n async function resolve(event: E): Promise<SubjectRbac | null> {\n const context = event.context as Record<string, unknown>\n if (resolved.has(context)) return resolved.get(context)!\n\n const subject = await getSubject(event)\n let result: SubjectRbac | null = null\n if (subject) {\n const teamId = team ? await team(event) : undefined\n result = rbac.for(subject, {\n ...(guard !== undefined && { guard }),\n ...(teamId !== undefined && { team: teamId }),\n })\n }\n resolved.set(context, result)\n // The client plugin hydrates from `event.context.rbac`: the first instance to resolve fills it.\n if (context[CONTEXT_KEY] === undefined) context[CONTEXT_KEY] = result\n return result\n }\n\n // Async so an empty requirement (TypeError) rejects the returned promise instead of throwing synchronously.\n async function enforce(event: E, build: () => RbacRequirement, scope: RequireGuardOptions = {}): Promise<SubjectRbac> {\n const requirement = build()\n const resolvedSubject = await resolve(event)\n if (resolvedSubject === null) throw new RbacHttpError({ error: 'UNAUTHENTICATED', message: UNAUTHENTICATED_MESSAGE })\n const subject = scope.guard === undefined ? resolvedSubject : resolvedSubject.withScope({ guard: scope.guard })\n if (!(await satisfiesRequirement(subject, requirement))) {\n throw new RbacHttpError({\n error: 'FORBIDDEN',\n reason: requirement.kind,\n message: REQUIREMENT_MESSAGES[requirement.kind],\n ...(exposeRequirements && { required: requirement.requirements }),\n })\n }\n return subject\n }\n\n return {\n resolve,\n async snapshot(event) {\n const subject = await resolve(event)\n return subject ? subject.snapshot() : null\n },\n requirePermission(event, ...args) {\n const { names, options } = splitRequirementArgs<RequirePermissionOptions>(args)\n return enforce(event, () => permissionRequirement(names, options), options)\n },\n requireRole(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireRoleOptions>(args)\n return enforce(event, () => roleRequirement(names, options), options)\n },\n requireRoleOrPermission(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => roleOrPermissionRequirement(names), options)\n },\n canAny(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => permissionRequirement(names, { any: true }), options)\n },\n canAll(event, ...args) {\n const { names, options } = splitRequirementArgs<RequireGuardOptions>(args)\n return enforce(event, () => permissionRequirement(names), options)\n },\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anievo-id/rbac-evolution-nuxt",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Nuxt 3/4 module for rbac-evolution: SSR-hydrated permissions, auto-imported composables, route middleware & Nitro helpers.",
5
5
  "license": "MIT",
6
6
  "author": "AniEvo-ID (https://github.com/AniEvo-ID)",
@@ -50,8 +50,8 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@nuxt/kit": "^3.13.0 || ^4.0.0",
53
- "@anievo-id/rbac-evolution": "^1.0.0",
54
- "@anievo-id/rbac-evolution-vue": "^1.0.0"
53
+ "@anievo-id/rbac-evolution": "^1.2.0",
54
+ "@anievo-id/rbac-evolution-vue": "^1.2.0"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "nuxt": "^3.0.0 || ^4.0.0",