652 lines
26 KiB
Python
652 lines
26 KiB
Python
import json
|
|
|
|
# 1. Load the faithful JSON data
|
|
with open("scratch/faithful_docs_data.json") as f:
|
|
docs_data = json.load(f)
|
|
|
|
# 2. Load the original FASTAPI_BACKEND_API_DOCS.html
|
|
with open("/home/creserver/Desktop/Vaishno_workspace/docs/FASTAPI_BACKEND_API_DOCS.html") as f:
|
|
orig_html = f.read()
|
|
|
|
# Extract head from orig_html up to </style>
|
|
orig_head = orig_html.split('</style>')[0]
|
|
|
|
# Add our clean CSS additions that preserve 100% of the original CSS grid and variables
|
|
clean_css_additions = """
|
|
/* ─── URL Bar & Endpoint Tabs (Clean integration) ─── */
|
|
.ep-full-url-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
background: var(--soft);
|
|
border: 1px solid var(--line);
|
|
border-radius: 12px;
|
|
padding: 10px 14px;
|
|
margin: 16px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
.ep-full-url-bar code {
|
|
font-family: var(--mono);
|
|
font-size: 13.5px;
|
|
color: var(--acc);
|
|
word-break: break-all;
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
.ep-tabs-wrapper {
|
|
margin: 16px 0 14px;
|
|
padding-bottom: 12px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
.ep-tabs-label {
|
|
font-size: 11.5px;
|
|
font-weight: 700;
|
|
letter-spacing: 0.05em;
|
|
text-transform: uppercase;
|
|
color: var(--ink3);
|
|
margin-bottom: 8px;
|
|
display: block;
|
|
}
|
|
.ep-tabs {
|
|
display: flex;
|
|
gap: 8px;
|
|
flex-wrap: wrap;
|
|
}
|
|
.ep-tab {
|
|
padding: 7px 12px;
|
|
border-radius: 10px;
|
|
background: var(--soft);
|
|
border: 1px solid var(--line);
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
color: var(--ink2);
|
|
cursor: pointer;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
transition: all 0.15s;
|
|
}
|
|
.ep-tab:hover {
|
|
border-color: var(--acc);
|
|
color: var(--ink);
|
|
}
|
|
.ep-tab.active {
|
|
background: var(--card);
|
|
border-color: var(--acc);
|
|
color: var(--acc);
|
|
font-weight: 600;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
|
|
}
|
|
.ep-tab .m { padding: 3px 6px; font-size: 10.5px; margin: 0; }
|
|
|
|
.chip.copied {
|
|
background: var(--opbg) !important;
|
|
color: var(--opfg) !important;
|
|
border-color: var(--opbd) !important;
|
|
}
|
|
|
|
/* ─── Header Session Pill & Admin Button ─── */
|
|
.session-pill {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
padding: 6px 12px;
|
|
border-radius: 10px;
|
|
background: var(--card);
|
|
border: 1px solid var(--line);
|
|
font-size: 13px;
|
|
font-family: var(--mono);
|
|
color: var(--ink2);
|
|
font-weight: 500;
|
|
}
|
|
.session-pill.warning {
|
|
background: var(--rqbg);
|
|
color: var(--rqfg);
|
|
border-color: var(--rqbd);
|
|
}
|
|
.btn-admin {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
color: var(--acc);
|
|
border-color: var(--acc);
|
|
font-weight: 600;
|
|
}
|
|
.btn-admin:hover {
|
|
background: var(--tint);
|
|
}
|
|
"""
|
|
|
|
html_head = orig_head + clean_css_additions + "\n</style>\n</head>\n<body>\n<div id=\"app\"></div>\n"
|
|
json_tag = f'<script id="api-data" type="application/json">{json.dumps(docs_data)}</script>\n'
|
|
|
|
# Perfect script that preserves the original accordion and renders single endpoints identically to multi endpoints
|
|
js_code = """<script>
|
|
(()=>{
|
|
const $=(s,r=document)=>r.querySelector(s),$$=(s,r=document)=>[...r.querySelectorAll(s)];
|
|
const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
let D=JSON.parse($('#api-data').textContent);
|
|
const open=new Set(),picked={},activeSubEp={};
|
|
let toastT;
|
|
|
|
const base=e=>{
|
|
if (e.full_url) {
|
|
try {
|
|
const u = new URL(e.full_url);
|
|
return u.origin;
|
|
} catch(err){}
|
|
}
|
|
const b=(D.meta.baseUrl||'https://apigp0006.creinnovations.in').replace(/\\/$/,''),p=(String(e.port).match(/^\\d+/)||[''])[0];
|
|
return p?b+':'+p:b;
|
|
};
|
|
|
|
const meaningful=v=>v&&!/^(none|null|-|—)/i.test(v.trim());
|
|
const clean=v=>v.replace(/^['"]|['"]$/g,'');
|
|
|
|
function fields(e){
|
|
let used=false;
|
|
return (e.params||[]).filter(p=>p.rq).map(p=>{
|
|
const file=/file|binary/i.test(p.ty);
|
|
let v;
|
|
if(file) v=(p.n||'file')+'.jpg';
|
|
else if(meaningful(p.df)&&!/auto/i.test(p.df)) v=clean(p.df);
|
|
else if(!used&&e.chips&&e.chips.length&&/id$/i.test(p.n)){ v=e.chips[0]; used=true; }
|
|
else v='<'+p.n+'>';
|
|
return {n:p.n, file, v, q:/query/i.test(p.lc)};
|
|
});
|
|
}
|
|
|
|
function snippets(e, targetUrl){
|
|
const url=targetUrl || e.full_url || (base(e)+e.path);
|
|
const f=fields(e);
|
|
const isImg=/image|video|audio/.test(e.resp);
|
|
const q=f.filter(x=>x.q), b=f.filter(x=>!x.q);
|
|
const qs=q.length?'?'+q.map(x=>x.n+'='+encodeURIComponent(x.v)).join('&'):'';
|
|
const omitted=(e.params||[]).length-f.length;
|
|
|
|
let curl=e.curl?e.curl.replace(/\\{\\{base\\}\\}/g,(D.meta.baseUrl||'https://apigp0006.creinnovations.in').replace(/\\/$/,'')):
|
|
`curl -X ${e.method==='MULTI'?'POST':e.method} "${url}${qs}"`+b.map(x=>` \\\\
|
|
-F "${x.n}=${x.file?'@':''}${x.v}"`).join('')+(isImg?` \\\\
|
|
-o result.${/jpeg/.test(e.resp)?'jpg':'png'}`:'');
|
|
|
|
const files=b.filter(x=>x.file), data=b.filter(x=>!x.file);
|
|
const py=`import requests
|
|
|
|
res = requests.${(e.method==='MULTI'?'post':e.method).toLowerCase()}(
|
|
"${url}${qs}"`+(b.length?`,
|
|
files={${files.map(x=>`"${x.n}": open("${x.v}", "rb")`).join(', ')}},
|
|
data={${data.map(x=>`"${x.n}": "${x.v}"`).join(', ')}}`:'')+`,
|
|
)
|
|
res.raise_for_status()
|
|
`+(isImg?`open("result.${/jpeg/.test(e.resp)?'jpg':'png'}", "wb").write(res.content)`:`print(res.json())`);
|
|
|
|
const js=(b.length?`const form = new FormData();\\n`+files.map(x=>`form.append("${x.n}", fileInput.files[0]); // ${x.v}`).join('\\n')+(files.length?'\\n':'')+data.map(x=>`form.append("${x.n}", "${x.v}");`).join('\\n')+'\\n\\n':'')+`const res = await fetch("${url}${qs}", {\\n method: "${e.method==='MULTI'?'POST':e.method}",`+(b.length?'\\n body: form,':'')+`\\n});\\n`+(isImg?`const image = URL.createObjectURL(await res.blob());`:`const result = await res.json();`);
|
|
|
|
return {curl, py, js, omitted};
|
|
}
|
|
|
|
const ICON='<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>';
|
|
const COPY_ICON='<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>';
|
|
|
|
function detail(api, s){
|
|
const curIdx = activeSubEp[api.id] || 0;
|
|
const subEps = (api.endpoints && api.endpoints.length) ? api.endpoints : [api];
|
|
const e = subEps[curIdx] || api;
|
|
const cleanParams = (e.params || []).filter(p=>p.n && p.n.trim());
|
|
const targetUrl = e.full_url || api.full_url || (base(e) + e.path);
|
|
const sn = snippets(e, targetUrl);
|
|
|
|
// Universal Endpoint Tabs: Shown for every API (single and multiple)
|
|
const tabsHtml = `
|
|
<div class="ep-tabs-wrapper">
|
|
<span class="ep-tabs-label">Endpoints within this API (${subEps.length}):</span>
|
|
<div class="ep-tabs" role="tablist">
|
|
${subEps.map((sub, i) => `
|
|
<button class="ep-tab ${i === curIdx ? 'active' : ''}" data-act="subtab" data-api="${esc(api.id)}" data-idx="${i}">
|
|
<span class="m m-${esc(sub.method)}">${esc(sub.method)}</span>
|
|
<code>${esc(sub.subpath || sub.path)}</code>
|
|
<span>${esc(sub.title)}</span>
|
|
</button>
|
|
`).join('')}
|
|
</div>
|
|
</div>`;
|
|
|
|
// Direct Full URL Bar
|
|
const urlBarHtml = `
|
|
<div class="ep-full-url-bar">
|
|
<span class="m m-${esc(e.method)}">${esc(e.method)}</span>
|
|
<code>${esc(targetUrl)}</code>
|
|
<button class="btn sm" data-act="copyurl" data-url="${esc(targetUrl)}" title="Copy Complete URL">
|
|
${COPY_ICON} Copy Full URL
|
|
</button>
|
|
</div>`;
|
|
|
|
return tabsHtml + urlBarHtml + `<p class="desc">${esc(e.desc || e.summary || api.desc || api.summary)}</p>` +
|
|
(e.subs && e.subs.length ? `<h4>Also served at <span>${e.subs.length} more routes share this schema</span></h4><ul class="subs">${e.subs.map(x=>`<li><span class="m m-${esc(x.m)}">${esc(x.m)}</span><code>${esc(x.p)}</code><span>${esc(x.d)}</span></li>`).join('')}</ul>` : '') +
|
|
`<h4>Parameters <span>${cleanParams.length} total, ${cleanParams.filter(p=>p.rq).length} required</span></h4>` +
|
|
(cleanParams.length ? `<table class="pt"><thead><tr><th>Name</th><th>Type</th><th>Required</th><th>Default</th><th>What it does</th></tr></thead><tbody>` +
|
|
cleanParams.map(p=>`<tr class="${p.rq?'req':''}"><td><code class="pn">${esc(p.n)}</code>${p.al?`<span class="al">also accepts <code>${esc(p.al)}</code></span>`:''}</td><td><code class="ty">${esc(p.ty)}</code><span class="lc">${esc(p.lc)}</span></td><td>${p.rq?'<span class="rq">Required</span>':'<span class="op">Optional</span>'}</td><td><code class="df">${meaningful(p.df)?esc(clean(p.df)):'—'}</code></td><td>${esc(p.ds)}</td></tr>`).join('') +
|
|
`</tbody></table>` : '<p class="hint">This endpoint takes no parameters.</p>') +
|
|
(e.chips && e.chips.length ? `<h4>Verified presets <span>${e.chips.length} accepted values</span></h4><div class="pr-tools"><input type="search" placeholder="Filter ${e.chips.length} presets" data-act="chipfilter" aria-label="Filter presets"><button class="btn" data-act="copyall" data-ep="${esc(api.id)}">Copy all</button><button class="btn" data-act="copyjson" data-ep="${esc(api.id)}">Copy as JSON</button><button class="btn" data-act="copysel" data-ep="${esc(api.id)}">Copy selected <span data-n>0</span></button><button class="btn" data-act="clearsel" data-ep="${esc(api.id)}">Clear</button></div><div class="chips">${e.chips.map(c=>`<button class="chip" data-act="chip" data-v="${esc(c)}" title="Click to copy: ${esc(c)}">${esc(c)}</button>`).join('')}</div>` : '') +
|
|
`<h4>Response</h4><div class="resp"><code>${esc(e.resp||'—')}</code><span>${/json/.test(e.resp)?'Parsed JSON body':'Binary image body, write it straight to a file'}</span></div>` +
|
|
`<h4>Try it</h4><div class="code" data-code><div class="ctab"><button class="on" data-act="tab" data-t="curl">cURL</button><button data-act="tab" data-t="py">Python</button><button data-act="tab" data-t="js">JavaScript</button><button class="cp" data-act="copycode">Copy</button></div><pre data-t="curl">${esc(sn.curl)}</pre><pre data-t="py" hidden>${esc(sn.py)}</pre><pre data-t="js" hidden>${esc(sn.js)}</pre></div>` +
|
|
(sn.omitted?`<p class="hint">${sn.omitted} optional parameter${sn.omitted>1?'s are':' is'} left out of these examples.</p>`:'');
|
|
}
|
|
|
|
function epHTML(e, s){
|
|
const subEps = (e.endpoints && e.endpoints.length) ? e.endpoints : [e];
|
|
const displayPath = e.full_url || (base(e) + e.path);
|
|
|
|
// Exact 3-column .eh grid structure from original: col1: method, col2: info, col3: stats & chevron
|
|
return `<article class="ep" id="${esc(e.id)}" data-sec="${esc(s.id)}">
|
|
<button class="eh" data-act="toggle" aria-expanded="false">
|
|
<span class="m m-${esc(e.method)}">${esc(e.method)}</span>
|
|
<span style="min-width:0;">
|
|
<span class="et">${esc(e.api_key || e.title)}</span>
|
|
<code class="ep-path">${esc(displayPath)}</code>
|
|
<span class="es">${esc(e.summary)}</span>
|
|
</span>
|
|
<span class="ek">
|
|
<span style="background:var(--soft);padding:3px 8px;border-radius:6px;">${subEps.length} endpoint${subEps.length===1?'':'s'}</span>
|
|
${e.chips && e.chips.length ? `<span>${e.chips.length} presets</span>` : ''}
|
|
<span class="chev">${ICON}</span>
|
|
</span>
|
|
</button>
|
|
<div class="eb"><div class="ei"><div class="ew"></div></div></div>
|
|
</article>`;
|
|
}
|
|
|
|
const find=id=>{
|
|
for(const s of D.sections){
|
|
const e=s.eps.find(x=>x.id===id);
|
|
if(e) return {e, s};
|
|
}
|
|
};
|
|
|
|
function render(){
|
|
const all = D.sections.flatMap(s=>s.eps);
|
|
const pre = all.reduce((n,e)=>n+(e.chips?e.chips.length:0), 0);
|
|
const ports = new Set(all.map(e=>String(e.port).match(/^\\d+/)?.[0]).filter(Boolean));
|
|
const m = D.meta;
|
|
|
|
document.title = m.title;
|
|
$('#app').innerHTML = `
|
|
<nav class="rail" aria-label="Endpoint families">
|
|
<a class="brand" href="#top">
|
|
<svg viewBox="0 0 34 34">
|
|
<rect width="34" height="34" rx="10" fill="var(--ink)"/>
|
|
<circle cx="17" cy="17" r="8" fill="none" stroke="var(--paper)" stroke-width="2.4"/>
|
|
<path d="M17 9v8l6 4" stroke="hsl(172 70% 70%)" stroke-width="2.4" fill="none" stroke-linecap="round"/>
|
|
</svg>
|
|
<div>
|
|
<b>${esc(m.title.replace(/ API Reference$/,''))}</b>
|
|
<span>API reference</span>
|
|
</div>
|
|
</a>
|
|
${D.sections.map(s=>`
|
|
<a class="fam" href="#${esc(s.id)}" data-s="${esc(s.id)}" style="--h:${+s.hue||250}">
|
|
<i></i>${esc(s.title)}<em>${s.eps.length}</em>
|
|
</a>
|
|
`).join('')}
|
|
<div class="railfoot">
|
|
<div style="margin-bottom:8px;">Press <b>/</b> to search APIs.</div>
|
|
<div>Auth: <code>secretKey='abcdef'</code></div>
|
|
</div>
|
|
</nav>
|
|
<div class="shell">
|
|
<header class="top">
|
|
<button class="find" data-act="pal">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2">
|
|
<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>
|
|
</svg>
|
|
Search by API Key, endpoint path or presets
|
|
<kbd>/</kbd>
|
|
</button>
|
|
<span class="sp"></span>
|
|
<div class="session-pill" id="sessionPill" title="Security session expires in 10 minutes">
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
|
<circle cx="12" cy="12" r="10"/>
|
|
<polyline points="12 6 12 12 16 14"/>
|
|
</svg>
|
|
<span id="timerCountdown">10:00</span>
|
|
</div>
|
|
<a href="/docs/logout" class="btn sm" style="text-decoration:none;" title="Log out of documentation">Log out</a>
|
|
<button class="btn" data-act="all">Expand all</button>
|
|
<button class="btn" data-act="theme" aria-label="Switch theme">Theme</button>
|
|
<a href="/docs/editor" target="_blank" class="btn btn-admin" title="Documentation Admin Panel & Editor">
|
|
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
|
<path d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z"/>
|
|
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1Z"/>
|
|
</svg>
|
|
Admin
|
|
</a>
|
|
</header>
|
|
<main id="top">
|
|
<section class="hero">
|
|
<h1>${esc(m.heading || m.title)}</h1>
|
|
<p class="lead">${esc(m.intro)}</p>
|
|
<div class="facts">
|
|
<div><b>${all.length}</b>APIs</div>
|
|
<div><b>${D.sections.length}</b>families</div>
|
|
<div><b>${ports.size}</b>ports in use</div>
|
|
<div><b>${pre.toLocaleString()}</b>verified presets</div>
|
|
</div>
|
|
${m.authNote ? `<p class="note">${esc(m.authNote)}</p>` : ''}
|
|
</section>
|
|
${D.sections.map(s=>`
|
|
<section class="fsec" id="${esc(s.id)}" style="--h:${+s.hue||250}">
|
|
<div class="fhead">
|
|
<h2>${esc(s.title)}</h2>
|
|
<div class="tag">
|
|
${s.tag ? `<span>${esc(s.tag)}</span>` : ''}
|
|
${s.port ? `<span>Port <code>${esc(s.port)}</code></span>` : ''}
|
|
<span>${s.eps.length} API${s.eps.length===1?'':'s'}</span>
|
|
</div>
|
|
${s.lead ? `<p>${esc(s.lead)}</p>` : ''}
|
|
</div>
|
|
${s.eps.map(e=>epHTML(e, s)).join('')}
|
|
</section>
|
|
`).join('')}
|
|
</main>
|
|
</div>
|
|
<div class="pal" hidden data-act="palbg">
|
|
<div class="pbox" role="dialog" aria-label="Search APIs">
|
|
<input id="pq" placeholder="Search ${all.length} APIs (name, path, preset)" autocomplete="off">
|
|
<div class="pl" id="pl"></div>
|
|
</div>
|
|
</div>
|
|
<div class="toast" id="toast" hidden></div>
|
|
`;
|
|
|
|
// Reopen cards that were previously open
|
|
open.forEach(id => {
|
|
const a = document.getElementById(id);
|
|
if (a) setOpen(a, true);
|
|
});
|
|
updateSessionDisplay();
|
|
}
|
|
|
|
function setOpen(a, on){
|
|
const f = find(a.id);
|
|
if (!f) return;
|
|
a.classList.toggle('open', on);
|
|
$('.eh', a)?.setAttribute('aria-expanded', on);
|
|
a.style.setProperty('--h', f.s.hue);
|
|
a.style.setProperty('--a', `hsl(${+f.s.hue||250} 55% 40%)`);
|
|
const w = $('.ew', a);
|
|
if (on && w && !w.innerHTML) {
|
|
w.innerHTML = detail(f.e, f.s);
|
|
}
|
|
on ? open.add(a.id) : open.delete(a.id);
|
|
}
|
|
|
|
function toast(t){
|
|
const n = $('#toast');
|
|
if (!n) return;
|
|
n.textContent = t;
|
|
n.hidden = false;
|
|
clearTimeout(toastT);
|
|
toastT = setTimeout(() => n.hidden = true, 2000);
|
|
}
|
|
|
|
async function copy(t, msg){
|
|
try {
|
|
await navigator.clipboard.writeText(t);
|
|
} catch(err){
|
|
const x = document.createElement('textarea');
|
|
x.value = t;
|
|
document.body.append(x);
|
|
x.select();
|
|
document.execCommand('copy');
|
|
x.remove();
|
|
}
|
|
toast(msg || 'Copied');
|
|
}
|
|
|
|
function goto(id){
|
|
const a = document.getElementById(id);
|
|
if (!a) return;
|
|
setOpen(a, true);
|
|
history.replaceState(null, '', '#' + id);
|
|
setTimeout(() => a.scrollIntoView({behavior: 'smooth', block: 'start'}), 60);
|
|
}
|
|
|
|
let ro;
|
|
function spy(){
|
|
ro && ro.disconnect();
|
|
ro = new IntersectionObserver(es => es.forEach(en => {
|
|
if (en.isIntersecting) {
|
|
$$('.fam').forEach(f => f.classList.toggle('on', f.dataset.s === en.target.id));
|
|
}
|
|
}), {rootMargin: '-20% 0px -70% 0px'});
|
|
$$('.fsec').forEach(s => ro.observe(s));
|
|
}
|
|
|
|
function palList(q){
|
|
const t = q.toLowerCase().split(/\\s+/).filter(Boolean);
|
|
const out = [];
|
|
for (const s of D.sections) {
|
|
for (const e of s.eps) {
|
|
const subPaths = (e.endpoints || []).map(x => x.subpath || x.path).join(' ');
|
|
const hay = [e.api_key, e.title, e.path, e.full_url, e.summary, e.desc, s.title, subPaths, ...(e.chips || [])].join(' ').toLowerCase();
|
|
if (t.every(w => hay.includes(w))) {
|
|
out.push(e);
|
|
}
|
|
}
|
|
}
|
|
return out.slice(0, 40);
|
|
}
|
|
|
|
let pi = 0, pr = [];
|
|
function palDraw(){
|
|
const l = $('#pl');
|
|
pr = palList($('#pq').value);
|
|
pi = Math.min(pi, Math.max(pr.length - 1, 0));
|
|
l.innerHTML = pr.length ? pr.map((x, i) => `
|
|
<button class="pi ${i === pi ? 'on' : ''}" data-act="pick" data-id="${esc(x.id)}">
|
|
<span class="m m-${esc(x.method)}">${esc(x.method)}</span>
|
|
<span><b>${esc(x.api_key || x.title)}</b><code>${esc(x.full_url || x.path)}</code></span>
|
|
</button>
|
|
`).join('') : '<p class="pe">No matching APIs found.</p>';
|
|
$('.pi.on')?.scrollIntoView({block: 'nearest'});
|
|
}
|
|
|
|
function palOpen(on){
|
|
const p = $('.pal');
|
|
p.hidden = !on;
|
|
if (on) {
|
|
$('#pq').value = '';
|
|
pi = 0;
|
|
palDraw();
|
|
$('#pq').focus();
|
|
}
|
|
}
|
|
|
|
/* Event delegation */
|
|
document.addEventListener('click', ev => {
|
|
const t = ev.target.closest('[data-act]');
|
|
if (!t) return;
|
|
const a = t.dataset.act, ep = t.closest('.ep');
|
|
|
|
if (a === 'palbg') {
|
|
if (ev.target === t) palOpen(false);
|
|
return;
|
|
}
|
|
if (a === 'toggle' && ep) {
|
|
setOpen(ep, !ep.classList.contains('open'));
|
|
} else if (a === 'subtab') {
|
|
const apiId = t.dataset.api;
|
|
const idx = +t.dataset.idx;
|
|
activeSubEp[apiId] = idx;
|
|
const art = document.getElementById(apiId);
|
|
const f = find(apiId);
|
|
if (art && f) {
|
|
const w = $('.ew', art);
|
|
if (w) w.innerHTML = detail(f.e, f.s);
|
|
}
|
|
} else if (a === 'copyurl') {
|
|
const url = t.dataset.url;
|
|
copy(url, 'Copied full URL');
|
|
} else if (a === 'chip') {
|
|
// Clicking preset chip copies that preset directly
|
|
const val = t.dataset.v || t.textContent;
|
|
copy(val, 'Copied preset: ' + val);
|
|
t.classList.add('copied');
|
|
setTimeout(() => t.classList.remove('copied'), 1200);
|
|
} else if (a === 'copyall') {
|
|
const apiId = t.dataset.ep;
|
|
const f = find(apiId);
|
|
if (f && f.e.chips) copy(f.e.chips.join(', '), 'Copied all presets');
|
|
} else if (a === 'copyjson') {
|
|
const apiId = t.dataset.ep;
|
|
const f = find(apiId);
|
|
if (f && f.e.chips) copy(JSON.stringify(f.e.chips), 'Copied presets as JSON');
|
|
} else if (a === 'copysel') {
|
|
const v = $$('.chip.sel', ep).map(c => c.dataset.v || c.textContent);
|
|
v.length ? copy(v.join(', '), `Copied ${v.length} presets`) : toast('Click presets to select first');
|
|
} else if (a === 'clearsel') {
|
|
$$('.chip.sel', ep).forEach(c => c.classList.remove('sel'));
|
|
$('[data-n]', ep).textContent = '0';
|
|
} else if (a === 'pal') {
|
|
palOpen(true);
|
|
} else if (a === 'pick') {
|
|
palOpen(false);
|
|
goto(t.dataset.id);
|
|
} else if (a === 'theme') {
|
|
const n = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
|
|
document.documentElement.dataset.theme = n;
|
|
try { localStorage.setItem('apidocs-theme', n); } catch(e){}
|
|
} else if (a === 'all') {
|
|
const on = t.textContent === 'Expand all';
|
|
$$('.ep').forEach(x => setOpen(x, on));
|
|
t.textContent = on ? 'Collapse all' : 'Expand all';
|
|
} else if (a === 'tab') {
|
|
const box = t.closest('[data-code]');
|
|
$$('.ctab button[data-t]', box).forEach(b => b.classList.toggle('on', b === t));
|
|
$$('pre', box).forEach(p => p.hidden = p.dataset.t !== t.dataset.t);
|
|
} else if (a === 'copycode') {
|
|
copy($$('pre', t.closest('[data-code]')).find(p => !p.hidden).textContent, 'Copied snippet');
|
|
}
|
|
});
|
|
|
|
document.addEventListener('input', ev => {
|
|
const t = ev.target;
|
|
if (t.id === 'pq') {
|
|
pi = 0;
|
|
palDraw();
|
|
} else if (t.dataset.act === 'chipfilter') {
|
|
const q = t.value.toLowerCase();
|
|
$$('.chip', t.closest('.ew')).forEach(c => {
|
|
const v = (c.dataset.v || c.textContent).toLowerCase();
|
|
c.hidden = !!q && !v.includes(q);
|
|
});
|
|
}
|
|
});
|
|
|
|
document.addEventListener('keydown', ev => {
|
|
const p = $('.pal'), typing = /INPUT|TEXTAREA/.test(document.activeElement.tagName);
|
|
if (((ev.ctrlKey || ev.metaKey) && ev.key === 'k') || (ev.key === '/' && !typing)) {
|
|
ev.preventDefault();
|
|
palOpen(true);
|
|
} else if (p && !p.hidden) {
|
|
if (ev.key === 'Escape') palOpen(false);
|
|
else if (ev.key === 'ArrowDown') { ev.preventDefault(); pi = Math.min(pi + 1, pr.length - 1); palDraw(); }
|
|
else if (ev.key === 'ArrowUp') { ev.preventDefault(); pi = Math.max(pi - 1, 0); palDraw(); }
|
|
else if (ev.key === 'Enter' && pr[pi]) { palOpen(false); goto(pr[pi].id); }
|
|
}
|
|
});
|
|
|
|
/* ---------- 10-Minute Session Auto-Logout ---------- */
|
|
let sessionRemaining = 600;
|
|
async function checkSession(){
|
|
try {
|
|
const res = await fetch('/docs/session');
|
|
if (!res.ok) {
|
|
window.location.href = '/docs/logout';
|
|
return;
|
|
}
|
|
const data = await res.json();
|
|
if (data.authenticated && typeof data.remaining_seconds === 'number') {
|
|
sessionRemaining = data.remaining_seconds;
|
|
updateSessionDisplay();
|
|
} else {
|
|
window.location.href = '/docs/logout';
|
|
}
|
|
} catch(err){}
|
|
}
|
|
|
|
function updateSessionDisplay(){
|
|
const el = document.getElementById('timerCountdown');
|
|
const pill = document.getElementById('sessionPill');
|
|
if (!el) return;
|
|
const m = Math.floor(sessionRemaining / 60);
|
|
const s = sessionRemaining % 60;
|
|
el.textContent = `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
|
|
if (pill) {
|
|
pill.classList.toggle('warning', sessionRemaining <= 60);
|
|
}
|
|
}
|
|
|
|
setInterval(() => {
|
|
sessionRemaining--;
|
|
if (sessionRemaining <= 0) {
|
|
toast('Session expired after 10 minutes. Logging out...');
|
|
setTimeout(() => {
|
|
window.location.href = '/docs/logout';
|
|
}, 600);
|
|
return;
|
|
}
|
|
updateSessionDisplay();
|
|
}, 1000);
|
|
|
|
setInterval(checkSession, 25000);
|
|
checkSession();
|
|
|
|
/* PostMessage Editor Communication */
|
|
function exportHtml(data){
|
|
const c = document.documentElement.cloneNode(true);
|
|
c.removeAttribute('data-theme');
|
|
c.removeAttribute('style');
|
|
$('#app', c).innerHTML = '';
|
|
$('#api-data', c).textContent = JSON.stringify(data).replace(/</g, '\\\\u003c');
|
|
$('title', c).textContent = data.meta.title;
|
|
return '<!DOCTYPE html>\\n' + c.outerHTML;
|
|
}
|
|
|
|
addEventListener('message', ev => {
|
|
const m = ev.data;
|
|
if (!m || m.apidocs !== 1) return;
|
|
const re = o => ev.source && ev.source.postMessage({ apidocs: 1, ...o }, '*');
|
|
if (m.type === 'hello') {
|
|
re({ type: 'data', schema: 1, data: D, file: decodeURIComponent(location.pathname.split('/').pop()) });
|
|
} else if (m.type === 'render') {
|
|
D = m.data;
|
|
render();
|
|
} else if (m.type === 'focus') {
|
|
goto(m.id);
|
|
} else if (m.type === 'export') {
|
|
re({ type: 'html', html: exportHtml(m.data || D) });
|
|
}
|
|
});
|
|
|
|
render();
|
|
spy();
|
|
if (location.hash.length > 1) {
|
|
setTimeout(() => goto(decodeURIComponent(location.hash.slice(1))), 200);
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
full_html = html_head + json_tag + js_code
|
|
|
|
with open("web/docs/index.html", "w") as f:
|
|
f.write(full_html)
|
|
|
|
with open("web/docs/FASTAPI_BACKEND_API_DOCS.html", "w") as f:
|
|
f.write(full_html)
|
|
|
|
print("Generated clean, responsive index.html and FASTAPI_BACKEND_API_DOCS.html successfully!")
|