import axios, { AxiosInstance, AxiosError } from 'axios';
import {
JsmConfig,
AqlSearchRequest,
AqlSearchResponse,
ObjectSchemaListResponse,
ObjectType,
ObjectTypeAttribute,
ApiError
} from '../types';
export class JsmClient {
private client: AxiosInstance;
private config: JsmConfig;
constructor(config: JsmConfig) {
this.config = config;
this.client = axios.create({
baseURL: `${config.baseUrl}/${config.workspaceId}/v1`,
headers: {
'Authorization': config.authToken,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
timeout: 30000
});
this.client.interceptors.response.use(
(response) => response,
(error: AxiosError) => {
const apiError: ApiError = {
message: error.message,
status: error.response?.status || 500,
details: error.response?.data
};
throw apiError;
}
);
}
async searchAssetsAql(request: AqlSearchRequest): Promise<AqlSearchResponse> {
try {
const response = await this.client.post('/object/aql', {
qlQuery: request.qlQuery,
startAt: request.startAt || 0,
maxResults: request.maxResults || 1000
}, {
params: {
startAt: request.startAt || 0,
maxResults: request.maxResults || 1000
}
});
return response.data;
} catch (error) {
console.error('Error searching assets with AQL:', error);
throw error;
}
}
async getObjectSchemas(): Promise<ObjectSchemaListResponse> {
try {
const response = await this.client.get('/objectschema/list');
return response.data;
} catch (error) {
console.error('Error getting object schemas:', error);
throw error;
}
}
async getObjectTypes(schemaId: number): Promise<ObjectType[]> {
try {
const response = await this.client.get(`/objectschema/${schemaId}/objecttypes/flat`, {
params: {
includeObjectCounts: true
}
});
return response.data;
} catch (error) {
console.error('Error getting object types:', error);
throw error;
}
}
async getObjectAttributes(objectTypeId: number): Promise<ObjectTypeAttribute[]> {
try {
const response = await this.client.get(`/objecttype/${objectTypeId}/attributes`, {
params: {
excludeParentAttributes: false,
includeValueExist: true
}
});
return response.data;
} catch (error) {
console.error('Error getting object attributes:', error);
throw error;
}
}
}