feat(apiclient): add TypeScript API client (ts_apiclient) with resource managers and package exports

This commit is contained in:
2026-03-06 07:52:10 +00:00
parent ca2d2b09ad
commit abde872ab2
18 changed files with 1850 additions and 236 deletions

View File

@@ -0,0 +1,157 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class ApiToken {
private clientRef: DcRouterApiClient;
// Data from IApiTokenInfo
public id: string;
public name: string;
public scopes: interfaces.data.TApiTokenScope[];
public createdAt: number;
public expiresAt: number | null;
public lastUsedAt: number | null;
public enabled: boolean;
/** Only set on creation or roll. Not persisted on server side. */
public tokenValue?: string;
constructor(clientRef: DcRouterApiClient, data: interfaces.data.IApiTokenInfo, tokenValue?: string) {
this.clientRef = clientRef;
this.id = data.id;
this.name = data.name;
this.scopes = data.scopes;
this.createdAt = data.createdAt;
this.expiresAt = data.expiresAt;
this.lastUsedAt = data.lastUsedAt;
this.enabled = data.enabled;
this.tokenValue = tokenValue;
}
public async revoke(): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_RevokeApiToken>(
'revokeApiToken',
this.clientRef.buildRequestPayload({ id: this.id }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to revoke token');
}
}
public async roll(): Promise<string> {
const response = await this.clientRef.request<interfaces.requests.IReq_RollApiToken>(
'rollApiToken',
this.clientRef.buildRequestPayload({ id: this.id }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to roll token');
}
this.tokenValue = response.tokenValue;
return response.tokenValue!;
}
public async toggle(enabled: boolean): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_ToggleApiToken>(
'toggleApiToken',
this.clientRef.buildRequestPayload({ id: this.id, enabled }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to toggle token');
}
this.enabled = enabled;
}
}
export class ApiTokenBuilder {
private clientRef: DcRouterApiClient;
private tokenName: string = '';
private tokenScopes: interfaces.data.TApiTokenScope[] = [];
private tokenExpiresInDays?: number | null;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public setName(name: string): this {
this.tokenName = name;
return this;
}
public setScopes(scopes: interfaces.data.TApiTokenScope[]): this {
this.tokenScopes = scopes;
return this;
}
public addScope(scope: interfaces.data.TApiTokenScope): this {
if (!this.tokenScopes.includes(scope)) {
this.tokenScopes.push(scope);
}
return this;
}
public setExpiresInDays(days: number | null): this {
this.tokenExpiresInDays = days;
return this;
}
public async save(): Promise<ApiToken> {
const response = await this.clientRef.request<interfaces.requests.IReq_CreateApiToken>(
'createApiToken',
this.clientRef.buildRequestPayload({
name: this.tokenName,
scopes: this.tokenScopes,
expiresInDays: this.tokenExpiresInDays,
}) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to create API token');
}
return new ApiToken(
this.clientRef,
{
id: response.tokenId!,
name: this.tokenName,
scopes: this.tokenScopes,
createdAt: Date.now(),
expiresAt: this.tokenExpiresInDays
? Date.now() + this.tokenExpiresInDays * 24 * 60 * 60 * 1000
: null,
lastUsedAt: null,
enabled: true,
},
response.tokenValue,
);
}
}
export class ApiTokenManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<ApiToken[]> {
const response = await this.clientRef.request<interfaces.requests.IReq_ListApiTokens>(
'listApiTokens',
this.clientRef.buildRequestPayload() as any,
);
return response.tokens.map((t) => new ApiToken(this.clientRef, t));
}
public async create(options: {
name: string;
scopes: interfaces.data.TApiTokenScope[];
expiresInDays?: number | null;
}): Promise<ApiToken> {
return this.build()
.setName(options.name)
.setScopes(options.scopes)
.setExpiresInDays(options.expiresInDays ?? null)
.save();
}
public build(): ApiTokenBuilder {
return new ApiTokenBuilder(this.clientRef);
}
}

View File

@@ -0,0 +1,123 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class Certificate {
private clientRef: DcRouterApiClient;
// Data from ICertificateInfo
public domain: string;
public routeNames: string[];
public status: interfaces.requests.TCertificateStatus;
public source: interfaces.requests.TCertificateSource;
public tlsMode: 'terminate' | 'terminate-and-reencrypt' | 'passthrough';
public expiryDate?: string;
public issuer?: string;
public issuedAt?: string;
public error?: string;
public canReprovision: boolean;
public backoffInfo?: {
failures: number;
retryAfter?: string;
lastError?: string;
};
constructor(clientRef: DcRouterApiClient, data: interfaces.requests.ICertificateInfo) {
this.clientRef = clientRef;
this.domain = data.domain;
this.routeNames = data.routeNames;
this.status = data.status;
this.source = data.source;
this.tlsMode = data.tlsMode;
this.expiryDate = data.expiryDate;
this.issuer = data.issuer;
this.issuedAt = data.issuedAt;
this.error = data.error;
this.canReprovision = data.canReprovision;
this.backoffInfo = data.backoffInfo;
}
public async reprovision(): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_ReprovisionCertificateDomain>(
'reprovisionCertificateDomain',
this.clientRef.buildRequestPayload({ domain: this.domain }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to reprovision certificate');
}
}
public async delete(): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_DeleteCertificate>(
'deleteCertificate',
this.clientRef.buildRequestPayload({ domain: this.domain }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to delete certificate');
}
}
public async export(): Promise<{
id: string;
domainName: string;
created: number;
validUntil: number;
privateKey: string;
publicKey: string;
csr: string;
} | undefined> {
const response = await this.clientRef.request<interfaces.requests.IReq_ExportCertificate>(
'exportCertificate',
this.clientRef.buildRequestPayload({ domain: this.domain }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to export certificate');
}
return response.cert;
}
}
export interface ICertificateSummary {
total: number;
valid: number;
expiring: number;
expired: number;
failed: number;
unknown: number;
}
export class CertificateManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<{ certificates: Certificate[]; summary: ICertificateSummary }> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetCertificateOverview>(
'getCertificateOverview',
this.clientRef.buildRequestPayload() as any,
);
return {
certificates: response.certificates.map((c) => new Certificate(this.clientRef, c)),
summary: response.summary,
};
}
public async import(cert: {
id: string;
domainName: string;
created: number;
validUntil: number;
privateKey: string;
publicKey: string;
csr: string;
}): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_ImportCertificate>(
'importCertificate',
this.clientRef.buildRequestPayload({ cert }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to import certificate');
}
}
}

View File

@@ -0,0 +1,17 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class ConfigManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async get(section?: string): Promise<interfaces.requests.IReq_GetConfiguration['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetConfiguration>(
'getConfiguration',
this.clientRef.buildRequestPayload({ section }) as any,
);
}
}

View File

@@ -0,0 +1,112 @@
import * as plugins from './plugins.js';
import * as interfaces from '../ts_interfaces/index.js';
import { RouteManager } from './classes.route.js';
import { CertificateManager } from './classes.certificate.js';
import { ApiTokenManager } from './classes.apitoken.js';
import { RemoteIngressManager } from './classes.remoteingress.js';
import { StatsManager } from './classes.stats.js';
import { ConfigManager } from './classes.config.js';
import { LogManager } from './classes.logs.js';
import { EmailManager } from './classes.email.js';
import { RadiusManager } from './classes.radius.js';
export interface IDcRouterApiClientOptions {
baseUrl: string;
apiToken?: string;
}
export class DcRouterApiClient {
public baseUrl: string;
public apiToken?: string;
public identity?: interfaces.data.IIdentity;
// Resource managers
public routes: RouteManager;
public certificates: CertificateManager;
public apiTokens: ApiTokenManager;
public remoteIngress: RemoteIngressManager;
public stats: StatsManager;
public config: ConfigManager;
public logs: LogManager;
public emails: EmailManager;
public radius: RadiusManager;
constructor(options: IDcRouterApiClientOptions) {
this.baseUrl = options.baseUrl.replace(/\/+$/, '');
this.apiToken = options.apiToken;
this.routes = new RouteManager(this);
this.certificates = new CertificateManager(this);
this.apiTokens = new ApiTokenManager(this);
this.remoteIngress = new RemoteIngressManager(this);
this.stats = new StatsManager(this);
this.config = new ConfigManager(this);
this.logs = new LogManager(this);
this.emails = new EmailManager(this);
this.radius = new RadiusManager(this);
}
// =====================
// Auth
// =====================
public async login(username: string, password: string): Promise<interfaces.data.IIdentity> {
const response = await this.request<interfaces.requests.IReq_AdminLoginWithUsernameAndPassword>(
'adminLoginWithUsernameAndPassword',
{ username, password },
);
if (response.identity) {
this.identity = response.identity;
}
return response.identity!;
}
public async logout(): Promise<void> {
await this.request<interfaces.requests.IReq_AdminLogout>(
'adminLogout',
{ identity: this.identity! },
);
this.identity = undefined;
}
public async verifyIdentity(): Promise<{ valid: boolean; identity?: interfaces.data.IIdentity }> {
const response = await this.request<interfaces.requests.IReq_VerifyIdentity>(
'verifyIdentity',
{ identity: this.identity! },
);
if (response.identity) {
this.identity = response.identity;
}
return response;
}
// =====================
// Internal request helper
// =====================
public async request<T extends plugins.typedrequestInterfaces.ITypedRequest>(
method: string,
requestData: T['request'],
): Promise<T['response']> {
const typedRequest = new plugins.typedrequest.TypedRequest<T>(
`${this.baseUrl}/typedrequest`,
method,
);
return typedRequest.fire(requestData);
}
/**
* Build a request payload with identity and optional API token auto-injected.
*/
public buildRequestPayload(extra: Record<string, any> = {}): Record<string, any> {
const payload: Record<string, any> = { ...extra };
if (this.identity) {
payload.identity = this.identity;
}
if (this.apiToken) {
payload.apiToken = this.apiToken;
}
return payload;
}
}

View File

@@ -0,0 +1,77 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class Email {
private clientRef: DcRouterApiClient;
// Data from IEmail
public id: string;
public direction: interfaces.requests.TEmailDirection;
public status: interfaces.requests.TEmailStatus;
public from: string;
public to: string;
public subject: string;
public timestamp: string;
public messageId: string;
public size: string;
constructor(clientRef: DcRouterApiClient, data: interfaces.requests.IEmail) {
this.clientRef = clientRef;
this.id = data.id;
this.direction = data.direction;
this.status = data.status;
this.from = data.from;
this.to = data.to;
this.subject = data.subject;
this.timestamp = data.timestamp;
this.messageId = data.messageId;
this.size = data.size;
}
public async getDetail(): Promise<interfaces.requests.IEmailDetail | null> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetEmailDetail>(
'getEmailDetail',
this.clientRef.buildRequestPayload({ emailId: this.id }) as any,
);
return response.email;
}
public async resend(): Promise<{ success: boolean; newQueueId?: string }> {
const response = await this.clientRef.request<interfaces.requests.IReq_ResendEmail>(
'resendEmail',
this.clientRef.buildRequestPayload({ emailId: this.id }) as any,
);
return response;
}
}
export class EmailManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<Email[]> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetAllEmails>(
'getAllEmails',
this.clientRef.buildRequestPayload() as any,
);
return response.emails.map((e) => new Email(this.clientRef, e));
}
public async getDetail(emailId: string): Promise<interfaces.requests.IEmailDetail | null> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetEmailDetail>(
'getEmailDetail',
this.clientRef.buildRequestPayload({ emailId }) as any,
);
return response.email;
}
public async resend(emailId: string): Promise<{ success: boolean; newQueueId?: string }> {
return this.clientRef.request<interfaces.requests.IReq_ResendEmail>(
'resendEmail',
this.clientRef.buildRequestPayload({ emailId }) as any,
);
}
}

View File

