@dima_aryze/reforge
Version:
TypeScript/JavaScript SDK for Reforge - Cross-chain token operations
243 lines • 7.54 kB
JavaScript
;
/**
* Main Reforge SDK class providing methods for reforge operations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReforgeSDK = void 0;
const client_1 = require("./client");
const errors_1 = require("./errors");
const utils_1 = require("./utils");
/**
* Main Reforge SDK class providing methods for reforge operations
*/
class ReforgeSDK {
constructor(config) {
Object.defineProperty(this, "client", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "isConfigured", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: (0, utils_1.createLogger)('warn')
});
if (config) {
this.configure(config);
}
}
/**
* Configure the SDK with API credentials
*/
configure(config) {
this.validateConfig(config);
try {
this.client = new client_1.ReforgeClient({
apiKey: config.apiKey,
baseUrl: config.apiUrl,
timeout: config.timeout || 30000,
debug: config.debug || false,
});
this.isConfigured = true;
this.logger.info('Reforge SDK configured successfully');
}
catch (error) {
throw new errors_1.ReforgeConfigurationError('Failed to configure SDK', undefined, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Validate SDK configuration
*/
validateConfig(config) {
if (!config) {
throw new errors_1.ReforgeConfigurationError('Configuration object is required');
}
if (!config.apiKey || typeof config.apiKey !== 'string') {
throw errors_1.ReforgeConfigurationError.missing('apiKey');
}
if (!config.apiUrl || typeof config.apiUrl !== 'string') {
throw errors_1.ReforgeConfigurationError.missing('apiUrl');
}
// Validate URL format
try {
new URL(config.apiUrl);
}
catch {
throw new errors_1.ReforgeConfigurationError('apiUrl must be a valid URL', 'apiUrl');
}
}
/**
* Check if SDK is properly configured
*/
ensureConfigured() {
if (!this.isConfigured || !this.client) {
throw new errors_1.ReforgeConfigurationError('SDK not configured. Please call reforge.config({apiKey: "...", apiUrl: "..."}) first.');
}
}
/**
* Initiate a reforge operation
* @param data - Reforge initiation data
* @returns Promise resolving to action response
*/
async initiateReforge(data) {
this.ensureConfigured();
try {
const response = await this.client.post('/initiateReforge', data);
this.logger.info('Reforge initiation successful');
return { data: response.data || { success: true } };
}
catch (error) {
return this.handleOperationError(error, 'initiate', data);
}
}
/**
* Finish a reforge operation
* @param data - Reforge completion data
* @returns Promise resolving to action response
*/
async finishReforge(data) {
this.ensureConfigured();
try {
const response = await this.client.post('/finishReforge', data);
this.logger.info('Reforge completion successful');
return { data: response.data || { success: true } };
}
catch (error) {
return this.handleOperationError(error, 'finish', data);
}
}
/**
* Handle operation errors with proper error transformation
*/
handleOperationError(error, operation, data) {
let errorMessage;
if (error instanceof errors_1.ReforgeApiError) {
errorMessage = error.details?.message || error.message || `Failed to ${operation} reforge`;
}
else if (error instanceof errors_1.ReforgeNetworkError) {
errorMessage = 'Network connection failed';
}
else if (error instanceof errors_1.ReforgeError) {
errorMessage = error.message;
}
else {
errorMessage = error instanceof Error ? error.message : 'An unexpected error occurred';
// Create operation error for logging context
errors_1.ReforgeOperationError[operation === 'initiate' ? 'initiateFailed' : 'finishFailed'](errorMessage, data);
}
this.logger.error(`Reforge ${operation} failed`, {
error: errorMessage,
operation,
hasData: !!data,
});
return { error: errorMessage };
}
/**
* Get the underlying HTTP client for advanced usage
* @returns The HTTP client instance
*/
getClient() {
this.ensureConfigured();
return this.client;
}
/**
* Update the API key
* @param apiKey - New API key
*/
setApiKey(apiKey) {
this.ensureConfigured();
this.client.setApiKey(apiKey);
}
/**
* Get the current API key
* @returns Current API key or undefined if not set
*/
getApiKey() {
this.ensureConfigured();
return this.client.getApiKey();
}
/**
* Check if the SDK is configured
* @returns True if configured, false otherwise
*/
isReady() {
return this.isConfigured && this.client !== null;
}
/**
* Test the connection to the API
* @returns Promise resolving to connection status
*/
async testConnection() {
this.ensureConfigured();
try {
const startTime = Date.now();
await this.client.health();
const latency = Date.now() - startTime;
return { connected: true, latency };
}
catch (error) {
this.logger.warn('Connection test failed', { error });
return { connected: false };
}
}
}
exports.ReforgeSDK = ReforgeSDK;
// Create a default instance that can be imported and used directly
let defaultInstance = null;
/**
* Get or create the default Reforge SDK instance
*/
function getDefaultInstance() {
if (!defaultInstance) {
defaultInstance = new ReforgeSDK();
}
return defaultInstance;
}
/**
* Default export object with reforge methods
*/
const reforge = {
/**
* Initiate a reforge operation using the default instance
*/
initiateReforge: (data) => {
return getDefaultInstance().initiateReforge(data);
},
/**
* Finish a reforge operation using the default instance
*/
finishReforge: (data) => {
return getDefaultInstance().finishReforge(data);
},
/**
* Configure the default instance
*/
config: (config) => {
if (!defaultInstance) {
defaultInstance = new ReforgeSDK();
}
defaultInstance.configure(config);
},
/**
* Create a new Reforge SDK instance
*/
create: (config) => {
return new ReforgeSDK(config);
},
/**
* Get access to the default SDK instance
*/
getInstance: () => {
return getDefaultInstance();
},
};
exports.default = reforge;
//# sourceMappingURL=reforge.js.map