import type { AxisForm, Card, CardRef, Composed, FinishHow, Job, Options, Pack, Picture, Recipe, ReferenceCard, RefWay, Sheet, SheetRequest, Stats, Stroke, } from './types' type FixTool = 'anatomy' | 'text' | 'repaint' | 'letter' const BASE = '/nyxxworks' async function unwrap(response: Response): Promise { 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 } const get = (path: string) => fetch(BASE + path).then((response) => unwrap(response)) const post = (path: string, body: unknown) => fetch(BASE + path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then( (response) => unwrap(response), ) export const api = { options: () => get('/options'), library: () => get<{ sheets: Sheet[]; tray: Picture[]; keep?: number }>('/library'), jobs: () => get<{ jobs: Job[]; model?: string }>('/jobs'), sheet: (request: SheetRequest) => post<{ job: Job; sheet: string }>('/sheet', { request }), /** A sheet varied along one or two axes, every picture with the same seed. */ matrix: (request: SheetRequest, x: unknown, y: unknown) => post<{ job: Job; sheet: string }>('/matrix', { request, x, y }), /** A sheet of pictures like one that is there. */ like: (id: string, strength: number, count: number) => post<{ job: Job; sheet: string }>('/sheet', { like: { id, strength, count } }), finish: (ids: string[], how: FinishHow) => post<{ job: Job }>('/finish', { ids, how }), cancel: (job: string) => post<{ job: Job }>('/jobs/cancel', { job }), mark: (id: string, changes: Partial>) => post<{ picture?: Picture; cleared?: boolean; sheets?: Sheet[]; tray: Picture[] }>('/mark', { id, ...changes }), /** A picture from outside: kept as a sheet of one, and read for how it was made. */ takeIn: (file: Blob, name: string) => fetch(`${BASE}/import?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) => unwrap<{ picture: Picture; request: SheetRequest; read: boolean }>(response), ), /** Keep what the Fix station made of a picked picture. */ keep: (session: string, id: string, tool: FixTool) => post<{ picture: Picture; tray: Picture[] }>('/fix/keep', { session, id, tool }), /** Variants of a picked picture with a painted spot repainted by a description. */ repaint: (body: { id: string; strokes: Stroke[]; prompt: string; strength: number; count: number }) => post<{ job: Job }>('/fix/repaint', body), /** A variant chosen: it takes the place of the picked picture in the tray. */ promote: (id: string, variant: string) => post<{ picture: Picture; tray: Picture[] }>('/fix/promote', { id, variant }), /** The reference cards, and the ways this host can follow one. */ references: (model = '') => get<{ cards: ReferenceCard[]; ways: RefWay[] }>(`/references${model ? `?model=${encodeURIComponent(model)}` : ''}`), addReference: (file: Blob, name: string) => fetch(`${BASE}/references/add?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) => unwrap<{ card: ReferenceCard; cards: ReferenceCard[] }>(response), ), /** A picture of the library kept as a reference card. */ referenceFrom: (id: string) => post<{ card: ReferenceCard; cards: ReferenceCard[] }>('/references/from', { id }), deleteReference: (id: string) => post<{ cards: ReferenceCard[] }>('/references/delete', { id }), referenceUrl: (id: string, size: 'thumb' | 'full') => `${BASE}/reference/${id}/${size}`, recipes: () => get<{ recipes: Recipe[] }>('/recipes'), /** Keep the way of working the page has now as a recipe. */ saveRecipe: (body: { name: string; note: string; request: SheetRequest; finish: FinishHow | null; axes: { x: AxisForm; y: AxisForm } | null }) => post<{ recipe: Recipe; recipes: Recipe[] }>('/recipes/save', body), /** A recipe of how a picture was made. */ recipeFrom: (id: string) => post<{ recipe: Recipe; recipes: Recipe[] }>('/recipes/from', { id }), /** A recipe made ready for this host; `notes` say what of it this host does not have. */ useRecipe: (id: string) => post<{ recipe: Recipe; request: SheetRequest; finish: FinishHow | null; axes: Recipe['axes'] | null; notes: string[] }>('/recipes/use', { id }), renameRecipe: (id: string, name: string, note: string) => post<{ recipes: Recipe[] }>('/recipes/rename', { id, name, note }), deleteRecipe: (id: string) => post<{ recipes: Recipe[] }>('/recipes/delete', { id }), importRecipe: (file: Blob) => fetch(`${BASE}/recipes/import`, { method: 'POST', body: file }).then((response) => unwrap<{ recipe: Recipe; recipes: Recipe[] }>(response)), recipeFileUrl: (id: string) => `${BASE}/recipes/${id}/file`, stats: () => get('/stats'), /** Open the data folder on the host's machine: only from that machine, and only where the host allows it. */ openFolder: () => post>('/folder/open', {}), /** The packs of cards with their categories. */ cards: () => get<{ packs: Pack[]; total: number }>('/cards'), findCards: (source: string, category: string, query: string, offset: number) => get<{ total: number; offset: number; cards: Card[]; groups?: { source: string; category: string; count: number }[] }>( `/cards/find?source=${encodeURIComponent(source)}&category=${encodeURIComponent(category)}&q=${encodeURIComponent(query)}&offset=${offset}`, ), /** What a text and cards give together, to show before a sheet is asked for. */ compose: (prompt: string, negative: string, cards: CardRef[]) => post('/cards/compose', { prompt, negative, cards }), imageUrl: (id: string, size: 'thumb' | 'full') => `${BASE}/image/${id}/${size}`, }