import {z} from 'zod' export type GetApiV2RewardSuppliersTangoCatalogOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const tangoBrandItemResponse = z.object({countries: z.array(z.string()).optional(), currency_code: z.enum(["AUD", "BRL", "CAD", "CNY", "EUR", "GBP", "HKD", "INR", "JPY", "KRW", "MONTH", "MXN", "NZD", "PERCENT_OFF", "POINTS", "TRY", "TWD", "USD"]).optional(), face_value: z.number().nullable().optional(), max_value: z.number().nullable().optional(), min_value: z.number().nullable().optional(), reward_name: z.string().optional(), utid: z.string().optional(), value_type: z.enum(["FIXED_VALUE", "UNSUPPORTED", "VARIABLE_VALUE"]).optional()}); export const tangoBrandResponse = z.object({brand_name: z.string(), description: z.string(), disclaimer: z.string(), image_url: z.string().nullable(), items: z.array(tangoBrandItemResponse)}); export const getApiV2RewardSuppliersTangoCatalogResponse = z.array(tangoBrandResponse); 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 }; /** * Get Tango catalog * * GET /v2/reward-suppliers/tango/catalog * * Also accepts auth: QUERY, COOKIE */ export const getApiV2RewardSuppliersTangoCatalog = async (options?: GetApiV2RewardSuppliersTangoCatalogOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v2/reward-suppliers/tango/catalog${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(getApiV2RewardSuppliersTangoCatalogResponse, await res.json()) };