/**
* WC Licensed Product API Client for JavaScript
*
* A JavaScript client for interacting with the WC Licensed Product REST API.
* Works in both browser and Node.js environments.
*
* Browser usage:
*
* const client = new WcLicensedProductClient('https://your-site.com');
* const result = await client.validate('XXXX-XXXX-XXXX-XXXX', 'example.com');
*
* Node.js usage:
* const WcLicensedProductClient = require('./javascript-client');
* const client = new WcLicensedProductClient('https://your-site.com');
*/
class WcLicensedProductError extends Error {
constructor(message, errorCode = null, httpCode = null) {
super(message);
this.name = 'WcLicensedProductError';
this.errorCode = errorCode;
this.httpCode = httpCode;
}
}
class RateLimitError extends WcLicensedProductError {
constructor(retryAfter) {
super(`Rate limit exceeded. Retry after ${retryAfter} seconds.`);
this.name = 'RateLimitError';
this.retryAfter = retryAfter;
}
}
class WcLicensedProductClient {
/**
* Initialize the client
* @param {string} siteUrl - Your WordPress site URL (e.g., 'https://example.com')
* @param {Object} options - Optional configuration
* @param {number} options.timeout - Request timeout in milliseconds (default: 30000)
*/
constructor(siteUrl, options = {}) {
this.baseUrl = siteUrl.replace(/\/$/, '') + '/wp-json/wc-licensed-product/v1';
this.timeout = options.timeout || 30000;
}
/**
* Validate a license key for a specific domain
* @param {string} licenseKey - The license key to validate
* @param {string} domain - The domain to validate against
* @returns {Promise