Skip to content

Commit 9292b23

Browse files
authored
Ability to set host and scheme (#35)
* add setHost and setScheme * update baseurl * version bump * add test for custom host and scheme * update readme
1 parent dcf06f4 commit 9292b23

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ Sets the device pixel ratio of the chart. This will multiply the number of pixe
9595

9696
Sets the Chart.js version to use (e.g. `2.9.4` or `3.4.0`). Valid options are shown in the [documentation](https://quickchart.io/documentation/#parameters).
9797

98+
### setHost(host: string)
99+
100+
Sets the host of generated URLs. `quickchart.io` by default.
101+
102+
### setScheme(scheme: string)
103+
104+
Sets the scheme of generated URLs. `https` by default.
105+
98106
## Getting outputs
99107

100108
There are two ways to get a URL for your chart object.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickchart-js",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "Javascript client for QuickChart.io",
55
"main": "build/quickchart.cjs.js",
66
"module": "build/quickchart.mjs",

src/index.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function postJson(url: string, payload: PostData): Promise<Response> {
4848
class QuickChart {
4949
private host: string;
5050
private scheme: string;
51-
private baseUrl: string;
5251
private width: number;
5352
private height: number;
5453
private devicePixelRatio: number;
@@ -66,7 +65,6 @@ class QuickChart {
6665

6766
this.host = 'quickchart.io';
6867
this.scheme = 'https';
69-
this.baseUrl = `${this.scheme}://${this.host}`;
7068

7169
this.chart = undefined;
7270
this.width = 500;
@@ -82,6 +80,16 @@ class QuickChart {
8280
return this;
8381
}
8482

83+
setHost(host: string): QuickChart {
84+
this.host = host;
85+
return this;
86+
}
87+
88+
setScheme(scheme: string): QuickChart {
89+
this.scheme = scheme;
90+
return this;
91+
}
92+
8593
setWidth(width: number): QuickChart {
8694
this.width = width;
8795
return this;
@@ -119,11 +127,15 @@ class QuickChart {
119127
return true;
120128
}
121129

130+
private getBaseUrl(): string {
131+
return `${this.scheme}://${this.host}`;
132+
}
133+
122134
private getUrlObject(): URL {
123135
if (!this.isValid()) {
124136
throw new Error('You must call setConfig before getUrl');
125137
}
126-
const ret = new URL(`${this.baseUrl}/chart`);
138+
const ret = new URL(`${this.getBaseUrl()}/chart`);
127139
ret.searchParams.append('c', this.chart!);
128140
ret.searchParams.append('w', String(this.width));
129141
ret.searchParams.append('h', String(this.height));
@@ -204,7 +216,7 @@ class QuickChart {
204216
throw new Error('Short URLs must use quickchart.io host');
205217
}
206218

207-
const resp = await postJson(`${this.baseUrl}/chart/create`, this.getPostData());
219+
const resp = await postJson(`${this.getBaseUrl()}/chart/create`, this.getPostData());
208220
if (!resp.ok) {
209221
const quickchartError = resp.headers.get('x-quickchart-error');
210222
const details = quickchartError ? `\n${quickchartError}` : '';
@@ -224,7 +236,7 @@ class QuickChart {
224236
throw new Error('You must call setConfig before getUrl');
225237
}
226238

227-
const resp = await postJson(`${this.baseUrl}/chart`, this.getPostData());
239+
const resp = await postJson(`${this.getBaseUrl()}/chart`, this.getPostData());
228240
if (!resp.ok) {
229241
const quickchartError = resp.headers.get('x-quickchart-error');
230242
const details = quickchartError ? `\n${quickchartError}` : '';

test/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ test('basic chart, no auth', () => {
1515
expect(qc.getUrl()).toContain('h=300');
1616
});
1717

18+
test('basic chart with custom host', () => {
19+
const qc = new QuickChart();
20+
qc.setHost('foo.com');
21+
qc.setScheme('http');
22+
qc.setConfig({
23+
type: 'bar',
24+
data: { labels: ['Hello world', 'Foo bar'], datasets: [{ label: 'Foo', data: [1, 2] }] },
25+
});
26+
27+
expect(qc.getUrl()).toContain('http://foo.com/chart?');
28+
expect(qc.getUrl()).toContain('Hello+world');
29+
expect(qc.getUrl()).toContain('w=500');
30+
expect(qc.getUrl()).toContain('h=300');
31+
});
32+
1833
test('basic chart with auth', () => {
1934
const qc = new QuickChart('abc123', '12345');
2035
qc.setConfig({

0 commit comments

Comments
 (0)