import {z} from 'zod' export type GetApiApiV1PagesArgs = {page_size?: number | undefined, cursor?: string | undefined, id?: Array | undefined, annotation?: Array | undefined, number?: Array | undefined, ordering?: ('id' | '-id' | 'number' | '-number' | 's3_name' | '-s3_name') | undefined}; export type GetApiApiV1PagesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const metadata = z.object({}); export const pageObject = z.object({id: z.number().int(), url: z.string(), annotation: z.string(), number: z.number().int(), rotation_deg: z.number().int(), mime_type: z.string(), s3_name: z.string(), content: z.string(), metadata: metadata, width: z.number().int().nullable(), height: z.number().int().nullable()}); export const getApiApiV1PagesResponse = z.object({pagination: pagination, results: z.array(pageObject)}); 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 }; /** * List pages * * GET /api/v1/pages */ export const getApiApiV1Pages = async (args: GetApiApiV1PagesArgs, options?: GetApiApiV1PagesOptions) => { const {baseUrl = "https://example.rossum.app", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.cursor != null) query.append("cursor", String(args.cursor)) for (const item of args.id ?? []) query.append("id", String(item)) for (const item of args.annotation ?? []) query.append("annotation", String(item)) for (const item of args.number ?? []) query.append("number", String(item)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/pages${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(getApiApiV1PagesResponse, await res.json()) };