@aryze/reforge
Version:
TypeScript SDK for Aryze Reforge cross-chain bridge protocol
216 lines • 7.2 kB
JavaScript
/**
* Simplified Reforge SDK - Only core functionality
*/
/**
* Simple HTTP client for making API requests
*/
class SimpleHttpClient {
constructor(baseUrl, apiKey, timeout = 30000, debug = false, includeUserAgent = true, minimalMode = false) {
Object.defineProperty(this, "baseUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "apiKey", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "timeout", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "debug", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "includeUserAgent", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "minimalMode", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.baseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
this.apiKey = apiKey;
this.timeout = timeout;
this.debug = debug;
this.includeUserAgent = includeUserAgent;
this.minimalMode = minimalMode;
}
log(message, data) {
if (this.debug) {
console.log(`[REFORGE SDK] ${message}`, data || '');
}
}
async post(path, data) {
const url = `${this.baseUrl}${path}`;
this.log('Making request', { method: 'POST', url, hasData: !!data });
if (this.minimalMode) {
// Minimal mode: exactly match the user's working code
try {
const headers = {
'X-API-Key': this.apiKey,
'Content-Type': 'application/json',
};
const fetchOptions = {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
};
this.log('Minimal mode request details', {
url,
headers: Object.keys(headers),
hasBody: !!data,
bodyLength: JSON.stringify(data).length,
});
const response = await fetch(url, fetchOptions);
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText || response.statusText || 'Request failed');
}
const result = await response.json();
this.log('Request successful', { status: response.status });
return result;
}
catch (error) {
this.log('Request failed', {
error: error instanceof Error ? error.message : 'Unknown error',
});
throw error;
}
}
// Standard mode: with timeout and abort controller
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
try {
const headers = {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
};
if (this.includeUserAgent) {
headers['User-Agent'] = 'reforge-sdk/1.0.0';
}
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText || response.statusText || 'Request failed');
}
const result = await response.json();
this.log('Request successful', { status: response.status });
return result;
}
catch (error) {
clearTimeout(timeoutId);
this.log('Request failed', {
error: error instanceof Error ? error.message : 'Unknown error',
});
throw error;
}
}
}
/**
* Global SDK configuration
*/
let sdkConfig = null;
let httpClient = null;
/**
* Create AWS API Gateway compatible configuration
* Automatically disables headers that commonly cause issues with AWS API Gateway
*/
export function createAWSConfig(config) {
return {
...config,
includeUserAgent: false, // Disable User-Agent for AWS compatibility
minimalMode: true, // Use minimal fetch options for maximum compatibility
debug: false, // Disable debug logging
};
}
/**
* Configure the SDK with API credentials
*/
export function configure(config) {
if (!config) {
throw new Error('Configuration object is required');
}
if (!config.apiKey || typeof config.apiKey !== 'string') {
throw new Error('apiKey is required and must be a string');
}
if (!config.apiUrl || typeof config.apiUrl !== 'string') {
throw new Error('apiUrl is required and must be a string');
}
// Validate URL format
try {
new URL(config.apiUrl);
}
catch {
throw new Error('apiUrl must be a valid URL');
}
sdkConfig = config;
httpClient = new SimpleHttpClient(config.apiUrl, config.apiKey, config.timeout, config.debug, config.includeUserAgent, config.minimalMode);
if (config.debug) {
console.log('[REFORGE SDK] SDK configured successfully');
}
}
/**
* Ensure SDK is configured before making API calls
*/
function ensureConfigured() {
if (!sdkConfig || !httpClient) {
throw new Error('SDK not configured. Please call configure({apiKey: "...", apiUrl: "..."}) first.');
}
}
/**
* Initiate a reforge operation
*/
export async function initiateReforge(data) {
ensureConfigured();
try {
const result = await httpClient.post('/initiateReforge', data);
return { data: result || { success: true } };
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Failed to initiate reforge';
return { error: errorMessage };
}
}
/**
* Finish a reforge operation
*/
export async function finishReforge(data) {
ensureConfigured();
try {
const result = await httpClient.post('/finishReforge', data);
return { data: result || { success: true } };
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Failed to finish reforge';
return { error: errorMessage };
}
}
// Default export object for convenience
const reforge = {
configure,
createAWSConfig,
initiateReforge,
finishReforge,
};
export default reforge;
//# sourceMappingURL=reforge.js.map