@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
47 lines (46 loc) • 1.81 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestApiService = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const qs_1 = __importDefault(require("qs"));
const constants_1 = require("./constants");
class RestApiService {
async makeRequest(endpoint, method = 'GET', queryParams = {}, body = null) {
const queryString = qs_1.default.stringify(this.buildQueryParams(queryParams), { indices: false });
const url = new URL(`${constants_1.BACKEND_API_ENDPOINT}/${endpoint}?${queryString}`);
const requestOptions = {
method,
headers: {
'Content-Type': 'application/json',
},
body: body ? JSON.stringify(body) : null,
};
try {
const response = await (0, cross_fetch_1.default)(url.toString(), requestOptions);
const data = await response.json();
if (!response.ok) {
if (response.status === 403) {
throw new Error('Invalid API Key');
}
throw new Error(data.message || data.error || 'Request failed');
}
return data;
}
catch (error) {
throw new Error(error.message || 'Something went wrong');
}
}
buildQueryParams(params) {
const queryParams = {};
for (const key in params) {
if (params.hasOwnProperty(key) && params[key] !== undefined && params[key] !== null) {
queryParams[key] = params[key];
}
}
return queryParams;
}
}
exports.RestApiService = RestApiService;
;