Skip to content

Commit

Permalink
Merge pull request #13 from voiceflow/trs/multi-config-support/PL-000
Browse files Browse the repository at this point in the history
feat: configure before each request
  • Loading branch information
pmvrmc authored Jan 11, 2024
2 parents 4dbddee + 7a89590 commit 1b55d49
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 75 deletions.
91 changes: 45 additions & 46 deletions src/chargebee-resource-wrapper.class.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
}
10 changes: 8 additions & 2 deletions src/chargebee-resource.class.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -40,6 +44,7 @@ export class ChargebeeResource {
] as MethodDefinition;

return async (...args: Parameters<MethodDefinition>) => {
configureChargebee(this.chargebee, this.options);
return functionDef(...args)
.request()
.then(this.resolveResult(returning));
Expand Down Expand Up @@ -71,6 +76,7 @@ export class ChargebeeResource {
] as MethodDefinition;

const method = async (...args: Parameters<MethodDefinition>) => {
configureChargebee(this.chargebee, this.options);
return functionDef(...args)
.request()
.then((listResult) => {
Expand Down
29 changes: 2 additions & 27 deletions src/chargebee.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
};
}
26 changes: 26 additions & 0 deletions src/chargebee.utils.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}

0 comments on commit 1b55d49

Please sign in to comment.