Skip to content

Commit 50f8cb8

Browse files
authored
Merge pull request #74 from FacturAPI/chore/base_url
chore(baseUrl): add set and getter for choosing base url
2 parents b194100 + 84d5c55 commit 50f8cb8

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/index.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Retentions from './resources/retentions';
88
import Webhooks from './tools/webhooks';
99
import Tools from './tools/tools';
1010
import * as enums from './enums';
11-
import { createWrapper } from './wrapper';
11+
import { createWrapper, WrapperClient } from './wrapper';
1212
import { DEFAULT_API_VERSION } from './constants';
1313

1414
export * from './enums';
@@ -30,6 +30,7 @@ export interface FacturapiOptions {
3030
*/
3131
export default class Facturapi {
3232
apiVersion: ApiVersion;
33+
private _wrapper: WrapperClient;
3334
customers: Customers;
3435
products: Products;
3536
invoices: Invoices;
@@ -40,6 +41,18 @@ export default class Facturapi {
4041
tools: Tools;
4142
webhooks: Webhooks;
4243

44+
/**
45+
* Get or set the base URL used for API requests.
46+
* Allows overriding the default API host, e.g. for testing.
47+
* Usage: facturapi.BASE_URL = 'http://localhost:3000/v2'
48+
*/
49+
get BASE_URL(): string {
50+
return this._wrapper.baseURL;
51+
}
52+
set BASE_URL(url: string) {
53+
this._wrapper.baseURL = url;
54+
}
55+
4356
static get TaxType() {
4457
return enums.TaxType;
4558
}
@@ -96,15 +109,15 @@ export default class Facturapi {
96109
} else {
97110
this.apiVersion = DEFAULT_API_VERSION;
98111
}
99-
const wrapper = createWrapper(apiKey, this.apiVersion);
100-
this.customers = new Customers(wrapper);
101-
this.products = new Products(wrapper);
102-
this.invoices = new Invoices(wrapper);
103-
this.organizations = new Organizations(wrapper);
104-
this.catalogs = new Catalogs(wrapper);
105-
this.receipts = new Receipts(wrapper);
106-
this.retentions = new Retentions(wrapper);
107-
this.tools = new Tools(wrapper);
108-
this.webhooks = new Webhooks(wrapper);
112+
this._wrapper = createWrapper(apiKey, this.apiVersion);
113+
this.customers = new Customers(this._wrapper);
114+
this.products = new Products(this._wrapper);
115+
this.invoices = new Invoices(this._wrapper);
116+
this.organizations = new Organizations(this._wrapper);
117+
this.catalogs = new Catalogs(this._wrapper);
118+
this.receipts = new Receipts(this._wrapper);
119+
this.retentions = new Retentions(this._wrapper);
120+
this.tools = new Tools(this._wrapper);
121+
this.webhooks = new Webhooks(this._wrapper);
109122
}
110123
}

src/wrapper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@ export const createWrapper = (
7575
apiKey: string,
7676
apiVersion: 'v1' | 'v2' = DEFAULT_API_VERSION,
7777
) => {
78-
const baseURL = apiVersion === 'v1' ? BASE_URL_V1 : BASE_URL;
78+
let baseURL = apiVersion === 'v1' ? BASE_URL_V1 : BASE_URL;
7979
const defaultHeaders = {
8080
Authorization: `Basic ${encodeStringToBase64(apiKey + ':')}`,
8181
};
8282

8383
const client = {
84+
get baseURL(): string {
85+
return baseURL;
86+
},
87+
set baseURL(url: string) {
88+
baseURL = url;
89+
},
8490
async request(
8591
url: string,
8692
options?: {

0 commit comments

Comments
 (0)