import {z} from 'zod' export type GetApiV1CrmMetricsGeneralRequestsArgs = {date: string, ordering?: string | undefined, page?: number | undefined, pageSize?: number | undefined, rental_point?: number | undefined}; export type GetApiV1CrmMetricsGeneralRequestsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metricsGeneralRequestDetail = z.object({date: z.string(), client_id: z.number().int(), client_name: z.string(), client_phone: z.string(), client_avatar: z.string(), request_id: z.number().int(), request_status: z.number().int(), prediction_sum: z.string(), profit_sum: z.string()}); export const paginatedMetricsGeneralRequestDetailList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(metricsGeneralRequestDetail)}); 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 }; /** * GET /v1/crm/metrics/general/requests/ */ export const getApiV1CrmMetricsGeneralRequests = async (args: GetApiV1CrmMetricsGeneralRequestsArgs, options?: GetApiV1CrmMetricsGeneralRequestsOptions) => { const {baseUrl = "https://api.yume.cloud", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.rental_point != null) query.append("rental_point", String(args.rental_point)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/crm/metrics/general/requests/${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(paginatedMetricsGeneralRequestDetailList, await res.json()) };