| 1 | import { CreateTaxSettings } 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 { |
| 7 | attachStripeToken, |
| 8 | authorizeWith, |
| 9 | resolveStripeAuth, |
| 10 | } from "./StripeHttp.ts"; |
| 11 | import type { TaxSettings } from "./TaxSettings.ts"; |
| 12 | import { |
| 13 | UpdateTaxSettings, |
| 14 | type UpdateTaxSettingsRequest, |
| 15 | } from "./UpdateTaxSettings.ts"; |
| 16 | |
| 17 | /** |
| 18 | * HTTP implementation of {@link UpdateTaxSettings}. 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.UpdateTaxSettings |
| 25 | */ |
| 26 | export const UpdateTaxSettingsHttp = Layer.effect( |
| 27 | UpdateTaxSettings, |
| 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_write"], |
| 36 | "Stripe.UpdateTaxSettings", |
| 37 | ); |
| 38 | const auth = |
| 39 | host !== undefined ? authorizeWith(bound) : ambient.authorize; |
| 40 | return Effect.fn(`Stripe.UpdateTaxSettings(${settings.LogicalId})`)( |
| 41 | function* (request?: UpdateTaxSettingsRequest) { |
| 42 | return yield* auth(CreateTaxSettings(request ?? {})); |
| 43 | }, |
| 44 | ); |
| 45 | }); |
| 46 | }), |
| 47 | ); |