import {z} from 'zod' export type GetApiBlogsPostsIdArgs = {id: string, 'expand[]'?: Array<'markup'> | undefined}; export type GetApiBlogsPostsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const blogsPostAttributes = z.object({id: z.number().int().optional(), public_id: z.string().optional(), blog_id: z.number().int().optional(), title: z.string().optional(), published_at: z.string().nullable().optional(), current_path: z.string().optional(), url: z.string().optional(), visibility: z.enum(["draft", "public", "members_only", "scheduled"]).optional(), excerpt_content: z.string().nullable().optional(), seo_title: z.string().nullable().optional(), seo_description: z.string().nullable().optional(), image_id: z.number().int().nullable().optional(), image_url: z.string().nullable().optional(), categories: z.array(z.object({name: z.string().optional()})).optional(), tags: z.array(z.object({name: z.string().optional()})).optional(), authors: z.array(z.object({first_name: z.string().optional(), last_name: z.string().optional(), email_address: z.string().optional()})).optional(), markup: z.string().optional(), created_at: z.string().optional(), updated_at: 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 }; /** * Fetch Blog Post * * GET /blogs/posts/{id} * * Also accepts auth: OAuth2 */ export const getApiBlogsPostsId = async (args: GetApiBlogsPostsIdArgs, options?: GetApiBlogsPostsIdOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['expand[]'] ?? []) query.append("expand[]", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/blogs/posts/${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(blogsPostAttributes, await res.json()) };