import {z} from 'zod' export type GetApiReportingV1ReportsArgs = {count?: number | undefined, fromDate?: number | undefined, start?: number | undefined, toDate?: number | undefined, type?: ('ACTIVE_UNCONVERTED_USERS' | 'APPLICATION_USAGE' | 'CANCELLED_ENTITLEMENTS' | 'COMPANIES_CSV' | 'CONTRACT_TERM' | 'CUSTOMERS_CSV' | 'CUSTOMER_ACQUISITION_BONUS' | 'CUSTOMER_RETENTION_BONUS' | 'FAILED_ORDERS' | 'FAILURE_INTEGRATION_EVENT' | 'FREE_TRIALS' | 'GENERAL_PERFORMANCE_REPORT' | 'INVOICES' | 'INVOICES_CSV' | 'MICROSOFT_RECONCILIATION' | 'MIGRATION_PRODUCT_DATA' | 'MOSI_ACQUISITION_BONUS' | 'MOSI_RETENTION_BONUS' | 'NOT_USED_APP' | 'ORDER_EDITIONS_CSV' | 'ORDER_PRODUCTS_CSV' | 'PAYMENTS_DETAIL_CSV' | 'PAYMENTS_OVERVIEW_CSV' | 'RECONCILIATION' | 'RE_CALCULATE_SUBSCRIPTION_PRICES' | 'SUSPENDED_PURCHASED' | 'TAXES' | 'TRANSACTIONS' | 'VOICE_CONN_CSV') | undefined}; export type GetApiReportingV1ReportsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportType = z.enum(["ACTIVE_UNCONVERTED_USERS", "APPLICATION_USAGE", "CANCELLED_ENTITLEMENTS", "COMPANIES_CSV", "CONTRACT_TERM", "CUSTOMERS_CSV", "CUSTOMER_ACQUISITION_BONUS", "CUSTOMER_RETENTION_BONUS", "FAILED_ORDERS", "FAILURE_INTEGRATION_EVENT", "FREE_TRIALS", "GENERAL_PERFORMANCE_REPORT", "INVOICES", "INVOICES_CSV", "MICROSOFT_RECONCILIATION", "MIGRATION_PRODUCT_DATA", "MOSI_ACQUISITION_BONUS", "MOSI_RETENTION_BONUS", "NOT_USED_APP", "ORDER_EDITIONS_CSV", "ORDER_PRODUCTS_CSV", "PAYMENTS_DETAIL_CSV", "PAYMENTS_OVERVIEW_CSV", "RECONCILIATION", "RE_CALCULATE_SUBSCRIPTION_PRICES", "SUSPENDED_PURCHASED", "TAXES", "TRANSACTIONS", "VOICE_CONN_CSV"]); export const reportWS = z.object({createdDate: z.number().optional(), endDate: z.number().optional(), filename: z.string().optional(), id: z.number().optional(), reportType: reportType.optional(), startDate: z.number().optional()}); export const reportsWS = z.object({reports: z.array(reportWS).optional(), totalCount: z.number().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 all reports * * GET /reporting/v1/reports */ export const getApiReportingV1Reports = async (args: GetApiReportingV1ReportsArgs, options?: GetApiReportingV1ReportsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.count != null) query.append("count", String(args.count)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.start != null) query.append("start", String(args.start)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.type != null) query.append("type", String(args.type)) const url = `${baseUrl.replace(/\/+$/, '')}/reporting/v1/reports${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(reportsWS, await res.json()) };