import { Button } from '@heroui/button' import { Card, CardBody, CardHeader } from '@heroui/card' import { Input } from '@heroui/input' import { Snippet } from '@heroui/snippet' import { useLocalStorage } from '@uidotdev/usehooks' import { motion } from 'motion/react' import { useEffect, useRef, useState } from 'react' import toast from 'react-hot-toast' import { IoLink, IoSend } from 'react-icons/io5' import { PiCatDuotone } from 'react-icons/pi' import key from '@/const/key' import { OneBotHttpApiContent, OneBotHttpApiPath } from '@/const/ob_api' import ChatInputModal from '@/components/chat_input/modal' import CodeEditor from '@/components/code_editor' import PageLoading from '@/components/page_loading' import { request } from '@/utils/request' import { parseAxiosResponse } from '@/utils/url' import { generateDefaultJson, parse } from '@/utils/zod' import DisplayStruct from './display_struct' export interface OneBotApiDebugProps { path: OneBotHttpApiPath data: OneBotHttpApiContent } const OneBotApiDebug: React.FC = (props) => { const { path, data } = props const currentURL = new URL(window.location.origin) currentURL.port = '3000' const defaultHttpUrl = currentURL.href const [httpConfig, setHttpConfig] = useLocalStorage(key.httpDebugConfig, { url: defaultHttpUrl, token: '' }) const [requestBody, setRequestBody] = useState('{}') const [responseContent, setResponseContent] = useState('') const [isCodeEditorOpen, setIsCodeEditorOpen] = useState(false) const [isResponseOpen, setIsResponseOpen] = useState(false) const [isFetching, setIsFetching] = useState(false) const responseRef = useRef(null) const parsedRequest = parse(data.request) const parsedResponse = parse(data.response) const sendRequest = async () => { if (isFetching) return setIsFetching(true) const r = toast.loading('正在发送请求...') try { const parsedRequestBody = JSON.parse(requestBody) const requestURL = new URL(httpConfig.url) requestURL.pathname = path request .post(requestURL.href, parsedRequestBody, { headers: { Authorization: `Bearer ${httpConfig.token}` }, responseType: 'text' }) .then((res) => { setResponseContent(parseAxiosResponse(res)) toast.success('请求发送完成,请查看响应') }) .catch((err) => { toast.error('请求发送失败:' + err.message) setResponseContent(parseAxiosResponse(err.response)) }) .finally(() => { setIsFetching(false) setIsResponseOpen(true) responseRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }) toast.dismiss(r) }) } catch (error) { toast.error('请求体 JSON 格式错误') setIsFetching(false) toast.dismiss(r) } } useEffect(() => { setRequestBody(generateDefaultJson(data.request)) setResponseContent('') }, [path]) return (

{data.description}

} tooltipProps={{ content: '点击复制地址' }} > {path}

setHttpConfig({ ...httpConfig, url: e.target.value }) } /> setHttpConfig({ ...httpConfig, token: e.target.value }) } />
请求体 setRequestBody(value ?? '')} language="json" height="400px" />
响应
              
                {responseContent || (
                  
暂无响应
)}

请求体结构

响应体结构

) } export default OneBotApiDebug