1import { GetTaxSettings } 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 {
7 RetrieveTaxSettings,
8 type RetrieveTaxSettingsRequest,
9} from "./RetrieveTaxSettings.ts";
10import {
11 attachStripeToken,
12 authorizeWith,
13 resolveStripeAuth,
14} from "./StripeHttp.ts";
15import type { TaxSettings } from "./TaxSettings.ts";
16
17/**
18 * HTTP implementation of {@link RetrieveTaxSettings}. Provide it on the
19 * Function or Worker Effect. Tax Settings has no Stripe id — only the API
20 * key is bound.
21 *
22 * @layer
23 * @product Tax
24 * @provides Stripe.RetrieveTaxSettings
25 */
26export const RetrieveTaxSettingsHttp = Layer.effect(
27 RetrieveTaxSettings,
28 Effect.gen(function* () {
29 const ambient = yield* resolveStripeAuth;
30
31 return Effect.fn(function* (settings: TaxSettings) {
32 const host = yield* Binding.Host;
33 const bound = yield* attachStripeToken(
34 settings as unknown as ResourceLike,
35 ["tax_read"],
36 "Stripe.RetrieveTaxSettings",
37 );
38 const auth =
39 host !== undefined ? authorizeWith(bound) : ambient.authorize;
40 return Effect.fn(`Stripe.RetrieveTaxSettings(${settings.LogicalId})`)(
41 function* (request?: RetrieveTaxSettingsRequest) {
42 return yield* auth(GetTaxSettings(request ?? {}));
43 },
44 );
45 });
46 }),
47);