nuxt-x-affiliate
nuxt-x-affiliate
Affiliate/referral program layer for Nuxt 4. Provides 10 auto-imported XAF-prefixed components, 2 composables for state management and referral tracking, 2 pre-built pages, and a fully typed app.config.ts interface — all built on Nuxt UI v4. The consuming app is responsible for data fetching; the layer handles all UI, state, and referral tracking logic.
Installation
npm install @xenterprises/nuxt-x-affiliate
// nuxt.config.ts
export default defineNuxtConfig({
extends: [['@xenterprises/nuxt-x-affiliate', { install: true }]],
})
Override defaults in app.config.ts:
export default defineAppConfig({
xAffiliate: {
referralParam: 'ref',
referralBaseUrl: 'https://yoursite.com',
cookieDays: 30,
currency: 'USD',
shareMessage: 'Check this out!',
},
})
What the Layer Provides
Components (auto-imported, XAF prefix):
XAFBanner— Promotional hero banner with CTA and variant styling (gradient / primary / dark)XAFCommissionTiers— Commission tier display with current-tier highlight and progress barXAFDashboard— Full affiliate dashboard composing all sub-componentsXAFLeaderboard— Top affiliates ranking with earnings and referral countsXAFPayoutHistory— Payout history table with status badgesXAFReferralLink— Referral link display with copy-to-clipboardXAFReferralTable— Referral tracking table (source, status, commission, date)XAFShareButtons— Social sharing buttons (Twitter, Facebook, LinkedIn, Email)XAFSignupForm— Affiliate application form with configurable fieldsXAFStatsCards— Stats overview cards (clicks, conversions, earnings, pending)
Composables:
useAffiliate()— SSR-safe state management for affiliate data, stats, referrals, payouts, and tiersuseReferralTracking()— Detects and persists referral codes via URL params and cookies
Pages:
/affiliate— Public landing page with hero, benefits, commission tiers, and signup form/affiliate/dashboard— Authenticated affiliate dashboard with full metrics and tools
App Config Options
Configure under the xAffiliate key in app.config.ts:
| Option | Type | Default | Description |
|---|---|---|---|
referralParam | string | 'ref' | URL query parameter for referral codes |
referralBaseUrl | string | '' | Base URL for generated referral links |
cookieName | string | 'x_affiliate_ref' | Cookie name for storing referral codes |
cookieDays | number | 30 | Cookie expiration in days |
currency | string | 'USD' | Default currency code for formatting |
shareMessage | string | 'Check this out!' | Default social share message |
landing.hero | object | See defaults | Hero section (title, description, ctaLabel, highlightText, variant) |
landing.benefits | array | 3 defaults | Benefits cards (icon, title, description) |
signup.* | object | See defaults | Signup form (title, subtitle, submitLabel, field visibility) |
Minimal Usage Example
<script setup>
const { affiliate, stats, referralUrl, setAffiliate, setStats } = useAffiliate()
// Your app fetches data and populates state
onMounted(async () => {
const data = await $fetch('/api/affiliate/me')
setAffiliate(data.affiliate)
setStats(data.stats)
})
</script>
<template>
<XAFDashboard />
</template>
Component Props Reference
XAFBanner
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | required | Banner headline text |
description | string | — | Subtitle text |
ctaLabel | string | 'Join Now' | CTA button label |
ctaTo | string | '/affiliate' | CTA link destination |
ctaColor | string | 'white' | CTA button color |
highlightText | string | — | Highlight badge text |
variant | 'primary' | 'gradient' | 'dark' | 'gradient' | Visual style |
showDecoration | boolean | true | Show background decoration |
XAFSignupForm
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | 'Join Our Affiliate Program' | Form title |
subtitle | string | 'Earn commissions...' | Form subtitle |
submitLabel | string | 'Apply Now' | Submit button label |
successMessage | string | 'Application submitted...' | Success message after submit |
showWebsite | boolean | true | Show website URL field |
showCompany | boolean | false | Show company name field |
showMessage | boolean | true | Show promotion strategy textarea |
Emits: submit(data: AffiliateFormData)
XAFStatsCards
| Prop | Type | Default | Description |
|---|---|---|---|
stats | AffiliateStats | null | required | Stats data object |
currency | string | 'USD' | Currency code for formatting |
XAFReferralLink
| Prop | Type | Default | Description |
|---|---|---|---|
affiliate | Affiliate | null | required | Current affiliate data |
referralUrl | string | required | Full referral URL to display and copy |
XAFCommissionTiers
| Prop | Type | Default | Description |
|---|---|---|---|
tiers | CommissionTier[] | required | Array of commission tier definitions |
currentTierId | string | null | null | ID of the affiliate's current tier |
nextTier | CommissionTier | null | null | Next tier to reach |
progressToNextTier | number | 0 | Progress percentage (0–100) |
XAFLeaderboard
| Prop | Type | Default | Description |
|---|---|---|---|
entries | { name, referrals, earnings }[] | required | Leaderboard entries |
title | string | 'Top Affiliates' | Section title |
period | string | 'This Month' | Time period badge |
currency | string | 'USD' | Currency code |
Composables
useAffiliate()
SSR-safe state management using Nuxt useState. The consuming app calls setters after fetching data from its API.
const {
affiliate, // Affiliate | null
stats, // AffiliateStats | null
referrals, // Referral[]
payouts, // Payout[]
tiers, // CommissionTier[]
isLoading, // boolean
error, // string | null
isAuthenticated, // computed — whether affiliate is set
isActive, // computed — whether status is 'active'
referralUrl, // computed — full referral URL with code
currentTier, // computed — current CommissionTier
nextTier, // computed — next CommissionTier
progressToNextTier, // computed — progress % to next tier
setAffiliate,
setStats,
setReferrals,
setPayouts,
setTiers,
reset,
} = useAffiliate()
useReferralTracking()
Detects referral codes from URL query parameters and persists them in cookies. SSR-safe — cookie operations are guarded by import.meta.server checks.
const {
referralCode, // string | null
referralSource, // string | null (UTM source)
detectReferral, // () => void — detect from URL params or cookie
clearReferral, // () => void
hasReferral, // () => boolean
getReferralCode, // () => string | null
} = useReferralTracking()
Environment Variables
This layer reads no environment variables directly. All configuration is through app.config.ts.
Layer Architecture
| Path | Purpose |
|---|---|
nuxt.config.ts | Registers @nuxt/ui module and Tailwind CSS |
app.config.ts | All configurable options under xAffiliate namespace with TypeScript type augmentation |
app/composables/ | useAffiliate and useReferralTracking — all state and tracking logic |
app/components/X/AF/ | 10 auto-imported XAF-prefixed components built on Nuxt UI v4 |
app/pages/affiliate/ | Landing page and dashboard route |
app/types/ | TypeScript interfaces: Affiliate, Referral, AffiliateStats, Payout, CommissionTier, AffiliateResource, SharePlatform |
AI Context
package: "@xenterprises/nuxt-x-affiliate"
use-when: >
Adding an affiliate/referral program to a Nuxt 4 app. Provides the full
landing page (/affiliate), authenticated dashboard (/affiliate/dashboard),
10 XAF* components, and 2 composables. The consuming app fetches data
from its own API and populates useAffiliate() state via setters — the
layer handles all UI, referral link generation, and social sharing.
Call useReferralTracking().detectReferral() in a plugin or layout to
capture ?ref= params and persist them in a cookie. Configure branding
and form fields under xAffiliate in app.config.ts.
