fix(DockerContainer): Fix getContainerById to return undefined for non-existent containers
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/docker',
|
||||
version: '5.0.0',
|
||||
version: '5.0.2',
|
||||
description: 'Provides easy communication with Docker remote API from Node.js, with TypeScript support.'
|
||||
}
|
||||
|
||||
@@ -28,13 +28,14 @@ export class DockerContainer extends DockerResource {
|
||||
/**
|
||||
* Internal: Get a container by ID
|
||||
* Public API: Use dockerHost.getContainerById(id) instead
|
||||
* Returns undefined if container does not exist
|
||||
*/
|
||||
public static async _fromId(
|
||||
dockerHostArg: DockerHost,
|
||||
containerId: string,
|
||||
): Promise<DockerContainer> {
|
||||
const response = await dockerHostArg.request('GET', `/containers/${containerId}/json`);
|
||||
return new DockerContainer(dockerHostArg, response.body);
|
||||
): Promise<DockerContainer | undefined> {
|
||||
const containers = await this._list(dockerHostArg);
|
||||
return containers.find((container) => container.Id === containerId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -164,8 +164,9 @@ export class DockerHost {
|
||||
|
||||
/**
|
||||
* Gets a container by ID
|
||||
* Returns undefined if container does not exist
|
||||
*/
|
||||
public async getContainerById(containerId: string) {
|
||||
public async getContainerById(containerId: string): Promise<DockerContainer | undefined> {
|
||||
return await DockerContainer._fromId(this, containerId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user