import {z} from 'zod' export type GetApiApiV1ChargePointsChargePointIdGridParticipationReportArgs = {chargePointId: number, from?: (string | null) | undefined}; export type GetApiApiV1ChargePointsChargePointIdGridParticipationReportOptions = {baseUrl?: string | undefined, BearerAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyDto = z.object({id: z.number().int(), identifier: z.string(), name: z.string(), decimals: z.number().int(), name_localized: z.record(z.string(), z.string())}); export const participationHistoryRecord = z.object({id: z.number().int(), start_time: z.string(), kwh: z.number(), payout: z.number(), payout_label: z.string()}); export const participationHistoryMessage = z.object({id: z.number().int(), title: z.string(), body: z.string(), button_text: z.string(), start_time: z.string(), end_time: z.string()}); export const participationHistoryReport = z.object({subtitle: z.string(), currency: currencyDto, records: z.array(participationHistoryRecord).nullable().optional(), messages: z.array(participationHistoryMessage).nullable().optional(), registered_only_reason: z.string().nullable().optional(), info_url: z.string().nullable().optional(), total_kwh: z.number(), total_payout: z.number(), total_kwh_label: z.string(), total_payout_label: 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 PowerBank participation report * * GET /api/v1/charge-points/{chargePointId}/grid/participation-report */ export const getApiApiV1ChargePointsChargePointIdGridParticipationReport = async (args: GetApiApiV1ChargePointsChargePointIdGridParticipationReportArgs, options?: GetApiApiV1ChargePointsChargePointIdGridParticipationReportOptions) => { const {baseUrl = "https://partner-api.monta.com", fetch: fetchFn = fetch, BearerAccessToken} = 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 (BearerAccessToken !== undefined) headers.set('authorization', `Bearer ${BearerAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/charge-points/${encodeURIComponent(String(args.chargePointId))}/grid/participation-report${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(participationHistoryReport, await res.json()) };