File size: 2,986 Bytes
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * Affiliate URL validation utilities for marketplace links
 * Ensures affiliate tracking IDs are properly formatted
 */

export interface ValidationResult {
  isValid: boolean;
  error?: string;
}

/**
 * Validate Amazon Associate tag format
 * Expected format: tag=XXXX-20
 */
export function validateAmazonAffiliateUrl(url: string): ValidationResult {
  try {
    const urlObj = new URL(url);

    // Check if it's an Amazon domain
    if (!urlObj.hostname.includes('amazon.com')) {
      return { isValid: false, error: 'Not an Amazon URL' };
    }

    // Check for tag parameter
    const tag = urlObj.searchParams.get('tag');
    if (!tag) {
      return { isValid: false, error: 'Missing affiliate tag parameter' };
    }

    // Validate tag format (should end with -20)
    if (!tag.endsWith('-20')) {
      return { isValid: false, error: 'Invalid Amazon tag format (must end with -20)' };
    }

    return { isValid: true };
  } catch {
    return { isValid: false, error: 'Invalid URL format' };
  }
}

/**
 * Validate eBay Partner Network campaign ID
 * Expected format: rover.ebay.com with campid parameter
 */
export function validateEbayAffiliateUrl(url: string): ValidationResult {
  try {
    const urlObj = new URL(url);

    // Check if it's an eBay rover URL
    if (!urlObj.hostname.includes('rover.ebay.com')) {
      return { isValid: false, error: 'Not an eBay Partner Network URL' };
    }

    // Check for campid parameter
    const campid = urlObj.searchParams.get('campid');
    if (!campid) {
      return { isValid: false, error: 'Missing campaign ID parameter' };
    }

    // Validate campid is numeric
    if (!/^\d+$/.test(campid)) {
      return { isValid: false, error: 'Invalid campaign ID format' };
    }

    return { isValid: true };
  } catch {
    return { isValid: false, error: 'Invalid URL format' };
  }
}

/**
 * Validate Etsy affiliate parameters
 * Expected format: etsy.com with ref parameter
 */
export function validateEtsyAffiliateUrl(url: string): ValidationResult {
  try {
    const urlObj = new URL(url);

    // Check if it's an Etsy domain
    if (!urlObj.hostname.includes('etsy.com')) {
      return { isValid: false, error: 'Not an Etsy URL' };
    }

    // Check for ref parameter
    const ref = urlObj.searchParams.get('ref');
    if (!ref) {
      return { isValid: false, error: 'Missing affiliate ref parameter' };
    }

    return { isValid: true };
  } catch {
    return { isValid: false, error: 'Invalid URL format' };
  }
}

/**
 * Validate affiliate URL based on marketplace type
 */
export function validateAffiliateUrl(
  url: string,
  marketplace: 'amazon' | 'ebay' | 'etsy'
): ValidationResult {
  switch (marketplace) {
    case 'amazon':
      return validateAmazonAffiliateUrl(url);
    case 'ebay':
      return validateEbayAffiliateUrl(url);
    case 'etsy':
      return validateEtsyAffiliateUrl(url);
    default:
      return { isValid: false, error: 'Unknown marketplace type' };
  }
}