Git Product home page Git Product logo

guantr's Introduction

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

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

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

guantr's People

Contributors

hrdtr avatar renovate[bot] avatar

Stargazers

 avatar Asapdotid avatar

Watchers

 avatar

guantr's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/autofix.yml
  • actions/checkout v4
  • actions/setup-node v4
  • autofix-ci/action ff86a557419858bb967097bfc916833f5647fa8c
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4
.github/workflows/deploy.yml
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/configure-pages v5
  • actions/upload-pages-artifact v3
  • actions/deploy-pages v4
npm
package.json
  • @commitlint/config-conventional ^19.2.2
  • @commitlint/types ^19.0.3
  • @prisma/client ^5.17.0
  • @types/node ^20.14.13
  • @vitest/coverage-v8 ^2.0.5
  • automd ^0.3.8
  • changelogen ^0.5.5
  • commitlint ^19.3.0
  • eslint ^9.8.0
  • eslint-config-unjs ^0.3.2
  • husky ^9.1.4
  • jiti ^1.21.6
  • lint-staged ^15.2.7
  • typescript ^5.5.4
  • unbuild ^2.0.0
  • vitepress ^1.3.1
  • vitest ^2.0.5
  • @prisma/client ^5.0.0
  • pnpm 9.6.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.