import {z} from 'zod' export type BookingCreate = {start_time: number, end_time: number, event_id?: string | undefined, master_id?: string | undefined, calendar_id?: string | undefined, participants?: (Array<{email?: string | undefined}>) | undefined, guest: {email?: string | undefined, name?: string | undefined}, timezone?: string | undefined, email_language?: ('en' | 'fr' | 'de' | 'es' | 'nl' | 'sv' | 'ja' | 'zh') | undefined, additional_guests?: (Array<{email?: string | undefined, name?: string | undefined}>) | undefined, additional_fields?: {[key: string]: string} | undefined}; export type CreateApiV3SchedulingBookingsArgs = {body: BookingCreate}; export type CreateApiV3SchedulingBookingsOptions = {baseUrl?: string | undefined, SCHEDULERSESSIONTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV3SchedulingBookingsResponse = z.object({request_id: z.string().optional(), data: z.object({booking_id: z.string(), event_id: z.string(), title: z.string(), organizer: z.object({email: z.string().optional(), name: z.string().optional()}), status: z.enum(["booked", "pending", "cancelled"]), description: z.string().optional()}).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 }; /** * Book an event * * POST /v3/scheduling/bookings */ export const createApiV3SchedulingBookings = async (args: CreateApiV3SchedulingBookingsArgs, options?: CreateApiV3SchedulingBookingsOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, SCHEDULERSESSIONTOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SCHEDULERSESSIONTOKEN !== undefined) headers.set('authorization', `Bearer ${SCHEDULERSESSIONTOKEN}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/scheduling/bookings${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(createApiV3SchedulingBookingsResponse, await res.json()) };