@jokoor/sdk
Version:
Jokoor SMS API SDK for JavaScript/TypeScript
104 lines • 3.64 kB
JavaScript
"use strict";
/**
* Jokoor SDK Client
* Main client class for interacting with the Jokoor API
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Jokoor = void 0;
exports.createClient = createClient;
const configuration_1 = require("./generated/configuration");
const resources_1 = require("./resources");
const errors_1 = require("./errors");
const DEFAULT_BASE_URL = 'https://api.jokoor.com';
const DEFAULT_TIMEOUT = 30000; // 30 seconds
const DEFAULT_MAX_RETRIES = 3;
class Jokoor {
constructor(apiKey, config) {
if (!apiKey) {
throw new errors_1.JokoorAuthenticationError('API key is required');
}
this.apiKey = apiKey;
this.debug = config.debug || false;
// Validate and parse API key
const { mode, keyType } = this.parseAPIKey(this.apiKey);
this.mode = mode;
this.keyType = keyType;
// Create configuration for generated API with enhanced options
this.configuration = new configuration_1.Configuration({
apiKey: `Bearer ${apiKey}`,
basePath: config.baseURL || DEFAULT_BASE_URL,
baseOptions: {
timeout: config.timeout || DEFAULT_TIMEOUT,
maxRetries: config.maxRetries || DEFAULT_MAX_RETRIES,
headers: {
'User-Agent': '@jokoor/sdk/1.0.0',
'X-SDK-Version': '1.0.0',
'X-SDK-Language': 'typescript'
}
}
});
// Initialize user-friendly resource wrappers
this.sms = new resources_1.SMS(this.configuration);
this.campaigns = new resources_1.Campaigns(this.configuration);
this.templates = new resources_1.Templates(this.configuration);
this.contacts = new resources_1.Contacts(this.configuration);
this.contactGroups = new resources_1.ContactGroups(this.configuration);
// Setup debug logging if enabled
if (this.debug) {
this.setupDebugLogging();
}
}
/**
* Parse API key to extract mode and type
*/
parseAPIKey(apiKey) {
const patterns = {
'sk_test_': { mode: 'test', keyType: 'secret' },
'sk_live_': { mode: 'live', keyType: 'secret' },
'pk_test_': { mode: 'test', keyType: 'publishable' },
'pk_live_': { mode: 'live', keyType: 'publishable' },
'svc_test_': { mode: 'test', keyType: 'service' },
'svc_live_': { mode: 'live', keyType: 'service' }
};
for (const [prefix, config] of Object.entries(patterns)) {
if (apiKey.startsWith(prefix)) {
return config;
}
}
throw new errors_1.JokoorAuthenticationError('Invalid API key format');
}
/**
* Setup debug logging for API calls
*/
setupDebugLogging() {
if (this.debug) {
// The generated API classes handle their own axios instances
// We can add debug logging through axios interceptors in the configuration
console.log('Jokoor SDK Debug mode enabled');
}
}
/**
* Get current mode (test or live)
*/
getMode() {
return this.mode;
}
/**
* Check if using test mode
*/
isTestMode() {
return this.mode === 'test';
}
/**
* Get API key type
*/
getKeyType() {
return this.keyType;
}
}
exports.Jokoor = Jokoor;
// Export a factory function for convenience
function createClient(apiKey, config) {
return new Jokoor(apiKey, config);
}
//# sourceMappingURL=client.js.map