the-moby-effect
Version:
Moby/Docker API client built using effect-ts
96 lines • 5.91 kB
JavaScript
import * as PlatformError from "@effect/platform/Error";
import * as HttpClient from "@effect/platform/HttpClient";
import * as HttpClientRequest from "@effect/platform/HttpClientRequest";
import * as HttpClientResponse from "@effect/platform/HttpClientResponse";
import * as Effect from "effect/Effect";
import * as Function from "effect/Function";
import * as Option from "effect/Option";
import * as Predicate from "effect/Predicate";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as Tuple from "effect/Tuple";
import { SwarmService, SwarmServiceCreateResponse, SwarmServiceSpec, SwarmServiceUpdateResponse } from "../generated/index.js";
import { maybeAddQueryParameter } from "./common.js";
/**
* @since 1.0.0
* @category Errors
*/
export const ServicesErrorTypeId = /*#__PURE__*/Symbol.for("@the-moby-effect/endpoints/ServicesError");
/**
* @since 1.0.0
* @category Errors
*/
export const isServicesError = u => Predicate.hasProperty(u, ServicesErrorTypeId);
/**
* @since 1.0.0
* @category Errors
*/
export class ServicesError extends /*#__PURE__*/PlatformError.TypeIdError(ServicesErrorTypeId, "ServicesError") {
get message() {
return `${this.method}`;
}
}
/**
* Services are the definitions of tasks to run on a swarm. Swarm mode must be
* enabled for these endpoints to work.
*
* @since 1.0.0
* @category Tags
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Service
*/
export class Services extends /*#__PURE__*/Effect.Service()("@the-moby-effect/endpoints/Services", {
accessors: false,
dependencies: [],
effect: /*#__PURE__*/Effect.gen(function* () {
const defaultClient = yield* HttpClient.HttpClient;
const client = defaultClient.pipe(HttpClient.filterStatusOk);
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceList */
const list_ = options => Function.pipe(HttpClientRequest.get("/services"), maybeAddQueryParameter("filters", Function.pipe(options?.filters, Option.fromNullable, Option.map(JSON.stringify))), maybeAddQueryParameter("status", Option.fromNullable(options?.status)), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Array(SwarmService))), Effect.mapError(cause => new ServicesError({
method: "list",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceCreate */
const create_ = options => Function.pipe(Schema.decode(SwarmServiceSpec)(options.body), Effect.map(body => Tuple.make(HttpClientRequest.post("/services/create"), body)), Effect.flatMap(Function.tupled(HttpClientRequest.schemaBodyJson(SwarmServiceSpec))), Effect.map(HttpClientRequest.setHeader("X-Registry-Auth", "")), Effect.flatMap(client.execute), Effect.flatMap(HttpClientResponse.schemaBodyJson(SwarmServiceCreateResponse)), Effect.mapError(cause => new ServicesError({
method: "create",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceDelete */
const delete_ = id => Function.pipe(HttpClientRequest.del(`/services/${encodeURIComponent(id)}`), client.execute, Effect.asVoid, Effect.mapError(cause => new ServicesError({
method: "delete",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceInspect */
const inspect_ = (id, options) => Function.pipe(HttpClientRequest.get(`/services/${encodeURIComponent(id)}`), maybeAddQueryParameter("insertDefaults", Option.fromNullable(options?.insertDefaults)), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(SwarmService)), Effect.mapError(cause => new ServicesError({
method: "inspect",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceUpdate */
const update_ = (id, options) => Function.pipe(HttpClientRequest.post(`/services/${encodeURIComponent(id)}/update`), HttpClientRequest.setHeader("X-Registry-Auth", ""), maybeAddQueryParameter("version", Option.some(options.version)), maybeAddQueryParameter("registryAuthFrom", Option.fromNullable(options.registryAuthFrom)), maybeAddQueryParameter("rollback", Option.fromNullable(options.rollback)), HttpClientRequest.schemaBodyJson(SwarmServiceSpec)(options.body), Effect.flatMap(client.execute), Effect.flatMap(HttpClientResponse.schemaBodyJson(SwarmServiceUpdateResponse)), Effect.mapError(cause => new ServicesError({
method: "update",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceLogs */
const logs_ = (id, options) => Function.pipe(HttpClientRequest.get(`/services/${encodeURIComponent(id)}/logs`), maybeAddQueryParameter("details", Option.fromNullable(options?.details)), maybeAddQueryParameter("follow", Option.fromNullable(options?.follow)), maybeAddQueryParameter("stdout", Option.fromNullable(options?.stdout)), maybeAddQueryParameter("stderr", Option.fromNullable(options?.stderr)), maybeAddQueryParameter("since", Option.fromNullable(options?.since)), maybeAddQueryParameter("timestamps", Option.fromNullable(options?.timestamps)), maybeAddQueryParameter("tail", Option.fromNullable(options?.tail)), client.execute, HttpClientResponse.stream, Stream.decodeText(), Stream.mapError(cause => new ServicesError({
method: "logs",
cause
})));
return {
list: list_,
create: create_,
delete: delete_,
inspect: inspect_,
update: update_,
logs: logs_
};
})
}) {}
/**
* Services are the definitions of tasks to run on a swarm. Swarm mode must be
* enabled for these endpoints to work.
*
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Service
*/
export const ServicesLayer = Services.Default;
//# sourceMappingURL=services.js.map