import {z} from 'zod' export type Junction = 'and' | 'or'; export type FilterConditionsDateTime = {$after?: string | undefined, $before?: string | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $more_than?: number | undefined, $less_than?: number | undefined}; export type FilterConditionsString = {$in?: Array | undefined, $not_in?: Array | undefined, $is?: string | undefined, $is_not?: string | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined, $contains?: string | undefined, $not_contain?: string | undefined}; export type ExportCampaignTransactionsFilters = {junction?: Junction | undefined, created_at?: ({conditions?: FilterConditionsDateTime | undefined}) | undefined, voucher_id?: ({conditions?: FilterConditionsString | undefined}) | undefined}; export type CampaignsTransactionsExportCreateRequestBody = {parameters?: ({order?: ('-created_at' | 'created_at') | undefined, fields?: (Array<'id' | 'type' | 'source_id' | 'reason' | 'balance' | 'amount' | 'created_at' | 'voucher_id' | 'source' | 'details' | 'related_transaction_id'>) | undefined, filters?: ExportCampaignTransactionsFilters | undefined}) | undefined}; export type CreateApiV1CampaignsCampaignIdTransactionsExportArgs = {body: CampaignsTransactionsExportCreateRequestBody}; export type CreateApiV1CampaignsCampaignIdTransactionsExportOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const junction = z.enum(["and", "or"]); export const filterConditionsDateTime = z.object({$after: z.string().optional(), $before: z.string().optional(), $has_value: z.string().optional(), $is_unknown: z.string().optional(), $more_than: z.number().int().optional(), $less_than: z.number().int().optional()}); export const filterConditionsString = z.object({$in: z.array(z.string()).optional(), $not_in: z.array(z.string()).optional(), $is: z.string().optional(), $is_not: z.string().optional(), $has_value: z.string().optional(), $is_unknown: z.string().optional(), $starts_with: z.string().optional(), $ends_with: z.string().optional(), $contains: z.string().optional(), $not_contain: z.string().optional()}); export const campaignsTransactionsExportCreateResponseBody = z.object({id: z.string(), object: z.literal("export"), created_at: z.string(), status: z.literal("SCHEDULED"), channel: z.literal("API"), exported_object: z.literal("voucher_transactions"), parameters: z.object({order: z.enum(["-created_at", "created_at"]).optional(), fields: z.array(z.enum(["id", "type", "source_id", "reason", "balance", "amount", "created_at", "voucher_id", "source", "details", "related_transaction_id"])).optional(), filters: z.object({junction: junction.optional(), created_at: z.object({conditions: filterConditionsDateTime.optional()}).optional(), voucher_id: z.object({conditions: filterConditionsString.optional()}).optional(), campaign_id: z.object({conditions: z.object({$in: z.array(z.string().min(1).max(1))})})}).optional()}), result: z.object({url: z.string()}).nullable(), user_id: z.string().nullable()}); 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 }; /** * Export Campaign Transactions * * POST /v1/campaigns/{campaignId}/transactions/export * * Also accepts auth: X-Voucherify-OAuth */ export const createApiV1CampaignsCampaignIdTransactionsExport = async (args: CreateApiV1CampaignsCampaignIdTransactionsExportArgs, options?: CreateApiV1CampaignsCampaignIdTransactionsExportOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/campaigns/${encodeURIComponent(String(args.campaignId))}/transactions/export${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(campaignsTransactionsExportCreateResponseBody, await res.json()) };