@accelerate/authorization-react 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ import { type ReactNode } from "react";
2
+ export interface AuthorizationSnapshot {
3
+ userId: string;
4
+ roles: readonly string[];
5
+ permissions: readonly string[];
6
+ }
7
+ export interface AuthorizationClient {
8
+ load(): Promise<AuthorizationSnapshot>;
9
+ }
10
+ type AuthorizationState = {
11
+ status: "idle" | "loading" | "ready" | "error";
12
+ userId: string | null;
13
+ permissions: readonly string[];
14
+ roles: readonly string[];
15
+ };
16
+ type AuthorizationContextValue = AuthorizationState & {
17
+ can(permission: string): boolean;
18
+ canAny(permissions: readonly string[]): boolean;
19
+ canAll(permissions: readonly string[]): boolean;
20
+ refresh(): Promise<void>;
21
+ reset(): void;
22
+ };
23
+ export declare function AuthorizationProvider({ client, children }: {
24
+ client: AuthorizationClient;
25
+ children: ReactNode;
26
+ }): import("react").JSX.Element;
27
+ export declare function useAuthorization(): AuthorizationContextValue;
28
+ export declare function usePermission(permission: string): boolean;
29
+ export declare function Can({ permission, anyOf, allOf, children, fallback }: {
30
+ permission?: string;
31
+ anyOf?: readonly string[];
32
+ allOf?: readonly string[];
33
+ children: ReactNode;
34
+ fallback?: ReactNode;
35
+ }): import("react").JSX.Element;
36
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
3
+ const empty = { status: "idle", userId: null, permissions: [], roles: [] };
4
+ const Context = createContext(null);
5
+ export function AuthorizationProvider({ client, children }) {
6
+ const [state, setState] = useState(empty);
7
+ const generation = useRef(0);
8
+ const refresh = useCallback(async () => {
9
+ const request = ++generation.current;
10
+ setState({ ...empty, status: "loading" });
11
+ try {
12
+ const snapshot = await client.load();
13
+ if (request !== generation.current)
14
+ return;
15
+ if (!snapshot || typeof snapshot.userId !== "string" || !Array.isArray(snapshot.roles) || !Array.isArray(snapshot.permissions)) {
16
+ throw new Error("Invalid authorization snapshot");
17
+ }
18
+ setState({ status: "ready", userId: snapshot.userId, permissions: [...snapshot.permissions], roles: [...snapshot.roles] });
19
+ }
20
+ catch (error) {
21
+ if (request !== generation.current)
22
+ return;
23
+ setState({ ...empty, status: "error" });
24
+ throw error;
25
+ }
26
+ }, [client]);
27
+ const reset = useCallback(() => {
28
+ ++generation.current;
29
+ setState(empty);
30
+ }, []);
31
+ useEffect(() => {
32
+ void refresh().catch(() => undefined);
33
+ return () => { ++generation.current; };
34
+ }, [refresh]);
35
+ const value = useMemo(() => {
36
+ const can = (permission) => state.status === "ready" && state.permissions.includes(permission);
37
+ return { ...state, can, canAny: ps => ps.some(can), canAll: ps => ps.every(can), refresh, reset };
38
+ }, [state, refresh, reset]);
39
+ return _jsx(Context.Provider, { value: value, children: children });
40
+ }
41
+ export function useAuthorization() {
42
+ const value = useContext(Context);
43
+ if (!value)
44
+ throw new Error("AuthorizationProvider is missing");
45
+ return value;
46
+ }
47
+ export function usePermission(permission) { return useAuthorization().can(permission); }
48
+ export function Can({ permission, anyOf, allOf, children, fallback = null }) {
49
+ const auth = useAuthorization();
50
+ const allowed = permission ? auth.can(permission) : anyOf ? auth.canAny(anyOf) : allOf ? auth.canAll(allOf) : false;
51
+ return _jsx(_Fragment, { children: allowed ? children : fallback });
52
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@accelerate/authorization-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "peerDependencies": {
18
+ "react": ">=18"
19
+ },
20
+ "devDependencies": {
21
+ "@types/react": "^18.3.18",
22
+ "react": "^18.3.1"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc -p tsconfig.json",
26
+ "typecheck": "tsc -p tsconfig.json --noEmit"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/archubbuck/devkit.git",
31
+ "directory": "packages/authorization-react"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public",
35
+ "registry": "https://registry.npmjs.org/"
36
+ }
37
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.