CyberStrikeAI/web/static/app/assets/api-docs-D1UmNuF-.js

3 lines
6.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{n as e}from"./rolldown-runtime-WkwZ5CU-.js";import{S as t,t as n}from"./react-QV7DAAIx.js";import{E as r,F as i,H as a,L as o,M as s,P as c,W as l,at as u,c as d,ot as f,st as p,v as m,x as h}from"./semi-CHxO7u8f.js";import{B as g,I as _,P as v,R as y,b,p as x,u as S}from"./index-SJWmtITN.js";var C=e(t(),1),w=e(n(),1),{Text:T,Paragraph:E}=l,D={get:`blue`,post:`green`,put:`orange`,delete:`red`,patch:`violet`,head:`grey`,options:`grey`};function O(e){let t=e?.paths||{},n=[];return Object.entries(t).forEach(([e,t])=>{Object.entries(t||{}).forEach(([t,r])=>{r&&typeof r==`object`&&n.push({id:`${t.toUpperCase()} ${e}`,method:t.toUpperCase(),path:e,summary:r.summary||r.description||``,tag:r.tags&&r.tags[0]||`default`,operation:r})})}),n.sort((e,t)=>e.path.localeCompare(t.path)||e.method.localeCompare(t.method))}function k(e){return Array.from(e.matchAll(/\{([^}]+)\}/g)).map(e=>e[1])}function A({operation:e,spec:t}){let n=e.operation||{},c=n.parameters||[],l=k(e.path),[d,f]=(0,C.useState)({}),[p,m]=(0,C.useState)(``),[_,v]=(0,C.useState)(null),[x,E]=(0,C.useState)(!1);return(0,C.useEffect)(()=>{let e={};l.forEach(t=>{let n=c.find(e=>e.name===t&&e.in===`path`);e[t]=n?.schema?.default??n?.example??``}),c.filter(e=>e.in===`query`).forEach(t=>{t.schema?.default!==void 0&&(e[`query:${t.name}`]=String(t.schema.default))}),f(e),m(n.requestBody?`{
}`:``),v(null)},[e.id]),(0,w.jsxs)(`div`,{className:`api-docs-try`,children:[(0,w.jsx)(r,{margin:`12px 0`}),(0,w.jsx)(T,{strong:!0,children:`在线调试`}),l.length>0&&(0,w.jsx)(`div`,{className:`api-docs-try-fields`,children:l.map(e=>(0,w.jsxs)(`label`,{className:`api-docs-try-field`,children:[(0,w.jsxs)(`span`,{children:[`路径参数 `,e]}),(0,w.jsx)(a,{size:`small`,value:d[e]??``,onChange:t=>f(n=>({...n,[e]:t}))})]},e))}),c.filter(e=>e.in===`query`).length>0&&(0,w.jsx)(`div`,{className:`api-docs-try-fields`,children:c.filter(e=>e.in===`query`).map(e=>(0,w.jsxs)(`label`,{className:`api-docs-try-field`,children:[(0,w.jsxs)(`span`,{children:[`查询参数 `,e.name,e.required?` *`:``]}),(0,w.jsx)(a,{size:`small`,value:d[`query:${e.name}`]??``,onChange:t=>f(n=>({...n,[`query:${e.name}`]:t})),placeholder:e.description||``})]},e.name))}),n.requestBody&&(0,w.jsxs)(`div`,{className:`api-docs-try-body`,children:[(0,w.jsx)(`span`,{children:`请求体(JSON)`}),(0,w.jsx)(h,{value:p,onChange:m,autosize:{minRows:5,maxRows:14}})]}),(0,w.jsxs)(i,{children:[(0,w.jsx)(o,{theme:`solid`,type:`primary`,icon:(0,w.jsx)(u,{}),loading:x,onClick:async()=>{E(!0),v(null);try{if(l.some(e=>String(d[e]??``).trim()===``)){b(`请填写路径参数`);return}let t=e.path;l.forEach(e=>{t=t.replace(`{${e}}`,encodeURIComponent(d[e]??``))});let r={};c.filter(e=>e.in===`query`).forEach(e=>{let t=d[`query:${e.name}`];t!==void 0&&t!==``&&(r[e.name]=t)});let i={method:e.method,query:r,raw:!0,timeout:12e4};if(n.requestBody&&p.trim())try{i.body=JSON.parse(p)}catch{b(`请求体不是合法 JSON`),E(!1);return}if(!t.startsWith(`/`)||t.startsWith(`//`))throw Error(`接口地址必须为站内 API 路径`);let a=await g(t.startsWith(`/api/`)?t.slice(4):t,i),o=a.headers.get(`content-type`)||``,s;s=o.includes(`application/json`)?await a.json():await a.text(),v({ok:!0,status:a.status,payload:s})}catch(e){v({ok:!1,status:e.status||0,payload:y(e)})}finally{E(!1)}},children:`发送请求`}),(0,w.jsx)(o,{onClick:()=>v(null),children:`清空结果`})]}),_&&(0,w.jsxs)(`div`,{className:`api-docs-try-result`,children:[(0,w.jsx)(s,{color:_.ok?`green`:`red`,children:_.ok?`HTTP ${_.status}`:`失败 ${_.status||``}`}),(0,w.jsx)(S,{value:_.payload,title:`响应`})]})]})}function j(){let e=v(),[t,n]=(0,C.useState)(null),[r,l]=(0,C.useState)(!0),[u,h]=(0,C.useState)(null),[g,b]=(0,C.useState)(``),[k,j]=(0,C.useState)(`all`),[M,N]=(0,C.useState)(null),P=async()=>{l(!0),h(null);try{let e=await _.get(`/openapi/spec`);n(e)}catch(e){h(e)}finally{l(!1)}};(0,C.useEffect)(()=>{P()},[]);let F=(0,C.useMemo)(()=>O(t),[t]),I=(0,C.useMemo)(()=>F.filter(e=>{if(k!==`all`&&e.method!==k)return!1;if(!g)return!0;let t=g.toLowerCase();return e.path.toLowerCase().includes(t)||e.summary.toLowerCase().includes(t)||e.method.toLowerCase().includes(t)}),[F,g,k]),L=I.find(e=>e.id===M)||I[0]||null,R=(0,C.useMemo)(()=>{let e=new Map;return I.forEach(t=>{e.has(t.tag)||e.set(t.tag,[]),e.get(t.tag).push(t)}),[...e.entries()]},[I]);return(0,w.jsxs)(`div`,{className:`api-docs-page`,children:[(0,w.jsx)(x,{title:e(`apiDocs.title`,null,`API 文档`),description:t?.info?.description||`CyberStrikeAI 开放接口`,extra:(0,w.jsx)(o,{icon:(0,w.jsx)(p,{}),onClick:P,loading:r,children:`重新加载`})}),(0,w.jsxs)(`div`,{className:`api-docs-layout`,children:[(0,w.jsxs)(c,{className:`api-docs-sidebar`,bordered:!1,children:[(0,w.jsxs)(i,{vertical:!0,style:{width:`100%`},spacing:8,children:[(0,w.jsx)(a,{prefix:(0,w.jsx)(f,{}),value:g,onChange:b,placeholder:`搜索路径或说明`}),(0,w.jsx)(m,{value:k,onChange:j,style:{width:`100%`},optionList:[{value:`all`,label:`全部方法`},...[`GET`,`POST`,`PUT`,`DELETE`,`PATCH`].map(e=>({value:e,label:e}))]})]}),(0,w.jsxs)(`div`,{className:`api-docs-list`,children:[R.map(([e,t])=>(0,w.jsxs)(`div`,{className:`api-docs-group`,children:[(0,w.jsx)(T,{type:`tertiary`,className:`api-docs-group-title`,children:e}),t.map(e=>(0,w.jsxs)(`button`,{type:`button`,className:`api-docs-item ${L?.id===e.id?`is-active`:``}`,onClick:()=>N(e.id),children:[(0,w.jsx)(s,{size:`small`,color:D[e.method.toLowerCase()],children:e.method}),(0,w.jsx)(`span`,{className:`api-docs-item-path`,children:e.path})]},e.id))]},e)),!r&&R.length===0&&(0,w.jsx)(T,{type:`tertiary`,children:`没有匹配的接口`})]})]}),(0,w.jsxs)(`div`,{className:`api-docs-detail`,children:[u&&(0,w.jsx)(c,{bordered:!1,children:(0,w.jsx)(T,{type:`danger`,children:y(u)})}),L?(0,w.jsxs)(c,{bordered:!1,children:[(0,w.jsxs)(`div`,{className:`api-docs-detail-head`,children:[(0,w.jsx)(s,{color:D[L.method.toLowerCase()],children:L.method}),(0,w.jsx)(`code`,{className:`api-docs-detail-path`,children:L.path})]}),(0,w.jsx)(E,{children:L.summary||`无说明`}),(0,w.jsxs)(d,{type:`line`,children:[(0,w.jsx)(d.TabPane,{itemKey:`params`,tab:`参数`,children:(0,w.jsx)(S,{value:L.operation.parameters||[],title:`Request Parameters`,collapsed:!0})}),(0,w.jsx)(d.TabPane,{itemKey:`body`,tab:`请求体`,children:(0,w.jsx)(S,{value:L.operation.requestBody||{},title:`Request Body`,collapsed:!0})}),(0,w.jsx)(d.TabPane,{itemKey:`responses`,tab:`响应`,children:(0,w.jsx)(S,{value:L.operation.responses||{},title:`Responses`,collapsed:!0})})]}),(0,w.jsx)(A,{operation:L,spec:t})]}):(0,w.jsx)(c,{bordered:!1,children:(0,w.jsx)(T,{type:`tertiary`,children:r?`加载中…`:`选择左侧接口查看详情`})})]})]})]})}export{j as default};