import {z} from 'zod' export type PostStatus = 'Tentative' | 'ToDo' | 'InProgress' | 'ForReview' | 'Blocked' | 'Finalized' | 'Scheduled' | 'Posted'; export type ChannelAssetRef = {assetId: string}; export type LinkedInConfig = {profileId?: string | undefined, copy?: string | undefined, assets?: Array | undefined, poll?: ({question?: string | undefined, duration?: ('ONE_DAY' | 'THREE_DAYS' | 'ONE_WEEK' | 'TWO_WEEKS') | undefined, options?: (Array<{id?: string | undefined, text?: string | undefined}>) | undefined}) | undefined, linkPreview?: ({url?: string | undefined, title?: string | undefined, description?: string | undefined, thumbnailId?: string | undefined}) | undefined}; export type TwitterConfig = {profileId: string, tweets: Array<{copy: string, assets?: Array | undefined}>}; export type InstagramAssetTagInput = {username: string, x?: number | undefined, y?: number | undefined}; export type InstagramAssetInput = {assetId: string, tags?: Array | undefined}; export type InstagramLocationInput = {pageId: string, name: string}; export type InstagramFeedConfig = {profileId?: string | undefined, type: 'Feed', copy?: string | undefined, assets?: Array | undefined, collaborators?: Array | undefined, location?: (InstagramLocationInput | null) | undefined}; export type InstagramReelConfig = {profileId?: string | undefined, type: 'Reel', copy?: string | undefined, assets?: Array | undefined, coverPhotoId?: string | undefined, shareToFeed?: boolean | undefined, trialReelGraduationStrategy?: ('MANUAL' | 'SS_PERFORMANCE' | null) | undefined, collaborators?: Array | undefined, location?: (InstagramLocationInput | null) | undefined}; export type InstagramStoryConfig = {profileId?: string | undefined, type: 'Story', assets?: Array | undefined}; export type InstagramConfig = InstagramFeedConfig | InstagramReelConfig | InstagramStoryConfig; export type TikTokPrivacyLevel = 'PUBLIC_TO_EVERYONE' | 'FOLLOWER_OF_CREATOR' | 'MUTUAL_FOLLOW_FRIENDS' | 'SELF_ONLY'; export type TikTokConfig = {profileId: string, copy: string, assets: Array, privacyLevel?: TikTokPrivacyLevel | undefined, isCommentDisabled?: boolean | undefined, isDuetDisabled?: boolean | undefined, isStitchDisabled?: boolean | undefined, isPromotingBusiness?: boolean | undefined, isPaidPartnership?: boolean | undefined, videoCoverTimestampMs?: (number | null) | undefined}; export type YouTubePrivacyStatus = 'public' | 'private' | 'unlisted'; export type YouTubeShortsConfig = {profileId: string, title: string, description?: string | undefined, assets: Array, privacyStatus?: YouTubePrivacyStatus | undefined, categoryId?: string | undefined, notifySubscribers?: boolean | undefined, isEmbeddable?: boolean | undefined, isSelfDeclaredMadeForKids?: boolean | undefined}; export type CreatePostInput = {title: string, publishAt: string, status: PostStatus, labelIds?: Array | undefined, campaignId?: string | undefined, notes?: string | undefined, linkedIn?: LinkedInConfig | undefined, x?: TwitterConfig | undefined, instagram?: InstagramConfig | undefined, tikTok?: TikTokConfig | undefined, youTubeShorts?: YouTubeShortsConfig | undefined}; export type CreateApiPostsArgs = {body: CreatePostInput}; export type CreateApiPostsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postStatus = z.enum(["Tentative", "ToDo", "InProgress", "ForReview", "Blocked", "Finalized", "Scheduled", "Posted"]); export const asset = z.object({id: z.string().optional(), name: z.string().optional(), url: z.string().optional(), mimetype: z.string().optional(), size: z.number().int().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), duration: z.number().nullable().optional()}); export const channelProfile = z.object({id: z.string().optional(), name: z.string().nullable().optional(), detail: z.string().nullable().optional()}); export const instagramAssetTag = z.object({username: z.string().optional(), x: z.number().nullable().optional(), y: z.number().nullable().optional()}); export const instagramAsset = z.object({id: z.string().optional(), name: z.string().optional(), url: z.string().optional(), mimetype: z.string().optional(), size: z.number().int().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), duration: z.number().nullable().optional(), tags: z.array(instagramAssetTag).optional()}); export const instagramLocation = z.object({pageId: z.string().optional(), name: z.string().optional()}); export const instagramFeedContent = z.object({profile: channelProfile.nullable().optional(), type: z.literal("Feed").optional(), copy: z.string().optional(), assets: z.array(instagramAsset).optional(), collaborators: z.array(z.string()).optional(), location: instagramLocation.nullable().optional()}); export const instagramReelContent = z.object({profile: channelProfile.nullable().optional(), type: z.literal("Reel").optional(), copy: z.string().optional(), assets: z.array(instagramAsset).optional(), coverPhoto: asset.nullable().optional(), shareToFeed: z.boolean().optional(), trialReelGraduationStrategy: z.enum(["MANUAL", "SS_PERFORMANCE"]).nullable().optional(), collaborators: z.array(z.string()).optional(), location: instagramLocation.nullable().optional()}); export const instagramStoryContent = z.object({profile: channelProfile.nullable().optional(), type: z.literal("Story").optional(), assets: z.array(instagramAsset).optional()}); export const instagramContent = z.discriminatedUnion("type", [instagramFeedContent, instagramReelContent, instagramStoryContent]).nullable(); export const postCreateResponse = z.object({id: z.string().optional(), url: z.string().optional(), title: z.string().optional(), channels: z.array(z.string()).optional(), status: postStatus.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), linkedIn: z.object({copy: z.string().optional(), assets: z.array(asset).optional()}).nullable().optional(), x: z.object({tweets: z.array(z.object({copy: z.string().optional(), assets: z.array(asset).optional()})).optional()}).nullable().optional(), instagram: instagramContent.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 post * * POST /posts */ export const createApiPosts = async (args: CreateApiPostsArgs, options?: CreateApiPostsOptions) => { const {baseUrl = "https://app.tryordinal.com/api/v1/company", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/posts${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(postCreateResponse, await res.json()) };