73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
|
|
import type { PeeringDbClient } from './peeringdb.classes.client.js';
|
||
|
|
import type { IOrganization } from './interfaces/peeringdb.api.organization.js';
|
||
|
|
import type { IQueryOptions } from './peeringdb.types.js';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Manager for Organization resources
|
||
|
|
*/
|
||
|
|
export class OrganizationManager {
|
||
|
|
constructor(private client: PeeringDbClient) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* List organizations with optional filtering
|
||
|
|
*/
|
||
|
|
async list(options: IQueryOptions = {}): Promise<IOrganization[]> {
|
||
|
|
return this.client.request<IOrganization>('org', 'GET', options);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get a single organization by ID
|
||
|
|
*/
|
||
|
|
async getById(id: number, depth?: 0 | 1 | 2): Promise<IOrganization | null> {
|
||
|
|
const options: IQueryOptions = { id };
|
||
|
|
if (depth !== undefined) {
|
||
|
|
options.depth = depth;
|
||
|
|
}
|
||
|
|
const results = await this.client.request<IOrganization>('org', 'GET', options);
|
||
|
|
return results[0] || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Search organizations by name
|
||
|
|
*/
|
||
|
|
async searchByName(name: string, options: IQueryOptions = {}): Promise<IOrganization[]> {
|
||
|
|
return this.client.request<IOrganization>('org', 'GET', {
|
||
|
|
...options,
|
||
|
|
name__contains: name,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get organizations by country
|
||
|
|
*/
|
||
|
|
async getByCountry(country: string, options: IQueryOptions = {}): Promise<IOrganization[]> {
|
||
|
|
return this.client.request<IOrganization>('org', 'GET', {
|
||
|
|
...options,
|
||
|
|
country,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new organization (requires authentication)
|
||
|
|
*/
|
||
|
|
async create(data: Partial<IOrganization>): Promise<IOrganization> {
|
||
|
|
const results = await this.client.request<IOrganization>('org', 'POST', {}, data);
|
||
|
|
return results[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update an organization (requires authentication)
|
||
|
|
*/
|
||
|
|
async update(id: number, data: Partial<IOrganization>): Promise<IOrganization> {
|
||
|
|
const results = await this.client.request<IOrganization>(`org/${id}`, 'PUT', {}, data);
|
||
|
|
return results[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete an organization (requires authentication)
|
||
|
|
*/
|
||
|
|
async delete(id: number): Promise<void> {
|
||
|
|
await this.client.request('org', 'DELETE', { id });
|
||
|
|
}
|
||
|
|
}
|