higress-console-api
v1.0.0
Published
Higress Console API client for managing consumers and route bindings
Maintainers
Readme
Higress Console API Client
一个用于管理Higress Console中消费者和路由绑定的Node.js客户端库。
功能特性
- ✅ 智能消费者绑定:自动创建不存在的消费者并绑定到路由
- ✅ 自动重新登录:检测到会话过期时自动重新登录
- ✅ 消费者管理(创建、删除、查询)
- ✅ 路由管理(查询、更新认证配置)
- ✅ 批量操作支持
- ✅ TypeScript支持
- ✅ 完整的错误处理
- ✅ Cookie会话管理
安装
npm install higress-console-api核心特性
🎯 智能消费者绑定
- 如果消费者不存在,自动创建(token格式:
{消费者名}_${20位随机字符串}) - 如果消费者存在,返回其第一个token
- 智能处理路由绑定逻辑
🔄 自动重新登录
- 检测到401错误时自动重新登录
- 无缝重试失败的API请求
- 保持会话状态
快速开始
import { HigressConsoleAPI } from 'higress-console-api';
// 初始化客户端
const api = new HigressConsoleAPI({
baseUrl: 'http://your-higress-console:8001',
username: 'admin',
password: 'your-password'
});
// 核心功能:绑定消费者到路由
const result = await api.bindConsumerToRoute('my-route', 'my-consumer');
console.log(result.message);API 文档
初始化
const api = new HigressConsoleAPI({
baseUrl: 'http://localhost:8001', // Higress Console URL
username: 'admin', // 登录用户名
password: 'password', // 登录密码
timeout: 30000 // 可选:请求超时时间(毫秒)
});核心功能
bindConsumerToRoute(routeName, consumerName)
将指定的消费者绑定到路由。如果路由已经有消费者,则返回现有消费者信息。
const result = await api.bindConsumerToRoute('my-route', 'my-consumer');
if (result.success) {
console.log('绑定成功:', result.message);
} else {
console.log('绑定失败:', result.message);
if (result.existingConsumers) {
console.log('现有消费者:', result.existingConsumers);
}
}消费者管理
createConsumer(options)
创建新消费者:
await api.createConsumer({
name: 'my-consumer',
authType: 'key-auth',
token: 'secret-token-123',
tokenSource: 'HEADER',
headerName: 'X-API-Key'
});getConsumers()
获取所有消费者:
const consumers = await api.getConsumers();
console.log('消费者列表:', consumers);deleteConsumer(name)
删除消费者:
await api.deleteConsumer('consumer-name');batchCreateConsumers(configs)
批量创建消费者:
const results = await api.batchCreateConsumers([
{
name: 'consumer1',
authType: 'key-auth',
token: 'token1'
},
{
name: 'consumer2',
authType: 'key-auth',
token: 'token2'
}
]);路由管理
getRoutes()
获取所有路由:
const routes = await api.getRoutes();getRouteByName(routeName)
根据名称获取路由:
const route = await api.getRouteByName('my-route');updateRouteConsumers(options)
更新路由的消费者绑定:
await api.updateRouteConsumers({
routeName: 'my-route',
consumerNames: ['consumer1', 'consumer2'],
enableAuth: true
});辅助方法
listConsumers()
打印格式化的消费者列表:
await api.listConsumers();listRoutes()
打印格式化的路由列表:
await api.listRoutes();在Next.js中使用
// pages/api/higress.ts
import { HigressConsoleAPI } from 'higress-console-api';
export default async function handler(req, res) {
const api = new HigressConsoleAPI({
baseUrl: process.env.HIGRESS_CONSOLE_URL,
username: process.env.HIGRESS_USERNAME,
password: process.env.HIGRESS_PASSWORD
});
try {
const result = await api.bindConsumerToRoute(
req.body.routeName,
req.body.consumerName
);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}类型定义
interface HigressConfig {
baseUrl: string;
username: string;
password: string;
timeout?: number;
}
interface CreateConsumerOptions {
name: string;
authType?: 'key-auth' | 'oauth2' | 'jwt';
token?: string;
tokenSource?: 'BEARER' | 'HEADER' | 'QUERY';
headerName?: string;
}
interface UpdateRouteConsumersOptions {
routeName: string;
consumerNames: string[];
enableAuth?: boolean;
}开发
# 安装依赖
npm install
# 构建
npm run build
# 运行测试
npm test
# 运行示例
npm run example许可证
MIT
