import {z} from 'zod' export type GetApiNetworksCouponcodesArgs = {filter?: Array | undefined, relationship?: ('offer' | 'affiliate') | undefined}; export type GetApiNetworksCouponcodesOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const couponCode = z.object({network_coupon_code_id: z.number().int().optional(), network_id: z.number().int().optional(), network_affiliate_id: z.number().int().optional(), network_offer_id: z.number().int().optional(), coupon_code: z.string().optional(), coupon_status: z.enum(["active", "paused"]).optional(), internal_notes: z.string().optional(), description: z.string().optional(), is_description_plain_text: z.boolean().optional(), start_date: z.string().optional(), end_date: z.string().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), time_deleted: z.number().int().optional(), relationship: z.object({offer: z.object({network_offer_id: z.number().int().optional(), network_id: z.number().int().optional(), name: z.string().optional(), offer_status: z.string().optional()}).optional(), affiliate: z.object({network_affiliate_id: z.number().int().optional(), network_id: z.number().int().optional(), name: z.string().optional(), account_status: z.string().optional()}).optional()}).optional()}); export const getApiNetworksCouponcodesResponse = z.object({coupon_codes: z.array(couponCode).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 Coupon Codes * * GET /networks/couponcodes */ export const getApiNetworksCouponcodes = async (args: GetApiNetworksCouponcodesArgs, options?: GetApiNetworksCouponcodesOptions) => { const {baseUrl = "https://api.eflow.team/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.filter ?? []) query.append("filter", String(item)) if (args.relationship != null) query.append("relationship", String(args.relationship)) if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/networks/couponcodes${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(getApiNetworksCouponcodesResponse, await res.json()) };