the-moby-effect
Version:
Moby/Docker API client built using effect-ts
241 lines (240 loc) • 19.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isContainersError = exports.ContainersLayer = exports.ContainersErrorTypeId = exports.ContainersError = exports.Containers = void 0;
var PlatformError = _interopRequireWildcard(require("@effect/platform/Error"));
var HttpClient = _interopRequireWildcard(require("@effect/platform/HttpClient"));
var HttpClientRequest = _interopRequireWildcard(require("@effect/platform/HttpClientRequest"));
var HttpClientResponse = _interopRequireWildcard(require("@effect/platform/HttpClientResponse"));
var Socket = _interopRequireWildcard(require("@effect/platform/Socket"));
var Effect = _interopRequireWildcard(require("effect/Effect"));
var Function = _interopRequireWildcard(require("effect/Function"));
var Option = _interopRequireWildcard(require("effect/Option"));
var Predicate = _interopRequireWildcard(require("effect/Predicate"));
var Schema = _interopRequireWildcard(require("effect/Schema"));
var Stream = _interopRequireWildcard(require("effect/Stream"));
var Tuple = _interopRequireWildcard(require("effect/Tuple"));
var MobyDemux = _interopRequireWildcard(require("../../MobyDemux.js"));
var _index = require("../generated/index.js");
var _agnostic = require("../platforms/agnostic.js");
var _common = require("./common.js");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* @since 1.0.0
* @category Errors
*/
const ContainersErrorTypeId = exports.ContainersErrorTypeId = /*#__PURE__*/Symbol.for("@the-moby-effect/endpoints/ContainersError");
/**
* @since 1.0.0
* @category Errors
*/
const isContainersError = u => Predicate.hasProperty(u, ContainersErrorTypeId);
/**
* @since 1.0.0
* @category Errors
*/
exports.isContainersError = isContainersError;
class ContainersError extends /*#__PURE__*/PlatformError.TypeIdError(ContainersErrorTypeId, "ContainersError") {
get message() {
return `${this.method}`;
}
}
/**
* @since 1.0.0
* @category Services
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Container
*/
exports.ContainersError = ContainersError;
class Containers extends /*#__PURE__*/Effect.Service()("@the-moby-effect/endpoints/Containers", {
accessors: false,
dependencies: [],
effect: /*#__PURE__*/Effect.gen(function* () {
const contextClient = yield* HttpClient.HttpClient;
const client = contextClient.pipe(HttpClient.filterStatusOk);
const maybeUpgradedClient = contextClient.pipe(HttpClient.filterStatus(status => status >= 200 && status < 300 || status === 101));
const websocketConstructor = yield* Socket.WebSocketConstructor;
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerList */
const list_ = options => Function.pipe(HttpClientRequest.get("/containers/json"), (0, _common.maybeAddQueryParameter)("all", Option.fromNullable(options?.all)), (0, _common.maybeAddQueryParameter)("limit", Option.fromNullable(options?.limit)), (0, _common.maybeAddQueryParameter)("size", Option.fromNullable(options?.size)), (0, _common.maybeAddFilters)(options?.filters), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Array(_index.ContainerListResponseItem))), Effect.mapError(cause => new ContainersError({
method: "list",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerCreate */
const create_ = options => Function.pipe(Schema.decode(_index.ContainerCreateRequest)(options.spec), Effect.map(body => Tuple.make(HttpClientRequest.post("/containers/create"), body)), Effect.map(Tuple.mapFirst((0, _common.maybeAddQueryParameter)("name", Option.fromNullable(options.name)))), Effect.map(Tuple.mapFirst((0, _common.maybeAddQueryParameter)("platform", Option.fromNullable(options.platform)))), Effect.flatMap(Function.tupled(HttpClientRequest.schemaBodyJson(_index.ContainerCreateRequest))), Effect.flatMap(client.execute), Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerCreateResponse)), Effect.mapError(cause => new ContainersError({
method: "create",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerInspect */
const inspect_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/json`), (0, _common.maybeAddQueryParameter)("size", Option.fromNullable(options?.size)), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerInspectResponse)), Effect.mapError(cause => new ContainersError({
method: "inspect",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerTop */
const top_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/top`), (0, _common.maybeAddQueryParameter)("ps_args", Option.fromNullable(options?.ps_args)), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerTopResponse)), Effect.mapError(cause => new ContainersError({
method: "top",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerLogs */
const logs_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/logs`), (0, _common.maybeAddQueryParameter)("follow", Option.fromNullable(options?.follow)), (0, _common.maybeAddQueryParameter)("stdout", Option.fromNullable(options?.stdout)), (0, _common.maybeAddQueryParameter)("stderr", Option.fromNullable(options?.stderr)), (0, _common.maybeAddQueryParameter)("since", Option.fromNullable(options?.since)), (0, _common.maybeAddQueryParameter)("until", Option.fromNullable(options?.until)), (0, _common.maybeAddQueryParameter)("timestamps", Option.fromNullable(options?.timestamps)), (0, _common.maybeAddQueryParameter)("tail", Option.fromNullable(options?.tail)), client.execute, HttpClientResponse.stream, Stream.decodeText(), Stream.mapError(cause => new ContainersError({
method: "logs",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerChanges */
const changes_ = id => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/changes`), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.NullOr(Schema.Array(_index.ContainerChange)))), Effect.mapError(cause => new ContainersError({
method: "changes",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerExport */
const export_ = id => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/export`), client.execute, HttpClientResponse.stream, Stream.mapError(cause => new ContainersError({
method: "export",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerStats */
const stats_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/stats`), (0, _common.maybeAddQueryParameter)("stream", Option.fromNullable(options?.stream)), (0, _common.maybeAddQueryParameter)("one-shot", Option.fromNullable(options?.["one-shot"])), client.execute, HttpClientResponse.stream, Stream.decodeText(), Stream.mapEffect(Schema.decode(Schema.parseJson(_index.ContainerStatsResponse))), Stream.mapError(cause => new ContainersError({
method: "stats",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerResize */
const resize_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/resize`), (0, _common.maybeAddQueryParameter)("h", Option.fromNullable(options?.h)), (0, _common.maybeAddQueryParameter)("w", Option.fromNullable(options?.w)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "resize",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerStart */
const start_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/start`), (0, _common.maybeAddQueryParameter)("detachKeys", Option.fromNullable(options?.detachKeys)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "start",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerStop */
const stop_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/stop`), (0, _common.maybeAddQueryParameter)("signal", Option.fromNullable(options?.signal)), (0, _common.maybeAddQueryParameter)("t", Option.fromNullable(options?.t)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "stop",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerRestart */
const restart_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/restart`), (0, _common.maybeAddQueryParameter)("signal", Option.fromNullable(options?.signal)), (0, _common.maybeAddQueryParameter)("t", Option.fromNullable(options?.t)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "restart",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerKill */
const kill_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/kill`), (0, _common.maybeAddQueryParameter)("signal", Option.fromNullable(options?.signal)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "kill",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerUpdate */
const update_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/update`), HttpClientRequest.schemaBodyJson(_index.ContainerConfig)(options.spec), Effect.flatMap(client.execute), Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerUpdateResponse)), Effect.mapError(cause => new ContainersError({
method: "update",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerRename */
const rename_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/rename`), (0, _common.maybeAddQueryParameter)("name", Option.fromNullable(options.name)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "rename",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerPause */
const pause_ = id => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/pause`), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "pause",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerUnpause */
const unpause_ = id => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/unpause`), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "unpause",
cause
})));
/**
* Attach to a container
*
* @param options.id - ID or name of the container
* @param detachKeys - Override the key sequence for detaching a
* container.Format is a single character `[a-Z]` or `ctrl-<value>`
* where `<value>` is one of: `a-z`, `@`, `^`, `[`, `,` or `_`.
* @param logs - Replay previous logs from the container.
*
* This is useful for attaching to a container that has started and you
* want to output everything since the container started.
*
* If `stream` is also enabled, once all the previous output has been
* returned, it will seamlessly transition into streaming current
* output.
* @param stream - Stream attached streams from the time the request was
* made onwards.
* @param stdin - Attach to `stdin`
* @param stdout - Attach to `stdout`
* @param stderr - Attach to `stderr`
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerAttach
*/
const attach_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/attach`), HttpClientRequest.setHeader("Upgrade", "tcp"), HttpClientRequest.setHeader("Connection", "Upgrade"), (0, _common.maybeAddQueryParameter)("detachKeys", Option.fromNullable(options?.detachKeys)), (0, _common.maybeAddQueryParameter)("logs", Option.fromNullable(options?.logs)), (0, _common.maybeAddQueryParameter)("stream", Option.fromNullable(options?.stream)), (0, _common.maybeAddQueryParameter)("stdin", Option.fromNullable(options?.stdin)), (0, _common.maybeAddQueryParameter)("stdout", Option.fromNullable(options?.stdout)), (0, _common.maybeAddQueryParameter)("stderr", Option.fromNullable(options?.stderr)), maybeUpgradedClient.execute, Effect.flatMap(MobyDemux.responseToStreamingSocketOrFailUnsafe), Effect.mapError(cause => new ContainersError({
method: "attach",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerAttachWebsocket */
const attachWebsocket_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/attach/ws`), (0, _common.maybeAddQueryParameter)("detachKeys", Option.fromNullable(options?.detachKeys)), (0, _common.maybeAddQueryParameter)("logs", Option.fromNullable(options?.logs)), (0, _common.maybeAddQueryParameter)("stream", Option.fromNullable(options?.stream)), (0, _common.maybeAddQueryParameter)("stdin", Option.fromNullable(options?.stdin)), (0, _common.maybeAddQueryParameter)("stdout", Option.fromNullable(options?.stdout)), (0, _common.maybeAddQueryParameter)("stderr", Option.fromNullable(options?.stderr)), _agnostic.websocketRequest, Effect.map(MobyDemux.makeRawSocket), Effect.provideService(HttpClient.HttpClient, contextClient), Effect.provideService(Socket.WebSocketConstructor, websocketConstructor), Effect.mapError(cause => new ContainersError({
method: "attachWebsocket",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerWait */
const wait_ = (id, options) => Function.pipe(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/wait`), (0, _common.maybeAddQueryParameter)("condition", Option.fromNullable(options?.condition)), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerWaitResponse)), Effect.mapError(cause => new ContainersError({
method: "wait",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerDelete */
const delete_ = (id, options) => Function.pipe(HttpClientRequest.del(`/containers/${encodeURIComponent(id)}`), (0, _common.maybeAddQueryParameter)("v", Option.fromNullable(options?.v)), (0, _common.maybeAddQueryParameter)("force", Option.fromNullable(options?.force)), (0, _common.maybeAddQueryParameter)("link", Option.fromNullable(options?.link)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "delete",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerArchive */
const archive_ = (id, options) => Function.pipe(HttpClientRequest.get(`/containers/${encodeURIComponent(id)}/archive`), (0, _common.maybeAddQueryParameter)("path", Option.some(options.path)), client.execute, HttpClientResponse.stream, Stream.mapError(cause => new ContainersError({
method: "archive",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerArchiveInfo */
const archiveInfo_ = (id, options) => Function.pipe(HttpClientRequest.head(`/containers/${encodeURIComponent(id)}/archive`), (0, _common.maybeAddQueryParameter)("path", Option.some(options.path)), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "archiveInfo",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/PutContainerArchive */
const putArchive_ = (id, options) => Function.pipe(HttpClientRequest.put(`/containers/${encodeURIComponent(id)}/archive`), (0, _common.maybeAddQueryParameter)("path", Option.some(options.path)), (0, _common.maybeAddQueryParameter)("noOverwriteDirNonDir", Option.fromNullable(options.noOverwriteDirNonDir)), (0, _common.maybeAddQueryParameter)("copyUIDGID", Option.fromNullable(options.copyUIDGID)), HttpClientRequest.bodyStream(options.stream), client.execute, Effect.asVoid, Effect.mapError(cause => new ContainersError({
method: "putArchive",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Container/operation/ContainerPrune */
const prune_ = options => Function.pipe(HttpClientRequest.post("/containers/prune"), (0, _common.maybeAddFilters)(options?.filters), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerPruneResponse)), Effect.mapError(cause => new ContainersError({
method: "prune",
cause
})));
return {
list: list_,
create: create_,
inspect: inspect_,
top: top_,
logs: logs_,
changes: changes_,
export: export_,
stats: stats_,
resize: resize_,
start: start_,
stop: stop_,
restart: restart_,
kill: kill_,
update: update_,
rename: rename_,
pause: pause_,
unpause: unpause_,
attach: attach_,
attachWebsocket: attachWebsocket_,
wait: wait_,
delete: delete_,
archive: archive_,
archiveInfo: archiveInfo_,
putArchive: putArchive_,
prune: prune_
};
})
}) {}
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Container
*/
exports.Containers = Containers;
const ContainersLayer = exports.ContainersLayer = Containers.Default;
//# sourceMappingURL=containers.js.map