| 1 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import type { Schema } from "./Schema.ts"; |
| 5 | import { bindGcpHost } from "../Host.ts"; |
| 6 | import { |
| 7 | ValidateMessage, |
| 8 | type ValidateMessageRequest, |
| 9 | } from "./ValidateMessage.ts"; |
| 10 | |
| 11 | /** |
| 12 | * HTTP implementation of {@link ValidateMessage}. |
| 13 | * |
| 14 | * @layer |
| 15 | * @provides GCP.PubSub.ValidateMessage |
| 16 | */ |
| 17 | export const ValidateMessageHttp = Layer.effect( |
| 18 | ValidateMessage, |
| 19 | Effect.gen(function* () { |
| 20 | const validate = yield* pubsub.validateMessageProjectsSchemas; |
| 21 | return Effect.fn(function* (schema: Schema) { |
| 22 | yield* bindGcpHost({ |
| 23 | tag: "GCP.PubSub.ValidateMessage", |
| 24 | resource: schema, |
| 25 | // schemas.validate is checked on the parent project, not the schema. |
| 26 | iam: [{ role: "roles/pubsub.viewer" }], |
| 27 | }); |
| 28 | const name = yield* schema.name; |
| 29 | const project = yield* schema.project; |
| 30 | return Effect.fn(`GCP.PubSub.ValidateMessage(${schema.LogicalId})`)( |
| 31 | function* (request: ValidateMessageRequest) { |
| 32 | return yield* validate({ |
| 33 | parent: `projects/${yield* project}`, |
| 34 | body: { |
| 35 | ...request, |
| 36 | name: yield* name, |
| 37 | }, |
| 38 | }); |
| 39 | }, |
| 40 | ); |
| 41 | }); |
| 42 | }), |
| 43 | ); |