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

3 lines
6.6 KiB
JavaScript
Raw Permalink 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{D as r,F as i,G as a,I as o,N as s,R as c,S as l,U as u,c as d,ct as f,lt as p,ut as m,y as h}from"./semi-65W_4I1v.js";import{I as g,P as _,R as v,V as y,b,p as x,u as S}from"./index-BdnarFZR.js";var C=e(t(),1),w=e(n(),1),{Text:T,Paragraph:E}=a,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||{},i=n.parameters||[],a=k(e.path),[d,p]=(0,C.useState)({}),[m,h]=(0,C.useState)(``),[g,_]=(0,C.useState)(null),[x,E]=(0,C.useState)(!1);return(0,C.useEffect)(()=>{let e={};a.forEach(t=>{let n=i.find(e=>e.name===t&&e.in===`path`);e[t]=n?.schema?.default??n?.example??``}),i.filter(e=>e.in===`query`).forEach(t=>{t.schema?.default!==void 0&&(e[`query:${t.name}`]=String(t.schema.default))}),p(e),h(n.requestBody?`{
}`:``),_(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:`在线调试`}),a.length>0&&(0,w.jsx)(`div`,{className:`api-docs-try-fields`,children:a.map(e=>(0,w.jsxs)(`label`,{className:`api-docs-try-field`,children:[(0,w.jsxs)(`span`,{children:[`路径参数 `,e]}),(0,w.jsx)(u,{size:`small`,value:d[e]??``,onChange:t=>p(n=>({...n,[e]:t}))})]},e))}),i.filter(e=>e.in===`query`).length>0&&(0,w.jsx)(`div`,{className:`api-docs-try-fields`,children:i.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)(u,{size:`small`,value:d[`query:${e.name}`]??``,onChange:t=>p(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)(l,{value:m,onChange:h,autosize:{minRows:5,maxRows:14}})]}),(0,w.jsxs)(o,{children:[(0,w.jsx)(c,{theme:`solid`,type:`primary`,icon:(0,w.jsx)(f,{}),loading:x,onClick:async()=>{E(!0),_(null);try{if(a.some(e=>String(d[e]??``).trim()===``)){b(`请填写路径参数`);return}let t=e.path;a.forEach(e=>{t=t.replace(`{${e}}`,encodeURIComponent(d[e]??``))});let r={};i.filter(e=>e.in===`query`).forEach(e=>{let t=d[`query:${e.name}`];t!==void 0&&t!==``&&(r[e.name]=t)});let o={method:e.method,query:r,raw:!0,timeout:12e4};if(n.requestBody&&m.trim())try{o.body=JSON.parse(m)}catch{b(`请求体不是合法 JSON`),E(!1);return}if(!t.startsWith(`/`)||t.startsWith(`//`))throw Error(`接口地址必须为站内 API 路径`);let s=await y(t.startsWith(`/api/`)?t.slice(4):t,o),c=s.headers.get(`content-type`)||``,l;l=c.includes(`application/json`)?await s.json():await s.text(),_({ok:!0,status:s.status,payload:l})}catch(e){_({ok:!1,status:e.status||0,payload:v(e)})}finally{E(!1)}},children:`发送请求`}),(0,w.jsx)(c,{onClick:()=>_(null),children:`清空结果`})]}),g&&(0,w.jsxs)(`div`,{className:`api-docs-try-result`,children:[(0,w.jsx)(s,{color:g.ok?`green`:`red`,children:g.ok?`HTTP ${g.status}`:`失败 ${g.status||``}`}),(0,w.jsx)(S,{value:g.payload,title:`响应`})]})]})}function j(){let e=_(),[t,n]=(0,C.useState)(null),[r,a]=(0,C.useState)(!0),[l,f]=(0,C.useState)(null),[y,b]=(0,C.useState)(``),[k,j]=(0,C.useState)(`all`),[M,N]=(0,C.useState)(null),P=async()=>{a(!0),f(null);try{let e=await g.get(`/openapi/spec`);n(e)}catch(e){f(e)}finally{a(!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(!y)return!0;let t=y.toLowerCase();return e.path.toLowerCase().includes(t)||e.summary.toLowerCase().includes(t)||e.method.toLowerCase().includes(t)}),[F,y,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)(c,{icon:(0,w.jsx)(m,{}),onClick:P,loading:r,children:`重新加载`})}),(0,w.jsxs)(`div`,{className:`api-docs-layout`,children:[(0,w.jsxs)(i,{className:`api-docs-sidebar`,bordered:!1,children:[(0,w.jsxs)(o,{vertical:!0,style:{width:`100%`},spacing:8,children:[(0,w.jsx)(u,{prefix:(0,w.jsx)(p,{}),value:y,onChange:b,placeholder:`搜索路径或说明`}),(0,w.jsx)(h,{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:[l&&(0,w.jsx)(i,{bordered:!1,children:(0,w.jsx)(T,{type:`danger`,children:v(l)})}),L?(0,w.jsxs)(i,{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)(i,{bordered:!1,children:(0,w.jsx)(T,{type:`tertiary`,children:r?`加载中…`:`选择左侧接口查看详情`})})]})]})]})}export{j as default};