import {z} from 'zod' export type GetApiChainIdStatsServiceApiV1LinesNameArgs = {name: string, from?: string | undefined, to?: string | undefined, resolution?: ('RESOLUTION_UNSPECIFIED' | 'DAY' | 'WEEK' | 'MONTH' | 'YEAR') | undefined, chain_id: string}; export type GetApiChainIdStatsServiceApiV1LinesNameOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Point = z.object({date: z.string().optional(), date_to: z.string().optional(), is_approximate: z.boolean().optional(), value: z.string().optional()}); export const v1LineChartInfo = z.object({description: z.string().optional(), id: z.string().optional(), resolutions: z.array(z.string()).optional(), title: z.string().optional(), units: z.string().optional()}); export const v1LineChart = z.object({chart: z.array(v1Point).optional(), info: v1LineChartInfo.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 }; /** * Returns data points for a specific line chart, with optional date range * and resolution filtering. * * GET /{chain_id}/stats-service/api/v1/lines/{name} * * Also accepts auth: apiKeyAuth */ export const getApiChainIdStatsServiceApiV1LinesName = async (args: GetApiChainIdStatsServiceApiV1LinesNameArgs, options?: GetApiChainIdStatsServiceApiV1LinesNameOptions) => { const {baseUrl = "https://api.blockscout.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.chain_id))}/stats-service/api/v1/lines/${encodeURIComponent(String(args.name))}${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(v1LineChart, await res.json()) };