Skip to content

Commit

Permalink
refactor: simplify options into single url string
Browse files Browse the repository at this point in the history
  • Loading branch information
trs committed Dec 6, 2023
1 parent fcaf8d6 commit 1692722
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/chargebee.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
export interface ChargebeeModuleOptions {
site: string;
apiKey: string;
override: {
hostSuffix?: string;
apiPath?: string;
protocol?: "https" | "http";
port?: number;
override?: {
url?: string;
timeout?: number;
};
}
20 changes: 12 additions & 8 deletions src/chargebee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ function configureChargebee(options: ChargebeeModuleOptions) {
client.configure({
site: options.site,
api_key: options.apiKey,
...(options.override?.hostSuffix
? { hostSuffix: options.override.hostSuffix }
: {}),
...(options.override?.apiPath ? { apiPath: options.override.apiPath } : {}),
...(options.override?.protocol
? { protocol: options.override.protocol }
: {}),
...(options.override?.port ? { port: options.override.port } : {}),
...(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,
};
}

0 comments on commit 1692722

Please sign in to comment.