import {z} from 'zod' export type GetApiV2CdnTaxonomyTermsIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const taxonomyTerm: z.ZodType = z.object({id: z.string(), display_name: z.string(), name: z.string(), description: z.string().nullable(), parent_id: z.string().nullable(), last_author: z.object({id: z.number().int(), userid: z.string(), friendly_name: z.string(), avatar: z.string().nullable().optional()}).nullable(), last_author_id: z.string().nullable(), created_at: z.string(), updated_at: z.string(), children: z.array(z.lazy(() => taxonomyTerm)).optional(), terms_count: z.number().int().optional(), last_activity_at: z.string().optional(), associated_content: z.array(z.object({id: z.string(), type: z.literal("Story"), name: z.string(), full_slug: z.string().optional(), updated_at: z.string()})).optional()}); export const getApiV2CdnTaxonomyTermsIdResponse = z.object({taxonomy_term: taxonomyTerm, cv: z.number().int().nullable()}); 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 a Single Taxonomy Term * * GET /v2/cdn/taxonomy_terms/{id} */ export const getApiV2CdnTaxonomyTermsId = async (options?: GetApiV2CdnTaxonomyTermsIdOptions) => { const {baseUrl = "https://api.storyblok.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cdn/taxonomy_terms/${encodeURIComponent(String(args.id))}${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(getApiV2CdnTaxonomyTermsIdResponse, await res.json()) };