1.0.11 • Published 5 months ago

@a5it/permission-types v1.0.11

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

🛡️ @a5it/permission-types

A shared TypeScript-only package that defines and exports strongly-typed permission constants and utility types for use across A5IT's applications (reseller platform, customer organization portal, internal tools, etc.).

📦 Installation

npm install @a5it/permission-types

🚀 Usage

import {
  RESELLER_PERMISSIONS,
  ResellerPermissionKeys,
  ResellerPermissionValues,
} from "@a5it/permission-types";

📚 API Documentation

Reseller Permission Utilities

This package provides a collection of utility functions designed to handle RESELLER_PERMISSIONS logic, including group-wise lookups, flat lists, schema generation, and more.

1. List All Permissions Per Group

Returns a list of permission strings for a given group.

function getPermissionListByGroup<K extends ResellerPermissionKeys>(
  group: K
): `${K}.${ResellerPermissionValues<K>}`[] {
  return Object.keys(RESELLER_PERMISSIONS[group]).map(
    (action) => `${group}.${action}` as `${K}.${ResellerPermissionValues<K>}`
  );
}

// Usage
const teamPerms = getPermissionListByGroup("TEAMS");
// → ["TEAMS.ALL", "TEAMS.VIEW", ...]

2. Create a Flat List of All Reseller Permissions

Useful for dropdowns, seeding roles, or exporting all permissions.

function getAllResellerPermissions(): ResellerPermission[] {
  return Object.values(RESELLER_PERMISSIONS).flatMap((group) =>
    Object.values(group)
  );
}

3. Check If a Permission Belongs to a Group

function getPermissionGroup(
  permission: ResellerPermission
): ResellerPermissionKeys | undefined {
  const [group] = permission.split(".") as [ResellerPermissionKeys, string];
  return group in RESELLER_PERMISSIONS ? group : undefined;
}

4. Build a Map of Groups → Permissions (for form UI)

function groupPermissionsByModule(): Record<
  ResellerPermissionKeys,
  ResellerPermission[]
> {
  const map = {} as Record<ResellerPermissionKeys, ResellerPermission[]>;
  for (const key in RESELLER_PERMISSIONS) {
    const group = key as ResellerPermissionKeys;
    map[group] = Object.values(RESELLER_PERMISSIONS[group]);
  }
  return map;
}

5. Build a Role Editor Schema

Generate a permission schema for a role creation/edit form:

type RolePermissionOption = {
  label: string;
  value: ResellerPermission;
  group: ResellerPermissionKeys;
};

function getRolePermissionOptions(): RolePermissionOption[] {
  return Object.entries(RESELLER_PERMISSIONS).flatMap(([group, perms]) =>
    Object.entries(perms).map(([key, value]) => ({
      label: `${group} → ${key}`,
      value: value as ResellerPermission,
      group: group as ResellerPermissionKeys,
    }))
  );
}

6. Generate a Zod Schema for Permissions Array

import { z } from "zod";

const resellerPermissionSchema = z.enum(
  getAllResellerPermissions() as [ResellerPermission, ...ResellerPermission[]]
);

const resellerRoleInputSchema = z.object({
  name: z.string(),
  permissions: z.array(resellerPermissionSchema),
});

7. Find Missing Permissions (for auditing)

function findMissingPermissions(
  granted: ResellerPermission[]
): ResellerPermission[] {
  const all = getAllResellerPermissions();
  return all.filter((p) => !granted.includes(p));
}

8. List Action Keys of Resource

function getActionKeys<K extends ResellerPermissionKeys>(
  group: K
): ResellerPermissionValues<K>[] {
  return Object.keys(RESELLER_PERMISSIONS[group]) as ResellerPermissionValues<K>[];
}

// Usage
getActionKeys("ORDERS"); // ["VIEW", "UPDATE"]

🔧 Development

# Install dependencies
npm install

# Build the package
npm run build

# Clean build artifacts
npm run clean

📝 License

ISC © A5IT

1.0.11

5 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago