import {z} from 'zod' export type GetApiV3ActivityTrackingArgs = {event_type?: (Array<'opened' | 'link_clicked'>) | undefined, label?: string | undefined, sending_domain?: string | undefined, from?: string | undefined, grant_id?: string | undefined, message_id?: string | undefined, thread_id?: string | undefined, start?: number | undefined, end?: number | undefined, order?: ('desc' | 'asc') | undefined, limit?: number | undefined, page_token?: string | undefined}; export type GetApiV3ActivityTrackingOptions = {baseUrl?: string | undefined, NYLASAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3ActivityTrackingResponse = z.object({request_id: z.string().optional(), data: z.array(z.object({id: z.string().optional(), event_type: z.enum(["opened", "link_clicked"]).optional(), timestamp: z.number().int().optional(), application_id: z.string().optional(), grant_id: z.string().optional(), message_id: z.string().optional(), thread_id: z.string().optional(), from: z.string().optional(), sending_domain: z.string().optional(), label: z.string().optional(), is_first: z.boolean().optional(), url: z.string().optional(), link_index: z.number().int().optional(), ip: z.string().optional(), user_agent: z.string().optional()})).optional(), next_cursor: z.string().nullable().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 tracking events * * GET /v3/activity/tracking */ export const getApiV3ActivityTracking = async (args: GetApiV3ActivityTrackingArgs, options?: GetApiV3ActivityTrackingOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, NYLASAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.event_type ?? []) query.append("event_type", String(item)) if (args.label != null) query.append("label", String(args.label)) if (args.sending_domain != null) query.append("sending_domain", String(args.sending_domain)) if (args.from != null) query.append("from", String(args.from)) if (args.grant_id != null) query.append("grant_id", String(args.grant_id)) if (args.message_id != null) query.append("message_id", String(args.message_id)) if (args.thread_id != null) query.append("thread_id", String(args.thread_id)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/activity/tracking${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(getApiV3ActivityTrackingResponse, await res.json()) };