import {z} from 'zod' export type GetApiPostsArgs = {page?: number | undefined, limit?: number | undefined, 'fields[posts]'?: string | undefined, include?: string | undefined, 'filter[slug]'?: string | undefined, 'filter[published]'?: boolean | undefined, 'filter[published_at_gte]'?: string | undefined, 'filter[published_at_lte]'?: string | undefined, 'filter[tag_id]'?: number | undefined, 'filter[author_id]'?: string | undefined, 'filter[content_type]'?: ('tiptap' | 'notion') | undefined, sort?: ('published_at' | 'created_at' | 'updated_at' | 'title') | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiPostsOptions = {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 meta = z.object({total: z.number().int().optional(), page: z.number().int().optional(), limit: z.number().int().optional(), totalPages: z.number().int().optional()}); export const postListResponse = z.object({jsonapi: z.object({version: z.string()}), data: z.array(postResource), included: z.array(z.union([tagResource, authorResource])).optional(), meta: meta.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 Posts * * GET /posts */ export const getApiPosts = async (args: GetApiPostsArgs, options?: GetApiPostsOptions) => { const {baseUrl = "https://inblog.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['fields[posts]'] != null) query.append("fields[posts]", String(args['fields[posts]'])) if (args.include != null) query.append("include", String(args.include)) if (args['filter[slug]'] != null) query.append("filter[slug]", String(args['filter[slug]'])) if (args['filter[published]'] != null) query.append("filter[published]", String(args['filter[published]'])) if (args['filter[published_at_gte]'] != null) query.append("filter[published_at_gte]", String(args['filter[published_at_gte]'])) if (args['filter[published_at_lte]'] != null) query.append("filter[published_at_lte]", String(args['filter[published_at_lte]'])) if (args['filter[tag_id]'] != null) query.append("filter[tag_id]", String(args['filter[tag_id]'])) if (args['filter[author_id]'] != null) query.append("filter[author_id]", String(args['filter[author_id]'])) if (args['filter[content_type]'] != null) query.append("filter[content_type]", String(args['filter[content_type]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(postListResponse, await res.json()) };