forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compatibility_shim.js
81 lines (63 loc) · 2.74 KB
/
compatibility_shim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { uriEncode } from '../lib/uri_encode';
export function compatibilityShimFactory(server) {
const logDeprecation = (msg) => {
server.log(['warning', 'reporting', 'deprecation'], msg + ' This functionality will be removed with the next major version.');
};
const getSavedObjectTitle = async (objectType, savedObjectId, savedObjectsClient) => {
if (!objectType) {
throw new Error('objectType is required to determine the title from the saved object');
}
if (!savedObjectId) {
throw new Error('savedObjectId is required to determine the title from the saved object');
}
logDeprecation('The title should be provided with the job generation request. Please use Kibana to regenerate your URLs.');
const savedObject = await savedObjectsClient.get(objectType, savedObjectId);
return savedObject.attributes.title;
};
const getSavedObjectRelativeUrl = (objectType, savedObjectId, queryString) => {
if (!objectType) {
throw new Error('objectType is required to determine the savedObject urlHash');
}
if (!savedObjectId) {
throw new Error('id is required to determine the savedObject relativeUrl');
}
logDeprecation('The relativeUrl should be provided with the job generation request. Please use Kibana to regenerate your URLs.');
const appPrefixes = {
dashboard: '/dashboard/',
visualization: '/visualize/edit/',
search: '/discover/'
};
const appPrefix = appPrefixes[objectType];
if (!appPrefix) throw new Error('Unexpected app type: ' + objectType);
const hash = appPrefix + uriEncode.string(savedObjectId, true);
return `/app/kibana#${hash}?${queryString || ''}`;
};
return function compatibilityShimFactory(createJob) {
return async function ({
objectType,
savedObjectId,
title,
relativeUrls,
queryString,
browserTimezone,
layout
}, headers, request) {
if (objectType && savedObjectId && relativeUrls) {
throw new Error('objectType and savedObjectId should not be provided in addition to the relativeUrls');
}
const transformedJobParams = {
objectType,
title: title || await getSavedObjectTitle(objectType, savedObjectId, request.getSavedObjectsClient()),
relativeUrls: objectType && savedObjectId ? [ getSavedObjectRelativeUrl(objectType, savedObjectId, queryString) ] : relativeUrls,
browserTimezone,
layout
};
return await createJob(transformedJobParams, headers, request);
};
};
}