sd-anatomy-fix / ui /src /api.ts
Kazzze's picture
Update to current version
3c9bc24 verified
Raw History Blame Contribute Delete
2.49 kB
import type { BoneItem, DraftInfo, Job, Line, Model, Options, Prompts, RunState, SessionInfo, Settings } from './types'
const BASE = '/anatomy_fix'
async function unwrap<T>(response: Response): Promise<T> {
let body: unknown = null
try {
body = await response.json()
} catch {
// not JSON: the status text has to do
}
if (!response.ok) {
const message = body && typeof body === 'object' && 'error' in body ? String((body as { error: unknown }).error) : ''
throw new Error(message || `${response.status} ${response.statusText}`)
}
return body as T
}
function post<T>(path: string, body: unknown): Promise<T> {
return fetch(BASE + path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}).then((response) => unwrap<T>(response))
}
export type ImageWhich = 'image' | 'draft' | 'levels' | 'guide' | 'result' | `variant-${number}`
export const api = {
options: () => fetch(`${BASE}/options`).then((response) => unwrap<Options>(response)),
upload: (file: Blob, name: string) =>
fetch(`${BASE}/session?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) =>
unwrap<SessionInfo>(response),
),
/** ComfyUI: the image the node last received. */
node: (node: string) => post<SessionInfo>('/session/node', { node }),
draft: (session: string, job: Job) => post<DraftInfo>('/draft', { session, job }),
fan: (line: Line, kind: string, thumb: string, spread: number, width: number) =>
post<{ bones: Omit<BoneItem, 'id'>[] }>('/fan', { line, kind, thumb, spread, width }),
run: (session: string, job: Job, model: Model & Prompts) => post<RunState>('/run', { session, job, model }),
runState: (session: string) => post<RunState>('/run/state', { session }),
stop: (session: string) => post<RunState>('/run/stop', { session }),
pick: (session: string, seed: number) => post<{ version: number }>('/pick', { session, seed }),
apply: (session: string) => post<SessionInfo>('/apply', { session }),
undo: (session: string) => post<SessionInfo>('/undo', { session }),
save: (session: string, tab: string) => post<{ path: string }>('/save', { session, tab }),
saveSettings: (settings: Settings) => post<{ settings: Settings }>('/settings', settings),
imageUrl: (session: string, which: ImageWhich, version: number) => `${BASE}/image/${session}/${which}?v=${version}`,
}