import {z} from 'zod' export type BlogUpdateRequest = {jsonapi: {version: string}, data?: ({type: 'blogs', attributes?: ({title?: string | undefined, description?: string | undefined, ga_measurement_id?: string | undefined, analytics_cookie_mode?: boolean | undefined, analytics_consent_form?: boolean | undefined, logo?: (string | string | {url?: string | undefined}) | undefined, favicon?: (string | string | {url?: string | undefined}) | undefined, og_image?: (string | string | {url?: string | undefined}) | undefined, blog_language?: string | undefined, timezone_diff?: number | undefined}) | undefined}) | undefined}; export type PatchApiBlogsMeArgs = {preserve_external_images?: boolean | undefined, body: BlogUpdateRequest}; export type PatchApiBlogsMeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const links = z.object({self: z.string().optional()}); export const blogResource = z.object({type: z.literal("blogs").optional(), id: z.string().optional(), attributes: z.object({id: z.number().int().optional(), title: z.string().optional(), subdomain: z.string().optional(), description: z.string().optional(), custom_domain: z.string().optional(), custom_domain_verified: z.boolean().optional(), custom_subdirectory: z.string().optional(), logo: z.string().optional(), logo_url: z.string().optional(), favicon: z.string().optional(), og_image: z.string().optional(), ga_measurement_id: z.string().nullable().optional(), analytics_cookie_mode: z.boolean().optional(), analytics_consent_form: z.boolean().optional(), is_search_console_connected: z.boolean().optional(), search_console_url: z.string().nullable().optional(), blog_language: z.string().optional(), timezone_diff: z.number().int().optional(), plan: z.enum(["free", "team", "enterprise"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), links: links.optional()}); export const blogResponse = z.object({jsonapi: z.object({version: z.string()}), data: blogResource.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 }; /** * Update My Blog Settings * * PATCH /blogs/me */ export const patchApiBlogsMe = async (args: PatchApiBlogsMeArgs, options?: PatchApiBlogsMeOptions) => { const {baseUrl = "https://inblog.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (args.preserve_external_images != null) query.append("preserve_external_images", String(args.preserve_external_images)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/blogs/me${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(blogResponse, await res.json()) };