|
3 months ago | |
---|---|---|
.. | ||
__tests__ | 3 months ago | |
config | 3 months ago | |
scripts | 3 months ago | |
src | 3 months ago | |
README.md | 3 months ago | |
eslint.config.js | 3 months ago | |
package.json | 3 months ago | |
tsconfig.build.json | 3 months ago | |
tsconfig.json | 3 months ago | |
tsconfig.misc.json | 3 months ago | |
vitest.config.mts | 3 months ago |
README.md
@coze-arch/bot-env
A TypeScript-based environment variable management system for the bot-studio monorepo, providing compile-time type safety and runtime environment configuration.
Features
- 🔧 Compile-time Environment Variables: Access to environment variables with full TypeScript support
- 🏗️ Multi-environment Support: Handles different deployment environments (BOE, production, development)
- 🌍 Multi-region Configuration: Supports CN, SG, and VA regions with region-specific configurations
- 🎯 Feature Flags: Built-in feature toggle system for conditional functionality
- 📝 Auto-generated Types: Automatically generates TypeScript declarations from environment configuration
- 🔒 Type Safety: Full TypeScript support with strict typing for all environment variables
- ⚡ Runtime Environment Access: Provides runtime environment detection and configuration
Get Started
Installation
# Install the package
rush add -p @coze-arch/bot-env
# Update dependencies
rush update
Basic Usage
// Import compile-time environment variables
import { GLOBAL_ENVS } from '@coze-arch/bot-env';
// Access environment variables with full type safety
console.log(GLOBAL_ENVS.REGION); // 'cn' | 'sg' | 'va'
console.log(GLOBAL_ENVS.IS_PROD); // boolean
console.log(GLOBAL_ENVS.BUILD_TYPE); // 'local' | 'online' | 'offline' | 'test'
// Import runtime environment utilities
import { runtimeEnv } from '@coze-arch/bot-env/runtime';
// Access runtime environment information
console.log(runtimeEnv.isPPE); // boolean
TypeScript Declarations
// Import TypeScript declarations
/// <reference types="@coze-arch/bot-env/typings" />
// All environment variables are available as global constants
declare const REGION: 'cn' | 'sg' | 'va';
declare const IS_PROD: boolean;
declare const FEATURE_ENABLE_SSO: boolean;
// ... and many more
API Reference
GLOBAL_ENVS
The main export containing all environment variables organized by category:
Base Environment Variables
BUILD_TYPE
: Build environment type ('local' | 'online' | 'offline' | 'test'
)CUSTOM_VERSION
: Version type ('inhouse' | 'release'
)REGION
: Deployment region ('cn' | 'sg' | 'va'
)NODE_ENV
: Node environment ('production' | 'development' | 'test'
)IS_PROD
: Production environment flagIS_BOE
: BOE environment flagIS_OVERSEA
: Overseas deployment flagIS_RELEASE_VERSION
: Release version flag
Feature Flags
FEATURE_ENABLE_SSO
: Single Sign-On featureFEATURE_ENABLE_APP_GUIDE
: Application guide featureFEATURE_ENABLE_MSG_DEBUG
: Message debugging featureFEATURE_AWEME_LOGIN
: Aweme login integrationFEATURE_GOOGLE_LOGIN
: Google login integration- And many more feature toggles...
Configuration Variables
CDN
: Content delivery network URLUPLOAD_CDN
: Upload CDN configurationCOZE_DOMAIN
: Coze service domainAPP_ID
: Application identifierAPP_KEY
: Application key- Various service-specific configurations
Runtime Environment
import { runtimeEnv } from '@coze-arch/bot-env/runtime';
// Runtime environment detection
runtimeEnv.isPPE // boolean - Production-like environment detection
Build Scripts
import { build } from '@coze-arch/bot-env/build';
// Generate TypeScript declarations from environment configuration
build();
Development
Project Structure
src/
├── index.ts # Main exports
├── runtime/ # Runtime environment utilities
├── typings.d.ts # Auto-generated TypeScript declarations
└── global.d.ts # Global type definitions
scripts/
├── build.ts # Build script exports
└── index.ts # Build script runner
Environment Configuration
This package acts as a wrapper around @coze-studio/bot-env-adapter
, which provides the actual environment configuration logic. The adapter package includes:
- Base configuration: Core environment variables like region, build type, and deployment flags
- Feature flags: Toggle switches for various application features
- Business configs: Service-specific configuration values
- Configuration helpers: Utilities for environment-specific value extraction
Auto-generated Types
The package automatically generates TypeScript declarations based on the environment configuration. The build process:
- Analyzes the
envs
object in the source code - Extracts type information for each environment variable
- Generates corresponding TypeScript declarations
- Updates the
typings.d.ts
file with the latest types
Environment-specific Configuration
Use the extractEnvValue
utility for environment-specific configurations:
const API_ENDPOINT = extractEnvValue<string>({
cn: {
boe: 'https://boe.api.example.com',
inhouse: 'https://inhouse.api.example.com',
release: 'https://api.example.com'
},
sg: {
inhouse: 'https://sg-inhouse.api.example.com',
release: 'https://sg.api.example.com'
},
va: {
release: 'https://va.api.example.com'
}
});
Running Tests
# Run tests
rush test -t @coze-arch/bot-env
# Run tests with coverage
rush test:cov -t @coze-arch/bot-env
Building
# Build the package
rush build -t @coze-arch/bot-env
# Generate type definitions
rush build:types -t @coze-arch/bot-env
Dependencies
Runtime Dependencies
@coze-studio/bot-env-adapter
: Core environment configuration adapter
Development Dependencies
@coze-arch/eslint-config
: ESLint configuration@coze-arch/ts-config
: TypeScript configuration@coze-arch/vitest-config
: Vitest testing configurationts-morph
: TypeScript AST manipulation for type generationsucrase
: Fast TypeScript/JSX compilervitest
: Testing framework
License
Apache-2.0 License - see package.json for details.
For more information about environment configuration patterns and best practices, refer to the bot-studio monorepo documentation.