npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@alphinex/permissions

v1.1.0

Published

Ability/permission checking: <Can>, usePermission, policy resolution.

Readme

@alphinex/permissions

Ability/permission checking for the UI layer: a PermissionsProvider/usePermission context, a <Can> component for declarative conditional rendering, and a pluggable adapter model for turning whatever shape your backend returns (roles, ability rules, a flat string list, ...) into a uniform PermissionResolver. This package never decides how permissions are computed — that's the app's PermissionsAdapter, per documentation/ARCHITECTURE.md §3.3.

PermissionsProvider + usePermission

PermissionsProvider takes a resolver: PermissionResolver — a (action: string, subject: string, field?: string) => boolean function — and makes it available to the tree below. usePermission(action, subject, field?) reads it:

import {
  PermissionsProvider,
  usePermission,
  createListPermissionsAdapter,
} from "@alphinex/permissions";

const resolver = createListPermissionsAdapter(["view:Invoice", "edit:Invoice"]);

function App() {
  return (
    <PermissionsProvider resolver={resolver}>
      <InvoiceToolbar />
    </PermissionsProvider>
  );
}

function InvoiceToolbar() {
  const canEdit = usePermission("edit", "Invoice");
  return <button disabled={!canEdit}>Edit invoice</button>;
}

usePermission() throws if called outside a <PermissionsProvider>.

Can

A CASL-flavored declarative gate for JSX, built on usePermission() under the hood. I is the action, a is the subject, field is an optional field-level check, fallback renders when the check fails (defaults to nothing), and children may also be a render-prop function that receives the boolean directly:

import { Can } from "@alphinex/permissions";

function DeleteInvoiceButton() {
  return (
    <Can I="delete" a="Invoice" fallback={<p className="text-fg-muted text-xs">Admins only</p>}>
      <button className="text-danger">Delete invoice</button>
    </Can>
  );
}

function AmountField() {
  return (
    <Can I="edit" a="Invoice" field="amount">
      {(allowed) => <input readOnly={!allowed} />}
    </Can>
  );
}

createListPermissionsAdapter

A ready-made PermissionsAdapter<string[]> for the common case of a flat "action:subject" rule list — the shape most simple Laravel policy-to-JSON serializers produce (e.g. ["view:Invoice", "edit:Invoice", "*:Report"]). "*" acts as a wildcard for either the action or the subject (or both). Field-level checks always pass, since this source shape carries no field granularity — write a custom PermissionsAdapter if you need that:

import { createListPermissionsAdapter } from "@alphinex/permissions";

const resolver = createListPermissionsAdapter(["*:Invoice", "view:Report"]);
resolver("edit", "Invoice"); // true — matches the "*:Invoice" wildcard rule
resolver("edit", "Report"); // false

PermissionResolver / PermissionsAdapter<TSource>

The two types that make this package pluggable. A PermissionsAdapter<TSource> is (source: TSource) => PermissionResolver — write your own to map any backend shape (a roles array, CASL rules, a permissions object keyed by subject, ...) into a PermissionResolver, and hand the result to PermissionsProvider.

Composing with @alphinex/auth

The common pattern is to derive the resolver from the currently authenticated user, so permissions update automatically on login/logout/role change. Compute the rule list from useAuth()'s user, memoize it, and feed it through an adapter:

import { useAuth } from "@alphinex/auth";
import { PermissionsProvider, createListPermissionsAdapter } from "@alphinex/permissions";
import { useMemo, type ReactNode } from "react";

const ROLE_RULES: Record<string, string[]> = {
  admin: ["*:Invoice"],
  member: ["view:Invoice", "edit:Invoice"],
};

function PermissionsFromAuth({ children }: { children: ReactNode }) {
  const { user } = useAuth();
  const resolver = useMemo(
    () => createListPermissionsAdapter(user ? (ROLE_RULES[user.role] ?? []) : []),
    [user]
  );
  return <PermissionsProvider resolver={resolver}>{children}</PermissionsProvider>;
}

Mount PermissionsFromAuth inside <AuthProvider> (from @alphinex/auth) so useAuth() resolves, and outside anything that calls usePermission()/<Can>.

See documentation/ARCHITECTURE.md for the full package contract, dependency rules, and roadmap placement.