import {z} from 'zod' export type GetApiTwitterUserFollowingsArgs = {userName: unknown, cursor?: unknown | undefined, pageSize?: number | undefined}; export type GetApiTwitterUserFollowingsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const userProfileSnake = z.object({id: z.string().optional(), name: z.string().optional(), screen_name: z.string().optional(), userName: z.string().optional(), username: z.string().nullable().optional(), location: z.string().optional(), url: z.string().nullable().optional(), description: z.string().optional(), email: z.string().nullable().optional(), protected: z.boolean().optional(), verified: z.boolean().optional(), isBlueVerified: z.boolean().optional(), followers_count: z.number().int().optional(), following_count: z.number().int().optional(), friends_count: z.number().int().optional(), favourites_count: z.number().int().optional(), statuses_count: z.number().int().optional(), media_tweets_count: z.number().int().optional(), created_at: z.string().optional(), profile_banner_url: z.string().optional(), profile_image_url_https: z.string().optional(), can_dm: z.boolean().optional()}); export const getApiTwitterUserFollowingsResponse = z.object({followings: z.array(userProfileSnake).optional(), has_next_page: z.boolean().optional(), next_cursor: z.string().optional(), status: z.enum(["success", "error"]).optional(), msg: z.string().optional(), code: z.number().int().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 }; /** * Get user followings (with full profile metadata). Sorted by follow date — most recent followings appear on the first page. Use `cursor` for pagination. * * GET /twitter/user/followings */ export const getApiTwitterUserFollowings = async (args: GetApiTwitterUserFollowingsArgs, options?: GetApiTwitterUserFollowingsOptions) => { const {baseUrl = "https://api.twitterapi.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.userName != null) query.append("userName", String(args.userName)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/twitter/user/followings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTwitterUserFollowingsResponse, await res.json()) };