更新 CNB 配置获取逻辑,修复未授权问题,添加测试文件

This commit is contained in:
xiongxiao
2026-03-13 04:33:16 +08:00
committed by cnb
parent cda1d0dc8f
commit d9fda44e78
4 changed files with 15 additions and 4 deletions

View File

@@ -4,7 +4,9 @@ import { useKey } from '@kevisual/context';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
export const getConfig = async (opts: { token?: string }) => { export const getConfig = async (opts: { token?: string }) => {
const kevisualEnv = useKey('KEVISUAL_ENV') const kevisualEnv = useKey('KEVISUAL_ENV')
const baseUrl = kevisualEnv === 'production' ? 'https://kevisual.cn/api/router' : 'https://kevisual.xiongxiao.me/api/router'; const isCNB = useKey('CNB');
let isProduction = kevisualEnv !== 'development' || (isCNB && !kevisualEnv);
const baseUrl = isProduction ? 'https://kevisual.cn/api/router' : 'https://kevisual.xiongxiao.me/api/router';
const res = await fetch(baseUrl, { const res = await fetch(baseUrl, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
@@ -70,6 +72,8 @@ export class CNBManager {
if (token) { if (token) {
return this.addCNB({ username, token, cookie }) return this.addCNB({ username, token, cookie })
} }
} else {
console.error('获取 CNB 配置失败', username, res)
} }
return null return null
} }

View File

@@ -9,7 +9,7 @@ app.route({
middleware: ['auth'], middleware: ['auth'],
}).define(async (ctx) => { }).define(async (ctx) => {
const tokenUser = ctx.tokenUser; const tokenUser = ctx.state?.tokenUser;
if (!tokenUser) { if (!tokenUser) {
ctx.throw(401, '未授权'); ctx.throw(401, '未授权');
} }
@@ -29,7 +29,8 @@ app.route({
description: '获取我的cnb配置', description: '获取我的cnb配置',
middleware: ['auth'], middleware: ['auth'],
}).define(async (ctx) => { }).define(async (ctx) => {
const username = ctx.tokenUser?.username; const tokenUser = ctx.state?.tokenUser;
const username = tokenUser?.username;
const token = ctx.query?.token; const token = ctx.query?.token;
if (!username) { if (!username) {
ctx.throw(400, '未授权'); ctx.throw(400, '未授权');

View File

@@ -0,0 +1,6 @@
const token = 'st_logh1b3ozq2resntxlnk4bccao0bon8e'
import { CNBManager } from "../../modules/cnb-manager.ts"
const cnbManager = new CNBManager()
const cnbItem = await cnbManager.getCNB({ username: 'root', kevisualToken: token });
console.log('cnbItem', cnbItem)

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/cnb", "name": "@kevisual/cnb",
"version": "0.0.43", "version": "0.0.45",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"basename": "/root/cnb", "basename": "/root/cnb",