Files
peeringdb/ts/peeringdb.classes.exchangemanager.ts
2025-11-18 20:47:48 +00:00

103 lines
2.7 KiB
TypeScript

import type { PeeringDbClient } from './peeringdb.classes.client.js';
import type { IExchange } from './interfaces/peeringdb.api.exchange.js';
import type { IQueryOptions } from './peeringdb.types.js';
/**
* Manager for Internet Exchange resources
*/
export class ExchangeManager {
constructor(private client: PeeringDbClient) {}
/**
* List exchanges with optional filtering
*/
async list(options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', options);
}
/**
* Get a single exchange by ID
*/
async getById(id: number, depth?: 0 | 1 | 2): Promise<IExchange | null> {
const options: IQueryOptions = { id };
if (depth !== undefined) {
options.depth = depth;
}
const results = await this.client.request<IExchange>('ix', 'GET', options);
return results[0] || null;
}
/**
* Search exchanges by name
*/
async searchByName(name: string, options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', {
...options,
name__contains: name,
});
}
/**
* Get exchanges by country
*/
async getByCountry(country: string, options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', {
...options,
country,
});
}
/**
* Get exchanges by city
*/
async getByCity(city: string, options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', {
...options,
city__contains: city,
});
}
/**
* Get exchanges by region/continent
*/
async getByRegion(region: string, options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', {
...options,
region_continent: region,
});
}
/**
* Get exchanges by organization ID
*/
async getByOrgId(orgId: number, options: IQueryOptions = {}): Promise<IExchange[]> {
return this.client.request<IExchange>('ix', 'GET', {
...options,
org_id: orgId,
});
}
/**
* Create a new exchange (requires authentication)
*/
async create(data: Partial<IExchange>): Promise<IExchange> {
const results = await this.client.request<IExchange>('ix', 'POST', {}, data);
return results[0];
}
/**
* Update an exchange (requires authentication)
*/
async update(id: number, data: Partial<IExchange>): Promise<IExchange> {
const results = await this.client.request<IExchange>(`ix/${id}`, 'PUT', {}, data);
return results[0];
}
/**
* Delete an exchange (requires authentication)
*/
async delete(id: number): Promise<void> {
await this.client.request('ix', 'DELETE', { id });
}
}