UNPKG

permix

Version:

Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.

94 lines (93 loc) 2.54 kB
import { PermixNotFoundError, createCheckContext, createHooks, createPermix as createPermix$1, createTemplate } from "../core/index.mjs"; import { createMiddleware } from "hono/factory"; //#region src/hono/permix.ts function keyToString(key) { return key; } function buildPermix(resolveKey, options = {}) { const onForbidden = options.onForbidden ?? (({ c }) => c.json({ error: "Forbidden" }, 403)); const hooks = createHooks(); function get(c) { return c.get(keyToString(resolveKey())) ?? null; } function getOrThrow(c) { const instance = get(c); if (!instance) throw new PermixNotFoundError(resolveKey()); return instance; } function setupMiddleware(callbackOrRules) { return createMiddleware(async (c, next) => { const instance = createPermix$1(typeof callbackOrRules === "function" ? await callbackOrRules({ c }) : callbackOrRules); instance.hook("check", (context) => hooks.callHook("check", context)); c.set(keyToString(resolveKey()), instance); await next(); }); } const checkMiddleware = (...args) => { return createMiddleware(async (c, next) => { const permix = get(c); if (!permix) throw new PermixNotFoundError(resolveKey()); if (!permix.check(...args)) return await onForbidden({ c, ...createCheckContext(...args) }); await next(); }); }; function getRules(c) { return get(c)?.getRules() ?? null; } function template(rules) { return createTemplate(rules); } return { setupMiddleware, checkMiddleware, template, get, getOrThrow, getRules, hook: hooks.hook, hookOnce: hooks.hookOnce, get key() { return resolveKey(); }, $inferDefinition: void 0, $inferPath: void 0 }; } /** * Create a middleware factory that wires Permix into Hono routes. * * Use `.contextKey('name')` to set a custom context key (defaults to a unique * `Symbol('permix')`). * * @example * ```ts * import { Hono } from 'hono' * import { createPermix } from 'permix/hono' * * const permix = createPermix<{ * post: ['create', 'read'] * }>() * * const app = new Hono() * app.use(permix.setupMiddleware(({ c }) => ({ * post: { create: true, read: true }, * }))) * * app.get('/posts', permix.checkMiddleware('post.read'), c => c.json({ ok: true })) * ``` * * @link https://permix.letstri.dev/docs/integrations/hono */ function createPermix(options = {}) { let key = Symbol("permix"); const permix = buildPermix(() => key, options); return Object.assign(permix, { contextKey(newKey) { key = newKey; return permix; } }); } //#endregion export { createPermix };