diff --git a/src/chargebee-resource-wrapper.class.ts b/src/chargebee-resource-wrapper.class.ts index 5c7fe37..685d4ec 100644 --- a/src/chargebee-resource-wrapper.class.ts +++ b/src/chargebee-resource-wrapper.class.ts @@ -1,5 +1,3 @@ -import type { ChargeBee } from "chargebee-typescript"; - import { ItemResource } from "./resources/item-resource"; import { AddonResource } from "./resources/addon-resource"; import { AddressResource } from "./resources/address-resource"; @@ -43,51 +41,52 @@ import { UsageResource } from "./resources/usage-resource"; import { VirtualBankAccountResource } from "./resources/virtual-bank-account-resource"; import { SubscriptionResource } from "./resources/subscription-resource"; import { DownloadResource } from "./resources/download.resource"; +import { ChargebeeModuleOptions } from "./chargebee.interface"; export class ChargebeeResourceWrapper { - constructor(private readonly client: ChargeBee) {} + constructor(private readonly options: ChargebeeModuleOptions) {} - subscription = new SubscriptionResource(this.client); - customer = new CustomerResource(this.client); - paymentSource = new PaymentSourceResource(this.client); - virtualBankAccount = new VirtualBankAccountResource(this.client); - card = new CardResource(this.client); - promotionalCredit = new PromotionalCreditResource(this.client); - invoice = new InvoiceResource(this.client); - creditNote = new CreditNoteResource(this.client); - unbilledCharge = new UnbilledChargeResource(this.client); - order = new OrderResource(this.client); - gift = new GiftResource(this.client); - transaction = new TransactionResource(this.client); - hostedPage = new HostedPageResource(this.client); - estimate = new EstimateResource(this.client); - quote = new QuoteResource(this.client); - plan = new PlanResource(this.client); - addon = new AddonResource(this.client); - coupon = new CouponResource(this.client); - couponSet = new CouponSetResource(this.client); - couponCode = new CouponCodeResource(this.client); - address = new AddressResource(this.client); - usage = new UsageResource(this.client); - comment = new CommentResource(this.client); - portalSession = new PortalSessionResource(this.client); - siteMigrationDetail = new SiteMigrationDetailResource(this.client); - resourceMigration = new ResourceMigrationResource(this.client); - timeMachine = new TimeMachineResource(this.client); - export = new ExportResource(this.client); - paymentIntent = new PaymentIntentResource(this.client); - itemFamily = new ItemFamilyResource(this.client); - item = new ItemResource(this.client); - itemPrice = new ItemPriceResource(this.client); - attachedItem = new AttachedItemResource(this.client); - differentialPrice = new DifferentialPriceResource(this.client); - feature = new FeatureResource(this.client); - subscriptionEntitlement = new SubscriptionEntitlementResource(this.client); - itemEntitlement = new ItemEntitlementResource(this.client); - inAppSubscription = new InAppSubscriptionResource(this.client); - nonSubscription = new NonSubscriptionResource(this.client); - entitlementOverride = new EntitlementOverrideResource(this.client); - purchase = new PurchaseResource(this.client); - paymentVoucher = new PaymentVoucherResource(this.client); - download = new DownloadResource(this.client); + subscription = new SubscriptionResource(this.options); + customer = new CustomerResource(this.options); + paymentSource = new PaymentSourceResource(this.options); + virtualBankAccount = new VirtualBankAccountResource(this.options); + card = new CardResource(this.options); + promotionalCredit = new PromotionalCreditResource(this.options); + invoice = new InvoiceResource(this.options); + creditNote = new CreditNoteResource(this.options); + unbilledCharge = new UnbilledChargeResource(this.options); + order = new OrderResource(this.options); + gift = new GiftResource(this.options); + transaction = new TransactionResource(this.options); + hostedPage = new HostedPageResource(this.options); + estimate = new EstimateResource(this.options); + quote = new QuoteResource(this.options); + plan = new PlanResource(this.options); + addon = new AddonResource(this.options); + coupon = new CouponResource(this.options); + couponSet = new CouponSetResource(this.options); + couponCode = new CouponCodeResource(this.options); + address = new AddressResource(this.options); + usage = new UsageResource(this.options); + comment = new CommentResource(this.options); + portalSession = new PortalSessionResource(this.options); + siteMigrationDetail = new SiteMigrationDetailResource(this.options); + resourceMigration = new ResourceMigrationResource(this.options); + timeMachine = new TimeMachineResource(this.options); + export = new ExportResource(this.options); + paymentIntent = new PaymentIntentResource(this.options); + itemFamily = new ItemFamilyResource(this.options); + item = new ItemResource(this.options); + itemPrice = new ItemPriceResource(this.options); + attachedItem = new AttachedItemResource(this.options); + differentialPrice = new DifferentialPriceResource(this.options); + feature = new FeatureResource(this.options); + subscriptionEntitlement = new SubscriptionEntitlementResource(this.options); + itemEntitlement = new ItemEntitlementResource(this.options); + inAppSubscription = new InAppSubscriptionResource(this.options); + nonSubscription = new NonSubscriptionResource(this.options); + entitlementOverride = new EntitlementOverrideResource(this.options); + purchase = new PurchaseResource(this.options); + paymentVoucher = new PaymentVoucherResource(this.options); + download = new DownloadResource(this.options); } diff --git a/src/chargebee-resource.class.ts b/src/chargebee-resource.class.ts index dac69b2..93f5832 100644 --- a/src/chargebee-resource.class.ts +++ b/src/chargebee-resource.class.ts @@ -1,4 +1,4 @@ -import type { ChargeBee } from "chargebee-typescript"; +import { ChargeBee } from "chargebee-typescript"; import type { RequestWrapper } from "chargebee-typescript/lib/request_wrapper"; import type { ListResult } from "chargebee-typescript/lib/list_result"; @@ -12,9 +12,13 @@ import { type ResolveResultReturn, isListOffsetOption, } from "./chargebee-resource.types"; +import { ChargebeeModuleOptions } from "./chargebee.interface"; +import { configureChargebee } from "./chargebee.utils"; export class ChargebeeResource { - constructor(protected readonly chargebee: ChargeBee) {} + protected readonly chargebee = new ChargeBee(); + + constructor(protected readonly options: ChargebeeModuleOptions) {} protected request< TResourceName extends keyof ChargeBee, @@ -40,6 +44,7 @@ export class ChargebeeResource { ] as MethodDefinition; return async (...args: Parameters) => { + configureChargebee(this.chargebee, this.options); return functionDef(...args) .request() .then(this.resolveResult(returning)); @@ -71,6 +76,7 @@ export class ChargebeeResource { ] as MethodDefinition; const method = async (...args: Parameters) => { + configureChargebee(this.chargebee, this.options); return functionDef(...args) .request() .then((listResult) => { diff --git a/src/chargebee.service.ts b/src/chargebee.service.ts index 3aabaed..a15d2c0 100644 --- a/src/chargebee.service.ts +++ b/src/chargebee.service.ts @@ -1,5 +1,4 @@ -import { Inject, Injectable, Optional } from "@nestjs/common"; -import { ChargeBee } from "chargebee-typescript"; +import { Inject, Injectable } from "@nestjs/common"; import { ChargebeeModuleOptions } from "./chargebee.interface"; import { CHARGEBEE_MODULE_OPTIONS_TOKEN } from "./chargebee.module-definition"; @@ -10,31 +9,7 @@ export class ChargebeeService extends ChargebeeResourceWrapper { constructor( @Inject(CHARGEBEE_MODULE_OPTIONS_TOKEN) options: ChargebeeModuleOptions, - @Optional() - client = configureChargebee(options), ) { - super(client); + super(options); } } - -function configureChargebee(options: ChargebeeModuleOptions) { - const client = new ChargeBee(); - client.configure({ - site: options.override?.url ? "" : options.site, - api_key: options.apiKey, - ...(options.override?.url ? extractURLOptions(options.override.url) : {}), - ...(options.override?.timeout ? { timeout: options.override.timeout } : {}), - }); - return client; -} - -function extractURLOptions(urlStr: string) { - const url = new URL(urlStr); - - return { - hostSuffix: url.host, - apiPath: url.pathname, - protocol: url.protocol.replace(":", ""), - port: url.port, - }; -} diff --git a/src/chargebee.utils.ts b/src/chargebee.utils.ts new file mode 100644 index 0000000..52fa539 --- /dev/null +++ b/src/chargebee.utils.ts @@ -0,0 +1,26 @@ +import type { ChargeBee } from "chargebee-typescript"; +import type { ChargebeeModuleOptions } from "./chargebee.interface"; + +export function configureChargebee( + client: ChargeBee, + options: ChargebeeModuleOptions, +) { + client.configure({ + site: options.override?.url ? "" : options.site, + api_key: options.apiKey, + ...(options.override?.url ? extractURLOptions(options.override.url) : {}), + ...(options.override?.timeout ? { timeout: options.override.timeout } : {}), + }); + return client; +} + +export function extractURLOptions(urlStr: string) { + const url = new URL(urlStr); + + return { + hostSuffix: url.host, + apiPath: url.pathname, + protocol: url.protocol.replace(":", ""), + port: url.port, + }; +}