@foolishandi_tech/utils
v0.1.4
Published
Utility functions for One Tech project
Maintainers
Readme
@foolishandi_tech/utils
Utility functions for One Tech project.
Installation
npm install @foolishandi_tech/utils
# or
yarn add @foolishandi_tech/utils
# or
pnpm add @foolishandi_tech/utilsUsage
Platform Detection
import { getPlatform, isPC, isMobile, Platform } from '@foolishandi_tech/utils';
// or
import { getPlatform } from '@foolishandi_tech/utils/platform';
// Get platform info
const platform = await getPlatform();
// Check platform type
const isDesktop = await isPC();
const isMobileDevice = await isMobile();
// Check specific platform
const isWindows = await Platform.isWindows();
const isMacOS = await Platform.isMacOS();Error Handling
import {
createErrorInfo,
formatErrorMessage,
isNetworkError,
logError,
retryWithBackoff,
} from '@foolishandi_tech/utils';
// or
import { createErrorInfo } from '@foolishandi_tech/utils/error';
// Create error info
const errorInfo = createErrorInfo(error, {
componentStack: '...',
errorBoundary: 'ErrorBoundary',
});
// Check error type
if (isNetworkError(error)) {
// Handle network error
}
// Retry with backoff
const result = await retryWithBackoff(
async () => {
return await fetchData();
},
{
maxRetries: 3,
initialDelay: 1000,
shouldRetry: (error) => isNetworkError(error),
}
);API
Platform Utilities
getPlatform(): Promise<PlatformInfo>- Get platform informationisPC(): Promise<boolean>- Check if platform is PCisMobile(): Promise<boolean>- Check if platform is mobileisDesktop(): Promise<boolean>- Check if platform is desktopgetPlatformName(): Promise<Platform>- Get platform nameclearPlatformCache(): void- Clear platform cachePlatform.isWindows()- Check if WindowsPlatform.isMacOS()- Check if macOSPlatform.isLinux()- Check if LinuxPlatform.isIOS()- Check if iOSPlatform.isAndroid()- Check if Android
Error Utilities
createErrorInfo(error, errorInfo?)- Create error info objectformatErrorMessage(error)- Format error messageisNetworkError(error)- Check if network errorisTimeoutError(error)- Check if timeout errorisAuthError(error)- Check if authentication errorisPermissionError(error)- Check if permission errorgetErrorCode(error)- Get error codelogError(error, context?)- Log error with contextcreateErrorHandler(handler, options?)- Create error handlerretryWithBackoff(fn, options?)- Retry function with exponential backoff
Development
# Build
npm run build
# Watch mode
npm run dev
# Clean
npm run clean