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 PatchApiIdeasIdArgs = {id: string, body: {title?: string | undefined, labelIds?: Array | undefined, campaignId?: (string | null) | undefined, linkedIn?: ({copy?: string | undefined, assets?: Array | undefined}) | undefined, x?: ({tweets?: (Array<{copy?: string | undefined, assets?: Array | undefined}>) | undefined}) | undefined, tikTok?: TikTokConfig | undefined, youTubeShorts?: YouTubeShortsConfig | undefined, notes?: (string | null) | undefined}}; export type PatchApiIdeasIdOptions = {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 ideaUpdateResponse = z.object({id: z.string().optional(), url: z.string().optional(), title: z.string().optional(), channels: z.array(z.string()).optional(), status: 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 }; /** * Update idea * * PATCH /ideas/{id} */ export const patchApiIdeasId = async (args: PatchApiIdeasIdArgs, options?: PatchApiIdeasIdOptions) => { const {baseUrl = "https://app.tryordinal.com/api/v1", 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/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(ideaUpdateResponse, await res.json()) };