UNPKG

the-moby-effect

Version:
232 lines 8.84 kB
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 { SwarmService, SwarmServiceSpec } from "../generated/index.js"; import { ServiceIdentifier } from "../schemas/id.js"; import { WithRegistryAuthHeader } from "./auth.js"; import { DockerError } from "./circular.js"; import { BadRequest, Conflict, Forbidden, InternalServerError, NotFound, ServiceUnavailable } from "./errors.js"; /** @since 1.0.0 */ export const ListFilters = /*#__PURE__*/Schema.fromJsonString(/*#__PURE__*/Schema.Struct({ id: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)), label: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)), mode: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(/*#__PURE__*/Schema.Literals(["replicated", "global"]))) })); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceList */ const listServicesEndpoint = /*#__PURE__*/HttpApiEndpoint.get("list", "/", { query: { filters: /*#__PURE__*/Schema.optional(ListFilters), status: /*#__PURE__*/Schema.optional(Schema.Boolean) }, success: /*#__PURE__*/Schema.Array(SwarmService), // 200 OK error: [ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceCreate */ const createServiceEndpoint = /*#__PURE__*/HttpApiEndpoint.post("create", "/create", { payload: SwarmServiceSpec, headers: { "X-Registry-Auth": /*#__PURE__*/Schema.optional(Schema.String) }, success: /*#__PURE__*/Schema.Struct({ ID: ServiceIdentifier, Warnings: Schema.optional(Schema.Array(Schema.String)) }).pipe(/*#__PURE__*/HttpApiSchema.status(201)), // 201 Created error: [BadRequest, // 400 Bad request Forbidden, // 403 network is not eligible for services Conflict, // 409 name conflicts with an existing object ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceDelete */ const deleteServiceEndpoint = /*#__PURE__*/HttpApiEndpoint.delete("delete", "/:id", { params: { id: Schema.String }, success: /*#__PURE__*/HttpApiSchema.Empty(200), // 200 OK error: [NotFound, // 404 No such service ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceInspect */ const inspectServiceEndpoint = /*#__PURE__*/HttpApiEndpoint.get("inspect", "/:id", { params: { id: Schema.String }, query: { insertDefaults: /*#__PURE__*/Schema.optional(Schema.Boolean) }, success: SwarmService, // 200 OK error: [NotFound, // 404 No such service ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceUpdate */ const updateServiceEndpoint = /*#__PURE__*/HttpApiEndpoint.post("update", "/:id/update", { params: { id: Schema.String }, query: { version: Schema.Number, rollback: /*#__PURE__*/Schema.optional(Schema.String), registryAuthFrom: /*#__PURE__*/Schema.optional(Schema.String) }, headers: { "X-Registry-Auth": /*#__PURE__*/Schema.optional(Schema.String) }, payload: SwarmServiceSpec, success: /*#__PURE__*/Schema.Struct({ Warnings: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)) }), // 200 OK error: [BadRequest, // 400 Bad request NotFound, // 404 No such service ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service/operation/ServiceLogs */ const logsServiceEndpoint = /*#__PURE__*/HttpApiEndpoint.get("logs", "/:id/logs", { params: { id: Schema.String }, query: { details: /*#__PURE__*/Schema.optional(Schema.Boolean), follow: /*#__PURE__*/Schema.optional(Schema.Boolean), stdout: /*#__PURE__*/Schema.optional(Schema.Boolean), stderr: /*#__PURE__*/Schema.optional(Schema.Boolean), since: /*#__PURE__*/Schema.optional(Schema.Number), timestamps: /*#__PURE__*/Schema.optional(Schema.Boolean), tail: /*#__PURE__*/Schema.optional(Schema.String) }, success: /*#__PURE__*/HttpApiSchema.StreamUint8Array(), // 200 OK (streaming response) error: [NotFound, // 404 No such service ServiceUnavailable, // 503 Node is not part of a swarm InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Service */ const ServicesGroup = /*#__PURE__*/HttpApiGroup.make("services").add(listServicesEndpoint, createServiceEndpoint, deleteServiceEndpoint, inspectServiceEndpoint, updateServiceEndpoint, logsServiceEndpoint).prefix("/services"); /** * 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 HttpApi * @see https://docs.docker.com/reference/api/engine/latest/#tag/Service */ export const ServicesApi = /*#__PURE__*/HttpApi.make("ServicesApi").add(ServicesGroup); /** * 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 Services * @see https://docs.docker.com/reference/api/engine/latest/#tag/Service */ export class Services extends /*#__PURE__*/Context.Service()("@the-moby-effect/endpoints/Services", { make: /*#__PURE__*/Effect.gen(function* () { const httpClient = yield* Effect.map(HttpClient.HttpClient, WithRegistryAuthHeader(createServiceEndpoint, updateServiceEndpoint)); const ServicesError = DockerError.WrapForModule("services"); const client = yield* HttpApiClient.group(ServicesApi, { group: "services", httpClient }); const list_ = options => client.list({ query: { ...options } }).pipe(Effect.mapError(ServicesError("list"))); const create_ = payload => SwarmServiceSpec.makeEffect(payload).pipe(Effect.flatMap(payload => client.create({ headers: {}, payload })), Effect.mapError(ServicesError("create"))); const delete_ = id => client.delete({ params: { id } }).pipe(Effect.mapError(ServicesError("delete"))); const inspect_ = (id, options) => client.inspect({ params: { id }, query: { ...options } }).pipe(Effect.mapError(ServicesError("inspect"))); const update_ = (id, options, payload) => SwarmServiceSpec.makeEffect(payload).pipe(Effect.flatMap(payload => client.update({ headers: {}, params: { id }, query: { ...options }, payload })), Effect.mapError(ServicesError("update"))); const logs_ = (id, options) => client.logs({ params: { id }, query: { ...options } }).pipe(Effect.map(Stream.decodeText()), Effect.map(Stream.splitLines), Effect.map(Stream.mapError(ServicesError("logs"))), Effect.mapError(ServicesError("logs"))); 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 = /*#__PURE__*/Layer.effect(Services, Services.make); /** * 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 ServicesLayerLocalSocket = /*#__PURE__*/ServicesLayer.pipe(/*#__PURE__*/Layer.provide(/*#__PURE__*/makeAgnosticHttpClientLayer(/*#__PURE__*/MobyConnectionOptions.socket({ socketPath: "/var/run/docker.sock" })))); //# sourceMappingURL=services.js.map