fix: Generic utilities (isRecord, coerceString, getErrorMessage, ensureAddress) have no shared home (#363)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
openhands 2026-03-03 05:37:14 +00:00
parent e53591de9d
commit 59ae30bb37
6 changed files with 63 additions and 35 deletions

View file

@ -0,0 +1,11 @@
{
"name": "@harb/utils",
"version": "0.1.0",
"private": true,
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",
"dependencies": {
"viem": "^2.22.13"
}
}

View file

@ -0,0 +1,34 @@
import { getAddress, isAddress, type Address } from 'viem';
export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
export function coerceString(value: unknown): string | null {
if (typeof value === 'string') {
const trimmed = value.trim();
if (trimmed.length > 0) return trimmed;
}
return null;
}
export function getErrorMessage(error: unknown, fallback: string): string {
if (error instanceof Error) {
const msg = coerceString(error.message);
if (msg) return msg;
}
if (isRecord(error)) {
const short = coerceString(error.shortMessage);
if (short) return short;
const msg = coerceString(error.message);
if (msg) return msg;
}
return fallback;
}
export function ensureAddress(value: string, label: string): Address {
if (!value || !isAddress(value)) {
throw new Error(`${label} is not a valid address`);
}
return getAddress(value);
}

View file

@ -6,6 +6,7 @@
"main": "src/index.ts",
"types": "src/index.ts",
"dependencies": {
"@harb/utils": "*",
"@wagmi/vue": "^0.2.8",
"viem": "^2.22.13"
},