Skip to content

Commit 9a05b70

Browse files
committed
Encode custom query parameters
Fixes #15
1 parent e4635ba commit 9a05b70

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

src/api.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ import cache from 'memory-cache';
44
export default class Api {
55
cache: any;
66
baseUrl: string;
7-
params: string;
7+
params: URLSearchParams;
88
lastCacheDuration: number | undefined;
99

1010
constructor(baseUrl: string, params: string) {
1111
this.baseUrl = baseUrl;
12-
this.params = params;
12+
this.params = new URLSearchParams('?' + params);
1313
this.cache = new cache.Cache();
1414
}
1515

1616
/**
1717
* Queries the API and returns the response data.
1818
*/
19-
async get(params: string) {
20-
const allParams = new URLSearchParams('?' + this.params);
21-
new URLSearchParams('?' + params).forEach((value, key) => {
22-
allParams.set(key, value);
19+
async get(params?: string) {
20+
const data: Record<string, string> = {};
21+
22+
this.params.forEach((key, value) => {
23+
data[key] = value;
2324
});
2425

25-
const req = {
26-
url: `${this.baseUrl}${allParams.toString().length ? `?${allParams.toString()}` : ''}`,
27-
method: 'GET',
28-
};
26+
new URLSearchParams('?' + params).forEach((value, key) => {
27+
data[key] = value;
28+
});
2929

30-
const response = await getBackendSrv().datasourceRequest(req);
30+
const response = await this._request(data);
3131

3232
return response.data;
3333
}
@@ -36,11 +36,13 @@ export default class Api {
3636
* Used as a health check.
3737
*/
3838
async test() {
39-
const req = {
40-
url: `${this.baseUrl}${this.params.length ? `?${this.params}` : ''}`,
41-
method: 'GET',
42-
};
43-
return getBackendSrv().datasourceRequest(req);
39+
const data: Record<string, string> = {};
40+
41+
this.params.forEach((value, key) => {
42+
data[key] = value;
43+
});
44+
45+
return this._request(data);
4446
}
4547

4648
/**
@@ -69,4 +71,26 @@ export default class Api {
6971

7072
return result;
7173
}
74+
75+
async _request(data?: Record<string, string>) {
76+
const req = {
77+
url: `${this.baseUrl}`,
78+
method: 'GET',
79+
};
80+
81+
console.log(data);
82+
83+
if (data && Object.keys(data).length > 0) {
84+
req.url =
85+
req.url +
86+
(req.url.search(/\?/) >= 0 ? '&' : '?') +
87+
Object.entries(data)
88+
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
89+
.join('&');
90+
}
91+
92+
console.log(req.url);
93+
94+
return getBackendSrv().datasourceRequest(req);
95+
}
7296
}

src/ConfigEditor.tsx renamed to src/components/ConfigEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { ChangeEvent } from 'react';
22

33
import { LegacyForms, DataSourceHttpSettings } from '@grafana/ui';
44
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
5-
import { JsonApiDataSourceOptions } from './types';
5+
import { JsonApiDataSourceOptions } from '../types';
66

77
import {} from '@emotion/core';
88

File renamed without changes.

src/QueryEditor.tsx renamed to src/components/QueryEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import defaults from 'lodash/defaults';
22
import React, { ChangeEvent } from 'react';
33
import { Icon, InlineFormLabel, Segment } from '@grafana/ui';
44
import { QueryEditorProps } from '@grafana/data';
5-
import { DataSource } from './DataSource';
6-
import { JsonApiDataSourceOptions, JsonApiQuery, defaultQuery } from './types';
5+
import { DataSource } from '../datasource';
6+
import { JsonApiDataSourceOptions, JsonApiQuery, defaultQuery } from '../types';
77
import { JsonPathQueryField } from './JsonPathQueryField';
88
import { cx } from 'emotion';
99

src/VariableQueryEditor.tsx renamed to src/components/VariableQueryEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import defaults from 'lodash/defaults';
22

33
import React, { useState } from 'react';
4-
import { JsonApiVariableQuery, defaultVariableQuery } from './types';
4+
import { JsonApiVariableQuery, defaultVariableQuery } from '../types';
55
import { InlineFormLabel } from '@grafana/ui';
66
import { JsonPathQueryField } from './JsonPathQueryField';
77

src/detectFieldType.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { detectFieldType } from './DataSource';
1+
import { detectFieldType } from './datasource';
22
import { format } from 'date-fns';
33

44
test('years and months gets parsed as string to reduce false positives', () => {

src/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { DataSourcePlugin } from '@grafana/data';
2-
import { DataSource } from './DataSource';
3-
import { ConfigEditor } from './ConfigEditor';
4-
import { QueryEditor } from './QueryEditor';
5-
import { VariableQueryEditor } from './VariableQueryEditor';
2+
import { DataSource } from './datasource';
3+
import { ConfigEditor } from './components/ConfigEditor';
4+
import { QueryEditor } from './components/QueryEditor';
5+
import { VariableQueryEditor } from './components/VariableQueryEditor';
66
import { JsonApiQuery, JsonApiDataSourceOptions } from './types';
77

88
export const plugin = new DataSourcePlugin<DataSource, JsonApiQuery, JsonApiDataSourceOptions>(DataSource)

0 commit comments

Comments
 (0)