1import * as aoss from "@distilled.cloud/aws/opensearchserverless";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import * as Output from "../../Output.ts";
6import { isBindingHost } from "../Lambda/Function.ts";
7import type { Collection } from "./Collection.ts";
8import { GetCollection } from "./GetCollection.ts";
9
10// Bespoke (not the shared scaffolding): BatchGetCollection takes an `ids`
11// array rather than a single injected `id`, and the callable unwraps the
12// batch response to the bound collection's detail.
13export const GetCollectionHttp = Layer.effect(
14 GetCollection,
15 Effect.gen(function* () {
16 const op = yield* aoss.batchGetCollection;
17
18 return Effect.fn(function* (collection: Collection) {
19 const CollectionId = yield* collection.collectionId;
20 if (!globalThis.__ALCHEMY_RUNTIME__) {
21 const host = yield* Binding.Host;
22 if (isBindingHost(host)) {
23 yield* host.bind`Allow(${host}, AWS.OpenSearchServerless.GetCollection(${collection}))`(
24 {
25 policyStatements: [
26 {
27 Effect: "Allow",
28 Action: ["aoss:BatchGetCollection"],
29 Resource: [Output.interpolate`${collection.collectionArn}`],
30 },
31 ],
32 },
33 );
34 }
35 }
36 return Effect.fn(
37 `AWS.OpenSearchServerless.GetCollection(${collection.LogicalId})`,
38 )(function* () {
39 const response = yield* op({ ids: [yield* CollectionId] });
40 return response.collectionDetails?.[0];
41 });
42 });
43 }),
44);