BREAKING CHANGE(DockerHost): Refactor public API to DockerHost facade; introduce DockerResource base; make resource static methods internal; support flexible descriptors and stream compatibility

This commit is contained in:
2025-11-24 12:20:30 +00:00
parent cc9c20882e
commit 6fe70e0a1d
16 changed files with 1388 additions and 335 deletions

View File

@@ -2,11 +2,18 @@ import * as plugins from './plugins.js';
import * as interfaces from './interfaces/index.js';
import { DockerHost } from './classes.host.js';
import { DockerResource } from './classes.base.js';
import { DockerService } from './classes.service.js';
import { logger } from './logger.js';
export class DockerNetwork {
public static async getNetworks(
export class DockerNetwork extends DockerResource {
// STATIC (Internal - prefixed with _ to indicate internal use)
/**
* Internal: Get all networks
* Public API: Use dockerHost.getNetworks() instead
*/
public static async _list(
dockerHost: DockerHost,
): Promise<DockerNetwork[]> {
const dockerNetworks: DockerNetwork[] = [];
@@ -19,17 +26,25 @@ export class DockerNetwork {
return dockerNetworks;
}
public static async getNetworkByName(
/**
* Internal: Get network by name
* Public API: Use dockerHost.getNetworkByName(name) instead
*/
public static async _fromName(
dockerHost: DockerHost,
dockerNetworkNameArg: string,
) {
const networks = await DockerNetwork.getNetworks(dockerHost);
const networks = await DockerNetwork._list(dockerHost);
return networks.find(
(dockerNetwork) => dockerNetwork.Name === dockerNetworkNameArg,
);
}
public static async createNetwork(
/**
* Internal: Create a network
* Public API: Use dockerHost.createNetwork(descriptor) instead
*/
public static async _create(
dockerHost: DockerHost,
networkCreationDescriptor: interfaces.INetworkCreationDescriptor,
): Promise<DockerNetwork> {
@@ -54,7 +69,7 @@ export class DockerNetwork {
});
if (response.statusCode < 300) {
logger.log('info', 'Created network successfully');
return await DockerNetwork.getNetworkByName(
return await DockerNetwork._fromName(
dockerHost,
networkCreationDescriptor.Name,
);
@@ -67,11 +82,7 @@ export class DockerNetwork {
}
}
// INSTANCE
// references
public dockerHost: DockerHost;
// properties
// INSTANCE PROPERTIES
public Name: string;
public Id: string;
public Created: string;
@@ -93,11 +104,23 @@ export class DockerNetwork {
};
constructor(dockerHostArg: DockerHost) {
this.dockerHost = dockerHostArg;
super(dockerHostArg);
}
// INSTANCE METHODS
/**
* Refreshes this network's state from the Docker daemon
*/
public async refresh(): Promise<void> {
const updated = await DockerNetwork._fromName(this.dockerHost, this.Name);
if (updated) {
Object.assign(this, updated);
}
}
/**
* removes the network
* Removes the network
*/
public async remove() {
const response = await this.dockerHost.request(