import {z} from 'zod' export type GetApiGbModoBenchmarkingLeaderboardLiveArgs = {settlement_date_from: string, settlement_date_to: string, asset_name?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiGbModoBenchmarkingLeaderboardLiveOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const gBAssetAggregation = z.object({id: z.number().int().optional(), settlement_date: z.string(), settlement_period: z.number().int().gte(0).lte(32767), asset: z.string().min(1).nullable().optional(), market: z.string().min(1).max(50), rated_power: z.number(), energy_capacity: z.number(), export_mwh: z.number(), import_mwh: z.number(), revenue: z.number(), contract_capacity: z.number(), buy_price_mwh: z.number(), sell_price_mwh: z.number(), revspermw: z.number(), revspermwh: z.number()}); export const getApiGbModoBenchmarkingLeaderboardLiveResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(gBAssetAggregation)}); 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 }; /** * Index Breakdown - GB * * GET /gb/modo/benchmarking/leaderboard-live */ export const getApiGbModoBenchmarkingLeaderboardLive = async (args: GetApiGbModoBenchmarkingLeaderboardLiveArgs, options?: GetApiGbModoBenchmarkingLeaderboardLiveOptions) => { const {baseUrl = "https://api.modoenergy.com/pub/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.settlement_date_from != null) query.append("settlement_date_from", String(args.settlement_date_from)) if (args.settlement_date_to != null) query.append("settlement_date_to", String(args.settlement_date_to)) if (args.asset_name != null) query.append("asset_name", String(args.asset_name)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (apiKey !== undefined) headers.set("X-Token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/gb/modo/benchmarking/leaderboard-live${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(getApiGbModoBenchmarkingLeaderboardLiveResponse, await res.json()) };