import {z} from 'zod' export type GetApiAppYelpLeadsArgs = {businessId?: string | undefined, status?: string | undefined, limit?: number | undefined}; export type GetApiAppYelpLeadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpLeadSchema = z.object({id: z.string(), businessId: z.string().nullable(), businessName: z.string().nullable(), conversationId: z.string().nullable(), consumer: z.string().nullable(), type: z.string(), excerpt: z.string().nullable(), status: z.string(), responseMinutes: z.number().int().nullable(), createdAt: z.string().nullable(), lastEventAt: z.string().nullable(), conversationMessages: z.array(z.object({})), timelineEvents: z.array(z.object({})), replyDraftId: z.number().int().nullable(), replyDraftStatus: z.string().nullable(), replyDraftBody: z.string().nullable(), replyDraftMessage: z.string().nullable(), replyDraftGeneratedAt: z.string().nullable(), assignedUserId: z.number().int().nullable().optional(), assignedUserName: z.string().nullable().optional(), claimedAt: z.string().nullable().optional(), phoneShared: z.boolean().optional(), sharedPhone: z.string().nullable().optional(), phoneSource: z.string().nullable().optional(), emailShared: z.boolean().optional(), sharedEmail: z.string().nullable().optional(), emailSource: z.string().nullable().optional(), businessTimezone: z.string().nullable().optional()}); export const getApiAppYelpLeadsResponse = z.array(appYelpLeadSchema); 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 synced Yelp leads for the current user * * GET /app/yelp/leads */ export const getApiAppYelpLeads = async (args: GetApiAppYelpLeadsArgs, options: GetApiAppYelpLeadsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.businessId != null) query.append("businessId", String(args.businessId)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/leads${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(getApiAppYelpLeadsResponse, await res.json()) };