import {z} from 'zod' export type Post = {text: string, media_ids?: Array | undefined}; export type XSettings = {reply_to_url?: (string | null) | undefined, community_id?: (string | null) | undefined, share_with_followers?: (boolean | null) | undefined}; export type EnabledXPlatform = {enabled: true, posts: Array, settings?: (XSettings | null) | undefined}; export type DisabledPlatform = {enabled: false}; export type LinkedInSettings = Record; export type EnabledLinkedInPlatform = {enabled: true, posts: Array, settings?: (LinkedInSettings | null) | undefined}; export type MastodonSettings = Record; export type EnabledMastodonPlatform = {enabled: true, posts: Array, settings?: (MastodonSettings | null) | undefined}; export type ThreadsSettings = Record; export type EnabledThreadsPlatform = {enabled: true, posts: Array, settings?: (ThreadsSettings | null) | undefined}; export type BlueskySettings = Record; export type EnabledBlueskyPlatform = {enabled: true, posts: Array, settings?: (BlueskySettings | null) | undefined}; export type Platforms = {x?: (EnabledXPlatform | DisabledPlatform | null) | undefined, linkedin?: (EnabledLinkedInPlatform | DisabledPlatform | null) | undefined, mastodon?: (EnabledMastodonPlatform | DisabledPlatform | null) | undefined, threads?: (EnabledThreadsPlatform | DisabledPlatform | null) | undefined, bluesky?: (EnabledBlueskyPlatform | DisabledPlatform | null) | undefined}; export type DraftCreateRequest = {platforms: Platforms, draft_title?: (string | null) | undefined, scratchpad_text?: (string | null) | undefined, tags?: Array | undefined, share?: boolean | undefined, publish_at?: (string | 'now' | 'next-free-slot' | null) | undefined}; export type CreateApiV1PostsRawArgs = {body: DraftCreateRequest}; export type CreateApiV1PostsRawOptions = {baseUrl?: string | undefined, PublicAPIAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const post = z.object({text: z.string().max(50000), media_ids: z.array(z.string()).optional()}); export const xSettings = z.object({reply_to_url: z.string().max(2048).nullable().optional(), community_id: z.string().max(255).nullable().optional(), share_with_followers: z.boolean().nullable().optional()}); export const enabledXPlatformResponse = z.object({enabled: z.literal(true), posts: z.array(post), settings: xSettings.nullable().optional()}); export const disabledPlatform = z.object({enabled: z.literal(false)}); export const linkedInSettings = z.object({}); export const enabledLinkedInPlatformResponse = z.object({enabled: z.literal(true), posts: z.array(post), settings: linkedInSettings.nullable().optional()}); export const mastodonSettings = z.object({}); export const enabledMastodonPlatformResponse = z.object({enabled: z.literal(true), posts: z.array(post), settings: mastodonSettings.nullable().optional()}); export const threadsSettings = z.object({}); export const enabledThreadsPlatformResponse = z.object({enabled: z.literal(true), posts: z.array(post), settings: threadsSettings.nullable().optional()}); export const blueskySettings = z.object({}); export const enabledBlueskyPlatformResponse = z.object({enabled: z.literal(true), posts: z.array(post), settings: blueskySettings.nullable().optional()}); export const platformsResponse = z.object({x: z.union([enabledXPlatformResponse, disabledPlatform]).nullable().optional(), linkedin: z.union([enabledLinkedInPlatformResponse, disabledPlatform]).nullable().optional(), mastodon: z.union([enabledMastodonPlatformResponse, disabledPlatform]).nullable().optional(), threads: z.union([enabledThreadsPlatformResponse, disabledPlatform]).nullable().optional(), bluesky: z.union([enabledBlueskyPlatformResponse, disabledPlatform]).nullable().optional()}); export const draftDetailResponse = z.object({id: z.number().int(), social_set_id: z.number().int(), draft_id: z.number().int(), status: z.enum(["draft", "scheduled", "published", "publishing", "error"]), created_at: z.string(), updated_at: z.string().nullable().optional(), scheduled_date: z.string().nullable().optional(), published_at: z.string().nullable().optional(), draft_title: z.string().nullable().optional(), tags: z.array(z.string()).optional(), preview: z.string(), share_url: z.string().nullable().optional(), private_url: z.string(), platforms: platformsResponse, x_published_url: z.string().nullable().optional(), linkedin_published_url: z.string().nullable().optional(), x_post_published_at: z.string().nullable().optional(), linkedin_post_published_at: z.string().nullable().optional(), mastodon_post_published_at: z.string().nullable().optional(), threads_post_published_at: z.string().nullable().optional(), bluesky_post_published_at: z.string().nullable().optional(), scratchpad_text: z.string().nullable().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 }; /** * Create raw post * * POST /v1/posts/raw */ export const createApiV1PostsRaw = async (args: CreateApiV1PostsRawArgs, options?: CreateApiV1PostsRawOptions) => { const {baseUrl = "https://dev.srvr.postey.ai", fetch: fetchFn = fetch, PublicAPIAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (PublicAPIAuthentication !== undefined) headers.set('authorization', `Bearer ${PublicAPIAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/posts/raw${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(draftDetailResponse, await res.json()) };