the-moby-effect
Version:
Moby/Docker API client built using effect-ts
139 lines • 6.13 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 Stream from "effect/Stream";
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 { RegistryAuthenticateOKBody as AuthResponse, TypesDiskUsage as DiskUsage, RegistryAuthConfig, SystemInfo, TypesVersion as SystemVersion } from "../generated/index.js";
import { DockerError } from "./circular.js";
import { BadRequest, InternalServerError, Unauthorized } from "./errors.js";
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemAuth */
const authEndpoint = /*#__PURE__*/HttpApiEndpoint.post("auth", "/auth", {
payload: RegistryAuthConfig,
success: [AuthResponse,
/*#__PURE__*/
// 200 OK
HttpApiSchema.Empty(204) // 204 No Content
],
error: [Unauthorized,
// 401 Unauthorized
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemInfo */
const infoEndpoint = /*#__PURE__*/HttpApiEndpoint.get("info", "/info", {
success: SystemInfo,
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemVersion */
const versionEndpoint = /*#__PURE__*/HttpApiEndpoint.get("version", "/version", {
success: SystemVersion,
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemPing */
const pingEndpoint = /*#__PURE__*/HttpApiEndpoint.get("ping", "/_ping", {
success: /*#__PURE__*/Schema.Literal("OK").pipe(/*#__PURE__*/HttpApiSchema.asText({
contentType: "text/plain"
})),
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemPingHead */
const pingHeadEndpoint = /*#__PURE__*/HttpApiEndpoint.head("pingHead", "/_ping", {
success: /*#__PURE__*/HttpApiSchema.Empty(200),
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemEvents */
const eventsEndpoint = /*#__PURE__*/HttpApiEndpoint.get("events", "/events", {
query: {
since: /*#__PURE__*/Schema.optional(Schema.String),
until: /*#__PURE__*/Schema.optional(Schema.String)
},
success: /*#__PURE__*/HttpApiSchema.StreamUint8Array(),
// 200 OK (streaming response)
error: [BadRequest,
// 400 Bad request
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/System/operation/SystemDataUsage */
const dataUsageEndpoint = /*#__PURE__*/HttpApiEndpoint.get("dataUsage", "/system/df", {
query: {
type: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(/*#__PURE__*/Schema.Literals(["container", "image", "volume", "build-cache"])))
},
success: DiskUsage,
// 200 OK
error: [InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/version/v1.51/#tag/System */
const SystemGroup = /*#__PURE__*/HttpApiGroup.make("system").add(authEndpoint, infoEndpoint, versionEndpoint, pingEndpoint, pingHeadEndpoint, eventsEndpoint, dataUsageEndpoint);
/**
* @since 1.0.0
* @category HttpApi
* @see https://docs.docker.com/reference/api/engine/version/v1.51/#tag/System
*/
export const SystemApi = /*#__PURE__*/HttpApi.make("SystemApi").add(SystemGroup);
/**
* @since 1.0.0
* @category Services
* @see https://docs.docker.com/reference/api/engine/version/v1.51/#tag/System
*/
export class System extends /*#__PURE__*/Context.Service()("@the-moby-effect/endpoints/System", {
make: /*#__PURE__*/Effect.gen(function* () {
const httpClient = yield* HttpClient.HttpClient;
const SystemsError = DockerError.WrapForModule("system");
const client = yield* HttpApiClient.group(SystemApi, {
group: "system",
httpClient
});
const auth_ = payload => RegistryAuthConfig.makeEffect(payload).pipe(Effect.flatMap(payload => client.auth({
payload
})), Effect.mapError(SystemsError("auth")));
const info_ = () => client.info().pipe(Effect.mapError(SystemsError("info")));
const version_ = () => client.version().pipe(Effect.mapError(SystemsError("version")));
const ping_ = () => client.ping().pipe(Effect.mapError(SystemsError("ping")));
const pingHead_ = () => client.pingHead().pipe(Effect.mapError(SystemsError("pingHead")));
const events_ = options => client.events({
query: {
...options
}
}).pipe(Stream.unwrap, Stream.decodeText(), Stream.splitLines, Stream.mapEffect(line => Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown))(line)), Stream.mapError(SystemsError("events")));
const dataUsage_ = params => client.dataUsage({
query: {
...params
}
}).pipe(Effect.mapError(SystemsError("dataUsage")));
return {
auth: auth_,
info: info_,
version: version_,
ping: ping_,
pingHead: pingHead_,
events: events_,
dataUsage: dataUsage_
};
})
}) {}
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/version/v1.51/#tag/System
*/
export const SystemLayer = /*#__PURE__*/Layer.effect(System, System.make);
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/version/v1.51/#tag/System
*/
export const SystemLayerLocalSocket = /*#__PURE__*/SystemLayer.pipe(/*#__PURE__*/Layer.provide(/*#__PURE__*/makeAgnosticHttpClientLayer(/*#__PURE__*/MobyConnectionOptions.socket({
socketPath: "/var/run/docker.sock"
}))));
//# sourceMappingURL=system.js.map