Skip to content

Commit 62ff4a9

Browse files
committed
Add interpolation to all values
1 parent 4876087 commit 62ff4a9

File tree

1 file changed

+36
-73
lines changed

1 file changed

+36
-73
lines changed

src/datasource.ts

Lines changed: 36 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,7 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
3535
* name it as you like.
3636
*/
3737
async metadataRequest(query: JsonApiQuery, range?: TimeRange) {
38-
const scopedVars = {};
39-
const templateSrv = getTemplateSrv();
40-
41-
const replaceMacros = (str: string) => {
42-
return range
43-
? str
44-
.replace(/\$__unixEpochFrom\(\)/g, range.from.unix().toString())
45-
.replace(/\$__unixEpochTo\(\)/g, range.to.unix().toString())
46-
: str;
47-
};
48-
49-
const urlPathTreated = templateSrv.replace(query.urlPath, scopedVars);
50-
const bodyTreated = templateSrv.replace(query.body, scopedVars);
51-
52-
const paramsTreated: Array<Pair<string, string>> = (query.params ?? []).map(([key, value]) => {
53-
const keyTreated = replaceMacros(templateSrv.replace(key, scopedVars));
54-
const valueTreated = replaceMacros(templateSrv.replace(value, scopedVars));
55-
return [keyTreated, valueTreated];
56-
});
57-
58-
const headersTreated: Array<Pair<string, string>> = (query.headers ?? []).map(([key, value]) => {
59-
const keyTreated = templateSrv.replace(key, scopedVars);
60-
const valueTreated = templateSrv.replace(value, scopedVars);
61-
return [keyTreated, valueTreated];
62-
});
63-
64-
return await this.api.cachedGet(
65-
query.cacheDurationSeconds,
66-
query.method,
67-
urlPathTreated,
68-
paramsTreated,
69-
headersTreated,
70-
bodyTreated
71-
);
38+
return this.requestJson(query, replace({}, range));
7239
}
7340

7441
async query(request: DataQueryRequest<JsonApiQuery>): Promise<DataQueryResponse> {
@@ -135,62 +102,30 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
135102
}
136103

137104
async doRequest(query: JsonApiQuery, range?: TimeRange, scopedVars?: ScopedVars) {
138-
const templateSrv = getTemplateSrv();
139-
140-
const replaceMacros = (str: string) => {
141-
return range
142-
? str
143-
.replace(/\$__unixEpochFrom\(\)/g, range.from.unix().toString())
144-
.replace(/\$__unixEpochTo\(\)/g, range.to.unix().toString())
145-
: str;
146-
};
147-
148-
const urlPathTreated = templateSrv.replace(query.urlPath, scopedVars);
149-
const bodyTreated = templateSrv.replace(query.body, scopedVars);
105+
const replaceWithVars = replace(scopedVars, range);
150106

151-
const paramsTreated: Array<Pair<string, string>> = (query.params ?? []).map(([key, value]) => {
152-
const keyTreated = replaceMacros(templateSrv.replace(key, scopedVars));
153-
const valueTreated = replaceMacros(templateSrv.replace(value, scopedVars));
154-
return [keyTreated, valueTreated];
155-
});
156-
157-
const headersTreated: Array<Pair<string, string>> = (query.headers ?? []).map(([key, value]) => {
158-
const keyTreated = templateSrv.replace(key, scopedVars);
159-
const valueTreated = templateSrv.replace(value, scopedVars);
160-
return [keyTreated, valueTreated];
161-
});
107+
const json = await this.requestJson(query, replaceWithVars);
162108

163-
const response = await this.api.cachedGet(
164-
query.cacheDurationSeconds,
165-
query.method,
166-
urlPathTreated,
167-
paramsTreated,
168-
headersTreated,
169-
bodyTreated
170-
);
171-
172-
if (!response) {
109+
if (!json) {
173110
throw new Error('Query returned empty data');
174111
}
175112

176113
const fields = query.fields
177114
.filter((field) => field.jsonPath)
178115
.map((field) => {
179-
const jsonPathTreated = replaceMacros(templateSrv.replace(field.jsonPath, scopedVars));
180-
const nameTreated = templateSrv.replace(field.name, scopedVars);
181-
182-
const values = JSONPath({ path: jsonPathTreated, json: response });
116+
const path = replaceWithVars(field.jsonPath);
117+
const values = JSONPath({ path, json });
183118

184119
// Get the path for automatic setting of the field name.
185120
//
186121
// Casted to any due to typing issues with JSONPath-Plus
187-
const paths = (JSONPath as any).toPathArray(jsonPathTreated);
122+
const paths = (JSONPath as any).toPathArray(path);
188123

189124
const propertyType = field.type ? field.type : detectFieldType(values);
190125
const typedValues = parseValues(values, propertyType);
191126

192127
return {
193-
name: nameTreated || paths[paths.length - 1],
128+
name: paths[paths.length - 1],
194129
type: propertyType,
195130
values: typedValues,
196131
};
@@ -210,8 +145,36 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
210145
fields: fields,
211146
});
212147
}
148+
149+
async requestJson(query: JsonApiQuery, interpolate: (text: string) => string) {
150+
const interpolateKeyValue = ([key, value]: Pair<string, string>): Pair<string, string> => {
151+
return [interpolate(key), interpolate(value)];
152+
};
153+
154+
return await this.api.cachedGet(
155+
query.cacheDurationSeconds,
156+
query.method,
157+
interpolate(query.urlPath),
158+
(query.params ?? []).map(interpolateKeyValue),
159+
(query.headers ?? []).map(interpolateKeyValue),
160+
interpolate(query.body)
161+
);
162+
}
213163
}
214164

165+
const replace = (scopedVars?: any, range?: TimeRange) => (str: string): string => {
166+
return replaceMacros(getTemplateSrv().replace(str, scopedVars), range);
167+
};
168+
169+
// replaceMacros substitutes all available macros with their current value.
170+
const replaceMacros = (str: string, range?: TimeRange) => {
171+
return range
172+
? str
173+
.replace(/\$__unixEpochFrom\(\)/g, range.from.unix().toString())
174+
.replace(/\$__unixEpochTo\(\)/g, range.to.unix().toString())
175+
: str;
176+
};
177+
215178
/**
216179
* Detects the field type from an array of values.
217180
*/

0 commit comments

Comments
 (0)