import {z} from 'zod' export type GetApiAuthoringV1ContentArgs = {offset?: number | undefined, fields?: string | undefined, limit?: number | undefined}; export type GetApiAuthoringV1ContentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiAuthoringV1ContentResponse = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), href: z.string().optional(), next: z.string().optional(), previous: z.string().optional(), items: z.array(z.object({id: z.string().optional(), rev: z.string().optional(), name: z.string().optional(), description: z.string().optional(), typeId: z.string().optional(), type: z.string().optional(), kind: z.string().optional(), lastModified: z.string().optional(), lastModifierId: z.string().optional(), lastModifier: z.string().optional(), created: z.string().optional(), creatorId: z.string().optional(), creator: z.string().optional(), classification: z.literal("content").optional(), status: z.enum(["draft", "ready", "retired"]).optional(), linkedDocId: z.string().optional(), elements: z.object({}).optional(), tags: z.array(z.string()).optional(), links: z.object({self: z.object({href: z.string().optional()}).optional(), linkedDoc: z.object({href: z.string().optional()}).optional(), draft: z.object({href: z.string().optional()}).optional(), "create-draft": z.object({href: z.string().optional()}).optional(), ready: z.object({href: z.string().optional()}).optional(), retire: z.object({href: z.string().optional()}).optional(), type: z.object({href: z.string().optional()}).optional()}).optional(), thumbnail: z.object({id: z.string().optional(), path: z.string().optional(), url: z.string().optional()}).optional(), isSystem: z.boolean().optional(), libraryId: z.string().optional()})).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 }; /** * List all content items in the database. * * GET /authoring/v1/content */ export const getApiAuthoringV1Content = async (args: GetApiAuthoringV1ContentArgs, options: GetApiAuthoringV1ContentOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/authoring/v1/content${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(getApiAuthoringV1ContentResponse, await res.json()) };