83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import { logger } from './unifi.logger.js';
|
|
import { UnifiHttp } from './classes.unifihttp.js';
|
|
import { SiteManager } from './classes.sitemanager.js';
|
|
import { HostManager } from './classes.hostmanager.js';
|
|
import type { IUnifiAccountOptions, THttpMethod } from './interfaces/index.js';
|
|
|
|
/**
|
|
* UniFi Account - Entry point for Site Manager (cloud) API
|
|
*
|
|
* This class provides access to the UniFi Site Manager API using API key authentication.
|
|
* It's used to manage sites and hosts across your UniFi deployment via ui.com.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const account = new UnifiAccount({ apiKey: 'your-api-key' });
|
|
* const sites = await account.siteManager.listSites();
|
|
* const hosts = await account.hostManager.listHosts();
|
|
* ```
|
|
*/
|
|
export class UnifiAccount {
|
|
/** Site Manager API base URL */
|
|
private static readonly BASE_URL = 'https://api.ui.com/v1';
|
|
|
|
/** API key for authentication */
|
|
private apiKey: string;
|
|
|
|
/** HTTP client */
|
|
private http: UnifiHttp;
|
|
|
|
/** Site manager instance */
|
|
public siteManager: SiteManager;
|
|
|
|
/** Host manager instance */
|
|
public hostManager: HostManager;
|
|
|
|
constructor(options: IUnifiAccountOptions) {
|
|
this.apiKey = options.apiKey;
|
|
|
|
// Initialize HTTP client
|
|
this.http = new UnifiHttp(UnifiAccount.BASE_URL, true); // Cloud API uses valid SSL
|
|
this.http.setHeader('X-API-Key', this.apiKey);
|
|
|
|
// Initialize managers
|
|
this.siteManager = new SiteManager(this);
|
|
this.hostManager = new HostManager(this);
|
|
|
|
logger.log('info', 'UnifiAccount initialized for Site Manager API');
|
|
}
|
|
|
|
/**
|
|
* Make a request to the Site Manager API
|
|
*/
|
|
public async request<T>(
|
|
method: THttpMethod,
|
|
endpoint: string,
|
|
data?: unknown
|
|
): Promise<T> {
|
|
return this.http.request<T>(method, endpoint, data);
|
|
}
|
|
|
|
/**
|
|
* Get account info
|
|
*/
|
|
public async getAccountInfo(): Promise<unknown> {
|
|
return this.request('GET', '/ea/account');
|
|
}
|
|
|
|
/**
|
|
* Get all sites (convenience method)
|
|
*/
|
|
public async getSites() {
|
|
return this.siteManager.listSites();
|
|
}
|
|
|
|
/**
|
|
* Get all hosts (convenience method)
|
|
*/
|
|
public async getHosts() {
|
|
return this.hostManager.listHosts();
|
|
}
|
|
}
|