import {z} from 'zod' export type CampaignReportAvailableColumnsSponsoredProductAdsReportingV1 = 'TOTAL_VIEWS' | 'TOTAL_CLICKS' | 'TOTAL_COSTS' | 'TOTAL_SALES' | 'TOTAL_ORDERED_SKUS' | 'CAMPAIGN_ID' | 'EVENT_DATE_CET'; export type CampaignReportGroupByColumnsSponsoredProductAdsReportingV1 = 'EVENT_DATE_CET' | 'CAMPAIGN_ID'; export type FilterableColumnSponsoredProductAdsReportingV1 = 'CAMPAIGN_ID' | 'SKU'; export type FilterOperatorSponsoredProductAdsReportingV1 = '=' | '>=' | '<=' | '>' | '<' | '!='; export type ReportFilterSponsoredProductAdsReportingV1 = {column: FilterableColumnSponsoredProductAdsReportingV1, operator: FilterOperatorSponsoredProductAdsReportingV1, value: string}; export type ReportFormatSponsoredProductAdsReportingV1 = 'CSV'; export type CampaignReportConfigurationSponsoredProductAdsReportingV1 = {columns: Array, groupBy?: (Array | null) | undefined, filters?: (Array | null) | undefined, format: ReportFormatSponsoredProductAdsReportingV1}; export type CampaignReportRequestSponsoredProductAdsReportingV1 = {name: string, fromDate: string, toDate: string, configuration: CampaignReportConfigurationSponsoredProductAdsReportingV1}; export type CreateApiV1SpaReportingCampaignPerformanceArgs = {body: CampaignReportRequestSponsoredProductAdsReportingV1}; export type CreateApiV1SpaReportingCampaignPerformanceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportJobStatusSponsoredProductAdsReportingV1 = z.enum(["IN_PROGRESS", "FAILED", "READY"]); export const reportStatusResponseSponsoredProductAdsReportingV1 = z.object({reportId: z.string(), status: reportJobStatusSponsoredProductAdsReportingV1, downloadUrl: 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 }; /** * Create Campaign Performance Report * * POST /v1/spa-reporting/campaign-performance */ export const createApiV1SpaReportingCampaignPerformance = async (args: CreateApiV1SpaReportingCampaignPerformanceArgs, options?: CreateApiV1SpaReportingCampaignPerformanceOptions) => { const {baseUrl = "https://api.otto.market", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/spa-reporting/campaign-performance${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(reportStatusResponseSponsoredProductAdsReportingV1, await res.json()) };