import {z} from 'zod' export type GetApiV1YieldsYieldIdTvlHistoryArgs = {yieldId: string, offset?: number | undefined, limit?: number | undefined, from?: string | undefined, to?: string | undefined, period?: ('1d' | '7d' | '30d' | '90d' | '1y' | 'all') | undefined, interval?: ('day' | 'week' | 'month') | undefined, feeConfigurationId?: string | undefined}; export type GetApiV1YieldsYieldIdTvlHistoryOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tvlHistoryResponseDto = z.object({total: z.number(), offset: z.number(), limit: z.number(), yieldId: z.string(), interval: z.enum(["day", "week", "month"]), from: z.string(), to: z.string()}); 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 historical TVL snapshots for a yield * * GET /v1/yields/{yieldId}/tvl/history */ export const getApiV1YieldsYieldIdTvlHistory = async (args: GetApiV1YieldsYieldIdTvlHistoryArgs, options?: GetApiV1YieldsYieldIdTvlHistoryOptions) => { const {baseUrl = "https://api.yield.xyz", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.period != null) query.append("period", String(args.period)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.feeConfigurationId != null) query.append("feeConfigurationId", String(args.feeConfigurationId)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/yields/${encodeURIComponent(String(args.yieldId))}/tvl/history${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(tvlHistoryResponseDto, await res.json()) };