1import { GetAccountExternalAccount } from "@distilled.cloud/stripe/stripe";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../Binding.ts";
5import type { ResourceLike } from "../Resource.ts";
6import type { AccountExternalAccount } from "./AccountExternalAccount.ts";
7import { RetrieveAccountExternalAccount } from "./RetrieveAccountExternalAccount.ts";
8import {
9 asStringEffect,
10 attachStripeToken,
11 authorizeWith,
12 resolveStripeAuth,
13} from "./StripeHttp.ts";
14
15/**
16 * HTTP implementation of {@link RetrieveAccountExternalAccount}. The
17 * nested retrieve takes both `account` and `id`.
18 *
19 * @layer
20 * @product Account
21 * @provides Stripe.RetrieveAccountExternalAccount
22 */
23export const RetrieveAccountExternalAccountHttp = Layer.effect(
24 RetrieveAccountExternalAccount,
25 Effect.gen(function* () {
26 const ambient = yield* resolveStripeAuth;
27
28 return Effect.fn(function* (externalAccount: AccountExternalAccount) {
29 const host = yield* Binding.Host;
30 const bound = yield* attachStripeToken(
31 externalAccount as unknown as ResourceLike,
32 ["accounts_read"],
33 "Stripe.RetrieveAccountExternalAccount",
34 );
35 const id = yield* asStringEffect(externalAccount.id);
36 const account = yield* asStringEffect(externalAccount.account);
37 const auth =
38 host !== undefined ? authorizeWith(bound) : ambient.authorize;
39
40 return Effect.fn(
41 `Stripe.RetrieveAccountExternalAccount(${externalAccount.LogicalId})`,
42 )(function* (request?: { expand?: string[] }) {
43 return yield* auth(
44 GetAccountExternalAccount({
45 ...(request ?? {}),
46 id: yield* id,
47 account: yield* account,
48 }),
49 );
50 });
51 });
52 }),
53);