import {z} from 'zod' export type ChannelAssetRef = {assetId: string}; 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 CreateApiIdeasArgs = {body: {title: string, labelIds?: Array | undefined, campaignId?: string | undefined, linkedIn?: ({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}) | undefined, x?: ({profileId: string, tweets: Array<{copy: string, assets?: Array | undefined}>}) | undefined, tikTok?: TikTokConfig | undefined, youTubeShorts?: YouTubeShortsConfig | undefined, notes?: string | undefined}}; export type CreateApiIdeasOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 tikTokPrivacyLevel = z.enum(["PUBLIC_TO_EVERYONE", "FOLLOWER_OF_CREATOR", "MUTUAL_FOLLOW_FRIENDS", "SELF_ONLY"]); export const tikTokContent = z.object({profile: channelProfile.nullable().optional(), copy: z.string().optional(), assets: z.array(asset).optional(), privacyLevel: tikTokPrivacyLevel.optional(), isCommentDisabled: z.boolean().optional(), isDuetDisabled: z.boolean().optional(), isStitchDisabled: z.boolean().optional(), isPromotingBusiness: z.boolean().optional(), isPaidPartnership: z.boolean().optional(), videoCoverTimestampMs: z.number().int().nullable().optional()}).nullable(); export const youTubePrivacyStatus = z.enum(["public", "private", "unlisted"]); export const youTubeShortsContent = z.object({profile: channelProfile.nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), assets: z.array(asset).optional(), privacyStatus: youTubePrivacyStatus.optional(), categoryId: z.string().nullable().optional(), notifySubscribers: z.boolean().optional(), isEmbeddable: z.boolean().optional(), isSelfDeclaredMadeForKids: z.boolean().optional()}).nullable(); export const ideaCreateResponse = z.object({id: z.string().optional(), url: z.string().optional(), title: z.string().optional(), channels: z.array(z.string()).optional(), status: z.string().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(), tikTok: tikTokContent.optional(), youTubeShorts: youTubeShortsContent.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 idea * * POST /ideas */ export const createApiIdeas = async (args: CreateApiIdeasArgs, options?: CreateApiIdeasOptions) => { 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(/\/+$/, '')}/ideas${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(ideaCreateResponse, await res.json()) };