Stop reinventing error handling and logging. Get type-safe, battle-tested utilities that work everywhere.
import utils from '@simwai/utils'
// Beautiful logging with colors and timestamps
utils.logger.log('Application started')
utils.logger.error('Something went wrong')
// Resilient operations with automatic retries
const result = await utils.retry.execute(async () => {
const response = await fetch('/api/data')
if (!response.ok) throw new Error('API failed')
return response.json()
})
if (result.isOk()) {
console.log('Success:', result.value)
} else {
console.log('Failed after retries:', result.error)
}β Manual retry logic scattered across your codebase
β Inconsistent error handling for async operations
β Plain console.log lacks context, colors, and timestamps
β Environment-specific bugs between Node.js and browser code
β Standardized utilities with exponential backoff and type safety
β Universal compatibility - one import works everywhere
β Beautiful output with Dracula-inspired colors
β Zero configuration - works perfectly out of the box
npm install @simwai/utilsimport { ConsoleLogger } from '@simwai/utils'
const logger = new ConsoleLogger({ isTimeEnabled: true })
logger.log('Info message') // Light gray with timestamp
logger.warn('Warning') // Yellow
logger.error('Error occurred') // Redimport { FileLogger } from '@simwai/utils'
const fileLogger = new FileLogger({ logFilePath: './logs/app.log' })
fileLogger.error('Persistent error logging')import { Retry } from '@simwai/utils'
const retry = new Retry({
timeout: 1000, // Start with 1 second
retries: 3, // Try 3 times
isExponential: true // 1s β 2s β 4s
})
// Type-safe results with neverthrow
const result = await retry.execute(async () => {
// Your potentially failing operation
return await riskyApiCall()
})Supported Environments:
- β Node.js 18+
- β Modern browsers (Chrome 80+, Firefox 72+, Safari 13+, Edge 80+)
- β ES2020+ environments
- β Internet Explorer (uses modern timer APIs)
Automatic Environment Detection:
- Node.js: Uses native timers, includes FileLogger (~44KB + external deps)
- Browser: Uses setTimeout, excludes FileLogger (~330KB self-contained)
interface ConsoleLoggerOptions {
isTimeEnabled?: boolean // Show timestamps (default: false)
}
const logger = new ConsoleLogger({ isTimeEnabled: true })
logger.log('message') // Info level
logger.warn('message') // Warning level
logger.error('message') // Error level
logger.trace('message') // Debug levelinterface RetryOptions {
timeout?: number // Delay between retries (default: 125ms)
retries?: number // Max retry attempts (default: 4)
isExponential?: boolean // Use exponential backoff (default: true)
}
const retry = new Retry(options)
const result = await retry.execute(operation, overrideOptions?)// Results use neverthrow's Result type
if (result.isOk()) {
// TypeScript knows this is your success type
console.log(result.value)
} else {
// TypeScript knows this is an Error
console.error(result.error.message)
}Consistent, beautiful colors across all environments:
- π€ Log:
#f8f8f2(Light foreground) - π‘ Warn:
#f1fa8c(Yellow) - π΄ Error:
#ff5555(Red) - π£ Trace:
#bd93f9(Purple)
| Environment | Size | Dependencies | Justification |
|---|---|---|---|
| Node.js | ~44KB | External | Optimal - deps installed separately |
| Browser | ~330KB | Bundled | Includes Luxon (dates) + Chalk (colors) |
Why 330KB for browser?
- Luxon (~200KB): Full-featured DateTime with timezone support
- Chalk (~30KB): Cross-platform color support
- Alternative: Use basic
console.logif bundle size is critical
- Building libraries that work in Node.js AND browsers
- Need consistent retry logic across projects
- Want type-safe error handling
- Prefer beautiful, structured logging
- Bundle size is absolutely critical (<50KB total)
- Only need basic
console.logfunctionality - Working in a single environment only
- Have existing logging infrastructure
ποΈ Advanced: Cross-Platform Architecture
@simwai/utils uses unbuild to create environment-specific builds:
build/
βββ node/ # Node.js optimized
β βββ index.cjs # CommonJS format
β βββ index.mjs # ESM format
β βββ index.d.ts # TypeScript definitions
βββ browser/ # Browser optimized
βββ index.mjs # ESM only
βββ index.d.ts # Browser-specific types
Package.json Conditional Exports:
{
"exports": {
".": {
"browser": "./build/browser/index.mjs",
"node": {
"require": "./build/node/index.cjs",
"import": "./build/node/index.mjs"
}
}
}
}Runtime Detection:
- Uses
node:timers/promisesin Node.js for optimal performance - Falls back to
setTimeoutin browsers automatically - FileLogger completely excluded from browser builds
π§ Development Stack
Built with modern tooling for quality and maintainability:
- π¨ XO: Strict linting with Prettier integration
- π¦ unbuild: Cross-platform bundling with Rollup
- π· TypeScript: Full type safety and modern features
- π§ͺ Ava: Fast test runner with TypeScript support
- π neverthrow: Functional error handling without exceptions
Ready to eliminate boilerplate and build more reliable applications?
npm install @simwai/utilsBuilt with β€οΈ for developers who value reliability and great developer experience.