import {z} from 'zod' export type GetApiApiV1AffiliatesJsonOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AffiliatesJsonResponse = z.object({schema_version: z.string().optional(), program_name: z.string().optional(), last_updated: z.string().optional(), landing_page: z.string().optional(), leaderboard_page: z.string().optional(), swipe_kit_page: z.string().optional(), signup_url: z.string().optional(), terms: z.record(z.string(), z.unknown()).optional(), bait: z.record(z.string(), z.unknown()).optional(), program_stats: z.record(z.string(), z.unknown()).optional(), leaderboard: z.array(z.record(z.string(), z.unknown())).optional(), swipe_kit: z.record(z.string(), z.unknown()).optional(), prohibited_channels: z.array(z.string()).optional(), contact: z.record(z.string(), z.unknown()).optional(), license: z.string().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 }; /** * Affiliate program, terms, leaderboard snapshot, swipe-kit templates * * GET /api/v1/affiliates.json */ export const getApiApiV1AffiliatesJson = async (options?: GetApiApiV1AffiliatesJsonOptions) => { const {baseUrl = "https://signals.gitdealflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/affiliates.json${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(getApiApiV1AffiliatesJsonResponse, await res.json()) };