the-moby-effect
Version:
Moby/Docker API client built using effect-ts
189 lines • 7.36 kB
JavaScript
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as HttpClient from "effect/unstable/http/HttpClient";
import * as HttpApi from "effect/unstable/httpapi/HttpApi";
import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient";
import * as HttpApiEndpoint from "effect/unstable/httpapi/HttpApiEndpoint";
import * as HttpApiGroup from "effect/unstable/httpapi/HttpApiGroup";
import * as HttpApiSchema from "effect/unstable/httpapi/HttpApiSchema";
import { MobyConnectionOptions } from "../../MobyConnection.js";
import { makeAgnosticHttpClientLayer } from "../../MobyPlatforms.js";
import { VolumeClusterVolumeSpec as ClusterVolumeSpec, VolumeVolume as Volume, VolumeCreateOptions } from "../generated/index.js";
import { VolumeIdentifier } from "../schemas/id.js";
import { DockerError } from "./circular.js";
import { BadRequest, Conflict, InternalServerError, NotFound, ServiceUnavailable } from "./errors.js";
/** @since 1.0.0 */
export const ListFilters = /*#__PURE__*/Schema.fromJsonString(/*#__PURE__*/Schema.Struct({
name: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)),
driver: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)),
label: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)),
dangling: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(/*#__PURE__*/Schema.Literals(["true", "false", "1", "0"])))
}));
/** @since 1.0.0 */
export const PruneFilters = /*#__PURE__*/Schema.fromJsonString(/*#__PURE__*/Schema.Struct({
label: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)),
all: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(/*#__PURE__*/Schema.Literals(["true", "false", "1", "0"])))
}));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumeList */
const listVolumesEndpoint = /*#__PURE__*/HttpApiEndpoint.get("list", "/", {
query: {
filters: /*#__PURE__*/Schema.optional(ListFilters)
},
success: /*#__PURE__*/Schema.Struct({
Volumes: /*#__PURE__*/Schema.Array(Volume),
Warnings: /*#__PURE__*/Schema.NullOr(/*#__PURE__*/Schema.Array(Schema.String))
}),
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumeCreate */
const createVolumeEndpoint = /*#__PURE__*/HttpApiEndpoint.post("create", "/create", {
payload: VolumeCreateOptions,
success: /*#__PURE__*/Volume.pipe(/*#__PURE__*/HttpApiSchema.status(201)),
// 201 Created
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumeInspect */
const inspectVolumeEndpoint = /*#__PURE__*/HttpApiEndpoint.get("inspect", "/:name", {
params: {
name: Schema.String
},
success: Volume,
// 200 OK
error: [NotFound,
// 404 Volume not found
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumeDelete */
const deleteVolumeEndpoint = /*#__PURE__*/HttpApiEndpoint.delete("delete", "/:name", {
params: {
name: Schema.String
},
query: {
force: /*#__PURE__*/Schema.optional(Schema.Boolean)
},
success: HttpApiSchema.NoContent,
// 204 No Content
error: [NotFound,
// 404 Volume not found
Conflict,
// 409 Volume is in use
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumeUpdate */
const updateVolumeEndpoint = /*#__PURE__*/HttpApiEndpoint.put("update", "/:name", {
params: {
name: Schema.String
},
query: {
version: Schema.Number
},
payload: ClusterVolumeSpec,
success: /*#__PURE__*/HttpApiSchema.Empty(200),
// 200 OK
error: [BadRequest,
// 400 Bad parameter
NotFound,
// 404 Volume not found
ServiceUnavailable,
// 503 Node is not part of a swarm
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume/operation/VolumePrune */
const pruneVolumeEndpoint = /*#__PURE__*/HttpApiEndpoint.post("prune", "/prune", {
query: {
filters: /*#__PURE__*/Schema.optional(PruneFilters)
},
success: /*#__PURE__*/Schema.Struct({
VolumesDeleted: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(VolumeIdentifier)),
SpaceReclaimed: /*#__PURE__*/Schema.BigIntFromString.check(/*#__PURE__*/Schema.isBetweenBigInt({
minimum: 0n,
maximum: 2n ** 64n - 1n
}))
}),
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume */
const VolumesGroup = /*#__PURE__*/HttpApiGroup.make("volumes").add(listVolumesEndpoint, createVolumeEndpoint, inspectVolumeEndpoint, deleteVolumeEndpoint, updateVolumeEndpoint, pruneVolumeEndpoint).prefix("/volumes");
/**
* @since 1.0.0
* @category HttpApi
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume
*/
export const VolumesApi = /*#__PURE__*/HttpApi.make("VolumesApi").add(VolumesGroup);
/**
* @since 1.0.0
* @category Services
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume
*/
export class Volumes extends /*#__PURE__*/Context.Service()("@the-moby-effect/endpoints/Volumes", {
make: /*#__PURE__*/Effect.gen(function* () {
const httpClient = yield* HttpClient.HttpClient;
const VolumesError = DockerError.WrapForModule("volumes");
const client = yield* HttpApiClient.group(VolumesApi, {
group: "volumes",
httpClient
});
const list_ = filters => client.list({
query: {
filters
}
}).pipe(Effect.mapError(VolumesError("list")));
const create_ = options => VolumeCreateOptions.makeEffect(options).pipe(Effect.flatMap(payload => client.create({
payload
})), Effect.mapError(VolumesError("create")));
const inspect_ = name => client.inspect({
params: {
name
}
}).pipe(Effect.mapError(VolumesError("inspect")));
const delete_ = (name, options) => client.delete({
params: {
name
},
query: {
...options
}
}).pipe(Effect.mapError(VolumesError("delete")));
const update_ = (name, version, spec) => ClusterVolumeSpec.makeEffect(spec).pipe(Effect.flatMap(payload => client.update({
params: {
name
},
query: {
version
},
payload
})), Effect.mapError(VolumesError("update")));
const prune_ = filters => client.prune({
query: {
filters
}
}).pipe(Effect.mapError(VolumesError("prune")));
return {
list: list_,
create: create_,
inspect: inspect_,
delete: delete_,
update: update_,
prune: prune_
};
})
}) {}
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume
*/
export const VolumesLayer = /*#__PURE__*/Layer.effect(Volumes, Volumes.make);
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Volume
*/
export const VolumesLayerLocalSocket = /*#__PURE__*/VolumesLayer.pipe(/*#__PURE__*/Layer.provide(/*#__PURE__*/makeAgnosticHttpClientLayer(/*#__PURE__*/MobyConnectionOptions.socket({
socketPath: "/var/run/docker.sock"
}))));
//# sourceMappingURL=volumes.js.map