105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
import type { PeeringDbClient } from './peeringdb.classes.client.js';
|
|
import type { INetwork } from './interfaces/peeringdb.api.network.js';
|
|
import type { IQueryOptions } from './peeringdb.types.js';
|
|
|
|
/**
|
|
* Manager for Network resources
|
|
*/
|
|
export class NetworkManager {
|
|
constructor(private client: PeeringDbClient) {}
|
|
|
|
/**
|
|
* List networks with optional filtering
|
|
*/
|
|
async list(options: IQueryOptions = {}): Promise<INetwork[]> {
|
|
return this.client.request<INetwork>('net', 'GET', options);
|
|
}
|
|
|
|
/**
|
|
* Get a single network by ID
|
|
*/
|
|
async getById(id: number, depth?: 0 | 1 | 2): Promise<INetwork | null> {
|
|
const options: IQueryOptions = { id };
|
|
if (depth !== undefined) {
|
|
options.depth = depth;
|
|
}
|
|
const results = await this.client.request<INetwork>('net', 'GET', options);
|
|
return results[0] || null;
|
|
}
|
|
|
|
/**
|
|
* Get a network by ASN
|
|
*/
|
|
async getByAsn(asn: number, depth?: 0 | 1 | 2): Promise<INetwork | null> {
|
|
const options: IQueryOptions = { asn };
|
|
if (depth !== undefined) {
|
|
options.depth = depth;
|
|
}
|
|
const results = await this.client.request<INetwork>('net', 'GET', options);
|
|
return results[0] || null;
|
|
}
|
|
|
|
/**
|
|
* Search networks by name
|
|
*/
|
|
async searchByName(name: string, options: IQueryOptions = {}): Promise<INetwork[]> {
|
|
return this.client.request<INetwork>('net', 'GET', {
|
|
...options,
|
|
name__contains: name,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get networks by organization ID
|
|
*/
|
|
async getByOrgId(orgId: number, options: IQueryOptions = {}): Promise<INetwork[]> {
|
|
return this.client.request<INetwork>('net', 'GET', {
|
|
...options,
|
|
org_id: orgId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Search networks by IRR as-set
|
|
*/
|
|
async searchByIrrAsSet(irrAsSet: string, options: IQueryOptions = {}): Promise<INetwork[]> {
|
|
return this.client.request<INetwork>('net', 'GET', {
|
|
...options,
|
|
irr_as_set__contains: irrAsSet,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get networks by peering policy
|
|
*/
|
|
async getByPolicy(policy: string, options: IQueryOptions = {}): Promise<INetwork[]> {
|
|
return this.client.request<INetwork>('net', 'GET', {
|
|
...options,
|
|
policy_general: policy,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a new network (requires authentication)
|
|
*/
|
|
async create(data: Partial<INetwork>): Promise<INetwork> {
|
|
const results = await this.client.request<INetwork>('net', 'POST', {}, data);
|
|
return results[0];
|
|
}
|
|
|
|
/**
|
|
* Update a network (requires authentication)
|
|
*/
|
|
async update(id: number, data: Partial<INetwork>): Promise<INetwork> {
|
|
const results = await this.client.request<INetwork>(`net/${id}`, 'PUT', {}, data);
|
|
return results[0];
|
|
}
|
|
|
|
/**
|
|
* Delete a network (requires authentication)
|
|
*/
|
|
async delete(id: number): Promise<void> {
|
|
await this.client.request('net', 'DELETE', { id });
|
|
}
|
|
}
|