import {z} from 'zod' export type GetApiAdvertisersAccountSIDPromoCodesArgs = {AccountSID: string, CampaignId?: number | undefined, Code?: string | undefined, StartDate?: string | undefined, EndDate?: string | undefined, State?: ('ACTIVE' | 'INACTIVE') | undefined}; export type GetApiAdvertisersAccountSIDPromoCodesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const promoCode = z.object({Id: z.number().int().optional(), Code: z.string().optional(), Type: z.enum(["LI", "RE"]).optional(), CreditPolicy: z.enum(["ALWAYS", "INVOLVED", "WINNER"]).optional(), CampaignId: z.number().int().optional(), CampaignName: z.string().optional(), AssignedPartnerId: z.number().int().optional(), AssignedPartnerName: z.string().optional(), CreatedDate: z.string().optional(), StartDate: z.string().nullable().optional(), EndDate: z.string().nullable().optional(), State: z.enum(["ACTIVE", "INACTIVE"]).optional(), DealId: z.number().int().nullable().optional(), DealName: z.string().nullable().optional(), DealState: z.string().nullable().optional(), DealType: z.string().nullable().optional(), DealScope: z.string().nullable().optional(), DealStartDate: z.string().nullable().optional(), DealEndDate: z.string().nullable().optional(), DiscountType: z.string().nullable().optional(), DiscountAmount: z.number().nullable().optional(), DiscountCurrency: z.string().nullable().optional(), DiscountPercent: z.number().nullable().optional(), MinimumPurchaseAmount: z.number().nullable().optional(), MaximumSavingsAmount: z.number().nullable().optional(), PurchaseLimitQuantity: z.number().int().optional(), BogoBuyQuantity: z.number().int().optional(), BogoBuyName: z.string().nullable().optional(), BogoGetQuantity: z.number().int().optional(), BogoGetName: z.string().nullable().optional(), BogoGetDiscountType: z.string().nullable().optional(), SynchAdsPromoCodes: z.boolean().optional(), Uri: z.string().optional()}); export const getApiAdvertisersAccountSIDPromoCodesResponse = z.object({PromoCodes: z.array(promoCode).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 }; /** * List All Promo Codes * * GET /Advertisers/{AccountSID}/PromoCodes */ export const getApiAdvertisersAccountSIDPromoCodes = async (args: GetApiAdvertisersAccountSIDPromoCodesArgs, options?: GetApiAdvertisersAccountSIDPromoCodesOptions) => { const {baseUrl = "https://api.impact.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.CampaignId != null) query.append("CampaignId", String(args.CampaignId)) if (args.Code != null) query.append("Code", String(args.Code)) if (args.StartDate != null) query.append("StartDate", String(args.StartDate)) if (args.EndDate != null) query.append("EndDate", String(args.EndDate)) if (args.State != null) query.append("State", String(args.State)) const url = `${baseUrl.replace(/\/+$/, '')}/Advertisers/${encodeURIComponent(String(args.AccountSID))}/PromoCodes${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(getApiAdvertisersAccountSIDPromoCodesResponse, await res.json()) };