Skip to content

Commit ec204ce

Browse files
committed
refactor(assert): consolidate browser-safe assert in browser-utils repo-wide
Addresses the re-review's two weak concerns (assert placement + incomplete rollout). Moves the shared assert helper from libs/ui-components (a leaf UI lib that other packages can't import without a dependency-direction inversion) into libs/browser-utils (@tangle-network/browser-utils), the canonical browser-safe utils package already imported across the monorepo. Migrates the remaining 9 files off Node's `assert` built-in too — repo-wide there are now zero `import assert from 'assert'` statements: apps/tangle-dapp: chainToNetwork, enumValueToNumber, useContractReadOnce libs/tangle-shared-ui: useWallet, useSubstrateTx, useEvmTxRelayer, useSubstrateStakingAssets libs/api-provider-environment: ConnectWallet libs/web3-api-provider: webb-provider Helper signature widened to `assert(condition, message?)` to cover the 1-arg `assert(condition)` call sites Node supports. `asserts condition` signature preserves type-narrowing at every call site. Gates: typecheck, lint, 197 tests, build dapp+cloud all green.
1 parent 84171e5 commit ec204ce

20 files changed

Lines changed: 36 additions & 33 deletions

File tree

apps/tangle-dapp/src/data/evm/useContractReadOnce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ensureError from '@tangle-network/tangle-shared-ui/utils/ensureError';
2-
import assert from 'assert';
2+
import { assert } from '@tangle-network/browser-utils';
33
import { useCallback } from 'react';
44
import {
55
Abi as ViemAbi,

apps/tangle-dapp/src/utils/chainToNetwork.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
TANGLE_MAINNET_NETWORK,
1111
TANGLE_TESTNET_NATIVE_NETWORK,
1212
} from '@tangle-network/ui-components/constants/networks';
13-
import assert from 'assert';
13+
import { assert } from '@tangle-network/browser-utils';
1414

1515
const chainToNetworkMap = {
1616
[PresetTypedChainId.TangleLocalEVM]: TANGLE_LOCAL_DEV_NETWORK,

apps/tangle-dapp/src/utils/enumValueToNumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from 'assert';
1+
import { assert } from '@tangle-network/browser-utils';
22

33
const enumValueToNumber = <T>(enumValue: T): number => {
44
assert(

libs/api-provider-environment/src/ConnectWallet/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
WebbErrorCodes,
1515
type WalletId,
1616
} from '@tangle-network/dapp-types';
17-
import assert from 'assert';
17+
import { assert } from '@tangle-network/browser-utils';
1818
import { useObservableState } from 'observable-hooks';
1919
import { useCallback, useEffect, useMemo } from 'react';
2020
import { useWebContext } from '../webb-context/webb-context';

libs/browser-utils/src/assert.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Browser-safe assertion. Throws a plain `Error` (not Node's `AssertionError`)
3+
* so callers introduce no Node-built-in dependency and stay bundle-clean for
4+
* browser targets. Carries a TypeScript `asserts condition` signature so call
5+
* sites retain type-narrowing, matching the ergonomics of Node's `assert`.
6+
*
7+
* @param condition - The condition to check.
8+
* @param message - Optional error message used when `condition` is falsy.
9+
* @throws {Error} when `condition` is falsy.
10+
*/
11+
export function assert(
12+
condition: unknown,
13+
message?: string,
14+
): asserts condition {
15+
if (!condition) {
16+
throw new Error(message ?? 'Assertion failed');
17+
}
18+
}

libs/browser-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { assert } from './assert';
12
export { default as isBrowser } from './isBrowser';
23
export * from './logger';
34
export * from './platform';

libs/tangle-shared-ui/src/data/staking/useSubstrateStakingAssets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TANGLE_TOKEN_DECIMALS } from '@tangle-network/dapp-config';
22
import { isEvmAddress } from '@tangle-network/ui-components';
33
import { BN } from '@polkadot/util';
4-
import assert from 'assert';
4+
import { assert } from '@tangle-network/browser-utils';
55
import { useCallback, useMemo } from 'react';
66
import { map } from 'rxjs';
77
import { NATIVE_ASSET_ID } from '../../constants/staking';

libs/tangle-shared-ui/src/hooks/useEvmTxRelayer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '../constants/evmPrecompiles';
1616
import { EvmAddress } from '@tangle-network/ui-components/types/address';
1717
import { assertEvmAddress } from '@tangle-network/ui-components';
18-
import assert from 'assert';
18+
import { assert } from '@tangle-network/browser-utils';
1919
import useViemWalletClient, {
2020
WalletClientTransport,
2121
} from './useViemWalletClient';

libs/tangle-shared-ui/src/hooks/useSubstrateTx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ensureError from '../utils/ensureError';
1111
import { getApiPromise } from '../utils/polkadot/api';
1212
import useIsMountedRef from '@tangle-network/ui-components/hooks/useIsMountedRef';
1313
import type { SubstrateAddress } from '@tangle-network/ui-components/types/address';
14-
import assert from 'assert';
14+
import { assert } from '@tangle-network/browser-utils';
1515
import { useCallback, useEffect, useState } from 'react';
1616
import { Hash } from 'viem';
1717
import useTxHistoryStore, {

libs/tangle-shared-ui/src/hooks/useWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import ensureError from '../utils/ensureError';
1818
import { EIP1193Provider } from 'viem';
1919
import { EthereumProvider } from '@walletconnect/ethereum-provider';
2020
import useEvmChain from './useEvmChain';
21-
import assert from 'assert';
21+
import { assert } from '@tangle-network/browser-utils';
2222

2323
const LAST_CONNECTED_ACCOUNT_KEY = 'lastConnectedAccountAddress';
2424

0 commit comments

Comments
 (0)