import {z} from 'zod' export type GetApiApiPartsDocFormatArgs = {doc: string, format: 'json' | 'html', odd?: string | undefined, view?: ('page' | 'div' | 'single') | undefined, root?: string | undefined, id?: string | undefined, xpath?: string | undefined, map?: string | undefined, highlight?: string | undefined, serialize?: ('xml' | 'html5') | undefined, wrap?: boolean | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiApiPartsDocFormatOptions = {baseUrl: string, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPartsDocFormatResponse = z.object({view: z.string().optional(), doc: z.string().optional(), root: z.string().nullable().optional(), odd: z.string().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), switchView: z.string().nullable().optional(), content: z.string().optional(), footnotes: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Retrieve parts of a document transformed via ODD * * GET /api/parts/{doc}/{format} * * Also accepts auth: basicAuth */ export const getApiApiPartsDocFormat = async (args: GetApiApiPartsDocFormatArgs, options: GetApiApiPartsDocFormatOptions) => { const {baseUrl, fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.odd != null) query.append("odd", String(args.odd)) if (args.view != null) query.append("view", String(args.view)) if (args.root != null) query.append("root", String(args.root)) if (args.id != null) query.append("id", String(args.id)) if (args.xpath != null) query.append("xpath", String(args.xpath)) if (args.map != null) query.append("map", String(args.map)) if (args.highlight != null) query.append("highlight", String(args.highlight)) if (args.serialize != null) query.append("serialize", String(args.serialize)) if (args.wrap != null) query.append("wrap", String(args.wrap)) if (args['If-Modified-Since'] != null) headers.set("If-Modified-Since", String(args['If-Modified-Since'])) if (cookieAuth !== undefined) cookies.push("teipublisher.com.login=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/parts/${encodeURIComponent(String(args.doc))}/${encodeURIComponent(String(args.format))}${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 validateResponse(getApiApiPartsDocFormatResponse, await res.json()) };