import {z} from 'zod' export type ResourceIdentifier = {type: string, id: string}; export type PostUpdateRequest = {jsonapi: {version: string}, data?: ({type: 'posts', attributes?: ({title?: string | undefined, slug?: string | undefined, description?: string | undefined, content_html?: string | undefined, published?: boolean | undefined, image?: (string | string | {url?: string | undefined, blurhash?: string | undefined, created_at?: string | undefined}) | undefined, canonical_url?: string | undefined, meta_title?: string | undefined, meta_description?: string | undefined, cta_text?: string | undefined, cta_link?: string | undefined, cta_color?: string | undefined, cta_color_type?: ('custom' | 'preset' | 'default') | undefined, cta_text_color?: string | undefined, cta_text_color_type?: ('custom' | 'preset' | 'default') | undefined, form_id?: (number | null) | undefined, custom_scripts?: ({head_start_script?: (string | null) | undefined, head_end_script?: (string | null) | undefined, body_start_script?: (string | null) | undefined, body_end_script?: (string | null) | undefined, json_ld_script?: Record | undefined} | null) | undefined, notion_url?: (string | null) | undefined}) | undefined, relationships?: ({tags?: ({data?: (Array | unknown) | undefined}) | undefined}) | undefined}) | undefined}; export type UpdateApiPostsIdArgs = {id: number, preserve_external_images?: boolean | undefined, body: PostUpdateRequest}; export type UpdateApiPostsIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceIdentifier = z.object({type: z.string(), id: z.string()}); export const relationship = z.object({data: z.union([resourceIdentifier, z.array(resourceIdentifier), z.unknown()]).optional()}); export const links = z.object({self: z.string().optional()}); export const postResource = z.object({type: z.literal("posts").optional(), id: z.string().optional(), attributes: z.object({title: z.string().max(200).optional(), slug: z.string().optional(), description: z.string().max(160).optional(), content_html: z.string().optional(), published_at: z.string().optional(), image: z.object({url: z.string().optional(), blurhash: z.string().optional(), created_at: z.string().optional()}).optional(), canonical_url: z.string().optional(), meta_title: z.string().max(60).optional(), meta_description: z.string().max(160).optional(), cta_text: z.string().optional(), cta_link: z.string().optional(), cta_color: z.string().optional(), cta_color_type: z.enum(["custom", "preset", "default"]).optional(), cta_text_color: z.string().optional(), cta_text_color_type: z.enum(["custom", "preset", "default"]).optional(), form_id: z.number().int().nullable().optional(), custom_scripts: z.object({head_start_script: z.string().nullable().optional(), head_end_script: z.string().nullable().optional(), body_start_script: z.string().nullable().optional(), body_end_script: z.string().nullable().optional(), json_ld_script: z.object({}).nullable().optional()}).nullable().optional()}).optional(), relationships: z.object({tags: relationship.optional(), authors: relationship.optional()}).optional(), links: links.optional()}); export const tagResource = z.object({type: z.literal("tags").optional(), id: z.string().optional(), attributes: z.object({name: z.string().max(50).optional(), slug: z.string().optional()}).optional()}); export const authorResource = z.object({type: z.literal("authors").optional(), id: z.string().optional(), attributes: z.object({author_name: z.string().max(100).optional(), avatar_url: z.string().optional()}).optional()}); export const postResponse = z.object({jsonapi: z.object({version: z.string()}), data: postResource, included: z.array(z.union([tagResource, authorResource])).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 Post (PUT) * * PUT /posts/{id} */ export const updateApiPostsId = async (args: UpdateApiPostsIdArgs, options?: UpdateApiPostsIdOptions) => { 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(/\/+$/, '')}/posts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(postResponse, await res.json()) };