import {z} from 'zod' export type GetApiV1PostsArgs = {account: number, status?: ('DRAFT' | 'SCHEDULED' | 'PUBLISHING' | 'PUBLISHED' | null) | undefined, tag_id?: (number | null) | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiV1PostsOptions = {baseUrl?: string | undefined, PublicAPIAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const draftListResponse = z.object({social_set_id: z.number().int(), share_url: z.string().nullable().optional(), private_url: z.string(), status: z.string(), tags: z.array(z.string()).optional(), created_at: z.string(), updated_at: z.string().nullable(), published_at: z.string().nullable(), mastodon_post_published_at: z.string().nullable(), linkedin_post_published_at: z.string().nullable(), threads_post_published_at: z.string().nullable(), bluesky_post_published_at: z.string().nullable(), x_post_published_at: z.string().nullable().optional(), x_post_enabled: z.boolean(), linkedin_post_enabled: z.boolean(), threads_post_enabled: z.boolean(), bluesky_post_enabled: z.boolean(), x_published_url: z.string().nullable().optional(), linkedin_published_url: z.string().nullable().optional(), id: z.number().int().nullable().optional(), preview: z.string().max(180).nullable().optional(), scheduled_date: z.string().nullable().optional(), draft_title: z.string().max(512).nullable().optional(), mastodon_post_enabled: z.boolean().optional()}); export const pagedDraftListResponse = z.object({results: z.array(draftListResponse), count: z.number().int(), limit: z.number().int(), offset: z.number().int(), next: z.string().nullable(), previous: z.string().nullable()}); 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 /v1/posts */ export const getApiV1Posts = async (args: GetApiV1PostsArgs, options?: GetApiV1PostsOptions) => { const {baseUrl = "https://dev.srvr.postey.ai", fetch: fetchFn = fetch, PublicAPIAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.status != null) query.append("status", String(args.status)) if (args.tag_id != null) query.append("tag_id", String(args.tag_id)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (PublicAPIAuthentication !== undefined) headers.set('authorization', `Bearer ${PublicAPIAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(pagedDraftListResponse, await res.json()) };