import {z} from 'zod' export type GetApiCreatorsCreatorUserUuidInsightsFansUserUuidArgs = {'X-Fanvue-API-Version': string, creatorUserUuid: string, userUuid: string}; export type GetApiCreatorsCreatorUserUuidInsightsFansUserUuidOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCreatorsCreatorUserUuidInsightsFansUserUuidResponse = z.object({status: z.enum(["subscriber", "expired", "follower", "not_contactable"]), spending: z.object({lastPurchaseAt: z.string().nullable(), lastValidPurchaseAt: z.string().nullable(), total: z.object({gross: z.number(), total: z.number(), netTotal: z.number()}), maxSinglePayment: z.object({gross: z.number(), total: z.number()}), sources: z.record(z.string(), z.object({gross: z.number(), total: z.number(), netTotal: z.number(), count: z.number().int(), netCount: z.number().int(), average: z.number(), netAverage: z.number().nullable()}))}), subscription: z.object({createdAt: z.string().nullable(), renewsAt: z.string().nullable(), autoRenewalEnabled: z.boolean()})}); 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 fan insights for a creator * * GET /creators/{creatorUserUuid}/insights/fans/{userUuid} */ export const getApiCreatorsCreatorUserUuidInsightsFansUserUuid = async (args: GetApiCreatorsCreatorUserUuidInsightsFansUserUuidArgs, options?: GetApiCreatorsCreatorUserUuidInsightsFansUserUuidOptions) => { const {baseUrl = "https://api.fanvue.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/creators/${encodeURIComponent(String(args.creatorUserUuid))}/insights/fans/${encodeURIComponent(String(args.userUuid))}${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(getApiCreatorsCreatorUserUuidInsightsFansUserUuidResponse, await res.json()) };