0.0.3 • Published 7 months ago

@bonny-kato/r3-utils v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

r3-utils

A library of reusable hooks, utilities, and components for React Router v7.

Installation

# Using npm
npm install @bonny-kato/r3-utils

# Using yarn
yarn add @bonny-kato/r3-utils

# Using pnpm
pnpm add @bonny-kato/r3-utils

Make sure you have the required peer dependencies installed:

pnpm add react react-dom react-router zod dayjs @bonny-kato/localstorage @bonnykato/simple-db

Features

  • 🪝 Hooks: Custom React hooks for working with React Router and common web application patterns
  • 🛠️ Utilities: Helper functions for common tasks like error handling, data validation, and query parameter manipulation
  • 🧩 Components: Reusable React components that extend React Router functionality
  • 📦 Submodule Imports: Import only what you need from specific submodules

Submodule Imports

You can import directly from submodules to improve code organization and potentially reduce bundle size:

// Import from specific submodules
import { CacheClient, CacheProvider } from "@bonny-kato/r3-utils/cache";
import { useBreadcrumb, useDebounce } from "@bonny-kato/r3-utils/hooks";
import { removeNullish, parseError } from "@bonny-kato/r3-utils/utils";

Available submodules:

  • @bonny-kato/r3-utils/auth
  • @bonny-kato/r3-utils/cache
  • @bonny-kato/r3-utils/access-control
  • @bonny-kato/r3-utils/hooks
  • @bonny-kato/r3-utils/http-client
  • @bonny-kato/r3-utils/utils
  • @bonny-kato/r3-utils/zod-common

For more details, see SUBMODULE_IMPORTS.md.

Available Utilities, Hooks, and Components

Below is a comprehensive list of all available utilities, hooks, and components organized by submodule.

Hooks (@bonny-kato/r3-utils/hooks)

HookDescription
useEventListenerHook for adding event listeners with automatic cleanup
useBreadcrumbGenerates breadcrumb navigation based on the current route path
useDebounceDebounces a value, useful for search inputs and other scenarios
useIsPathActiveDetermines if a given path is active in the current route
useHandleSelectAllItemsManages selection of all items in a list
useHandleSelectItemManages selection of individual items in a list
useLocationStateProvides access to location state with type safety
useNavigateWithQueryParamsNavigation hook that preserves query parameters
useScrollManages scroll position and provides scroll-related utilities
useSearchParamsStateProvides a state-like interface for URL search parameters
useSubmitDataSimplifies form submission with loading and error states
useUploadFileHandles file uploads with progress tracking

Components:

  • LinkWithQueryParams: A Link component that preserves query parameters

Utils (@bonny-kato/r3-utils/utils)

UtilityDescription
fakeNetworkSimulates network latency for testing and development
createSearchParamsCreates a search params string from an object
parseSearchParamsParses a search params string into an object
safeRedirectSafely redirects to a URL, preventing open redirects
parseErrorParses error objects into a standardized format
getDurationFromNowCalculates duration between now and a given date
conditionallyAddToArrayAdds items to an array based on conditions
removeNullishRemoves nullish values from objects and arrays recursively
formatAmountFormats numeric amounts with currency symbols and separators
createEnvSchemaCreates a schema for environment variables
validateEnvValidates environment variables against a schema

Auth (@bonny-kato/r3-utils/auth)

UtilityDescription
AuthAuthentication utility class for managing user sessions

Cache (@bonny-kato/r3-utils/cache)

UtilityDescription
CacheClientClient for caching data with various storage adapters
MemoryAdapterIn-memory cache adapter
LocalStorageAdapterLocalStorage-based cache adapter

Components:

  • CacheProvider: Provider component for the cache context
  • useCacheContext: Hook for accessing the cache context

Access Control (@bonny-kato/r3-utils/access-control)

UtilityDescription
AccessControlComponent for controlling access to UI elements
AccessControlProviderProvider component for access control context
useHasAccessHook for checking if a user has access to a resource
requireAccessUtility for requiring access to a resource
generateMenuAccessControlGenerates access control for menu items
generateUserAccessControlConfigGenerates access control configuration for users

HTTP Client (@bonny-kato/r3-utils/http-client)

UtilityDescription
HttpClientHTTP client for making API requests
tryCatchHttpUtility for handling HTTP errors

Zod Common (@bonny-kato/r3-utils/zod-common)

UtilityDescription
PaginationTypeZod schema for pagination types
createOptionalRefinementCreates optional refinements for Zod schemas
ApiListSchemaCreates API list schemas
ApiDetailsSchemaCreates API details schemas
ContainOnlyAlphabeticValidates that a string contains only alphabetic characters
SelectInputOptionTypeZod schema for select input options
NoneEmptyStringSchemaCreates a schema for non-empty strings

Development

# Install dependencies
pnpm install

# Build the library
pnpm build

# Run linting
pnpm lint

# Format code
pnpm format

License

MIT

0.0.3

7 months ago

0.0.2

8 months ago

1.0.0

8 months ago

0.0.10-alpha

8 months ago

0.0.9-alpha

8 months ago

0.0.8-alpha

8 months ago

0.0.7-alpha

8 months ago

0.0.6-alpha

8 months ago

0.0.5-alpha

8 months ago

0.0.4-alpha

8 months ago

0.0.3-alpha

8 months ago

0.0.2-alpha

8 months ago

0.0.1

8 months ago