| 1 | import * as amplify from "@distilled.cloud/aws/amplify"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import type { App } from "./App.ts"; |
| 8 | import { |
| 9 | GetArtifactUrl, |
| 10 | type GetArtifactUrlRequest, |
| 11 | } from "./GetArtifactUrl.ts"; |
| 12 | |
| 13 | // Bespoke (not via makeAmplifyHttpBinding): GetArtifactUrl addresses the |
| 14 | // artifact by its globally-unique `artifactId` alone — there is no `appId` |
| 15 | // to inject. The app is still bound for the IAM grant over its artifacts. |
| 16 | export const GetArtifactUrlHttp = Layer.effect( |
| 17 | GetArtifactUrl, |
| 18 | Effect.gen(function* () { |
| 19 | const getArtifactUrl = yield* amplify.getArtifactUrl; |
| 20 | |
| 21 | return Effect.fn(function* (app: App) { |
| 22 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 23 | const host = yield* Binding.Host; |
| 24 | if (isBindingHost(host)) { |
| 25 | yield* host.bind`Allow(${host}, AWS.Amplify.GetArtifactUrl(${app}))`({ |
| 26 | policyStatements: [ |
| 27 | { |
| 28 | Effect: "Allow", |
| 29 | Action: ["amplify:GetArtifactUrl"], |
| 30 | Resource: [ |
| 31 | Output.interpolate`${app.appArn}/branches/*/jobs/*/artifacts/*`, |
| 32 | ], |
| 33 | }, |
| 34 | ], |
| 35 | }); |
| 36 | } |
| 37 | } |
| 38 | return Effect.fn(`AWS.Amplify.GetArtifactUrl(${app.LogicalId})`)( |
| 39 | function* (request: GetArtifactUrlRequest) { |
| 40 | return yield* getArtifactUrl(request); |
| 41 | }, |
| 42 | ); |
| 43 | }); |
| 44 | }), |
| 45 | ); |