Skip to content

Commit 4c2aa36

Browse files
committed
Override custom parameters for queries
Closes #8
1 parent 0175888 commit 4c2aa36

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

src/DataSource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
2626

2727
async query(request: DataQueryRequest<JsonApiQuery>): Promise<DataQueryResponse> {
2828
const promises = request.targets.map(async query => {
29-
const response = await this.api.cachedGet(query.cacheDurationSeconds);
29+
const response = await this.api.cachedGet(query.cacheDurationSeconds, query.queryParams);
3030

3131
const fields = query.fields
3232
.filter(field => field.jsonPath)
@@ -77,7 +77,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
7777
return [];
7878
}
7979

80-
const response = await this.api.get();
80+
const response = await this.api.get(query.queryParams);
8181

8282
return JSONPath({
8383
path: query.jsonPath,

src/QueryEditor.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,32 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query }) =>
6161
<div className="gf-form-label gf-form-label--grow" />
6262
</div>
6363
</div>
64+
<div className="gf-form">
65+
<InlineFormLabel
66+
width={12}
67+
className="query-keyword"
68+
tooltip="Add custom parameters to your queries. Any parameters you add here overrides the custom parameters that have been configured by the data source."
69+
>
70+
Custom query parameters
71+
</InlineFormLabel>
72+
<input
73+
className="gf-form-input"
74+
placeholder="page=1&limit=100"
75+
value={query.queryParams}
76+
onChange={e => onChange({ ...query, queryParams: e.currentTarget.value })}
77+
></input>
78+
</div>
6479
{fields
6580
? fields.map((field, index) => (
6681
<div key={index} className="gf-form">
6782
<InlineFormLabel
6883
width={7}
6984
className="query-keyword"
7085
tooltip={
71-
<p>
86+
<div>
7287
A <a href="https://goessner.net/articles/JsonPath/">JSON Path</a> query that selects one or more
7388
values from a JSON object.
74-
</p>
89+
</div>
7590
}
7691
>
7792
Query

src/VariableQueryEditor.tsx

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,39 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
1616
};
1717

1818
const onChangePath = (jsonPath: string) => setState({ ...state, jsonPath });
19+
const onQueryParams = (queryParams: string) => setState({ ...state, queryParams });
1920

2021
return (
21-
<div className="gf-form">
22-
<InlineFormLabel
23-
width={10}
24-
tooltip={
25-
<p>
26-
A <a href="https://goessner.net/articles/JsonPath/">JSON Path</a> query that selects one or more values from
27-
a JSON object.
28-
</p>
29-
}
30-
>
31-
Query
32-
</InlineFormLabel>
33-
<JsonPathQueryField onBlur={saveQuery} onChange={onChangePath} query={state.jsonPath} />
34-
</div>
22+
<>
23+
<div className="gf-form">
24+
<InlineFormLabel
25+
width={12}
26+
tooltip="Add custom parameters to your queries. Any parameters you add here overrides the custom parameters that have been configured by the data source."
27+
>
28+
Custom query parameters
29+
</InlineFormLabel>
30+
<input
31+
className="gf-form-input"
32+
placeholder="page=1&limit=100"
33+
value={state.queryParams}
34+
onChange={e => onQueryParams(e.currentTarget.value)}
35+
onBlur={saveQuery}
36+
></input>
37+
</div>
38+
<div className="gf-form">
39+
<InlineFormLabel
40+
width={10}
41+
tooltip={
42+
<div>
43+
A <a href="https://goessner.net/articles/JsonPath/">JSON Path</a> query that selects one or more values
44+
from a JSON object.
45+
</div>
46+
}
47+
>
48+
Query
49+
</InlineFormLabel>
50+
<JsonPathQueryField onBlur={saveQuery} onChange={onChangePath} query={state.jsonPath} />
51+
</div>
52+
</>
3553
);
3654
};

src/api.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ export default class Api {
1616
/**
1717
* Queries the API and returns the response data.
1818
*/
19-
async get() {
19+
async get(params: string) {
20+
const allParams = new URLSearchParams('?' + this.params);
21+
new URLSearchParams('?' + params).forEach((value, key) => {
22+
allParams.set(key, value);
23+
});
24+
2025
const req = {
21-
url: `${this.baseUrl}${this.params.length ? `?${this.params}` : ''}`,
26+
url: `${this.baseUrl}${allParams.toString().length ? `?${allParams.toString()}` : ''}`,
2227
method: 'GET',
2328
};
2429

@@ -41,9 +46,9 @@ export default class Api {
4146
/**
4247
* Returns a cached API response if it exists, otherwise queries the API.
4348
*/
44-
async cachedGet(cacheDurationSeconds: number, params?: string) {
49+
async cachedGet(cacheDurationSeconds: number, params: string) {
4550
if (cacheDurationSeconds === 0) {
46-
return await this.get();
51+
return await this.get(params);
4752
}
4853

4954
const force = this.lastCacheDuration !== cacheDurationSeconds;
@@ -58,7 +63,7 @@ export default class Api {
5863
}
5964
this.lastCacheDuration = cacheDurationSeconds;
6065

61-
const result = await this.get();
66+
const result = await this.get(params);
6267

6368
this.cache.put(this.baseUrl, result, Math.max(cacheDurationSeconds * 1000, 1));
6469

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ interface JsonField {
88
export interface JsonApiQuery extends DataQuery {
99
fields: JsonField[];
1010
cacheDurationSeconds: number;
11+
queryParams: string;
1112
}
1213

1314
export interface JsonApiVariableQuery extends DataQuery {
1415
jsonPath: string;
16+
queryParams: string;
1517
}
1618

1719
export const defaultQuery: Partial<JsonApiQuery> = {
1820
fields: [{ name: '', jsonPath: '' }],
1921
cacheDurationSeconds: 300,
22+
queryParams: '',
2023
};
2124

2225
export interface JsonApiDataSourceOptions extends DataSourceJsonData {

0 commit comments

Comments
 (0)