import {z} from 'zod' export type ShopGmvTimeseriesRequest = {start_date?: (string | null) | undefined, end_date?: (string | null) | undefined}; export type CreateApiShopGmvTimeseriesArgs = {body: ShopGmvTimeseriesRequest}; export type CreateApiShopGmvTimeseriesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, ShopId?: string | undefined, fetch?: typeof fetch | undefined}; export const shopGmvVideoChannel = z.object({gmv: z.number().nullable().optional(), affiliate: z.number().nullable().optional(), seller: z.number().nullable().optional()}); export const shopGmvLiveChannel = z.object({gmv: z.number().nullable().optional(), affiliate: z.number().nullable().optional(), seller: z.number().nullable().optional()}); export const shopGmvProductCardChannel = z.object({gmv: z.number().nullable().optional(), shop_tab: z.number().nullable().optional(), search: z.number().nullable().optional()}); export const shopGmvChannels = z.object({video: shopGmvVideoChannel, live: shopGmvLiveChannel, product_card: shopGmvProductCardChannel}); export const shopGmvTraffic = z.object({product_impressions: z.number().int().nullable().optional(), product_clicks: z.number().int().nullable().optional()}); export const shopGmvSeriesPoint = z.object({date: z.string(), gmv: z.number().nullable().optional(), orders: z.number().int().nullable().optional(), items_sold: z.number().int().nullable().optional(), customers: z.number().int().nullable().optional(), aov: z.number().nullable().optional(), channels: shopGmvChannels, traffic: shopGmvTraffic}); export const shopGmvTimeseriesResponse = z.object({shop_id: z.number().int(), currency: z.string().nullable().optional(), currency_code: z.string().nullable().optional(), granularity: z.string().optional(), start_date: z.string(), end_date: z.string(), series: z.array(shopGmvSeriesPoint)}); 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 }; /** * Daily shop GMV time series (Seller Center, full shop view) * * POST /shop-gmv/timeseries */ export const createApiShopGmvTimeseries = async (args: CreateApiShopGmvTimeseriesArgs, options?: CreateApiShopGmvTimeseriesOptions) => { const {baseUrl = "https://api.reacherapp.com/public/v1", fetch: fetchFn = fetch, ApiKeyAuth, ShopId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (ShopId !== undefined) headers.set("x-shop-id", ShopId) const url = `${baseUrl.replace(/\/+$/, '')}/shop-gmv/timeseries${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(shopGmvTimeseriesResponse, await res.json()) };