import {z} from 'zod' export type GetApiAuthoringV1CategoriesArgs = {offset?: number | undefined, limit?: number | undefined}; export type GetApiAuthoringV1CategoriesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiAuthoringV1CategoriesResponse = 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(), rev: z.string(), name: z.string(), classification: z.string(), description: z.string().optional(), creatorId: z.string().optional(), permanent: z.boolean().optional(), hidden: z.boolean().optional(), firstClassFor: z.string().optional(), parent: z.string().optional(), ancestorIds: z.array(z.string()).optional(), namePath: z.array(z.string()), tags: z.array(z.string()).optional(), links: z.object({self: z.object({href: z.string().optional()}).optional(), taxonomy: z.object({href: z.string().optional()}).optional(), parent: z.object({href: z.string().optional()}).optional()}), lastModified: z.string(), created: z.string()})).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 all the taxonomies. * * GET /authoring/v1/categories */ export const getApiAuthoringV1Categories = async (args: GetApiAuthoringV1CategoriesArgs, options: GetApiAuthoringV1CategoriesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/authoring/v1/categories${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(getApiAuthoringV1CategoriesResponse, await res.json()) };