import {z} from 'zod' export type GetApiV1EventsArgs = {limit?: number | undefined, after?: string | undefined, before?: string | undefined, state?: ('all' | 'any' | 'confirmed' | 'canceled' | 'awaiting_reschedule' | 'awaiting_checkout' | 'checkout_expired' | 'awaiting_approval' | 'declined' | 'tentative') | undefined, period?: ('past' | 'upcoming' | 'fixed' | 'all') | undefined, from?: string | undefined, until?: string | undefined, time_zone?: string | undefined, q?: string | undefined, direction?: ('asc' | 'desc') | undefined, attendance?: ('attending' | 'any') | undefined, link?: string | undefined}; export type GetApiV1EventsOptions = {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 event = 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 paginationMeta = z.object({after: z.string().nullable().optional(), before: z.string().nullable().optional(), limit: z.number().int()}); export const eventListResponse = z.object({entries: z.array(event), metadata: paginationMeta}); 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 events * * GET /v1/events * * Also accepts auth: PersonalAccessToken */ export const getApiV1Events = async (args: GetApiV1EventsArgs, options?: GetApiV1EventsOptions) => { const {baseUrl = "https://api.savvycal.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.state != null) query.append("state", String(args.state)) if (args.period != null) query.append("period", String(args.period)) if (args.from != null) query.append("from", String(args.from)) if (args.until != null) query.append("until", String(args.until)) if (args.time_zone != null) query.append("time_zone", String(args.time_zone)) if (args.q != null) query.append("q", String(args.q)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.attendance != null) query.append("attendance", String(args.attendance)) if (args.link != null) query.append("link", String(args.link)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events${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(eventListResponse, await res.json()) };