import {z} from 'zod' export type GetApiV2CdnStoriesArgs = {version?: ('draft' | 'published') | undefined, cv?: number | undefined, starts_with?: string | undefined, search_term?: string | undefined, sort_by?: string | undefined, per_page?: number | undefined, page?: number | undefined, by_slugs?: string | undefined, excluding_slugs?: string | undefined, published_at_gt?: string | undefined, published_at_gte?: string | undefined, published_at_lt?: string | undefined, published_at_lte?: string | undefined, first_published_at_gt?: string | undefined, first_published_at_gte?: string | undefined, first_published_at_lt?: string | undefined, first_published_at_lte?: string | undefined, updated_at_gt?: string | undefined, updated_at_gte?: string | undefined, updated_at_lt?: string | undefined, updated_at_lte?: string | undefined, in_workflow_stages?: string | undefined, content_type?: string | undefined, level?: number | undefined, resolve_relations?: string | undefined, excluding_ids?: string | undefined, by_uuids?: string | undefined, by_uuids_ordered?: string | undefined, by_ids?: string | undefined, with_tag?: string | undefined, is_startpage?: (0 | 1) | undefined, resolve_links?: ('story' | 'url' | 'link') | undefined, resolve_links_level?: (1 | 2) | undefined, resolve_links_in_relations?: 1 | undefined, from_release?: string | undefined, fallback_lang?: string | undefined, language?: string | undefined, filter_query?: ({__or?: Array<{[key: string]: {[key: string]: string}}> | undefined} | {[key: string]: {in?: string | undefined, not_in?: string | undefined, like?: string | undefined, not_like?: string | undefined, exists?: string | undefined, in_array?: (Array | string) | undefined, all_in_array?: (Array | string) | undefined, gt_date?: string | undefined, lt_date?: string | undefined, gt_int?: number | undefined, lt_int?: number | undefined, gt_float?: number | undefined, lt_float?: number | undefined, is?: ('empty' | 'not_empty' | 'empty_array' | 'not_empty_array' | 'null' | 'not_null' | 'true' | 'false') | undefined}}) | undefined, excluding_fields?: string | undefined, including_fields?: string | undefined, excluding_story_fields?: string | undefined, resolve_assets?: 1 | undefined, resolve_level?: 2 | undefined, only_variants?: 'true' | undefined, token: string}; export type GetApiV2CdnStoriesOptions = {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 getApiV2CdnStoriesResponse = z.object({stories: z.array(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 Multiple Stories * * GET /v2/cdn/stories */ export const getApiV2CdnStories = async (args: GetApiV2CdnStoriesArgs, options?: GetApiV2CdnStoriesOptions) => { 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.version != null) query.append("version", String(args.version)) if (args.cv != null) query.append("cv", String(args.cv)) if (args.starts_with != null) query.append("starts_with", String(args.starts_with)) if (args.search_term != null) query.append("search_term", String(args.search_term)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (args.by_slugs != null) query.append("by_slugs", String(args.by_slugs)) if (args.excluding_slugs != null) query.append("excluding_slugs", String(args.excluding_slugs)) if (args.published_at_gt != null) query.append("published_at_gt", String(args.published_at_gt)) if (args.published_at_gte != null) query.append("published_at_gte", String(args.published_at_gte)) if (args.published_at_lt != null) query.append("published_at_lt", String(args.published_at_lt)) if (args.published_at_lte != null) query.append("published_at_lte", String(args.published_at_lte)) if (args.first_published_at_gt != null) query.append("first_published_at_gt", String(args.first_published_at_gt)) if (args.first_published_at_gte != null) query.append("first_published_at_gte", String(args.first_published_at_gte)) if (args.first_published_at_lt != null) query.append("first_published_at_lt", String(args.first_published_at_lt)) if (args.first_published_at_lte != null) query.append("first_published_at_lte", String(args.first_published_at_lte)) if (args.updated_at_gt != null) query.append("updated_at_gt", String(args.updated_at_gt)) if (args.updated_at_gte != null) query.append("updated_at_gte", String(args.updated_at_gte)) if (args.updated_at_lt != null) query.append("updated_at_lt", String(args.updated_at_lt)) if (args.updated_at_lte != null) query.append("updated_at_lte", String(args.updated_at_lte)) if (args.in_workflow_stages != null) query.append("in_workflow_stages", String(args.in_workflow_stages)) if (args.content_type != null) query.append("content_type", String(args.content_type)) if (args.level != null) query.append("level", String(args.level)) if (args.resolve_relations != null) query.append("resolve_relations", String(args.resolve_relations)) if (args.excluding_ids != null) query.append("excluding_ids", String(args.excluding_ids)) if (args.by_uuids != null) query.append("by_uuids", String(args.by_uuids)) if (args.by_uuids_ordered != null) query.append("by_uuids_ordered", String(args.by_uuids_ordered)) if (args.by_ids != null) query.append("by_ids", String(args.by_ids)) if (args.with_tag != null) query.append("with_tag", String(args.with_tag)) if (args.is_startpage != null) query.append("is_startpage", String(args.is_startpage)) 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.from_release != null) query.append("from_release", String(args.from_release)) if (args.fallback_lang != null) query.append("fallback_lang", String(args.fallback_lang)) if (args.language != null) query.append("language", String(args.language)) if (args.filter_query != null) query.append("filter_query", JSON.stringify(args.filter_query)) 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.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.only_variants != null) query.append("only_variants", String(args.only_variants)) if (args.token != null) query.append("token", String(args.token)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cdn/stories${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(getApiV2CdnStoriesResponse, await res.json()) };