permix
Version:
Permix is a lightweight, framework-agnostic, type-safe permissions management library for JavaScript applications on the client and server sides.
47 lines (44 loc) • 1.16 kB
JavaScript
import { c as createTemplate, a as createPermix$1 } from '../shared/permix.Q-O041RP.mjs';
import { p as pick, c as createCheckContext } from '../shared/permix.q13nxITM.mjs';
/**
* Create a middleware function that checks permissions for Elysia routes.
*
* @link https://permix.letstri.dev/docs/integrations/elysia
*/
function createPermix({
onForbidden = ({
context
}) => {
context.set.status = 403;
return {
error: 'Forbidden'
};
}
} = {}) {
const derive = rules => ({
permix: pick(createPermix$1(rules), ['check', 'dehydrate'])
});
const checkHandler = (...params) => {
return async context => {
if (!context.permix) {
throw new Error('[Permix]: Instance not found. Please use the `setupMiddleware` function.');
}
const hasPermission = context.permix.check(...params);
if (!hasPermission) {
return onForbidden({
context,
...createCheckContext(...params)
});
}
};
};
function template(...params) {
return createTemplate(...params);
}
return {
derive,
checkHandler,
template
};
}
export { createPermix };