UNPKG

credix

Version:

Official SDK for Credix Credit Management System

90 lines 3.73 kB
import { DEFAULT_CONFIG } from './config.js'; import { ValidationError } from './errors/index.js'; import { Allocations } from './resources/allocations.js'; import { Discounts } from './resources/discounts.js'; import { Coupons } from './resources/coupons.js'; import { Meters } from './resources/meters.js'; import { Referrals } from './resources/referrals.js'; import { Users } from './resources/users.js'; import { AuthManager } from './utils/auth.js'; import { HttpClient } from './utils/http-client.js'; // Idempotency support removed to simplify SDK usage /** * Main Client for Credix Credit Management System */ export class CredixClient { constructor(config) { // Validate required config if (!config.apiKey) { throw new ValidationError('apiKey is required in SDK configuration', 'apiKey'); } if (!config.secretKey) { throw new ValidationError('secretKey is required in SDK configuration', 'secretKey'); } this.config = config; // Initialize auth manager (validation is lenient; server enforces correctness) this.authManager = new AuthManager({ apiKey: config.apiKey, secretKey: config.secretKey, }); // Determine base URL (safely allow local override only in debug & non-production) let resolvedBaseUrl = DEFAULT_CONFIG.baseUrl; if (config.baseUrl) { const isProd = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'production'; if (isProd) { throw new ValidationError('baseUrl override is not allowed in production', 'baseUrl'); } if (!config.debug) { throw new ValidationError('baseUrl override requires debug: true', 'baseUrl'); } // Allow only localhost or 127.0.0.1 with optional port and path const allowedLocal = /^(https?:\/\/)(localhost|127\.0\.0\.1)(:\d+)?(\/.*)?$/i; if (!allowedLocal.test(config.baseUrl)) { throw new ValidationError('baseUrl override must be localhost/127.0.0.1', 'baseUrl'); } resolvedBaseUrl = config.baseUrl; } // Create HTTP client with merged config const internalConfig = { apiKey: config.apiKey, secretKey: config.secretKey, // baseUrlは既定を使用。明示的な安全条件を満たす場合のみローカル上書き可 baseUrl: resolvedBaseUrl, timeout: config.timeout || DEFAULT_CONFIG.timeout, maxRetries: config.maxRetries || DEFAULT_CONFIG.maxRetries, debug: config.debug || DEFAULT_CONFIG.debug, headers: config.headers, authManager: this.authManager, // no idempotency in simplified client }; this.httpClient = new HttpClient(internalConfig); // Initialize resources this.users = new Users(this.httpClient); this.meters = new Meters(this.httpClient); this.allocations = new Allocations(this.httpClient); this.coupons = new Coupons(this.httpClient); this.discounts = new Discounts(this.httpClient); this.referrals = new Referrals(this.httpClient); } /** * Gets the current configuration */ getConfig() { return { ...this.config }; } /** * Updates the debug mode */ setDebugMode(enabled) { this.config.debug = enabled; } } /** * Factory function to create a Credix client */ export function createCredix(config) { return new CredixClient(config); } //# sourceMappingURL=client.js.map