import {z} from 'zod' export type ValidityTimeframe = {duration?: string | undefined, interval?: string | undefined}; export type ValidityDayOfWeek = Array<0 | 1 | 2 | 3 | 4 | 5 | 6>; export type SimpleLoyaltyCard = {points: number, balance?: number | undefined, next_expiration_date?: string | undefined, next_expiration_points?: number | undefined, pending_points?: number | undefined, expired_points?: number | undefined, subtracted_points?: number | undefined}; export type VoucherImportLoyaltyCard = {code: string, redemption?: ({quantity?: number | undefined}) | undefined, active?: boolean | undefined, metadata?: Record | undefined, category?: string | undefined, start_date?: string | undefined, expiration_date?: string | undefined, validity_timeframe?: ValidityTimeframe | undefined, validity_day_of_week?: ValidityDayOfWeek | undefined, additional_info?: string | undefined, type?: 'LOYALTY_CARD' | undefined, loyalty_card: SimpleLoyaltyCard}; export type Gift = {amount: number, subtracted_amount?: number | undefined, balance: number, effect?: ('APPLY_TO_ORDER' | 'APPLY_TO_ITEMS') | undefined}; export type VoucherImportGift = {code: string, redemption?: ({quantity?: number | undefined}) | undefined, active?: boolean | undefined, metadata?: Record | undefined, category?: string | undefined, start_date?: string | undefined, expiration_date?: string | undefined, validity_timeframe?: ValidityTimeframe | undefined, validity_day_of_week?: ValidityDayOfWeek | undefined, additional_info?: string | undefined, type?: 'GIFT_VOUCHER' | undefined, gift: Gift}; export type DiscountAmountVouchersEffectTypes = 'APPLY_TO_ORDER' | 'APPLY_TO_ITEMS' | 'APPLY_TO_ITEMS_PROPORTIONALLY' | 'APPLY_TO_ITEMS_PROPORTIONALLY_BY_QUANTITY' | 'APPLY_TO_ITEMS_BY_QUANTITY'; export type DiscountUnitVouchersEffectTypes = 'ADD_MISSING_ITEMS' | 'ADD_NEW_ITEMS' | 'ADD_MANY_ITEMS' | 'ADD_SAME_ITEMS'; export type SimpleProductDiscountUnit = {id: string, source_id?: string | undefined, name: string}; export type SimpleSkuDiscountUnit = {id: string, source_id?: string | undefined, name: string}; export type DiscountUnitMultipleOneUnit = {unit_off?: number | undefined, unit_off_formula?: string | undefined, effect: 'ADD_NEW_ITEMS' | 'ADD_MISSING_ITEMS', unit_type: string, product?: SimpleProductDiscountUnit | undefined, sku?: SimpleSkuDiscountUnit | undefined}; export type DiscountPercentVouchersEffectTypes = 'APPLY_TO_ORDER' | 'APPLY_TO_ITEMS'; export type DiscountFixedVouchersEffectTypes = 'APPLY_TO_ORDER' | 'APPLY_TO_ITEMS'; export type Discount = {type: 'AMOUNT', amount_off: number, amount_off_formula?: string | undefined, aggregated_amount_limit?: number | undefined, effect?: DiscountAmountVouchersEffectTypes | undefined, is_dynamic?: boolean | undefined} | {type: 'UNIT', unit_off?: number | undefined, unit_off_formula?: string | undefined, effect?: DiscountUnitVouchersEffectTypes | undefined, unit_type: string, product?: SimpleProductDiscountUnit | undefined, sku?: SimpleSkuDiscountUnit | undefined, is_dynamic?: boolean | undefined} | {type: 'UNIT', effect?: 'ADD_MANY_ITEMS' | undefined, units: Array} | {type: 'PERCENT', percent_off: number, percent_off_formula?: string | undefined, amount_limit?: number | undefined, aggregated_amount_limit?: number | undefined, effect?: DiscountPercentVouchersEffectTypes | undefined, is_dynamic?: boolean | undefined} | {type: 'FIXED', fixed_amount: number, fixed_amount_formula?: string | undefined, effect?: DiscountFixedVouchersEffectTypes | undefined, is_dynamic?: boolean | undefined}; export type VoucherImportDiscount = {code: string, redemption?: ({quantity?: number | undefined}) | undefined, active?: boolean | undefined, metadata?: Record | undefined, category?: string | undefined, start_date?: string | undefined, expiration_date?: string | undefined, validity_timeframe?: ValidityTimeframe | undefined, validity_day_of_week?: ValidityDayOfWeek | undefined, additional_info?: string | undefined, type?: 'DISCOUNT_VOUCHER' | undefined, discount: Discount}; export type CampaignsImportVoucherItem = VoucherImportLoyaltyCard | VoucherImportGift | VoucherImportDiscount; export type CampaignsImportCreateRequestBody = Array; export type CreateApiV1CampaignsCampaignIdImportArgs = {body: CampaignsImportCreateRequestBody}; export type CreateApiV1CampaignsCampaignIdImportOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignsImportCreateResponseBody = z.object({async_action_id: z.string()}); 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 }; /** * Import Vouchers to Campaign * * POST /v1/campaigns/{campaignId}/import * * Also accepts auth: X-Voucherify-OAuth */ export const createApiV1CampaignsCampaignIdImport = async (args: CreateApiV1CampaignsCampaignIdImportArgs, options?: CreateApiV1CampaignsCampaignIdImportOptions) => { 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))}/import${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(campaignsImportCreateResponseBody, await res.json()) };