UNPKG

aggregator-sdk-test

Version:

SDK to connect with TechnoFex Aggregator Platform

74 lines (73 loc) 2.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AggregatorSdk = void 0; // src/aggregator-sdk.ts const axios_1 = __importDefault(require("axios")); class AggregatorSdk { constructor(config) { this.validated = false; if (!config.clientId || !config.apiKey || !config.sdkVersion) { throw new Error('Missing required SDK config: clientId, apiKey, sdkVersion'); } this.http = axios_1.default.create({ baseURL: AggregatorSdk.API_BASE_URL, headers: { 'x-client-id': config.clientId, 'x-api-key': config.apiKey, 'x-sdk-version': config.sdkVersion, }, }); } // this is best approach Use explicit .validate() or a static createAndValidate() method for clean async control, safety, and maintainability. // (Supported cleanly, Cleaner with try/catch, Easy to retry, More controllable, Clear developer intent) /** * Validate the client credentials */ async validate() { var _a; try { const response = await this.http.post('/clients/sdk/validate'); const { valid, client } = response.data; if (valid) { this.validated = true; this.licenseInfo = client; console.log('[Aggregator SDK] Validation successful'); return true; } console.warn('[Aggregator SDK] Invalid credentials'); return false; } catch (error) { const message = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || error.message || '[Aggregator SDK] Unknown validation error'; console.error('[Aggregator SDK] Validation failed:', message); return false; } } isValidated() { return this.validated; } getLicense() { return this.licenseInfo; } /** * Factory helper with automatic validation */ static async createAndValidate(config) { const sdk = new AggregatorSdk(config); const isValid = await sdk.validate(); return isValid ? sdk : null; } // Example SDK call async getMarketplaceItems() { if (!this.validated) { throw new Error('SDK not validated. Call .validate() first.'); } const res = await this.http.get('/marketplace/items'); return res.data; } } exports.AggregatorSdk = AggregatorSdk; AggregatorSdk.API_BASE_URL = 'http://localhost:3000/api';