import {z} from 'zod' export type CalendarEventAttendee = {id?: string | undefined, version?: string | undefined, attendeeStatus?: ('ACCEPT' | 'CANCEL' | 'DECLINE' | 'INVITATION' | 'TENTATIVE' | 'UNDEFINED') | undefined, calendarEventId?: string | undefined, createdDate?: number | undefined, eventPermission?: ('CAN_MODIFY_EVERYTHING' | 'CAN_MODIFY_EVERYTHING_BUT_NOT_PARTICIPIENT_LIST' | 'CAN_MODIFY_ONLY_PARTICIPIENT_LIST' | 'CAN_NOT_MODIFY_ANYTHING') | undefined, lastModifiedDate?: number | undefined, userId: string}; export type RecurringEvent = {id?: string | undefined, version?: string | undefined, createdDate?: number | undefined, endsOn?: number | undefined, eventInterval?: number | undefined, eventOccurrenceCount?: number | undefined, eventType?: ('DAILY' | 'MONTHLY' | 'WEEKLY' | 'YEARLY') | undefined, lastModifiedDate?: number | undefined, repeatOn?: string | undefined}; export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type CalendarEvent = {id?: string | undefined, version?: string | undefined, allDayEvent?: boolean | undefined, attendees?: Array | undefined, calendarId: string, concerningId?: string | undefined, contactId?: string | undefined, createdDate?: number | undefined, deleted?: boolean | undefined, description?: string | undefined, endDate: number, lastModifiedDate?: number | undefined, location?: string | undefined, privateEvent?: boolean | undefined, recurringEvent?: RecurringEvent | undefined, references?: Array | undefined, reminderSendType?: ('MAIL' | 'NOTIFICATION') | undefined, reminderTime?: number | undefined, showAs?: ('BUSY' | 'FREE' | 'OUT_OF_OFFICE' | 'TENTATIVE') | undefined, startDate: number, subject: string, userId?: string | undefined}; export type CreateApiCalendarEventArgs = {dryRun?: boolean | undefined, body: CalendarEvent}; export type CreateApiCalendarEventOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const calendarEventAttendee = z.object({id: z.string().optional(), version: z.string().optional(), attendeeStatus: z.enum(["ACCEPT", "CANCEL", "DECLINE", "INVITATION", "TENTATIVE", "UNDEFINED"]).optional(), calendarEventId: z.string().optional(), createdDate: z.number().int().optional(), eventPermission: z.enum(["CAN_MODIFY_EVERYTHING", "CAN_MODIFY_EVERYTHING_BUT_NOT_PARTICIPIENT_LIST", "CAN_MODIFY_ONLY_PARTICIPIENT_LIST", "CAN_NOT_MODIFY_ANYTHING"]).optional(), lastModifiedDate: z.number().int().optional(), userId: z.string()}); export const recurringEvent = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), endsOn: z.number().int().optional(), eventInterval: z.number().int().optional(), eventOccurrenceCount: z.number().int().optional(), eventType: z.enum(["DAILY", "MONTHLY", "WEEKLY", "YEARLY"]).optional(), lastModifiedDate: z.number().int().optional(), repeatOn: z.string().optional()}); export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const calendarEvent = z.object({id: z.string().optional(), version: z.string().optional(), allDayEvent: z.boolean().optional(), attendees: z.array(calendarEventAttendee).optional(), calendarId: z.string(), concerningId: z.string().optional(), contactId: z.string().optional(), createdDate: z.number().int().optional(), deleted: z.boolean().optional(), description: z.string().optional(), endDate: z.number().int(), lastModifiedDate: z.number().int().optional(), location: z.string().optional(), privateEvent: z.boolean().optional(), recurringEvent: recurringEvent.optional(), references: z.array(entityReference).optional(), reminderSendType: z.enum(["MAIL", "NOTIFICATION"]).optional(), reminderTime: z.number().int().optional(), showAs: z.enum(["BUSY", "FREE", "OUT_OF_OFFICE", "TENTATIVE"]).optional(), startDate: z.number().int(), subject: z.string(), userId: 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 a calendarEvent * * POST /calendarEvent */ export const createApiCalendarEvent = async (args: CreateApiCalendarEventArgs, options: CreateApiCalendarEventOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/calendarEvent${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(calendarEvent, await res.json()) };