import {z} from 'zod' export type GetApiCodesCodeArgs = {code: string, 'x-api-key': string}; export type GetApiCodesCodeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const affiliatePlatform = z.object({type: z.enum(["instagram", "tiktok", "youtube", "twitter", "website"]), handle: z.string(), verified: z.boolean()}); export const codeAffiliate = z.object({name: z.string(), logo_url: z.string().nullable(), platforms: z.array(affiliatePlatform)}); export const codePromoCode = z.object({limit: z.number().int()}); export const codeReferralUser = z.object({app_user_id: z.string()}); export const basicReward = z.object({key: z.string(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverRewardActivation = z.object({variant: z.enum(["claim", "conversion"])}); export const receiverRewardNeverDeactivation = z.object({variant: z.literal("never")}); export const receiverRewardIntervalDeactivation = z.object({variant: z.literal("interval"), duration: z.number().int(), period: z.enum(["days", "months", "years"])}); export const receiverBasicReward = z.object({reward: basicReward, activation: receiverRewardActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const creditReward = z.object({key: z.string(), credits: z.number().int(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverCreditReward = z.object({reward: creditReward, activation: receiverRewardActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const offerCodeReward = z.object({key: z.string(), offer_code_id: z.string(), offer_code_ref_name: z.string(), subscription_product_id: z.string(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverRewardClaimActivation = z.object({variant: z.literal("claim")}); export const receiverOfferCodeReward = z.object({reward: offerCodeReward, activation: receiverRewardClaimActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const googlePlayPromoCodeReward = z.object({key: z.string(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverGooglePlayPromoCodeReward = z.object({reward: googlePlayPromoCodeReward, activation: receiverRewardClaimActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const revenueCatEntitlementReward = z.object({key: z.string(), entitlement_id: z.string(), entitlement_key: z.string(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverRevenueCatEntitlementReward = z.object({reward: revenueCatEntitlementReward, activation: receiverRewardActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const revenueCatOfferingReward = z.object({key: z.string(), offering_id: z.string(), offering_key: z.string(), name: z.string(), description: z.string().nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: z.string().nullable()}); export const receiverRevenueCatOfferingReward = z.object({reward: revenueCatOfferingReward, activation: receiverRewardClaimActivation, deactivation: z.union([receiverRewardNeverDeactivation, receiverRewardIntervalDeactivation])}); export const receiverRewards = z.object({basic: z.array(receiverBasicReward), credit: z.array(receiverCreditReward), offer_code: z.array(receiverOfferCodeReward), googleplay_promo_code: z.array(receiverGooglePlayPromoCodeReward), revenuecat_entitlement: z.array(receiverRevenueCatEntitlementReward), revenuecat_offering: z.array(receiverRevenueCatOfferingReward)}); export const code = z.object({type: z.enum(["affiliate", "promo", "referral"]), code: z.string(), is_active: z.boolean(), entity: z.union([codeAffiliate, codePromoCode, codeReferralUser]), rewards: receiverRewards}); export const codeResponse = z.object({data: code}); 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 Code * * GET /codes/{code} */ export const getApiCodesCode = async (args: GetApiCodesCodeArgs, options?: GetApiCodesCodeOptions) => { const {baseUrl = "https://api.winwinkit.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/codes/${encodeURIComponent(String(args.code))}${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(codeResponse, await res.json()) };