import {z} from 'zod' export type GetApiV2CdnStoriesIdArgs = {id: string, find_by?: string | undefined, version?: ('draft' | 'published') | undefined, resolve_links?: ('story' | 'url' | 'link') | undefined, resolve_links_level?: (1 | 2) | undefined, resolve_links_in_relations?: 1 | undefined, resolve_relations?: string | undefined, from_release?: string | undefined, content_type?: string | undefined, cv?: number | undefined, excluding_fields?: string | undefined, including_fields?: string | undefined, excluding_story_fields?: string | undefined, language?: string | undefined, fallback_lang?: string | undefined, resolve_assets?: number | undefined, resolve_level?: 2 | undefined, token: string}; export type GetApiV2CdnStoriesIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const storyAlternate = z.object({id: z.number().int(), name: z.string(), slug: z.string(), published: z.boolean().nullable(), full_slug: z.string(), is_folder: z.boolean(), parent_id: z.number().int()}); export const draftStory = z.object({name: z.string(), created_at: z.string(), updated_at: z.string().nullable().optional(), published_at: z.string().nullable(), alternates: z.array(storyAlternate), id: z.number().int(), uuid: z.string(), content: z.object({_uid: z.string().optional(), component: z.string().optional()}).and(z.record(z.string(), z.unknown())), slug: z.string(), full_slug: z.string(), default_full_slug: z.string().nullable(), sort_by_date: z.string().nullable(), position: z.number().int(), tag_list: z.array(z.string()), is_startpage: z.boolean(), parent_id: z.number().int(), meta_data: z.record(z.string(), z.unknown()).nullable(), group_id: z.string(), first_published_at: z.string().nullable(), release_id: z.number().int().nullable(), lang: z.string(), path: z.string().nullable(), translated_slugs: z.array(z.object({path: z.string(), name: z.string().nullable(), lang: z.string(), published: z.boolean().nullable()})).nullable(), taxonomy_terms: z.array(z.object({id: z.string(), display_name: z.string(), name: z.string(), taxonomy_id: z.string()})).optional()}); export const publishedStory = z.object({name: z.string(), created_at: z.string(), updated_at: z.string().nullable().optional(), published_at: z.string().nullable(), id: z.number().int(), uuid: z.string(), content: z.object({_uid: z.string().optional(), component: z.string().optional()}).and(z.record(z.string(), z.unknown())), slug: z.string(), full_slug: z.string(), sort_by_date: z.string().nullable(), position: z.number().int(), tag_list: z.array(z.string()), is_startpage: z.boolean(), parent_id: z.number().int(), meta_data: z.record(z.string(), z.unknown()).nullable(), group_id: z.string(), first_published_at: z.string().nullable(), release_id: z.number().int().nullable().optional(), lang: z.string(), path: z.string().nullable(), alternates: z.array(storyAlternate), default_full_slug: z.string().nullable(), translated_slugs: z.array(z.object({path: z.string(), name: z.string().nullable(), lang: z.string(), published: z.boolean().nullable()})).nullable(), taxonomy_terms: z.array(z.object({id: z.string(), display_name: z.string(), name: z.string(), taxonomy_id: z.string()})).optional()}); export const story = z.union([draftStory, publishedStory]); export const alternate = z.object({path: z.string(), name: z.string(), lang: z.string(), published: z.boolean(), translated_slug: z.string()}); export const linkWithFullSlug = z.object({id: z.number().int(), uuid: z.string(), slug: z.string(), path: z.string().nullable().optional(), real_path: z.string().nullable().optional(), name: z.string(), published: z.boolean(), parent_id: z.number().int(), is_folder: z.boolean(), is_startpage: z.boolean(), position: z.number().int(), published_at: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), alternates: z.array(alternate).optional(), full_slug: z.string()}); export const resolvedLinkUrl = z.object({id: z.number().int(), uuid: z.string(), name: z.string(), slug: z.string(), full_slug: z.string(), url: z.string()}); export const link = z.object({id: z.number().int(), uuid: z.string(), slug: z.string(), path: z.string().nullable().optional(), real_path: z.string().nullable().optional(), name: z.string(), published: z.boolean(), parent_id: z.number().int(), is_folder: z.boolean(), is_startpage: z.boolean(), position: z.number().int(), published_at: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), alternates: z.array(alternate).optional()}); export const getApiV2CdnStoriesIdResponse = z.object({story: story, cv: z.number().int(), rels: z.array(story).optional(), links: z.array(z.union([linkWithFullSlug, resolvedLinkUrl, link, story])).optional(), rel_uuids: z.array(z.string()).optional(), link_uuids: z.array(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 a Single Story * * GET /v2/cdn/stories/{id} */ export const getApiV2CdnStoriesId = async (args: GetApiV2CdnStoriesIdArgs, options?: GetApiV2CdnStoriesIdOptions) => { 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 (args.find_by != null) query.append("find_by", String(args.find_by)) if (args.version != null) query.append("version", String(args.version)) if (args.resolve_links != null) query.append("resolve_links", String(args.resolve_links)) if (args.resolve_links_level != null) query.append("resolve_links_level", String(args.resolve_links_level)) if (args.resolve_links_in_relations != null) query.append("resolve_links_in_relations", String(args.resolve_links_in_relations)) if (args.resolve_relations != null) query.append("resolve_relations", String(args.resolve_relations)) if (args.from_release != null) query.append("from_release", String(args.from_release)) if (args.content_type != null) query.append("content_type", String(args.content_type)) if (args.cv != null) query.append("cv", String(args.cv)) if (args.excluding_fields != null) query.append("excluding_fields", String(args.excluding_fields)) if (args.including_fields != null) query.append("including_fields", String(args.including_fields)) if (args.excluding_story_fields != null) query.append("excluding_story_fields", String(args.excluding_story_fields)) if (args.language != null) query.append("language", String(args.language)) if (args.fallback_lang != null) query.append("fallback_lang", String(args.fallback_lang)) if (args.resolve_assets != null) query.append("resolve_assets", String(args.resolve_assets)) if (args.resolve_level != null) query.append("resolve_level", String(args.resolve_level)) if (args.token != null) query.append("token", String(args.token)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cdn/stories/${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(getApiV2CdnStoriesIdResponse, await res.json()) };