import {z} from 'zod' export type CancelEventRequest = {cancel_reason?: (string | null) | undefined}; export type CreateApiV1EventsEventIdCancelArgs = {event_id: string, body: CancelEventRequest}; export type CreateApiV1EventsEventIdCancelOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const linkPreview = z.object({description: z.string(), id: z.string(), name: z.string(), private_name: z.string().nullable(), slug: z.string()}); export const pollPreview = z.object({id: z.string(), name: z.string(), slug: z.string()}); export const fieldOption = z.object({id: z.string(), value: z.string()}); export const eventField = z.object({id: z.string(), label: z.string(), options: z.array(fieldOption), type: z.enum(["short_text", "long_text", "checkboxes", "radios", "select", "url", "hidden"]), value: z.string().nullable()}); export const eventAttendee = z.object({display_name: z.string(), email: z.string(), fields: z.array(eventField), first_name: z.string(), id: z.string(), is_organizer: z.boolean(), last_name: z.string(), marketing_opt_in: z.boolean(), phone_number: z.string().nullable().optional(), response_status: z.enum(["accepted", "tentative", "needs_action", "declined"]), time_zone: z.string()}); export const payment = z.object({amount_total: z.number().int().nullable(), state: z.enum(["awaiting_checkout", "paid"]), url: z.string().nullable()}); export const scopePreview = z.object({id: z.string(), name: z.string(), slug: z.string()}); export const singleEventResponse = z.object({canceled_at: z.string().nullable().optional(), rescheduled_at: z.string().nullable().optional(), location_settings: z.array(z.object({type: z.string()}).and(z.record(z.string(), z.unknown()))).optional(), description_settings: z.object({include_attendees: z.boolean(), include_fields: z.boolean(), include_link_name: z.boolean()}), location: z.string().nullable().optional(), buffer_before: z.number().int(), additional_info: z.string().nullable().optional(), duration: z.number().int(), uuid: z.string(), reschedule_reason: z.string().nullable().optional(), conferencing: z.object({instructions: z.string().nullable().optional(), join_url: z.string().nullable().optional(), meeting_id: z.string().nullable().optional(), type: z.string()}), link: linkPreview.nullable().optional(), poll: pollPreview.nullable().optional(), summary: z.string(), original_end_at: z.string().nullable().optional(), description: z.string().optional(), id: z.string(), cancel_reason: z.string().nullable().optional(), organizer: eventAttendee, is_group_session: z.boolean().nullable(), original_start_at: z.string().nullable().optional(), start_at: z.string(), url: z.string(), payment: payment.nullable().optional(), maximum_group_size: z.number().int().nullable(), end_at: z.string(), state: z.enum(["confirmed", "canceled", "awaiting_reschedule", "awaiting_checkout", "checkout_expired", "awaiting_approval", "declined", "tentative"]), attendees: z.array(eventAttendee), scheduler: eventAttendee.nullable().optional(), scope: scopePreview.nullable().optional(), buffer_after: z.number().int(), metadata: z.object({}), created_at: z.string()}); 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 }; /** * Cancel event * * POST /v1/events/{event_id}/cancel * * Also accepts auth: PersonalAccessToken */ export const createApiV1EventsEventIdCancel = async (args: CreateApiV1EventsEventIdCancelArgs, options?: CreateApiV1EventsEventIdCancelOptions) => { const {baseUrl = "https://api.savvycal.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events/${encodeURIComponent(String(args.event_id))}/cancel${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(singleEventResponse, await res.json()) };