UNPKG

autotrader-connect-api

Version:

Production-ready TypeScript wrapper for Auto Trader UK Connect APIs

355 lines 19 kB
"use strict"; /** * AutoTrader Connect API - Main Entry Point * Production-ready TypeScript wrapper for Auto Trader UK Connect APIs * * @author Your Organization * @version 1.0.0 * @license MIT */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupProduction = exports.setupSandbox = exports.quickSetup = exports.initializeClient = exports.PACKAGE_NAME = exports.VERSION = exports.listAdvertisers = exports.getAdvertisers = exports.getTopRatedAdvertisers = exports.getNearbyAdvertisers = exports.getAdvertiserVehicleCount = exports.getAdvertiserStats = exports.updateAdvertiser = exports.getAllAdvertisers = exports.searchAdvertisers = exports.getAdvertiser = exports.getCurrentAdvertiser = exports.listFinance = exports.getFinance = exports.listCoDriver = exports.getCoDriver = exports.listCalls = exports.getCalls = exports.listDelivery = exports.getDelivery = exports.listPartExchange = exports.getPartExchange = exports.listMessages = exports.getMessages = exports.listDeals = exports.getDeals = exports.getMarketInsights = exports.getRelatedSearches = exports.trackSearchInteraction = exports.getSearchAnalytics = exports.getPopularSearches = exports.compareVehicles = exports.executeSavedSearch = exports.deleteSavedSearch = exports.updateSavedSearch = exports.getSavedSearches = exports.saveSearch = exports.getSearchSuggestions = exports.quickSearch = exports.searchVehiclesAdvanced = exports.getToken = exports.getAuthManager = exports.AuthManager = exports.getClient = exports.ApiClient = void 0; exports.healthCheck = exports.validateAdvertiserId = exports.createAdvertiserClient = void 0; // Core exports var client_1 = require("./client"); Object.defineProperty(exports, "ApiClient", { enumerable: true, get: function () { return client_1.ApiClient; } }); Object.defineProperty(exports, "getClient", { enumerable: true, get: function () { return client_1.getClient; } }); var auth_1 = require("./auth"); Object.defineProperty(exports, "AuthManager", { enumerable: true, get: function () { return auth_1.AuthManager; } }); Object.defineProperty(exports, "getAuthManager", { enumerable: true, get: function () { return auth_1.getAuthManager; } }); Object.defineProperty(exports, "getToken", { enumerable: true, get: function () { return auth_1.getToken; } }); // Import for internal use const client_2 = require("./client"); const auth_2 = require("./auth"); const advertisers_1 = require("./modules/advertisers"); // Vehicle-related exports __exportStar(require("./modules/vehicles"), exports); var search_1 = require("./modules/search"); Object.defineProperty(exports, "searchVehiclesAdvanced", { enumerable: true, get: function () { return search_1.searchVehicles; } }); Object.defineProperty(exports, "quickSearch", { enumerable: true, get: function () { return search_1.quickSearch; } }); Object.defineProperty(exports, "getSearchSuggestions", { enumerable: true, get: function () { return search_1.getSearchSuggestions; } }); Object.defineProperty(exports, "saveSearch", { enumerable: true, get: function () { return search_1.saveSearch; } }); Object.defineProperty(exports, "getSavedSearches", { enumerable: true, get: function () { return search_1.getSavedSearches; } }); Object.defineProperty(exports, "updateSavedSearch", { enumerable: true, get: function () { return search_1.updateSavedSearch; } }); Object.defineProperty(exports, "deleteSavedSearch", { enumerable: true, get: function () { return search_1.deleteSavedSearch; } }); Object.defineProperty(exports, "executeSavedSearch", { enumerable: true, get: function () { return search_1.executeSavedSearch; } }); Object.defineProperty(exports, "compareVehicles", { enumerable: true, get: function () { return search_1.compareVehicles; } }); Object.defineProperty(exports, "getPopularSearches", { enumerable: true, get: function () { return search_1.getPopularSearches; } }); Object.defineProperty(exports, "getSearchAnalytics", { enumerable: true, get: function () { return search_1.getSearchAnalytics; } }); Object.defineProperty(exports, "trackSearchInteraction", { enumerable: true, get: function () { return search_1.trackSearchInteraction; } }); Object.defineProperty(exports, "getRelatedSearches", { enumerable: true, get: function () { return search_1.getRelatedSearches; } }); Object.defineProperty(exports, "getMarketInsights", { enumerable: true, get: function () { return search_1.getMarketInsights; } }); __exportStar(require("./modules/stock"), exports); __exportStar(require("./modules/images"), exports); __exportStar(require("./modules/valuations"), exports); __exportStar(require("./modules/taxonomy"), exports); // Placeholder exports for other modules // These would be fully implemented in a production environment __exportStar(require("./modules/vehicleMetrics"), exports); __exportStar(require("./modules/futureValuations"), exports); __exportStar(require("./modules/historicValuations"), exports); // Export specific functions to avoid conflicts var deals_1 = require("./modules/deals"); Object.defineProperty(exports, "getDeals", { enumerable: true, get: function () { return deals_1.getResource; } }); Object.defineProperty(exports, "listDeals", { enumerable: true, get: function () { return deals_1.listResources; } }); var messages_1 = require("./modules/messages"); Object.defineProperty(exports, "getMessages", { enumerable: true, get: function () { return messages_1.getResource; } }); Object.defineProperty(exports, "listMessages", { enumerable: true, get: function () { return messages_1.listResources; } }); var partExchange_1 = require("./modules/partExchange"); Object.defineProperty(exports, "getPartExchange", { enumerable: true, get: function () { return partExchange_1.getResource; } }); Object.defineProperty(exports, "listPartExchange", { enumerable: true, get: function () { return partExchange_1.listResources; } }); var delivery_1 = require("./modules/delivery"); Object.defineProperty(exports, "getDelivery", { enumerable: true, get: function () { return delivery_1.getResource; } }); Object.defineProperty(exports, "listDelivery", { enumerable: true, get: function () { return delivery_1.listResources; } }); var calls_1 = require("./modules/calls"); Object.defineProperty(exports, "getCalls", { enumerable: true, get: function () { return calls_1.getResource; } }); Object.defineProperty(exports, "listCalls", { enumerable: true, get: function () { return calls_1.listResources; } }); var coDriver_1 = require("./modules/coDriver"); Object.defineProperty(exports, "getCoDriver", { enumerable: true, get: function () { return coDriver_1.getResource; } }); Object.defineProperty(exports, "listCoDriver", { enumerable: true, get: function () { return coDriver_1.listResources; } }); var finance_1 = require("./modules/finance"); Object.defineProperty(exports, "getFinance", { enumerable: true, get: function () { return finance_1.getResource; } }); Object.defineProperty(exports, "listFinance", { enumerable: true, get: function () { return finance_1.listResources; } }); // Advertiser management exports var advertisers_2 = require("./modules/advertisers"); Object.defineProperty(exports, "getCurrentAdvertiser", { enumerable: true, get: function () { return advertisers_2.getCurrentAdvertiser; } }); Object.defineProperty(exports, "getAdvertiser", { enumerable: true, get: function () { return advertisers_2.getAdvertiser; } }); Object.defineProperty(exports, "searchAdvertisers", { enumerable: true, get: function () { return advertisers_2.searchAdvertisers; } }); Object.defineProperty(exports, "getAllAdvertisers", { enumerable: true, get: function () { return advertisers_2.getAllAdvertisers; } }); Object.defineProperty(exports, "updateAdvertiser", { enumerable: true, get: function () { return advertisers_2.updateAdvertiser; } }); Object.defineProperty(exports, "getAdvertiserStats", { enumerable: true, get: function () { return advertisers_2.getAdvertiserStats; } }); Object.defineProperty(exports, "getAdvertiserVehicleCount", { enumerable: true, get: function () { return advertisers_2.getAdvertiserVehicleCount; } }); Object.defineProperty(exports, "getNearbyAdvertisers", { enumerable: true, get: function () { return advertisers_2.getNearbyAdvertisers; } }); Object.defineProperty(exports, "getTopRatedAdvertisers", { enumerable: true, get: function () { return advertisers_2.getTopRatedAdvertisers; } }); Object.defineProperty(exports, "getAdvertisers", { enumerable: true, get: function () { return advertisers_2.getResource; } }); Object.defineProperty(exports, "listAdvertisers", { enumerable: true, get: function () { return advertisers_2.listResources; } }); // Type exports __exportStar(require("./types/common"), exports); __exportStar(require("./types/auth"), exports); __exportStar(require("./types/vehicle"), exports); __exportStar(require("./types/stock"), exports); __exportStar(require("./types/search"), exports); // Default client instance const client_3 = __importDefault(require("./client")); exports.default = client_3.default; /** * Package version */ exports.VERSION = '1.0.0'; /** * Package name */ exports.PACKAGE_NAME = 'autotrader-connect-api'; /** * Initialize the AutoTrader Connect API client with configuration * @param config API client configuration * @returns Configured API client instance */ function initializeClient(config) { const clientConfig = { apiKey: config.apiKey, apiSecret: config.apiSecret, baseURL: config.baseURL || 'https://api.autotrader.co.uk', ...(config.timeout && { timeout: config.timeout }), ...(config.maxRetries && { maxRetries: config.maxRetries }), ...(config.rateLimitRequests && { rateLimitRequests: config.rateLimitRequests }), ...(config.rateLimitWindow && { rateLimitWindow: config.rateLimitWindow }), ...(config.debug !== undefined && { debug: config.debug }), }; return (0, client_2.getClient)(clientConfig); } exports.initializeClient = initializeClient; /** * Quick setup with environment variables for rapid development * Automatically detects sandbox vs production based on NODE_ENV * @param options Optional configuration overrides * @returns Configured API client instance */ function quickSetup(options) { // Determine environment let useSandbox = process.env['NODE_ENV'] === 'development' || process.env['NODE_ENV'] === 'test'; if (options?.forceSandbox) { useSandbox = true; } else if (options?.forceProduction) { useSandbox = false; } else if (process.env['AT_USE_SANDBOX']) { useSandbox = process.env['AT_USE_SANDBOX'] === 'true'; } // Get appropriate credentials const apiKey = useSandbox ? process.env['AT_SANDBOX_API_KEY'] : process.env['AT_API_KEY']; const apiSecret = useSandbox ? process.env['AT_SANDBOX_API_SECRET'] : process.env['AT_API_SECRET']; const baseURL = useSandbox ? (process.env['AT_SANDBOX_BASE_URL'] || 'https://sandbox-api.autotrader.co.uk') : (process.env['AT_BASE_URL'] || 'https://api.autotrader.co.uk'); if (!apiKey || !apiSecret) { const envPrefix = useSandbox ? 'AT_SANDBOX_' : 'AT_'; throw new Error(`Missing API credentials for ${useSandbox ? 'sandbox' : 'production'} environment. ` + `Please set ${envPrefix}API_KEY and ${envPrefix}API_SECRET environment variables.`); } const clientConfig = { apiKey, apiSecret, baseURL, useSandbox, ...(options?.timeout && { timeout: options.timeout }), ...(options?.debug !== undefined && { debug: options.debug }), }; return (0, client_2.getClient)(clientConfig); } exports.quickSetup = quickSetup; /** * Setup client specifically for sandbox/testing environment * @param credentials Sandbox API credentials * @param options Optional configuration overrides * @returns Configured API client for sandbox */ function setupSandbox(credentials, options) { let apiKey; let apiSecret; let baseURL; if (credentials) { apiKey = credentials.apiKey; apiSecret = credentials.apiSecret; baseURL = credentials.baseURL || 'https://sandbox-api.autotrader.co.uk'; } else { // Use environment variables apiKey = process.env['AT_SANDBOX_API_KEY'] || ''; apiSecret = process.env['AT_SANDBOX_API_SECRET'] || ''; baseURL = process.env['AT_SANDBOX_BASE_URL'] || 'https://sandbox-api.autotrader.co.uk'; if (!apiKey || !apiSecret) { throw new Error('Sandbox credentials required. Provide credentials or set AT_SANDBOX_API_KEY and AT_SANDBOX_API_SECRET environment variables.'); } } const clientConfig = { apiKey, apiSecret, baseURL, useSandbox: true, debug: options?.debug ?? true, // Default to debug mode in sandbox ...(options?.timeout && { timeout: options.timeout }), }; return (0, client_2.getClient)(clientConfig); } exports.setupSandbox = setupSandbox; /** * Setup client specifically for production environment * @param credentials Production API credentials * @param options Optional configuration overrides * @returns Configured API client for production */ function setupProduction(credentials, options) { let apiKey; let apiSecret; let baseURL; if (credentials) { apiKey = credentials.apiKey; apiSecret = credentials.apiSecret; baseURL = credentials.baseURL || 'https://api.autotrader.co.uk'; } else { // Use environment variables apiKey = process.env['AT_API_KEY'] || ''; apiSecret = process.env['AT_API_SECRET'] || ''; baseURL = process.env['AT_BASE_URL'] || 'https://api.autotrader.co.uk'; if (!apiKey || !apiSecret) { throw new Error('Production credentials required. Provide credentials or set AT_API_KEY and AT_API_SECRET environment variables.'); } } const clientConfig = { apiKey, apiSecret, baseURL, useSandbox: false, debug: options?.debug ?? false, // Default to no debug in production ...(options?.timeout && { timeout: options.timeout }), }; return (0, client_2.getClient)(clientConfig); } exports.setupProduction = setupProduction; /** * Create a client scoped to a specific advertiser * @param advertiserId Advertiser ID to scope all operations to * @param options Optional configuration overrides * @returns Configured API client with advertiser context */ function createAdvertiserClient(advertiserId, options) { const baseClient = quickSetup(options); // Create a wrapper object that injects advertiserId into all requests const advertiserClient = { // Include all other client methods and properties first ...baseClient, // Override HTTP methods to inject advertiserId get: async (url, requestOptions) => { const params = { ...requestOptions?.params, advertiserId }; return baseClient.get(url, { ...requestOptions, params }); }, post: async (url, data, requestOptions) => { const enrichedData = data ? { ...data, advertiserId } : { advertiserId }; return baseClient.post(url, enrichedData, requestOptions); }, put: async (url, data, requestOptions) => { const enrichedData = data ? { ...data, advertiserId } : { advertiserId }; return baseClient.put(url, enrichedData, requestOptions); }, patch: async (url, data, requestOptions) => { const enrichedData = data ? { ...data, advertiserId } : { advertiserId }; return baseClient.patch(url, enrichedData, requestOptions); }, delete: async (url, requestOptions) => { const params = { ...requestOptions?.params, advertiserId }; return baseClient.delete(url, { ...requestOptions, params }); }, // Add advertiser context advertiserId, }; return advertiserClient; } exports.createAdvertiserClient = createAdvertiserClient; /** * Validate advertiser ID format and existence * @param advertiserId Advertiser ID to validate * @returns Promise resolving to validation result */ async function validateAdvertiserId(advertiserId) { try { if (!Number.isInteger(advertiserId) || advertiserId <= 0) { return { valid: false, error: 'Advertiser ID must be a positive integer' }; } const advertiser = await (0, advertisers_1.getAdvertiser)(advertiserId); return { valid: true, exists: true, advertiserName: advertiser.name }; } catch (error) { if (error.response?.status === 404) { return { valid: true, exists: false, error: 'Advertiser not found' }; } return { valid: false, error: error.message || 'Unknown error validating advertiser ID' }; } } exports.validateAdvertiserId = validateAdvertiserId; /** * Health check function to verify API connectivity */ async function healthCheck() { const startTime = Date.now(); try { const client = (0, client_2.getClient)(); // Attempt to get a token to verify auth await (0, auth_2.getToken)(); // Simple API call to verify connectivity // This would typically be a lightweight endpoint like /health or /status await client.get('/health'); const latency = Date.now() - startTime; return { status: 'healthy', timestamp: new Date().toISOString(), version: exports.VERSION, auth: true, latency, }; } catch (error) { return { status: 'unhealthy', timestamp: new Date().toISOString(), version: exports.VERSION, auth: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } exports.healthCheck = healthCheck; //# sourceMappingURL=index.js.map