import {z} from 'zod' export type GetApiV2ForecastsDynamicRulesArgs = {externalUnitId: string, externalSectionId?: string | undefined}; export type GetApiV2ForecastsDynamicRulesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const forecastDataIngestionExternalShiftType = z.object({externalShiftTypeId: z.string(), amount: z.number().int().lte(100000)}); export const forecastDataIngestionExternalDynamicRule = z.object({forecastExternalVariableId: z.string(), amount: z.number().int().lte(99999999999), weekdays: z.array(z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)])), startTime: z.string(), endTime: z.string(), shiftTypes: z.array(forecastDataIngestionExternalShiftType)}); export const getApiV2ForecastsDynamicRulesResponse = z.array(forecastDataIngestionExternalDynamicRule); 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 dynamic rules * * GET /v2/forecasts/dynamic-rules */ export const getApiV2ForecastsDynamicRules = async (args: GetApiV2ForecastsDynamicRulesArgs, options?: GetApiV2ForecastsDynamicRulesOptions) => { 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 (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/forecasts/dynamic-rules${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(getApiV2ForecastsDynamicRulesResponse, await res.json()) };