@anievo-id/rbac-evolution-nuxt 0.7.1 → 0.8.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/README.md CHANGED
@@ -86,3 +86,6 @@ export default defineEventHandler(async (event) => {
86
86
 
87
87
  `requirePermission`, `requireRole` and `requireRoleOrPermission` throw a 401 / 403 error whose `data` is the
88
88
  Express body (`{ error, reason, message }`).
89
+
90
+ With a typed engine (`createRbac<AppRbac>()`), `createRbacServer` checks every name.
91
+ `requirePermission(event, 'articles.edti')` is then a compile error. With an untyped engine, any string is accepted.
package/dist/server.d.ts CHANGED
@@ -3,17 +3,17 @@
3
3
  *
4
4
  * Server-side enforcement for Nitro / h3 event handlers. Runs on the server only.
5
5
  */
6
- import type { Rbac, RbacSnapshot, Subject, SubjectRbac, TeamId } from '@anievo-id/rbac-evolution';
7
- import { type PermissionRequirementOptions, type RequirementKind, type RequirementNames, type RoleRequirementOptions } from '@anievo-id/rbac-evolution';
6
+ import type { DefaultRbacTypes, PermissionName, Rbac, RbacSnapshot, RbacTypeConfig, RoleName, Subject, SubjectRbac, TeamId } from '@anievo-id/rbac-evolution';
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>;
10
10
  /** The part of an h3 `H3Event` this helper uses. */
11
11
  export interface RbacEvent {
12
12
  context: object;
13
13
  }
14
- export interface RbacServerOptions<E extends RbacEvent = RbacEvent> {
15
- /** The RBAC engine created with `createRbac(...)`. */
16
- rbac: Rbac;
14
+ export interface RbacServerOptions<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {
15
+ /** The RBAC engine created with `createRbac(...)`. Its typed names flow into the `require*` / `can*` helpers. */
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
19
  /** Guard name (e.g. `web` or `api`). Defaults to the engine's `defaultGuard`. */
@@ -58,25 +58,30 @@ export declare class RbacHttpError extends Error {
58
58
  data: RbacErrorBody;
59
59
  };
60
60
  }
61
- export interface RbacServer<E extends RbacEvent = RbacEvent> {
61
+ /**
62
+ * The helpers returned by `createRbacServer()`. `T` comes from the `rbac` option: with typed names, every
63
+ * name (including each part of a `'a|b'` list) must be one of them; untyped, any string is accepted.
64
+ * A call without names, or with options that are not the last argument, is a type error.
65
+ */
66
+ export interface RbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes> {
62
67
  /**
63
68
  * Resolves the subject once per request (cached per helper instance) and stores it on
64
69
  * `event.context.rbac` (a `SubjectRbac`, or `null` for guests) unless another instance did
65
70
  * first. The Nuxt plugin reads it to hydrate the client.
66
71
  */
67
- resolve(event: E): Promise<SubjectRbac | null>;
72
+ resolve(event: E): Promise<SubjectRbac<T> | null>;
68
73
  /** The current subject's `{ roles, permissions }`, or `null` for guests. */
69
74
  snapshot(event: E): Promise<RbacSnapshot | null>;
70
75
  /** Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`. */
71
- requirePermission(event: E, ...args: (RequirementNames | PermissionRequirementOptions)[]): Promise<SubjectRbac>;
76
+ requirePermission<const A extends readonly (RequirementNames | PermissionRequirementOptions)[]>(event: E, ...args: A & RequirementArgs<A, PermissionName<T>, PermissionRequirementOptions>): Promise<SubjectRbac<T>>;
72
77
  /** Like Express `requireRole`: any of the roles, `{ all: true }` for all. */
73
- requireRole(event: E, ...args: (RequirementNames | RoleRequirementOptions)[]): Promise<SubjectRbac>;
78
+ requireRole<const A extends readonly (RequirementNames | RoleRequirementOptions)[]>(event: E, ...args: A & RequirementArgs<A, RoleName<T>, RoleRequirementOptions>): Promise<SubjectRbac<T>>;
74
79
  /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */
75
- requireRoleOrPermission(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>;
80
+ requireRoleOrPermission<const A extends readonly RequirementNames[]>(event: E, ...names: A & RequirementArgs<A, PermissionName<T> | RoleName<T>, never>): Promise<SubjectRbac<T>>;
76
81
  /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */
77
- canAny(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>;
82
+ canAny<const A extends readonly RequirementNames[]>(event: E, ...names: A & RequirementArgs<A, PermissionName<T>, never>): Promise<SubjectRbac<T>>;
78
83
  /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means "a or b". */
79
- canAll(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>;
84
+ canAll<const A extends readonly RequirementNames[]>(event: E, ...names: A & RequirementArgs<A, PermissionName<T>, never>): Promise<SubjectRbac<T>>;
80
85
  }
81
86
  /**
82
87
  * Creates request helpers for Nitro event handlers.
@@ -96,6 +101,8 @@ export interface RbacServer<E extends RbacEvent = RbacEvent> {
96
101
  * await rbacServer.requirePermission(event, 'articles.create')
97
102
  * })
98
103
  * ```
104
+ *
105
+ * With a typed engine (`createRbac<AppTypes>(...)`), names are checked: `requirePermission(event, 'typo')` is a type error.
99
106
  */
100
- export declare function createRbacServer<E extends RbacEvent = RbacEvent>(options: RbacServerOptions<E>): RbacServer<E>;
107
+ export declare function createRbacServer<E extends RbacEvent = RbacEvent, T extends RbacTypeConfig = DefaultRbacTypes>(options: RbacServerOptions<E, T>): RbacServer<E, T>;
101
108
  //# 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,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACjG,OAAO,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AAW,CAAC,CAAC,CAAC,CAAC,CAAA;CAC3B;AAeD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,EAAE,CAAC,SAAS,cAAc,GAAG,gBAAgB,EAC3G,OAAO,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC/B,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAElB"}
package/dist/server.js CHANGED
@@ -25,6 +25,9 @@ var RbacHttpError = class extends Error {
25
25
  };
26
26
  var CONTEXT_KEY = "rbac";
27
27
  function createRbacServer(options) {
28
+ return buildRbacServer(options);
29
+ }
30
+ function buildRbacServer(options) {
28
31
  const { rbac, getSubject, guard, team, exposeRequirements = false } = options;
29
32
  const resolved = /* @__PURE__ */ new WeakMap();
30
33
  async function resolve(event) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/server.ts"],"names":["options"],"mappings":";;;AAyDO,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,IAAA,IAAA,CAAK,UAAA,GAAa,IAAA,CAAK,KAAA,KAAU,iBAAA,GAAoB,GAAA,GAAM,GAAA;AAC3D,IAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAK,KAAA,KAAU,iBAAA,GAAoB,cAAA,GAAiB,WAAA;AACzE,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,MAAA,GAAoB;AACtB,IAAA,OAAO,IAAA,CAAK,UAAA;AAAA,EACd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MAAM,eAAA,CAAgB,KAAA,EAAOA,QAAO,CAAC,CAAA;AAAA,IAC7D,CAAA;AAAA,IACA,uBAAA,CAAwB,UAAU,KAAA,EAAO;AACvC,MAAA,OAAO,OAAA,CAAQ,KAAA,EAAO,MAAM,2BAAA,CAA4B,KAAK,CAAC,CAAA;AAAA,IAChE,CAAA;AAAA,IACA,MAAA,CAAO,UAAU,KAAA,EAAO;AACtB,MAAA,OAAO,OAAA,CAAQ,OAAO,MAAM,qBAAA,CAAsB,OAAO,EAAE,GAAA,EAAK,IAAA,EAAM,CAAC,CAAA;AAAA,IACzE,CAAA;AAAA,IACA,MAAA,CAAO,UAAU,KAAA,EAAO;AACtB,MAAA,OAAO,OAAA,CAAQ,KAAA,EAAO,MAAM,qBAAA,CAAsB,KAAK,CAAC,CAAA;AAAA,IAC1D;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 { Rbac, RbacSnapshot, Subject, SubjectRbac, TeamId } from '@anievo-id/rbac-evolution'\nimport {\n permissionRequirement,\n type PermissionRequirementOptions,\n type RbacRequirement,\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> {\n /** The RBAC engine created with `createRbac(...)`. */\n rbac: Rbac\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 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\nexport interface RbacServer<E extends RbacEvent = RbacEvent> {\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 | null>\n /** The current subject's `{ roles, permissions }`, or `null` for guests. */\n snapshot(event: E): Promise<RbacSnapshot | null>\n /** Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`. */\n requirePermission(event: E, ...args: (RequirementNames | PermissionRequirementOptions)[]): Promise<SubjectRbac>\n /** Like Express `requireRole`: any of the roles, `{ all: true }` for all. */\n requireRole(event: E, ...args: (RequirementNames | RoleRequirementOptions)[]): Promise<SubjectRbac>\n /** Like Express `requireRoleOrPermission`: any of the names as a role OR a permission. */\n requireRoleOrPermission(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>\n /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */\n canAny(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>\n /** spatie v8.5 `canAll()`: requires EVERY listed entry; a `'a|b'` entry still means \"a or b\". */\n canAll(event: E, ...names: RequirementNames[]): 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 */\nexport function createRbacServer<E extends RbacEvent = RbacEvent>(options: RbacServerOptions<E>): RbacServer<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): Promise<SubjectRbac> {\n const requirement = build()\n const subject = await resolve(event)\n if (subject === null) throw new RbacHttpError({ error: 'UNAUTHENTICATED', message: UNAUTHENTICATED_MESSAGE })\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<PermissionRequirementOptions>(args)\n return enforce(event, () => permissionRequirement(names, options))\n },\n requireRole(event, ...args) {\n const { names, options } = splitRequirementArgs<RoleRequirementOptions>(args)\n return enforce(event, () => roleRequirement(names, options))\n },\n requireRoleOrPermission(event, ...names) {\n return enforce(event, () => roleOrPermissionRequirement(names))\n },\n canAny(event, ...names) {\n return enforce(event, () => permissionRequirement(names, { any: true }))\n },\n canAll(event, ...names) {\n return enforce(event, () => permissionRequirement(names))\n },\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/server.ts"],"names":["options"],"mappings":";;;AAoEO,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,IAAA,IAAA,CAAK,UAAA,GAAa,IAAA,CAAK,KAAA,KAAU,iBAAA,GAAoB,GAAA,GAAM,GAAA;AAC3D,IAAA,IAAA,CAAK,aA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;AAClF,MAAA,OAAO,QAAQ,KAAA,EAAO,MAAM,qBAAA,CAAsB,KAAA,EAAOA,QAAO,CAAC,CAAA;AAAA,IACnE,CAAA;AAAA,IACA,WAAA,CAAY,UAAU,IAAA,EAAM;AAC1B,MAAA,MAAM,EAAE,KAAA,EAAO,OAAA,EAAAA,QAAAA,EAAQ,GAAI,qBAA6C,IAAI,CAAA;AAC5E,MAAA,OAAO,QAAQ,KAAA,EAAO,MAAM,eAAA,CAAgB,KAAA,EAAOA,QAAO,CAAC,CAAA;AAAA,IAC7D,CAAA;AAAA,IACA,uBAAA,CAAwB,UAAU,KAAA,EAAO;AACvC,MAAA,OAAO,OAAA,CAAQ,KAAA,EAAO,MAAM,2BAAA,CAA4B,KAAK,CAAC,CAAA;AAAA,IAChE,CAAA;AAAA,IACA,MAAA,CAAO,UAAU,KAAA,EAAO;AACtB,MAAA,OAAO,OAAA,CAAQ,OAAO,MAAM,qBAAA,CAAsB,OAAO,EAAE,GAAA,EAAK,IAAA,EAAM,CAAC,CAAA;AAAA,IACzE,CAAA;AAAA,IACA,MAAA,CAAO,UAAU,KAAA,EAAO;AACtB,MAAA,OAAO,OAAA,CAAQ,KAAA,EAAO,MAAM,qBAAA,CAAsB,KAAK,CAAC,CAAA;AAAA,IAC1D;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 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 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 /** Like Express `requirePermission`: arguments are all required, pipes mean any, `{ any: true }`. */\n requirePermission<const A extends readonly (RequirementNames | PermissionRequirementOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, PermissionName<T>, PermissionRequirementOptions>\n ): Promise<SubjectRbac<T>>\n /** Like Express `requireRole`: any of the roles, `{ all: true }` for all. */\n requireRole<const A extends readonly (RequirementNames | RoleRequirementOptions)[]>(\n event: E,\n ...args: A & RequirementArgs<A, RoleName<T>, RoleRequirementOptions>\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[]>(\n event: E,\n ...names: A & RequirementArgs<A, PermissionName<T> | RoleName<T>, never>\n ): Promise<SubjectRbac<T>>\n /** spatie v8.5 `canAny()`: requires ANY of the listed permissions. */\n canAny<const A extends readonly RequirementNames[]>(\n event: E,\n ...names: A & RequirementArgs<A, PermissionName<T>, never>\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[]>(\n event: E,\n ...names: A & RequirementArgs<A, PermissionName<T>, never>\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 | PermissionRequirementOptions)[]): Promise<SubjectRbac>\n requireRole(event: E, ...args: (RequirementNames | RoleRequirementOptions)[]): Promise<SubjectRbac>\n requireRoleOrPermission(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>\n canAny(event: E, ...names: RequirementNames[]): Promise<SubjectRbac>\n canAll(event: E, ...names: RequirementNames[]): 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): Promise<SubjectRbac> {\n const requirement = build()\n const subject = await resolve(event)\n if (subject === null) throw new RbacHttpError({ error: 'UNAUTHENTICATED', message: UNAUTHENTICATED_MESSAGE })\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<PermissionRequirementOptions>(args)\n return enforce(event, () => permissionRequirement(names, options))\n },\n requireRole(event, ...args) {\n const { names, options } = splitRequirementArgs<RoleRequirementOptions>(args)\n return enforce(event, () => roleRequirement(names, options))\n },\n requireRoleOrPermission(event, ...names) {\n return enforce(event, () => roleOrPermissionRequirement(names))\n },\n canAny(event, ...names) {\n return enforce(event, () => permissionRequirement(names, { any: true }))\n },\n canAll(event, ...names) {\n return enforce(event, () => permissionRequirement(names))\n },\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anievo-id/rbac-evolution-nuxt",
3
- "version": "0.7.1",
3
+ "version": "0.8.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": "^0.7.1",
54
- "@anievo-id/rbac-evolution-vue": "^0.7.1"
53
+ "@anievo-id/rbac-evolution-vue": "^0.8.0",
54
+ "@anievo-id/rbac-evolution": "^0.8.0"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "nuxt": "^3.0.0 || ^4.0.0",