import {z} from 'zod' export type GetApiV20CalendarArgs = {leadId?: string | undefined, startTime?: string | undefined, endTime?: string | undefined, startTimeMs?: string | undefined, endTimeMs?: string | undefined, timeZoneCode?: string | undefined, includeFinished?: string | undefined, page?: string | undefined, pageSize?: string | undefined, sort?: string | undefined, desc?: string | undefined, sourceType?: string | undefined, Authorization: string}; export type GetApiV20CalendarOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const calendarItemV2 = z.object({id: z.string().optional(), type: z.string().optional(), taskId: z.number().int().optional(), sourceType: z.number().int().optional(), content: z.string().optional(), title: z.string().optional(), finished: z.boolean().optional(), startAt: z.string().optional(), endAt: z.string().optional(), startAtMs: z.number().int().optional(), endAtMs: z.number().int().optional(), timeZoneCode: z.string().optional(), leadId: z.number().int().optional()}); export const calendarListResponseV2 = z.object({total: z.number().int().optional(), page: z.number().int().optional(), pageSize: z.number().int().optional(), items: z.array(calendarItemV2).optional()}); export const calendarListResponseEnvelope = z.object({data: calendarListResponseV2.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 }; /** * List Calendar Events * * GET /v2.0/calendar */ export const getApiV20Calendar = async (args: GetApiV20CalendarArgs, options?: GetApiV20CalendarOptions) => { const {baseUrl = "https://api.lofty.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.leadId != null) query.append("leadId", String(args.leadId)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.startTimeMs != null) query.append("startTimeMs", String(args.startTimeMs)) if (args.endTimeMs != null) query.append("endTimeMs", String(args.endTimeMs)) if (args.timeZoneCode != null) query.append("timeZoneCode", String(args.timeZoneCode)) if (args.includeFinished != null) query.append("includeFinished", String(args.includeFinished)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.desc != null) query.append("desc", String(args.desc)) if (args.sourceType != null) query.append("sourceType", String(args.sourceType)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2.0/calendar${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(calendarListResponseEnvelope, await res.json()) };