@@ -0,0 +1,37 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class LogManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async getRecent(options?: {
level?: 'debug' | 'info' | 'warn' | 'error';
category?: 'smtp' | 'dns' | 'security' | 'system' | 'email';
limit?: number;
offset?: number;
search?: string;
timeRange?: string;
}): Promise<interfaces.requests.IReq_GetRecentLogs['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetRecentLogs>(
'getRecentLogs',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getStream(options?: {
follow?: boolean;
filters?: {
level?: string[];
category?: string[];
};
}): Promise<interfaces.requests.IReq_GetLogStream['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetLogStream>(
'getLogStream',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
}

View File

@@ -0,0 +1,180 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
// =====================
// Sub-managers
// =====================
export class RadiusClientManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<Array<{
name: string;
ipRange: string;
description?: string;
enabled: boolean;
}>> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetRadiusClients>(
'getRadiusClients',
this.clientRef.buildRequestPayload() as any,
);
return response.clients;
}
public async set(client: {
name: string;
ipRange: string;
secret: string;
description?: string;
enabled: boolean;
}): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_SetRadiusClient>(
'setRadiusClient',
this.clientRef.buildRequestPayload({ client }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to set RADIUS client');
}
}
public async remove(name: string): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_RemoveRadiusClient>(
'removeRadiusClient',
this.clientRef.buildRequestPayload({ name }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to remove RADIUS client');
}
}
}
export class RadiusVlanManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<interfaces.requests.IReq_GetVlanMappings['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetVlanMappings>(
'getVlanMappings',
this.clientRef.buildRequestPayload() as any,
);
}
public async set(mapping: {
mac: string;
vlan: number;
description?: string;
enabled: boolean;
}): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_SetVlanMapping>(
'setVlanMapping',
this.clientRef.buildRequestPayload({ mapping }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to set VLAN mapping');
}
}
public async remove(mac: string): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_RemoveVlanMapping>(
'removeVlanMapping',
this.clientRef.buildRequestPayload({ mac }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to remove VLAN mapping');
}
}
public async updateConfig(options: {
defaultVlan?: number;
allowUnknownMacs?: boolean;
}): Promise<{ defaultVlan: number; allowUnknownMacs: boolean }> {
const response = await this.clientRef.request<interfaces.requests.IReq_UpdateVlanConfig>(
'updateVlanConfig',
this.clientRef.buildRequestPayload(options) as any,
);
if (!response.success) {
throw new Error('Failed to update VLAN config');
}
return response.config;
}
public async testAssignment(mac: string): Promise<interfaces.requests.IReq_TestVlanAssignment['response']> {
return this.clientRef.request<interfaces.requests.IReq_TestVlanAssignment>(
'testVlanAssignment',
this.clientRef.buildRequestPayload({ mac }) as any,
);
}
}
export class RadiusSessionManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(filter?: {
username?: string;
nasIpAddress?: string;
vlanId?: number;
}): Promise<interfaces.requests.IReq_GetRadiusSessions['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetRadiusSessions>(
'getRadiusSessions',
this.clientRef.buildRequestPayload({ filter }) as any,
);
}
public async disconnect(sessionId: string, reason?: string): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_DisconnectRadiusSession>(
'disconnectRadiusSession',
this.clientRef.buildRequestPayload({ sessionId, reason }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to disconnect session');
}
}
}
// =====================
// Main RADIUS Manager
// =====================
export class RadiusManager {
private clientRef: DcRouterApiClient;
public clients: RadiusClientManager;
public vlans: RadiusVlanManager;
public sessions: RadiusSessionManager;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
this.clients = new RadiusClientManager(clientRef);
this.vlans = new RadiusVlanManager(clientRef);
this.sessions = new RadiusSessionManager(clientRef);
}
public async getAccountingSummary(
startTime: number,
endTime: number,
): Promise<interfaces.requests.IReq_GetRadiusAccountingSummary['response']['summary']> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetRadiusAccountingSummary>(
'getRadiusAccountingSummary',
this.clientRef.buildRequestPayload({ startTime, endTime }) as any,
);
return response.summary;
}
public async getStatistics(): Promise<interfaces.requests.IReq_GetRadiusStatistics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetRadiusStatistics>(
'getRadiusStatistics',
this.clientRef.buildRequestPayload() as any,
);
}
}

View File

