import {z} from 'zod' export type GetApiV10LogTypeArgs = {leadId: number, logType: 'logCall' | 'logEmail' | 'logText', offset?: number | undefined, limit?: number | undefined, currentId?: number | undefined, sort?: ('createTime' | 'id') | undefined, order?: ('asc' | 'desc') | undefined, Authorization: string}; export type GetApiV10LogTypeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const logTypeResponse = z.object({id: z.number().int().optional(), leadId: z.number().int().optional(), leadPhoneNumber: z.string().optional(), callingOutcome: z.enum(["Talked", "VoiceMessage", "NoAnswer", "BadNumber", "DNCNumber", "DNCContact"]).optional(), content: z.string().optional(), createTime: z.string().optional(), outboundOrInbound: z.enum(["outbound", "inbound"]).optional(), isPin: z.boolean().optional(), agentId: z.number().int().optional(), emailSubject: z.string().optional(), toEmail: z.string().optional(), fromEmail: z.string().optional()}); export const logTypeListResponse = z.object({logType: z.array(logTypeResponse).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 manual logs of a lead * * GET /v1.0/logType */ export const getApiV10LogType = async (args: GetApiV10LogTypeArgs, options?: GetApiV10LogTypeOptions) => { 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.logType != null) query.append("logType", String(args.logType)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.currentId != null) query.append("currentId", String(args.currentId)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/logType${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(logTypeListResponse, await res.json()) };