@openzeppelin/ui-utils 1.0.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.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # @openzeppelin/ui-utils
2
+
3
+ Shared, framework-agnostic utility functions for the OpenZeppelin UI ecosystem.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@openzeppelin/ui-utils.svg)](https://www.npmjs.com/package/@openzeppelin/ui-utils)
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ # Using npm
11
+ npm install @openzeppelin/ui-utils
12
+
13
+ # Using yarn
14
+ yarn add @openzeppelin/ui-utils
15
+
16
+ # Using pnpm
17
+ pnpm add @openzeppelin/ui-utils
18
+ ```
19
+
20
+ ## Purpose
21
+
22
+ This package centralizes common logic that is not tied to any specific blockchain adapter or UI framework (like React). This prevents code duplication and ensures that core functionalities like logging, configuration management, and ID generation are consistent everywhere.
23
+
24
+ ## Key Exports
25
+
26
+ - **`AppConfigService`**: A singleton service responsible for loading and providing runtime configuration. It can load settings from Vite environment variables or a `public/app.config.json` file, allowing for flexible configuration of RPC URLs, API keys, indexer endpoints, and other parameters.
27
+ - **RPC Endpoints**: Override default RPC URLs per network via `getRpcEndpointOverride(networkId)`.
28
+ - **Indexer Endpoints**: Override default indexer URLs per network via `getIndexerEndpointOverride(networkId)`.
29
+ - **API Keys**: Retrieve explorer API keys via `getExplorerApiKey(serviceId)`.
30
+ - **Global Services**: Access global service parameters like WalletConnect project IDs.
31
+ - **Feature Flags**: Check feature enablement via `isFeatureEnabled(flagName)`.
32
+
33
+ - **`logger`**: A pre-configured singleton logger for consistent, leveled logging across all packages. It can be enabled, disabled, or have its level changed globally.
34
+
35
+ - **`generateId`**: A utility for generating unique IDs, used for form fields and other components.
36
+
37
+ - **`cn`**: A utility (a wrapper around `clsx` and `tailwind-merge`) for conditionally joining CSS class names, essential for building dynamic and themeable UI components with Tailwind CSS.
38
+
39
+ - **Access Control Utilities** (`./src/access/`): Chain-agnostic utilities for access control operations:
40
+ - **`validateSnapshot`**: Validates the structure of an `AccessSnapshot` object
41
+ - **`serializeSnapshot`**: Serializes an access snapshot to JSON string
42
+ - **`deserializeSnapshot`**: Deserializes a JSON string to an access snapshot
43
+ - **`createEmptySnapshot`**: Creates an empty snapshot with no roles and no ownership
44
+ - **`findRoleAssignment`**: Finds a role assignment by role ID
45
+ - **`compareSnapshots`**: Compares two snapshots and returns differences
46
+ - **`isAccessControlError`**: Type guard to check if an error is an AccessControlError
47
+
48
+ - **Address Normalization** (`normalizeAddress`, `addressesEqual`): Utilities for normalizing and comparing addresses in a chain-agnostic way.
49
+
50
+ - **Type Guards and Helpers**: Various other small, reusable functions like `getDefaultValueForType`.
51
+
52
+ ## Package Structure
53
+
54
+ ```text
55
+ utils/
56
+ ├── src/
57
+ │ ├── access/ # Access control utilities (snapshot, errors)
58
+ │ ├── config/ # Configuration management
59
+ │ ├── logger/ # Logging utilities
60
+ │ ├── ui/ # UI utility functions
61
+ │ ├── validation/ # Validation and type utilities
62
+ │ ├── constants/ # Shared constants
63
+ │ └── index.ts # Main package exports
64
+ ├── package.json
65
+ ├── tsconfig.json
66
+ ├── tsdown.config.ts
67
+ ├── vitest.config.ts
68
+ └── README.md
69
+ ```
70
+
71
+ ## Usage
72
+
73
+ ```typescript
74
+ import { AppConfigService, cn, generateId, logger } from '@openzeppelin/ui-utils';
75
+
76
+ // Class name utility
77
+ const buttonClasses = cn('px-4 py-2 rounded', isActive && 'bg-blue-500', disabled && 'opacity-50');
78
+
79
+ // Logging
80
+ logger.info('MyComponent', 'Component initialized');
81
+ logger.error('MyComponent', 'An error occurred', error);
82
+
83
+ // ID generation
84
+ const fieldId = generateId('field_');
85
+
86
+ // Configuration
87
+ const appConfig = AppConfigService.getInstance();
88
+ const rpcOverride = appConfig.getRpcEndpointOverride('ethereum-mainnet');
89
+ ```
90
+
91
+ ## Development
92
+
93
+ ```bash
94
+ # Build the package
95
+ pnpm build
96
+
97
+ # Run tests
98
+ pnpm test
99
+
100
+ # Lint
101
+ pnpm lint
102
+ ```
103
+
104
+ ## License
105
+
106
+ [AGPL-3.0](https://github.com/OpenZeppelin/openzeppelin-ui/blob/main/LICENSE)