Flexible, type-safe JavaScript library for efficient authorization and permission checking. Easily manage permissions, and context-aware access control with minimal overhead and a simple API.
Install package:
# โจ Auto-detect
npx nypm install guantr
# npm
npm install guantr
# yarn
yarn add guantr
# pnpm
pnpm install guantr
# bun
bun install guantr
Import:
ESM (Node.js, Bun)
import { createGuantr } from "guantr";
CommonJS (Legacy Node.js)
const { createGuantr } = require("guantr");
CDN (Deno, Bun and Browsers)
import { createGuantr } from "https://esm.sh/guantr";
Initialize:
const guantr = createGuantr()
// With Typescript Meta:
type Meta = GuantrMeta<{
post: {
action: 'create' | 'read' | 'update' | 'delete'
model: {
id: number,
title: string,
published: boolean
}
}
}>;
const guantr = createGuantr<Meta>()
// Contextual
const guantrWithContext = guantr.withContext({
id: number,
name: 'John Doe',
roles: ['admin']
})
Setting permissions:
guantr.setPermission((can, cannot) => {
can('read', 'post')
cannot('read', ['post', { published: ['equals', false] }])
})
// Or
guantr.setPermissions([
{
resource: 'post',
action: 'read',
condition: null,
inverted: false
},
{
resource: 'post',
action: 'read',
condition: {
published: ['equals', false]
},
inverted: true
}
])
Checking Permission:
guantr.can('read', 'post') // true
guantr.can('read', ['post', { id: 1, title: 'Hello World', published: false }]) // false
local development
Published under the MIT license.
Made by community ๐
๐ค auto updated with automd