paste-bin/ui/client.js
Jarian 985e070757 port: FastAPI rewrite built by the arch-llm spec pipeline
Spec-first port of the Flask app (still on main):
- schema.arch: signed arch-llm spec (single source of truth)
- paste_bin/: generated FastAPI backend, verified 51/51 frozen owner
  tests, 92.5% coverage (build r3, 2026-11-18)
- ui/: SPA + emitted OpenAPI contract (openapi.json + generated
  client.js); the app calls only window.PasteBinAPI
- 7 old bugs fixed (file raw 500, silent expiry, AVI-as-webp,
  stripped content, limiter leak, trusted mimetype, stale tmp)
- 62/62 live probe green; /health + JSON index added; JSON API

Full report on the wiki: 'FastAPI port (arch-llm pipeline)'.
2026-11-18 23:00:23 +00:00

101 lines
4.1 KiB
JavaScript

// AUTO-GENERATED — do not edit by hand.
// source: openapi v1.0.0 (title: paste_bin)
// generator: archllm contract emission (pure function of the OpenAPI document)
// regenerate: archllm openapi <target> --client
"use strict";
(() => {
const API = { title: "paste_bin", version: "1.0.0", base: '' };
// pinned error literals (x-error-literals — exact strings from the signed spec)
const ERROR_MESSAGES = {
"POST /pastes": {
400: ["Content cannot be empty", "File content does not match image type.", "Invalid expiry", "Invalid image format", "Invalid paste type", "No file selected"],
413: ["File too large. Maximum size is 20 MB."],
429: ["Too many uploads. Please wait."],
},
"GET /pastes/{paste_id}": {
404: ["Paste not found"],
},
"GET /pastes/{paste_id}/download": {
404: ["Paste not found"],
},
"GET /pastes/{paste_id}/raw": {
404: ["Paste not found"],
},
};
// path-parameter patterns (from the contract)
const PASTE_ID_RE = /^[a-f0-9]{16}$/;
class ApiError extends Error {
constructor(status, message) { super(`[${status}] ${message}`); this.status = status; this.apiError = true; }
}
async function _req(method, path, body) {
const opts = { method };
if (body) opts.body = body;
const r = await fetch(API.base + path, opts);
const ct = r.headers.get("content-type") || "";
const data = ct.includes("application/json") ? await r.json() : null;
if (!r.ok) throw new ApiError(r.status, (data && data.error) || r.statusText);
return data;
}
const enc = encodeURIComponent;
// response/request shapes (components.schemas)
/** @typedef { type: string, title: string, created_at: string, expires_at: string | null, filename: string | null, mimetype: string | null, filepath: string | null } Paste */
/** @typedef { error: string } ErrOut */
/** @typedef { id: string, title: string, type: string, created_at: string, expires_at: string | null, view: string, raw: string, download: string } PasteOut */
/** @typedef { id: string, type: string, title: string, created_at: string, expires_at: string | null, content: string | null, filename: string | null, mimetype: string | null } ViewOut */
/** @typedef { status: string } HealthOut */
/** @typedef { service: string, endpoints: any[] } IndexOut */
// POST /pastes — multipart/form-data fields; ALL form fields have defaults so file-only uploads work withou
async function postPastes({paste_type = "text", title = "", expiry = "1d", content = "", file = null} = {}) {
const fd = new FormData();
fd.append("paste_type", paste_type);
fd.append("title", title);
fd.append("expiry", expiry);
fd.append("content", content);
if (file) fd.append("file", file);
return _req("POST", `/pastes`, fd);
}
// GET /pastes/{paste_id} — load_meta None -> 404 {error: 'Paste not found'}; expired -> store.delete then 404 same bo
async function getPastesPaste_id(paste_id) {
return _req("GET", `/pastes/${enc(paste_id)}`);
}
// GET /pastes/{paste_id}/download — same 404/expiry-delete handling (uploaded file path read from meta['filepath']); text -> t
function getPastesPaste_idDownloadUrl(paste_id) {
return API.base + `/pastes/${enc(paste_id)}/download`;
}
// GET /pastes/{paste_id}/raw — same 404/expiry-delete handling (uploaded file path read from meta['filepath']); text -> c
function getPastesPaste_idRawUrl(paste_id) {
return API.base + `/pastes/${enc(paste_id)}/raw`;
}
// GET /health — {status: 'ok'}
async function getHealth() {
return _req("GET", `/health`);
}
// GET / — {service: 'paste-bin', endpoints: ['/pastes', '/health']}
async function getIndex() {
return _req("GET", `/`);
}
// the OpenAPI document itself (serve it next to this client)
async function fetchContract() {
const r = await fetch(API.base + "/openapi.json");
return r.json();
}
window.PasteBinAPI = Object.freeze({
API, ERROR_MESSAGES, ApiError,
PASTE_ID_RE,
_req, postPastes, getPastesPaste_id, getPastesPaste_idDownloadUrl, getPastesPaste_idRawUrl, getHealth, getIndex, fetchContract,
});
})();