import { readFileSync, existsSync } from "fs";
import { join } from "path";
/**
* ION API configuration file structure
*/
export interface IonApiConfig {
ti: string; // Tenant ID
cn: string; // Connection name
dt: string; // Device type
ci: string; // Client ID
cs: string; // Client Secret
iu: string; // ION API Base URL
pu: string; // SSO/Token URL
oa: string; // Authorization endpoint
ot: string; // Token endpoint
or: string; // Revoke token endpoint
ev: string; // Event version
v: string; // Version
saak: string; // Service Account Access Key
sask: string; // Service Account Secret Key
}
/**
* Parse an .ionapi file
*/
export function parseIonApiFile(filePath: string): IonApiConfig {
if (!existsSync(filePath)) {
throw new Error(`ION API file not found: ${filePath}`);
}
const content = readFileSync(filePath, "utf-8");
try {
const config = JSON.parse(content) as IonApiConfig;
// Validate required fields
const requiredFields: (keyof IonApiConfig)[] = [
"ti", "ci", "cs", "iu", "pu", "ot", "saak", "sask"
];
for (const field of requiredFields) {
if (!config[field]) {
throw new Error(`Missing required field: ${field}`);
}
}
return config;
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(`Invalid JSON in ION API file: ${filePath}`);
}
throw error;
}
}
/**
* Get the token endpoint URL from ION API config
*/
export function getTokenEndpoint(config: IonApiConfig): string {
return `${config.pu}${config.ot}`;
}
/**
* Get the API base URL for Birst REST API
*/
export function getRestApiBaseUrl(config: IonApiConfig): string {
return `${config.iu}/${config.ti}/BIRST/InforBirstRestAPI`;
}
/**
* Get the API base URL for Birst GenAI API
*/
export function getGenAiApiBaseUrl(config: IonApiConfig): string {
return `${config.iu}/${config.ti}/BIRST/genai/tool`;
}
/**
* Get the API base URL for Birst ICW API
*/
export function getIcwApiBaseUrl(config: IonApiConfig): string {
return `${config.iu}/${config.ti}/BIRST/insights`;
}