credix
Version:
Official SDK for Credix Credit Management System
95 lines • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredixClient = void 0;
exports.createCredix = createCredix;
const config_js_1 = require("./config.js");
const index_js_1 = require("./errors/index.js");
const allocations_js_1 = require("./resources/allocations.js");
const discounts_js_1 = require("./resources/discounts.js");
const coupons_js_1 = require("./resources/coupons.js");
const meters_js_1 = require("./resources/meters.js");
const referrals_js_1 = require("./resources/referrals.js");
const users_js_1 = require("./resources/users.js");
const auth_js_1 = require("./utils/auth.js");
const http_client_js_1 = require("./utils/http-client.js");
// Idempotency support removed to simplify SDK usage
/**
* Main Client for Credix Credit Management System
*/
class CredixClient {
constructor(config) {
// Validate required config
if (!config.apiKey) {
throw new index_js_1.ValidationError('apiKey is required in SDK configuration', 'apiKey');
}
if (!config.secretKey) {
throw new index_js_1.ValidationError('secretKey is required in SDK configuration', 'secretKey');
}
this.config = config;
// Initialize auth manager (validation is lenient; server enforces correctness)
this.authManager = new auth_js_1.AuthManager({
apiKey: config.apiKey,
secretKey: config.secretKey,
});
// Determine base URL (safely allow local override only in debug & non-production)
let resolvedBaseUrl = config_js_1.DEFAULT_CONFIG.baseUrl;
if (config.baseUrl) {
const isProd = typeof process !== 'undefined' &&
typeof process.env !== 'undefined' &&
process.env.NODE_ENV === 'production';
if (isProd) {
throw new index_js_1.ValidationError('baseUrl override is not allowed in production', 'baseUrl');
}
if (!config.debug) {
throw new index_js_1.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 index_js_1.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 || config_js_1.DEFAULT_CONFIG.timeout,
maxRetries: config.maxRetries || config_js_1.DEFAULT_CONFIG.maxRetries,
debug: config.debug || config_js_1.DEFAULT_CONFIG.debug,
headers: config.headers,
authManager: this.authManager,
// no idempotency in simplified client
};
this.httpClient = new http_client_js_1.HttpClient(internalConfig);
// Initialize resources
this.users = new users_js_1.Users(this.httpClient);
this.meters = new meters_js_1.Meters(this.httpClient);
this.allocations = new allocations_js_1.Allocations(this.httpClient);
this.coupons = new coupons_js_1.Coupons(this.httpClient);
this.discounts = new discounts_js_1.Discounts(this.httpClient);
this.referrals = new referrals_js_1.Referrals(this.httpClient);
}
/**
* Gets the current configuration
*/
getConfig() {
return { ...this.config };
}
/**
* Updates the debug mode
*/
setDebugMode(enabled) {
this.config.debug = enabled;
}
}
exports.CredixClient = CredixClient;
/**
* Factory function to create a Credix client
*/
function createCredix(config) {
return new CredixClient(config);
}
//# sourceMappingURL=client.js.map