import {z} from 'zod' export type GetApiV1BlogPostsArgs = {sort?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, 'fields[blog_posts]'?: string | undefined, 'filter[title_cont]'?: string | undefined}; export type GetApiV1BlogPostsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const blogPostsAttributes = z.object({title: z.string().optional(), url: z.string().optional(), content: z.string().optional(), slug: z.string().optional(), page_title: z.string().optional(), page_description: z.string().optional(), published_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const resourceIdentifier = z.object({id: z.string(), type: z.string()}); export const blogPostsIndexResponse = z.object({data: z.array(z.object({id: z.string().optional(), type: z.literal("blog_posts").optional(), attributes: blogPostsAttributes.optional(), relationships: z.object({blog: z.object({data: resourceIdentifier.optional()}).optional()}).optional()})).optional(), links: z.object({self: z.string().optional(), current: 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 blog posts * * GET /v1/blog_posts */ export const getApiV1BlogPosts = async (args: GetApiV1BlogPostsArgs, options?: GetApiV1BlogPostsOptions) => { const {baseUrl = "https://api.kajabi.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.sort != null) query.append("sort", String(args.sort)) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['fields[blog_posts]'] != null) query.append("fields[blog_posts]", String(args['fields[blog_posts]'])) if (args['filter[title_cont]'] != null) query.append("filter[title_cont]", String(args['filter[title_cont]'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/blog_posts${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(blogPostsIndexResponse, await res.json()) };