@@ -0,0 +1,185 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class RemoteIngress {
private clientRef: DcRouterApiClient;
// Data from IRemoteIngress
public id: string;
public name: string;
public secret: string;
public listenPorts: number[];
public enabled: boolean;
public autoDerivePorts: boolean;
public tags?: string[];
public createdAt: number;
public updatedAt: number;
public effectiveListenPorts?: number[];
public manualPorts?: number[];
public derivedPorts?: number[];
constructor(clientRef: DcRouterApiClient, data: interfaces.data.IRemoteIngress) {
this.clientRef = clientRef;
this.id = data.id;
this.name = data.name;
this.secret = data.secret;
this.listenPorts = data.listenPorts;
this.enabled = data.enabled;
this.autoDerivePorts = data.autoDerivePorts;
this.tags = data.tags;
this.createdAt = data.createdAt;
this.updatedAt = data.updatedAt;
this.effectiveListenPorts = data.effectiveListenPorts;
this.manualPorts = data.manualPorts;
this.derivedPorts = data.derivedPorts;
}
public async update(changes: {
name?: string;
listenPorts?: number[];
autoDerivePorts?: boolean;
enabled?: boolean;
tags?: string[];
}): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_UpdateRemoteIngress>(
'updateRemoteIngress',
this.clientRef.buildRequestPayload({ id: this.id, ...changes }) as any,
);
if (!response.success) {
throw new Error('Failed to update remote ingress');
}
// Update local state from response
const edge = response.edge;
this.name = edge.name;
this.listenPorts = edge.listenPorts;
this.enabled = edge.enabled;
this.autoDerivePorts = edge.autoDerivePorts;
this.tags = edge.tags;
this.updatedAt = edge.updatedAt;
this.effectiveListenPorts = edge.effectiveListenPorts;
this.manualPorts = edge.manualPorts;
this.derivedPorts = edge.derivedPorts;
}
public async delete(): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_DeleteRemoteIngress>(
'deleteRemoteIngress',
this.clientRef.buildRequestPayload({ id: this.id }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to delete remote ingress');
}
}
public async regenerateSecret(): Promise<string> {
const response = await this.clientRef.request<interfaces.requests.IReq_RegenerateRemoteIngressSecret>(
'regenerateRemoteIngressSecret',
this.clientRef.buildRequestPayload({ id: this.id }) as any,
);
if (!response.success) {
throw new Error('Failed to regenerate secret');
}
this.secret = response.secret;
return response.secret;
}
public async getConnectionToken(hubHost?: string): Promise<string | undefined> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetRemoteIngressConnectionToken>(
'getRemoteIngressConnectionToken',
this.clientRef.buildRequestPayload({ edgeId: this.id, hubHost }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to get connection token');
}
return response.token;
}
}
export class RemoteIngressBuilder {
private clientRef: DcRouterApiClient;
private edgeName: string = '';
private edgeListenPorts?: number[];
private edgeAutoDerivePorts?: boolean;
private edgeTags?: string[];
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public setName(name: string): this {
this.edgeName = name;
return this;
}
public setListenPorts(ports: number[]): this {
this.edgeListenPorts = ports;
return this;
}
public setAutoDerivePorts(auto: boolean): this {
this.edgeAutoDerivePorts = auto;
return this;
}
public setTags(tags: string[]): this {
this.edgeTags = tags;
return this;
}
public async save(): Promise<RemoteIngress> {
const response = await this.clientRef.request<interfaces.requests.IReq_CreateRemoteIngress>(
'createRemoteIngress',
this.clientRef.buildRequestPayload({
name: this.edgeName,
listenPorts: this.edgeListenPorts,
autoDerivePorts: this.edgeAutoDerivePorts,
tags: this.edgeTags,
}) as any,
);
if (!response.success) {
throw new Error('Failed to create remote ingress');
}
return new RemoteIngress(this.clientRef, response.edge);
}
}
export class RemoteIngressManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<RemoteIngress[]> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetRemoteIngresses>(
'getRemoteIngresses',
this.clientRef.buildRequestPayload() as any,
);
return response.edges.map((e) => new RemoteIngress(this.clientRef, e));
}
public async getStatuses(): Promise<interfaces.data.IRemoteIngressStatus[]> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetRemoteIngressStatus>(
'getRemoteIngressStatus',
this.clientRef.buildRequestPayload() as any,
);
return response.statuses;
}
public async create(options: {
name: string;
listenPorts?: number[];
autoDerivePorts?: boolean;
tags?: string[];
}): Promise<RemoteIngress> {
const builder = this.build().setName(options.name);
if (options.listenPorts) builder.setListenPorts(options.listenPorts);
if (options.autoDerivePorts !== undefined) builder.setAutoDerivePorts(options.autoDerivePorts);
if (options.tags) builder.setTags(options.tags);
return builder.save();
}
public build(): RemoteIngressBuilder {
return new RemoteIngressBuilder(this.clientRef);
}
}

View File

