whales-pre-market-aptos-sdk
Version:
TypeScript SDK for Whales Pre-Market on Aptos blockchain
119 lines (118 loc) • 3.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLClient = void 0;
const axios_1 = __importDefault(require("axios"));
class GraphQLClient {
constructor(config) {
this.config = config;
const axiosConfig = {
baseURL: config.apiUrl,
headers: {
'Content-Type': 'application/json',
...config.headers,
},
};
// Add authorization header if provided
if (config.authorizationKey) {
axiosConfig.headers = {
...axiosConfig.headers,
// 'Authorization': "Bearer " + config.authorizationKey,
'x-hasura-admin-secret': config.authorizationKey,
};
}
this.client = axios_1.default.create(axiosConfig);
}
/**
* Execute a GraphQL query
*/
async query(query, variables) {
try {
const response = await this.client.post('', {
query,
variables,
});
return response.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
console.log(error);
throw new Error(`GraphQL request failed: ${error.response?.data?.message || error.message}`);
}
throw error;
}
}
/**
* Get events with pagination
*/
async getEvents(version = "0", limit = 100) {
const whereClause = {};
if (version !== "0") {
whereClause.version = { _gt: version };
}
const query = `
query GetEvents($where: event_table_bool_exp, $limit: Int!) {
event_table(
where: $where,
limit: $limit
) {
block_height
data
epoch
event_name
timestamp
version
}
}
`;
const variables = { limit };
if (Object.keys(whereClause).length > 0) {
variables.where = whereClause;
}
const response = await this.query(query, variables);
if (response.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(response.errors)}`);
}
return response.data.event_table;
}
/**
* Get events with custom query
*/
async getEventsWithCustomQuery(query, variables) {
const response = await this.query(query, variables);
if (response.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(response.errors)}`);
}
return response.data;
}
/**
* Update the configuration
*/
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
// Recreate axios instance with new config
const axiosConfig = {
baseURL: this.config.apiUrl,
headers: {
'Content-Type': 'application/json',
...this.config.headers,
},
};
if (this.config.authorizationKey) {
axiosConfig.headers = {
...axiosConfig.headers,
'Authorization': this.config.authorizationKey,
};
}
this.client = axios_1.default.create(axiosConfig);
}
/**
* Get current configuration
*/
getConfig() {
return { ...this.config };
}
}
exports.GraphQLClient = GraphQLClient;