frontend/packages/arch/bot-http/README.md
Global HTTP client and error handling utilities for Bot Studio web applications
This package provides a centralized HTTP client solution for the entire Bot Studio web application ecosystem. It offers a pre-configured axios instance with global interceptors, comprehensive error handling, event-driven API error management, and standardized response processing. This package ensures consistent HTTP behavior across all Bot Studio services while providing robust error reporting and handling capabilities.
Add this package to your package.json dependencies and set it to workspace:* version:
{
"dependencies": {
"@coze-arch/bot-http": "workspace:*"
}
}
Then run:
rush update
import { axiosInstance } from '@coze-arch/bot-http';
// Make HTTP requests
const response = await axiosInstance.get('/api/users');
const userData = await axiosInstance.post('/api/users', { name: 'John' });
// The instance is pre-configured with interceptors and error handling
import {
APIErrorEvent,
handleAPIErrorEvent,
isApiError,
ApiError
} from '@coze-arch/bot-http';
// Register global error handler
handleAPIErrorEvent((error: APIErrorEvent) => {
console.error('API Error occurred:', error);
// Handle error globally (show toast, redirect, etc.)
});
// Check if error is an API error
try {
await axiosInstance.get('/api/data');
} catch (error) {
if (isApiError(error)) {
console.log('API Error Code:', error.code);
console.log('API Error Message:', error.msg);
}
}
import {
addGlobalRequestInterceptor,
addGlobalResponseInterceptor,
removeGlobalRequestInterceptor
} from '@coze-arch/bot-http';
// Add request interceptor for authentication
const requestInterceptor = addGlobalRequestInterceptor((config) => {
config.headers.Authorization = `Bearer ${getToken()}`;
return config;
});
// Add response interceptor for data processing
addGlobalResponseInterceptor((response) => {
// Process response data
return response;
});
// Remove interceptor when no longer needed
removeGlobalRequestInterceptor(requestInterceptor);
import {
emitAPIErrorEvent,
startAPIErrorEvent,
stopAPIErrorEvent,
clearAPIErrorEvent
} from '@coze-arch/bot-http';
// Manually emit API error event
emitAPIErrorEvent({
code: '500',
msg: 'Internal server error',
type: 'custom'
});
// Control error event handling
stopAPIErrorEvent(); // Temporarily disable error events
startAPIErrorEvent(); // Re-enable error events
clearAPIErrorEvent(); // Clear all error handlers
axiosInstancePre-configured axios instance with global interceptors and error handling.
import { axiosInstance } from '@coze-arch/bot-http';
// Use like regular axios instance
ApiErrorExtended error class for API-specific errors.
class ApiError extends AxiosError {
code: string; // Error code
msg: string; // Error message
hasShowedError: boolean; // Whether error has been displayed
type: string; // Error type
raw?: any; // Raw error data
}
// Register error handler
handleAPIErrorEvent(handler: (error: APIErrorEvent) => void): void
// Remove error handler
removeAPIErrorEvent(handler: (error: APIErrorEvent) => void): void
// Control error events
startAPIErrorEvent(): void
stopAPIErrorEvent(): void
clearAPIErrorEvent(): void
// Emit custom error
emitAPIErrorEvent(error: APIErrorEvent): void
// Check if error is API error
isApiError(error: any): error is ApiError
// Add request interceptor
addGlobalRequestInterceptor(
interceptor: (config: AxiosRequestConfig) => AxiosRequestConfig
): number
// Remove request interceptor
removeGlobalRequestInterceptor(interceptorId: number): void
// Add response interceptor
addGlobalResponseInterceptor(
interceptor: (response: AxiosResponse) => AxiosResponse
): void
Built-in error code constants:
enum ErrorCodes {
NOT_LOGIN = 700012006,
COUNTRY_RESTRICTED = 700012015,
COZE_TOKEN_INSUFFICIENT = 702082020,
COZE_TOKEN_INSUFFICIENT_WORKFLOW = 702095072,
}
import { axiosInstance, ApiError } from '@coze-arch/bot-http';
axiosInstance.interceptors.response.use(
response => response,
error => {
if (error instanceof ApiError) {
// Handle specific API errors
switch (error.code) {
case '401':
// Redirect to login
break;
case '403':
// Show permission error
break;
default:
// Generic error handling
}
}
return Promise.reject(error);
}
);
The package automatically integrates with @coze-arch/web-context for handling redirects and navigation in unauthorized scenarios.
npm run build - Build the package (no-op, source-only package)npm run lint - Run ESLintnpm run test - Run tests with Vitestnpm run test:cov - Run tests with coveragesrc/
├── axios.ts # Axios instance configuration and interceptors
├── api-error.ts # API error classes and utilities
├── eventbus.ts # Error event management system
└── index.ts # Main exports
This package depends on:
@coze-arch/logger - Logging utilities for error reportingaxios - HTTP client libraryApache-2.0