2020-03-20 04:21:32 -05:00
|
|
|
import { BackendSrvRequest } from '@grafana/runtime';
|
|
|
|
import omitBy from 'lodash/omitBy';
|
2020-09-04 15:11:34 -05:00
|
|
|
import { deprecationWarning } from '@grafana/data';
|
2020-03-20 04:21:32 -05:00
|
|
|
|
|
|
|
export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => {
|
|
|
|
const method = options.method;
|
|
|
|
const headers = parseHeaders(options);
|
|
|
|
const isAppJson = isContentTypeApplicationJson(headers);
|
|
|
|
const body = parseBody(options, isAppJson);
|
2020-09-04 15:11:34 -05:00
|
|
|
const credentials = parseCredentials(options);
|
2020-04-07 09:09:02 -05:00
|
|
|
|
2020-03-20 04:21:32 -05:00
|
|
|
return {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body,
|
2020-09-04 15:11:34 -05:00
|
|
|
credentials,
|
2020-03-20 04:21:32 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
interface HeaderParser {
|
|
|
|
canParse: (options: BackendSrvRequest) => boolean;
|
|
|
|
parse: (headers: Headers) => Headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultHeaderParser: HeaderParser = {
|
|
|
|
canParse: () => true,
|
|
|
|
parse: headers => {
|
|
|
|
const accept = headers.get('accept');
|
|
|
|
if (accept) {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
headers.set('accept', 'application/json, text/plain, */*');
|
|
|
|
return headers;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const parseHeaderByMethodFactory = (methodPredicate: string): HeaderParser => ({
|
|
|
|
canParse: options => {
|
|
|
|
const method = options?.method ? options?.method.toLowerCase() : '';
|
|
|
|
return method === methodPredicate;
|
|
|
|
},
|
|
|
|
parse: headers => {
|
|
|
|
const contentType = headers.get('content-type');
|
|
|
|
if (contentType) {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
headers.set('content-type', 'application/json');
|
|
|
|
return headers;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const postHeaderParser: HeaderParser = parseHeaderByMethodFactory('post');
|
|
|
|
const putHeaderParser: HeaderParser = parseHeaderByMethodFactory('put');
|
|
|
|
|
|
|
|
const headerParsers = [postHeaderParser, putHeaderParser, defaultHeaderParser];
|
|
|
|
|
|
|
|
export const parseHeaders = (options: BackendSrvRequest) => {
|
|
|
|
const headers = options?.headers ? new Headers(options.headers) : new Headers();
|
|
|
|
const parsers = headerParsers.filter(parser => parser.canParse(options));
|
|
|
|
const combinedHeaders = parsers.reduce((prev, parser) => {
|
|
|
|
return parser.parse(prev);
|
|
|
|
}, headers);
|
|
|
|
|
|
|
|
return combinedHeaders;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isContentTypeApplicationJson = (headers: Headers) => {
|
|
|
|
if (!headers) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = headers.get('content-type');
|
|
|
|
if (contentType && contentType.toLowerCase() === 'application/json') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => {
|
|
|
|
if (!options) {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.data || typeof options.data === 'string') {
|
|
|
|
return options.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data);
|
|
|
|
};
|
|
|
|
|
2020-11-18 11:35:28 -06:00
|
|
|
export async function parseResponseBody<T>(
|
|
|
|
response: Response,
|
|
|
|
responseType?: 'json' | 'text' | 'arraybuffer' | 'blob'
|
|
|
|
): Promise<T> {
|
|
|
|
if (responseType) {
|
|
|
|
switch (responseType) {
|
|
|
|
case 'arraybuffer':
|
|
|
|
return response.arrayBuffer() as any;
|
|
|
|
|
|
|
|
case 'blob':
|
|
|
|
return response.blob() as any;
|
|
|
|
|
|
|
|
case 'json':
|
|
|
|
return response.json();
|
|
|
|
|
|
|
|
case 'text':
|
|
|
|
return response.text() as any;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const textData = await response.text(); // this could be just a string, prometheus requests for instance
|
|
|
|
try {
|
|
|
|
return JSON.parse(textData); // majority of the requests this will be something that can be parsed
|
|
|
|
} catch {}
|
|
|
|
return textData as any;
|
|
|
|
}
|
|
|
|
|
2020-03-25 06:25:39 -05:00
|
|
|
export function serializeParams(data: Record<string, any>): string {
|
2020-03-20 04:21:32 -05:00
|
|
|
return Object.keys(data)
|
|
|
|
.map(key => {
|
|
|
|
const value = data[key];
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.map(arrayValue => `${encodeURIComponent(key)}=${encodeURIComponent(arrayValue)}`).join('&');
|
|
|
|
}
|
|
|
|
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
|
|
})
|
|
|
|
.join('&');
|
|
|
|
}
|
|
|
|
|
|
|
|
export const parseUrlFromOptions = (options: BackendSrvRequest): string => {
|
|
|
|
const cleanParams = omitBy(options.params, v => v === undefined || (v && v.length === 0));
|
|
|
|
const serializedParams = serializeParams(cleanParams);
|
|
|
|
return options.params && serializedParams.length ? `${options.url}?${serializedParams}` : options.url;
|
|
|
|
};
|
2020-09-04 15:11:34 -05:00
|
|
|
|
|
|
|
export const parseCredentials = (options: BackendSrvRequest): RequestCredentials => {
|
|
|
|
if (!options) {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.credentials) {
|
|
|
|
return options.credentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.withCredentials) {
|
|
|
|
deprecationWarning('BackendSrvRequest', 'withCredentials', 'credentials');
|
|
|
|
return 'include';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'same-origin';
|
|
|
|
};
|