@dev-fastn-ai/ucl-sdk
Version:
Fastn UCL SDK - A robust TypeScript SDK for integrating AI agents with Fastn UCL
152 lines • 5.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FastnAPIClient = void 0;
const axios_1 = __importDefault(require("axios"));
const errors_1 = require("../utils/errors");
class FastnAPIClient {
constructor(config) {
this.validateConfig(config);
this.config = config;
this.client = axios_1.default.create({
timeout: 30000,
headers: {
'Content-Type': 'application/json',
'x-fastn-custom-auth': 'true',
'authorization': config.authToken,
'x-fastn-space-id': config.spaceId,
'x-fastn-space-tenantid': config.spaceId,
'stage': config.environment || 'LIVE'
}
});
this.setupInterceptors();
}
validateConfig(config) {
if (!config.authToken) {
throw new errors_1.ConfigurationError('authToken is required');
}
if (!config.spaceId) {
throw new errors_1.ConfigurationError('spaceId is required');
}
}
setupInterceptors() {
this.client.interceptors.response.use((response) => response, (error) => {
console.log("error.response", error.response);
if (error.response?.status === 401) {
throw new errors_1.AuthenticationError('Invalid authentication token');
}
const message = error.response?.data?.message || error.message || 'API request failed';
const statusCode = error.response?.status || 500;
throw new errors_1.APIError(message, statusCode, error.response?.data);
});
}
getBaseUrl() {
return this.config.baseUrl || 'https://live.fastn.ai';
}
async getTools(params = {}) {
const { limit = 100, offset = 0 } = params;
try {
const response = await this.client.post(`${this.getBaseUrl()}/api/ucl/getAllTools`, {
input: { limit, offset },
});
return {
success: true,
data: response.data ?? [],
};
}
catch (error) {
console.error('Error fetching tools:', error);
return {
success: false,
data: [],
error: error instanceof Error ? error.message : String(error),
};
}
}
async executeTool(request) {
try {
const response = await this.client.post(`${this.getBaseUrl()}/api/ucl/executeTool`, {
input: request
});
return {
success: true,
data: response.data.body
};
}
catch (error) {
return {
success: false,
data: null,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
async getConnectors(tenantId) {
try {
let query = ``;
if (tenantId) {
query = `
query Connectors($input: GetConnectorsListInput!) {
connectors(input: $input) {
name
id
description
status
}
}
`;
}
else {
query = `
query Connectors($input: GetConnectorsListInput!) {
connectors(input: $input) {
name
id
description
}
}
`;
}
const variables = {
input: {
projectId: this.config.spaceId,
tenantId: tenantId,
onlyActive: true,
environment: this.config.environment || 'DRAFT'
}
};
const response = await axios_1.default.post(this.getBaseUrl() + '/api/graphql', {
query,
variables
}, {
headers: {
'authorization': `Bearer ${this.config.authToken}`,
'realm': 'fastn',
'x-fastn-custom-auth': 'true',
'x-fastn-space-id': this.config.spaceId,
'Content-Type': 'application/json',
'stage': this.config.environment || 'DRAFT'
}
});
if (response.data.data.errors) {
throw new errors_1.APIError('GraphQL query failed', 400, response.data.data.errors);
}
return {
success: true,
data: response.data.data.connectors || []
};
}
catch (error) {
console.error('Error fetching connectors:', error);
return {
success: false,
data: [],
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
}
exports.FastnAPIClient = FastnAPIClient;
//# sourceMappingURL=api-client.js.map