import {z} from 'zod' export type GetApiV2LoyaltiesProgramsProgramIdReportsDailyTasksDailyArgs = {start_date: string, end_date: string, resolution: 'day' | 'week' | 'month' | 'quarter'}; export type GetApiV2LoyaltiesProgramsProgramIdReportsDailyTasksDailyOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const programDailyTasksDailyReport = z.object({date: z.string(), points_expirations: z.number().gte(0), points_activations: z.number().gte(0), points_cancellations: z.number().gte(0), tier_recalculations: z.number().gte(0), object: z.literal("program_daily_tasks_daily_report")}); export const loyaltiesProgramsReportsDailyTasksDailyListResponseBody = z.object({data: z.array(programDailyTasksDailyReport), object: z.literal("report")}); 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 program daily task reports * * GET /v2/loyalties/programs/{programId}/reports/daily-tasks/daily * * Also accepts auth: bearerAuth */ export const getApiV2LoyaltiesProgramsProgramIdReportsDailyTasksDaily = async (args: GetApiV2LoyaltiesProgramsProgramIdReportsDailyTasksDailyArgs, options?: GetApiV2LoyaltiesProgramsProgramIdReportsDailyTasksDailyOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/loyalties/programs/${encodeURIComponent(String(args.programId))}/reports/daily-tasks/daily${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(loyaltiesProgramsReportsDailyTasksDailyListResponseBody, await res.json()) };