@@ -0,0 +1,203 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { IRouteConfig } from '@push.rocks/smartproxy';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
export class Route {
private clientRef: DcRouterApiClient;
// Data from IMergedRoute
public routeConfig: IRouteConfig;
public source: 'hardcoded' | 'programmatic';
public enabled: boolean;
public overridden: boolean;
public storedRouteId?: string;
public createdAt?: number;
public updatedAt?: number;
// Convenience accessors
public get name(): string {
return this.routeConfig.name || '';
}
constructor(clientRef: DcRouterApiClient, data: interfaces.data.IMergedRoute) {
this.clientRef = clientRef;
this.routeConfig = data.route;
this.source = data.source;
this.enabled = data.enabled;
this.overridden = data.overridden;
this.storedRouteId = data.storedRouteId;
this.createdAt = data.createdAt;
this.updatedAt = data.updatedAt;
}
public async update(changes: Partial<IRouteConfig>): Promise<void> {
if (!this.storedRouteId) {
throw new Error('Cannot update a hardcoded route. Use setOverride() instead.');
}
const response = await this.clientRef.request<interfaces.requests.IReq_UpdateRoute>(
'updateRoute',
this.clientRef.buildRequestPayload({ id: this.storedRouteId, route: changes }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to update route');
}
}
public async delete(): Promise<void> {
if (!this.storedRouteId) {
throw new Error('Cannot delete a hardcoded route. Use setOverride() instead.');
}
const response = await this.clientRef.request<interfaces.requests.IReq_DeleteRoute>(
'deleteRoute',
this.clientRef.buildRequestPayload({ id: this.storedRouteId }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to delete route');
}
}
public async toggle(enabled: boolean): Promise<void> {
if (!this.storedRouteId) {
throw new Error('Cannot toggle a hardcoded route. Use setOverride() instead.');
}
const response = await this.clientRef.request<interfaces.requests.IReq_ToggleRoute>(
'toggleRoute',
this.clientRef.buildRequestPayload({ id: this.storedRouteId, enabled }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to toggle route');
}
this.enabled = enabled;
}
public async setOverride(enabled: boolean): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_SetRouteOverride>(
'setRouteOverride',
this.clientRef.buildRequestPayload({ routeName: this.name, enabled }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to set route override');
}
this.overridden = true;
this.enabled = enabled;
}
public async removeOverride(): Promise<void> {
const response = await this.clientRef.request<interfaces.requests.IReq_RemoveRouteOverride>(
'removeRouteOverride',
this.clientRef.buildRequestPayload({ routeName: this.name }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to remove route override');
}
this.overridden = false;
}
}
export class RouteBuilder {
private clientRef: DcRouterApiClient;
private routeConfig: Partial<IRouteConfig> = {};
private isEnabled: boolean = true;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public setName(name: string): this {
this.routeConfig.name = name;
return this;
}
public setMatch(match: IRouteConfig['match']): this {
this.routeConfig.match = match;
return this;
}
public setAction(action: IRouteConfig['action']): this {
this.routeConfig.action = action;
return this;
}
public setTls(tls: IRouteConfig['action']['tls']): this {
if (!this.routeConfig.action) {
this.routeConfig.action = { type: 'forward' } as IRouteConfig['action'];
}
this.routeConfig.action!.tls = tls;
return this;
}
public setEnabled(enabled: boolean): this {
this.isEnabled = enabled;
return this;
}
public async save(): Promise<Route> {
const response = await this.clientRef.request<interfaces.requests.IReq_CreateRoute>(
'createRoute',
this.clientRef.buildRequestPayload({
route: this.routeConfig as IRouteConfig,
enabled: this.isEnabled,
}) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to create route');
}
// Return a Route instance by re-fetching the list
// The created route is programmatic, so we find it by storedRouteId
const { routes } = await new RouteManager(this.clientRef).list();
const created = routes.find((r) => r.storedRouteId === response.storedRouteId);
if (created) {
return created;
}
// Fallback: construct from known data
return new Route(this.clientRef, {
route: this.routeConfig as IRouteConfig,
source: 'programmatic',
enabled: this.isEnabled,
overridden: false,
storedRouteId: response.storedRouteId,
});
}
}
export class RouteManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async list(): Promise<{ routes: Route[]; warnings: interfaces.data.IRouteWarning[] }> {
const response = await this.clientRef.request<interfaces.requests.IReq_GetMergedRoutes>(
'getMergedRoutes',
this.clientRef.buildRequestPayload() as any,
);
return {
routes: response.routes.map((r) => new Route(this.clientRef, r)),
warnings: response.warnings,
};
}
public async create(routeConfig: IRouteConfig, enabled?: boolean): Promise<Route> {
const response = await this.clientRef.request<interfaces.requests.IReq_CreateRoute>(
'createRoute',
this.clientRef.buildRequestPayload({ route: routeConfig, enabled: enabled ?? true }) as any,
);
if (!response.success) {
throw new Error(response.message || 'Failed to create route');
}
return new Route(this.clientRef, {
route: routeConfig,
source: 'programmatic',
enabled: enabled ?? true,
overridden: false,
storedRouteId: response.storedRouteId,
});
}
public build(): RouteBuilder {
return new RouteBuilder(this.clientRef);
}
}

View File

@@ -0,0 +1,111 @@
import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';
type TTimeRange = '1h' | '6h' | '24h' | '7d' | '30d';
export class StatsManager {
private clientRef: DcRouterApiClient;
constructor(clientRef: DcRouterApiClient) {
this.clientRef = clientRef;
}
public async getServer(options?: {
timeRange?: TTimeRange;
includeHistory?: boolean;
}): Promise<interfaces.requests.IReq_GetServerStatistics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetServerStatistics>(
'getServerStatistics',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getEmail(options?: {
timeRange?: TTimeRange;
domain?: string;
includeDetails?: boolean;
}): Promise<interfaces.requests.IReq_GetEmailStatistics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetEmailStatistics>(
'getEmailStatistics',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getDns(options?: {
timeRange?: TTimeRange;
domain?: string;
includeQueryTypes?: boolean;
}): Promise<interfaces.requests.IReq_GetDnsStatistics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetDnsStatistics>(
'getDnsStatistics',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getRateLimits(options?: {
domain?: string;
ip?: string;
includeBlocked?: boolean;
}): Promise<interfaces.requests.IReq_GetRateLimitStatus['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetRateLimitStatus>(
'getRateLimitStatus',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getSecurity(options?: {
timeRange?: TTimeRange;
includeDetails?: boolean;
}): Promise<interfaces.requests.IReq_GetSecurityMetrics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetSecurityMetrics>(
'getSecurityMetrics',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getConnections(options?: {
protocol?: 'smtp' | 'smtps' | 'http' | 'https';
state?: string;
}): Promise<interfaces.requests.IReq_GetActiveConnections['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetActiveConnections>(
'getActiveConnections',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getQueues(options?: {
queueName?: string;
}): Promise<interfaces.requests.IReq_GetQueueStatus['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetQueueStatus>(
'getQueueStatus',
this.clientRef.buildRequestPayload(options || {}) as any,
);
}
public async getHealth(detailed?: boolean): Promise<interfaces.requests.IReq_GetHealthStatus['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetHealthStatus>(
'getHealthStatus',
this.clientRef.buildRequestPayload({ detailed }) as any,
);
}
public async getNetwork(): Promise<interfaces.requests.IReq_GetNetworkStats['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetNetworkStats>(
'getNetworkStats',
this.clientRef.buildRequestPayload() as any,
);
}
public async getCombined(sections?: {
server?: boolean;
email?: boolean;
dns?: boolean;
security?: boolean;
network?: boolean;
}): Promise<interfaces.requests.IReq_GetCombinedMetrics['response']> {
return this.clientRef.request<interfaces.requests.IReq_GetCombinedMetrics>(
'getCombinedMetrics',
this.clientRef.buildRequestPayload({ sections }) as any,
);
}
}

15
ts_apiclient/index.ts Normal file
View File

@@ -0,0 +1,15 @@
// Main client
export { DcRouterApiClient, type IDcRouterApiClientOptions } from './classes.dcrouterapiclient.js';
// Resource classes
export { Route, RouteBuilder, RouteManager } from './classes.route.js';
export { Certificate, CertificateManager, type ICertificateSummary } from './classes.certificate.js';
export { ApiToken, ApiTokenBuilder, ApiTokenManager } from './classes.apitoken.js';
export { RemoteIngress, RemoteIngressBuilder, RemoteIngressManager } from './classes.remoteingress.js';
export { Email, EmailManager } from './classes.email.js';
// Read-only managers
export { StatsManager } from './classes.stats.js';
export { ConfigManager } from './classes.config.js';
export { LogManager } from './classes.logs.js';
export { RadiusManager, RadiusClientManager, RadiusVlanManager, RadiusSessionManager } from './classes.radius.js';

8
ts_apiclient/plugins.ts Normal file
View File

@@ -0,0 +1,8 @@
// @api.global scope
import * as typedrequest from '@api.global/typedrequest';
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
export {
typedrequest,
typedrequestInterfaces,
};