Skip to content
/ guantr Public

Flexible, type-safe JavaScript library for efficient authorization and permission checking

License

Notifications You must be signed in to change notification settings

Hrdtr/guantr

Repository files navigation

Guantr (WIP)

npm version npm downloads

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.

Usage

Install package:

# ✨ Auto-detect
npx nypm install guantr

# npm
npm install guantr

# yarn
yarn add guantr

# pnpm
pnpm install guantr

# bun
bun install guantr

# deno
deno install guantr

Import:

ESM (Node.js, Bun, Deno)

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

Development

local development
  • Clone this repository
  • Install latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Published under the MIT license. Made by community 💛


🤖 auto updated with automd

About

Flexible, type-safe JavaScript library for efficient authorization and permission checking

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •