import {z} from 'zod' export type GetApiV2ForecastEventsGroupsGroupIdEventsArgs = {groupId: number, startTime?: string | undefined, endTime?: string | undefined}; export type GetApiV2ForecastEventsGroupsGroupIdEventsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const forecastEventsEventEffect = z.object({forecastConfigurationId: z.number().int(), type: z.enum(["RELATIVE", "CLOSED", "ABSOLUTE", "AUTOMATIC"]), value: z.number()}); export const forecastEventsEvent = z.object({id: z.string().optional(), startTime: z.string(), endTime: z.string(), eventCategoryId: z.string().min(1), parentEvent: z.unknown(), groupId: z.number().int().optional(), eventEffects: z.array(forecastEventsEventEffect), eventEffectValue: z.number().optional(), comment: z.string().max(255).optional(), active: z.boolean().optional(), hierarchyEnabled: z.boolean().optional()}); export const forecastEventsIntegrationEvent = z.object({id: z.string().optional(), startTime: z.string(), endTime: z.string(), eventCategoryId: z.string().min(1), parentEvent: forecastEventsEvent.optional(), groupId: z.number().int().optional(), eventEffects: z.array(forecastEventsEventEffect), eventEffectValue: z.number().optional(), comment: z.string().max(255).optional(), active: z.boolean().optional(), hierarchyEnabled: z.boolean().optional()}); export const getApiV2ForecastEventsGroupsGroupIdEventsResponse = z.array(forecastEventsIntegrationEvent); 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 }; /** * List forecast events for group * * GET /v2/forecast-events/groups/{groupId}/events */ export const getApiV2ForecastEventsGroupsGroupIdEvents = async (args: GetApiV2ForecastEventsGroupsGroupIdEventsArgs, options?: GetApiV2ForecastEventsGroupsGroupIdEventsOptions) => { 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.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/forecast-events/groups/${encodeURIComponent(String(args.groupId))}/events${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(getApiV2ForecastEventsGroupsGroupIdEventsResponse, await res.json()) };