import {z} from 'zod' export type NotificationTargetDTO = {featureId?: string | undefined, recipient?: string | undefined}; export type CalendarAlertRuleDTO = {createdAt?: string | undefined, createdByEmail?: string | undefined, currencyCodes?: Array | undefined, enabled?: boolean | undefined, id?: string | undefined, includeMarketNews?: boolean | undefined, leadMinutes?: Array | undefined, minImpact?: ('HOLIDAY' | 'LOW' | 'MEDIUM' | 'HIGH') | undefined, name?: string | undefined, notificationTargets?: Array | undefined, projectId?: string | undefined}; export type CreateApiRestApiV1ProjectsProjectIdCalendarAlertRulesArgs = {projectId: string, body: CalendarAlertRuleDTO}; export type CreateApiRestApiV1ProjectsProjectIdCalendarAlertRulesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTargetDTO = z.object({featureId: z.string().optional(), recipient: z.string().optional()}); export const calendarAlertRuleDTO = z.object({createdAt: z.string().optional(), createdByEmail: z.string().optional(), currencyCodes: z.array(z.string()).optional(), enabled: z.boolean().optional(), id: z.string().optional(), includeMarketNews: z.boolean().optional(), leadMinutes: z.array(z.number().int()).optional(), minImpact: z.enum(["HOLIDAY", "LOW", "MEDIUM", "HIGH"]).optional(), name: z.string().optional(), notificationTargets: z.array(notificationTargetDTO).optional(), projectId: z.string().optional()}); 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 }; /** * Create or update a standing calendar alert rule * * POST /rest/api/v1/projects/{projectId}/calendarAlertRules */ export const createApiRestApiV1ProjectsProjectIdCalendarAlertRules = async (args: CreateApiRestApiV1ProjectsProjectIdCalendarAlertRulesArgs, options?: CreateApiRestApiV1ProjectsProjectIdCalendarAlertRulesOptions) => { const {baseUrl = "https://api.metacopier.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rest/api/v1/projects/${encodeURIComponent(String(args.projectId))}/calendarAlertRules${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(calendarAlertRuleDTO, await res.json()) };