export type GetApiDocidArgs = {docid: string, template?: string | undefined, view?: ('page' | 'div' | 'single') | undefined, odd?: string | undefined, action?: string | undefined}; export type GetApiDocidOptions = {baseUrl: string, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; /** * Retrieve the HTML template used for displaying a document * * GET /{docid} * * Also accepts auth: basicAuth */ export const getApiDocid = async (args: GetApiDocidArgs, options: GetApiDocidOptions) => { const {baseUrl, fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "text/html") if (args.template != null) query.append("template", String(args.template)) if (args.view != null) query.append("view", String(args.view)) if (args.odd != null) query.append("odd", String(args.odd)) if (args.action != null) query.append("action", String(args.action)) if (cookieAuth !== undefined) cookies.push("teipublisher.com.login=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.docid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.text() };