@workspace/core
The@workspace/core package is the shared foundation for the BE Monorepo, providing common utilities, type definitions, HTTP services, and API clients used across all applications.
Overview
This package is structured as a modular library with the following exports:- apis/ - API client repositories with Zod validation
- assets/ - Shared static assets (images, etc.)
- constants/ - Common constants and type definitions
- services/ - Shared services (HTTP client)
- types/ - TypeScript type definitions
- utils/ - Utility functions for common operations
Installation
The package is already available in the monorepo workspace:Package Structure
APIs
API repositories provide typed HTTP clients with built-in Zod validation.Auth API
Location:packages/core/src/apis/auth.ts:1
getSession()- GET/api/auth/get-sessionsignInEmail()- POST/api/auth/sign-in/emailsignUpEmail()- POST/api/auth/sign-up/emailsignOut()- POST/api/auth/sign-out
Constants
Core Constants
Location:packages/core/src/constants/core.ts:1
kilobyteMultiplier- 1024megabyteMultiplier- 1,048,576gigabyteMultiplier- 1,073,741,824indoTimezone-["WIB", "WITA", "WIT"]
HTTP Constants
Location:packages/core/src/constants/http.ts:1
InfoStatusCode- 1xx status codesSuccessStatusCode- 2xx status codesRedirectStatusCode- 3xx status codesClientErrorStatusCode- 4xx status codesServerErrorStatusCode- 5xx status codesStatusCode- Union of all status code types
Services
HTTP Service
Location:packages/core/src/services/http.ts:1
A wrapper around ky for making HTTP requests.
constructor(config)- Create new HTTP instanceupdateConfig(newConfig)- Extend current configurationresetConfig(newConfig)- Replace configuration entirely
Types
Location:packages/core/src/types/core.ts:1
URLSearchParamsInit- Flexible URL search params type
Utils
Core Utilities
Location:packages/core/src/utils/core.ts:1
clamp({ value, min, max })- Clamp value to rangetoCamelCase<T>(obj)- Convert object keys to camelCasetoSnakeCase<T>(obj)- Convert object keys to snake_caseobjectToFormData(obj, options?)- Convert object to FormDataobjectToFormDataArrayWithComma(obj, options?)- Convert with comma-separated arraysdeepReadObject<T>(obj, path, defaultValue?)- Safely read nested valuesindonesianPhoneNumberFormat(phoneNumber)- Format Indonesian phone numbersremoveLeadingZeros(value)- Remove leading zeros from stringsremoveLeadingWhitespace(value?)- Remove leading whitespace
Date Utilities
Location:packages/core/src/utils/date.ts:1
Invariant Utility
Location:packages/core/src/utils/invariant.ts:1
Assertion function for runtime validation with TypeScript type narrowing.
- TypeScript assertion function for type narrowing
- Messages stripped in production for smaller bundles
- Supports string or function messages
Logger Utility
Location:packages/core/src/utils/logger.ts:1
Colored console logger with timestamps and severity levels.
debug(message, ...attributes)- Green debug logslog(message, ...attributes)- Blue info logswarn(message, ...attributes)- Yellow warning logserror(message, ...attributes)- Red error logs
Assets
Location:packages/core/src/assets/
Shared static assets that can be imported across applications.
Usage in Applications
The Hono app demonstrates typical usage patterns:TypeScript Configuration
Applications extend the shared TypeScript configuration and map paths to the core package:Dependencies
The core package uses these external dependencies:- ky (1.14.3) - HTTP client
- radashi (12.7.1) - Utility functions
- type-fest (5.4.4) - TypeScript utility types
- zod (4.3.6) - Schema validation
Best Practices
API Client Usage
- Create a single HTTP instance per API base URL
- Use repository pattern for organizing endpoints
- Leverage Zod schemas for type safety
- Handle errors with try-catch blocks
Utility Functions
- Use
invariantfor runtime assertions and type narrowing - Prefer
deepReadObjectover manual property access for nested data - Use case conversion utilities for API data transformation
- Use the logger for consistent, colored output
Type Safety
- Import both schema and type from APIs
- Use type assertions with Zod validation
- Leverage TypeScript’s type narrowing with invariant
Development
Adding New APIs
- Create a new file in
src/apis/ - Define Zod schemas for entities, requests, and responses
- Create query keys for caching (if using React Query)
- Export a repository function that takes an Http instance
Adding New Utilities
- Add function to appropriate file in
src/utils/ - Include JSDoc comments with examples
- Export from the module
- Add tests if needed
