import {z} from 'zod' export type GetApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamArgs = {externalForecastVariableId: string, externalUnitId: string, externalSectionId?: string | undefined, startTime: string, endTime: string}; export type GetApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const forecastDataIngestionForecastDataPayload = z.object({data: z.number(), timestamp: z.string()}); export const forecastDataIngestionDataIngestionOutput = z.object({externalForecastVariableId: z.string().optional(), externalUnitId: z.string().optional(), externalSectionId: z.string().optional(), dataPayLoad: z.array(forecastDataIngestionForecastDataPayload).optional(), dataPayload: z.array(forecastDataIngestionForecastDataPayload).optional()}); export const getApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamResponse = z.array(forecastDataIngestionDataIngestionOutput); 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 }; /** * Stream uploaded raw data for forecast variable, up to 120 days * * GET /v2/forecasts/forecast-variables/{externalForecastVariableId}/raw-data-stream */ export const getApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStream = async (args: GetApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamArgs, options?: GetApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamOptions) => { const {baseUrl = "https://api.quinyx.com", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.externalUnitId != null) query.append("externalUnitId", String(args.externalUnitId)) if (args.externalSectionId != null) query.append("externalSectionId", String(args.externalSectionId)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/forecasts/forecast-variables/${encodeURIComponent(String(args.externalForecastVariableId))}/raw-data-stream${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(getApiV2ForecastsForecastVariablesExternalForecastVariableIdRawDataStreamResponse, await res.json()) };