import {z} from 'zod' export type FiscalAutomaticMonthDefinition = {type?: 'automatic' | undefined}; export type FiscalCatchUpMonth = {fiscal_year_number: number, fiscal_month_number: number}; export type FiscalWeekPatternMonthDefinition = {type?: 'week_pattern' | undefined, pattern: 'four_four_five' | 'four_five_four' | 'five_four_four', catch_up_months?: Array | undefined}; export type StructuredFiscalCalendarConfiguration = {timezone: string, fiscal_year_number: number, fiscal_year_start_date: string, month_definition: FiscalAutomaticMonthDefinition | FiscalWeekPatternMonthDefinition}; export type StructuredFiscalCalendarPreview = {configuration_mode: 'structured', configuration: StructuredFiscalCalendarConfiguration, fiscal_year_number: number}; export type FiscalQuarter = {fiscal_quarter_number: number, start_date: string, end_date: string}; export type FiscalMonth = {fiscal_month_number: number, label: string, start_date: string, end_date: string, is_catch_up_month?: boolean | undefined}; export type FiscalYear = {fiscal_year_number: number, start_date: string, end_date: string, quarters: Array, months: Array}; export type JsonFiscalCalendarConfiguration = {timezone: string, week_start_day: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday', fiscal_years: Array}; export type JsonFiscalCalendarPreview = {configuration_mode: 'json', configuration: JsonFiscalCalendarConfiguration, fiscal_year_number: number}; export type CreateApiTokenomicsV1FiscalCalendarPreviewArgs = {body: StructuredFiscalCalendarPreview | JsonFiscalCalendarPreview}; export type CreateApiTokenomicsV1FiscalCalendarPreviewOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const fiscalQuarter = z.object({fiscal_quarter_number: z.number().int().gte(1).lte(4), start_date: z.string(), end_date: z.string()}); export const fiscalMonth = z.object({fiscal_month_number: z.number().int().gte(1).lte(12), label: z.string().min(1).max(32), start_date: z.string(), end_date: z.string(), is_catch_up_month: z.boolean().optional()}); export const fiscalYear = z.object({fiscal_year_number: z.number().int().gte(1).lte(9999), start_date: z.string(), end_date: z.string(), quarters: z.array(fiscalQuarter), months: z.array(fiscalMonth)}); export const jsonFiscalCalendarConfiguration = z.object({timezone: z.string().min(1).max(64), week_start_day: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]), fiscal_years: z.array(fiscalYear)}); export const fiscalCalendarPreviewResponse = z.object({resolved_calendar: fiscalYear.nullable(), generated_json: jsonFiscalCalendarConfiguration.nullable()}); 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 }; /** * Preview Fiscal Calendar * * POST /tokenomics/v1/fiscal-calendar/preview * * Also accepts auth: ClassicAPIKeyHeader, OAuth2PasswordBearer, HTTPBasic */ export const createApiTokenomicsV1FiscalCalendarPreview = async (args: CreateApiTokenomicsV1FiscalCalendarPreviewArgs, options?: CreateApiTokenomicsV1FiscalCalendarPreviewOptions) => { const {baseUrl = "https://api.galileo.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Splunk-AO-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tokenomics/v1/fiscal-calendar/preview${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(fiscalCalendarPreviewResponse, await res.json()) };