import {z} from 'zod' export type CreateApiChatsMassMessagesArgs = {'X-Fanvue-API-Version': string, 'Idempotency-Key'?: string | undefined, body: {text?: string | undefined, mediaUuids?: Array | undefined, mediaPreviewUuid?: (string | null) | undefined, price?: (number | null) | undefined, includedLists: {smartListIds?: (Array<'subscribers' | 'auto_renewing' | 'non_renewing' | 'followers' | 'free_trial_subscribers' | 'expired_subscribers' | 'spent_more_than_50' | 'muted' | 'creators'>) | undefined, customListUuids?: Array | undefined, dynamicSmartListUuids?: Array | undefined}, excludedLists?: ({smartListIds?: (Array<'subscribers' | 'auto_renewing' | 'non_renewing' | 'followers' | 'free_trial_subscribers' | 'expired_subscribers' | 'spent_more_than_50' | 'muted' | 'creators'>) | undefined, customListUuids?: Array | undefined, dynamicSmartListUuids?: Array | undefined}) | undefined, scheduledAt?: string | undefined}}; export type CreateApiChatsMassMessagesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiChatsMassMessagesResponse = z.object({uuid: z.string(), status: z.enum(["SENT", "UNSENT", "SENDING", "FAILED", "MODERATED", "SCHEDULED"]).optional(), publishedAt: z.string().nullable(), recipientCount: z.number().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 }; /** * Send a mass message * * POST /chats/mass-messages */ export const createApiChatsMassMessages = async (args: CreateApiChatsMassMessagesArgs, options?: CreateApiChatsMassMessagesOptions) => { const {baseUrl = "https://api.fanvue.com", 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 (args['X-Fanvue-API-Version'] != null) headers.set("X-Fanvue-API-Version", String(args['X-Fanvue-API-Version'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/chats/mass-messages${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(createApiChatsMassMessagesResponse, await res.json()) };