| 1 | import { GetAppsSecretsFind } from "@distilled.cloud/stripe/stripe"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../Binding.ts"; |
| 5 | import type { ResourceLike } from "../Resource.ts"; |
| 6 | import type { AppsSecret, AppsSecretScopeType } from "./AppsSecret.ts"; |
| 7 | import { RetrieveAppsSecret } from "./RetrieveAppsSecret.ts"; |
| 8 | import { |
| 9 | asOptionalStringEffect, |
| 10 | asStringEffect, |
| 11 | attachStripeToken, |
| 12 | authorizeWith, |
| 13 | resolveStripeAuth, |
| 14 | } from "./StripeHttp.ts"; |
| 15 | |
| 16 | /** |
| 17 | * HTTP implementation of {@link RetrieveAppsSecret}. Find is keyed by |
| 18 | * `name` and `scope`. |
| 19 | * |
| 20 | * @layer |
| 21 | * @product Secrets |
| 22 | * @provides Stripe.RetrieveAppsSecret |
| 23 | */ |
| 24 | export const RetrieveAppsSecretHttp = Layer.effect( |
| 25 | RetrieveAppsSecret, |
| 26 | Effect.gen(function* () { |
| 27 | const ambient = yield* resolveStripeAuth; |
| 28 | |
| 29 | return Effect.fn(function* (secret: AppsSecret) { |
| 30 | const host = yield* Binding.Host; |
| 31 | const bound = yield* attachStripeToken( |
| 32 | secret as unknown as ResourceLike, |
| 33 | ["apps_secrets_read"], |
| 34 | "Stripe.RetrieveAppsSecret", |
| 35 | ); |
| 36 | const name = yield* asStringEffect(secret.name); |
| 37 | const scopeType = yield* asStringEffect(secret.scope.type); |
| 38 | const scopeUser = yield* asOptionalStringEffect(secret.scope.user); |
| 39 | const auth = |
| 40 | host !== undefined ? authorizeWith(bound) : ambient.authorize; |
| 41 | |
| 42 | return Effect.fn(`Stripe.RetrieveAppsSecret(${secret.LogicalId})`)( |
| 43 | function* (request?: { expand?: string[] }) { |
| 44 | const user = yield* scopeUser; |
| 45 | return yield* auth( |
| 46 | GetAppsSecretsFind({ |
| 47 | ...(request ?? {}), |
| 48 | name: yield* name, |
| 49 | scope: { |
| 50 | type: (yield* scopeType) as AppsSecretScopeType, |
| 51 | ...(user !== undefined && user.length > 0 ? { user } : {}), |
| 52 | }, |
| 53 | }), |
| 54 | ); |
| 55 | }, |
| 56 | ); |
| 57 | }); |
| 58 | }), |
| 59 | ); |