permix
Version:
Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.
100 lines (99 loc) • 2.53 kB
JavaScript
import { PermixNotFoundError, createCheckContext, createHooks, createPermix as createPermix$1, createTemplate } from "../core/index.mjs";
//#region src/express/permix.ts
function buildPermix(resolveKey, options = {}) {
const onForbidden = options.onForbidden ?? (({ res }) => {
res.status(403).json({ error: "Forbidden" });
});
const hooks = createHooks();
function get(req) {
return req[resolveKey()] ?? null;
}
function getOrThrow(req) {
const instance = get(req);
if (!instance) throw new PermixNotFoundError(resolveKey());
return instance;
}
function setupMiddleware(callbackOrRules) {
return async (req, res, next) => {
const instance = createPermix$1(typeof callbackOrRules === "function" ? await callbackOrRules({
req,
res,
next
}) : callbackOrRules);
instance.hook("check", (context) => hooks.callHook("check", context));
req[resolveKey()] = instance;
return next();
};
}
const checkMiddleware = (...args) => {
return async (req, res, next) => {
const permix = get(req);
if (!permix) return next(new PermixNotFoundError(resolveKey()));
if (!permix.check(...args)) return await onForbidden({
req,
res,
next,
...createCheckContext(...args)
});
return next();
};
};
function getRules(req) {
return get(req)?.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 Express routes.
*
* Use `.contextKey('name')` to set a custom request key (defaults to a unique
* `Symbol('permix')`).
*
* @example
* ```ts
* import express from 'express'
* import { createPermix } from 'permix/express'
*
* const permix = createPermix<{
* post: ['create', 'read']
* }>()
*
* const app = express()
* app.use(permix.setupMiddleware(({ req }) => ({
* post: { create: !!req.user, read: true },
* })))
*
* app.get('/posts', permix.checkMiddleware('post.read'), (req, res) => {
* res.json({ ok: true })
* })
* ```
*
* @link https://permix.letstri.dev/docs/integrations/express
*/
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 };