UNPKG

@magiceden/magiceden-sdk

Version:

A TypeScript SDK for interacting with Magic Eden's API across multiple chains.

120 lines (119 loc) 4.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiManager = void 0; const axios_1 = __importDefault(require("axios")); const errors_1 = require("../../errors"); const helpers_1 = require("../../helpers"); const https_1 = __importDefault(require("https")); const sdk_1 = require("../../constants/sdk"); /** * Manages API interactions with the Magic Eden API */ class ApiManager { constructor(baseURL, options) { // Create custom https agent that can disable certificate validation const httpsAgent = new https_1.default.Agent({ rejectUnauthorized: options.rejectUnauthorized ?? true, }); this.client = axios_1.default.create({ baseURL, headers: { 'Content-Type': 'application/json', 'x-sdk-name': sdk_1.USER_AGENT, ...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }), ...options.headers, }, timeout: options.timeout || 30000, httpsAgent, }); // Add response interceptor for error handling this.client.interceptors.response.use((response) => response, (error) => this.handleRequestError(error)); } /** * Makes a GET request */ get(url, params, config) { return helpers_1.RetryablePromise.from(() => this.client.get(url, { ...config, params }).then((response) => response.data)); } /** * Makes a POST request */ post(url, data, config) { return helpers_1.RetryablePromise.from(() => this.client.post(url, data, config).then((response) => response.data)); } /** * Makes a PUT request */ put(url, data, config) { return helpers_1.RetryablePromise.from(() => this.client.put(url, data, config).then((response) => response.data)); } /** * Makes a DELETE request */ delete(url, config) { return helpers_1.RetryablePromise.from(() => this.client.delete(url, config).then((response) => response.data)); } /** * Handles request errors and converts them to appropriate error types */ handleRequestError(error) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx const { status, statusText } = error.response; const errorData = error.response.data; // Extract error message using a safer approach const errorMessage = this.getErrorMessage(errorData); if (status === 401 || status === 403) { throw new errors_1.AuthenticationError(errorMessage || 'Authentication failed', status); } if (status === 429) { const retryAfter = error.response.headers['retry-after'] ? parseInt(error.response.headers['retry-after'], 10) : undefined; throw new errors_1.RateLimitError(errorMessage || 'Rate limit exceeded', retryAfter); } // Server errors (5xx) - generally retryable if (status >= 500 && status < 600) { throw new errors_1.ApiError(errorMessage || `Server error: ${status} ${statusText}`, status); } // Client errors (4xx) - generally not retryable (except those handled above) if (status >= 400 && status < 500) { throw new errors_1.ApiError(errorMessage || `Client error: ${status} ${statusText}`, status); } throw new errors_1.ApiError(errorMessage || `Unexpected status: ${status} ${statusText}`, status); } else if (error.request) { // The request was made but no response was received throw new errors_1.NetworkError(0, 'No response received', error.request); } else { // Something happened in setting up the request that triggered an Error throw new errors_1.ApiError(error.message || 'Request failed', 0); } } /** * Safely extracts error message from response data */ getErrorMessage(data) { // Try different common error message fields if (typeof data?.message === 'string') { return data.message; } if (typeof data?.error === 'string') { return data.error; } // For nested error objects if (data?.error && typeof data.error === 'object' && data.error !== null) { const errorObj = data.error; if (typeof errorObj.message === 'string') { return errorObj.message; } } return undefined; } } exports.ApiManager = ApiManager;