Skip to content

Commit dc47644

Browse files
committed
Add even more typescript
1 parent 2e05092 commit dc47644

File tree

5 files changed

+66
-48
lines changed

5 files changed

+66
-48
lines changed

src/legacy/ui/public/saved_objects/helpers/build_saved_object.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,12 @@ import { hydrateIndexPattern } from 'ui/saved_objects/helpers/hydrate_index_patt
2424
import { intializeSavedObject } from 'ui/saved_objects/helpers/initialize_saved_object';
2525
import { serializeSavedObject } from 'ui/saved_objects/helpers/serialize_saved_object';
2626
import { SavedObjectsClient } from 'kibana/public';
27-
import { SavedObject } from 'ui/saved_objects/types';
27+
import { SavedObject, SavedObjectConfig } from 'ui/saved_objects/types';
2828
import { applyEsResp } from 'ui/saved_objects/helpers/apply_es_resp';
2929
import { saveSavedObject } from 'ui/saved_objects/helpers/save_saved_object';
3030
import { expandShorthand } from '../../../../../plugins/kibana_utils/public';
3131
import { IndexPatterns } from '../../../../core_plugins/data/public';
3232

33-
interface SavedObjectConfig {
34-
id?: string;
35-
type?: string;
36-
defaults?: any;
37-
mapping?: any;
38-
afterESResp?: any;
39-
extractReferences?: any;
40-
injectReferences?: any;
41-
searchSource?: any;
42-
init?: any;
43-
migrationVersion?: any;
44-
clearSavedIndexPattern?: any;
45-
indexPattern?: any;
46-
}
47-
4833
export function buildSavedObject(
4934
savedObject: SavedObject,
5035
config: SavedObjectConfig = {},
@@ -101,9 +86,9 @@ export function buildSavedObject(
10186
return hydrateIndexPattern(
10287
id || '',
10388
savedObject,
104-
config.clearSavedIndexPattern,
105-
config.indexPattern,
106-
indexPatterns
89+
indexPatterns,
90+
!!config.clearSavedIndexPattern,
91+
config.indexPattern
10792
);
10893
};
10994

src/legacy/ui/public/saved_objects/helpers/hydrate_index_pattern.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import { IndexPatterns } from '../../../../core_plugins/data/public/index_patter
2929
export function hydrateIndexPattern(
3030
id: string,
3131
savedObject: SavedObject,
32+
indexPatterns: IndexPatterns,
3233
clearSavedIndexPattern: boolean,
33-
indexPattern: IIndexPattern,
34-
indexPatterns: IndexPatterns
34+
indexPattern?: IIndexPattern
3535
) {
3636
if (!savedObject.searchSource) {
3737
return Promise.resolve(null);

src/legacy/ui/public/saved_objects/saved_object.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* This class seems to interface with ES primarily through the es Angular
2828
* service and the saved object api.
2929
*/
30-
import _ from 'lodash';
30+
import { SavedObjectConfig } from 'ui/saved_objects/types';
3131
import { PromiseService } from 'ui/promises';
3232
import { buildSavedObject } from 'ui/saved_objects/helpers/build_saved_object';
3333
import { SavedObjectsClientProvider } from './saved_objects_client_provider';
@@ -62,9 +62,7 @@ export function SavedObjectProvider(
6262
* which returns instances of SimpleSavedObject which don't introduce additional type-specific complexity.
6363
* @param {*} config
6464
*/
65-
function SavedObject(config: any) {
66-
if (!_.isObject(config)) config = {};
67-
65+
function SavedObject(config: SavedObjectConfig = {}) {
6866
return buildSavedObject(
6967
// @ts-ignore
7068
this,

src/legacy/ui/public/saved_objects/saved_object_loader.js renamed to src/legacy/ui/public/saved_objects/saved_object_loader.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
19+
import { SavedObject } from 'ui/saved_objects/types';
20+
import { KbnUrl } from 'ui/url/kbn_url';
21+
import { Chrome } from 'ui/chrome';
22+
import { SavedObjectsClient } from 'kibana/public';
2023
import { StringUtils } from '../utils/string_utils';
2124

2225
/**
@@ -28,17 +31,30 @@ import { StringUtils } from '../utils/string_utils';
2831
* to avoid pulling in extra functionality which isn't used.
2932
*/
3033
export class SavedObjectLoader {
31-
constructor(SavedObjectClass, kbnUrl, chrome, savedObjectClient) {
34+
type: string;
35+
Class: (id: string) => SavedObject;
36+
lowercaseType: string;
37+
kbnUrl: any;
38+
chrome: any;
39+
loaderProperties: Record<string, string>;
40+
savedObjectsClient: SavedObjectsClient;
41+
42+
constructor(
43+
SavedObjectClass: any,
44+
kbnUrl: KbnUrl,
45+
chrome: Chrome,
46+
savedObjectClient: SavedObjectsClient
47+
) {
3248
this.type = SavedObjectClass.type;
3349
this.Class = SavedObjectClass;
3450
this.lowercaseType = this.type.toLowerCase();
3551
this.kbnUrl = kbnUrl;
3652
this.chrome = chrome;
3753

3854
this.loaderProperties = {
39-
name: `${ this.lowercaseType }s`,
55+
name: `${this.lowercaseType}s`,
4056
noun: StringUtils.upperFirst(this.type),
41-
nouns: `${ this.lowercaseType }s`,
57+
nouns: `${this.lowercaseType}s`,
4258
};
4359

4460
this.savedObjectsClient = savedObjectClient;
@@ -50,18 +66,20 @@ export class SavedObjectLoader {
5066
* @param id
5167
* @returns {Promise<SavedObject>}
5268
*/
53-
get(id) {
54-
return (new this.Class(id)).init();
69+
get(id: string) {
70+
// @ts-ignore
71+
return new this.Class(id).init();
5572
}
5673

57-
urlFor(id) {
58-
return this.kbnUrl.eval(`#/${ this.lowercaseType }/{{id}}`, { id: id });
74+
urlFor(id: string) {
75+
return this.kbnUrl.eval(`#/${this.lowercaseType}/{{id}}`, { id });
5976
}
6077

61-
delete(ids) {
78+
delete(ids: string | string[]) {
6279
ids = !Array.isArray(ids) ? [ids] : ids;
6380

6481
const deletions = ids.map(id => {
82+
// @ts-ignore
6583
const savedObject = new this.Class(id);
6684
return savedObject.delete();
6785
});
@@ -80,7 +98,7 @@ export class SavedObjectLoader {
8098
* @param id
8199
* @returns {source} The modified source object, with an id and url field.
82100
*/
83-
mapHitSource(source, id) {
101+
mapHitSource(source: any, id: string) {
84102
source.id = id;
85103
source.url = this.urlFor(id);
86104
return source;
@@ -92,7 +110,7 @@ export class SavedObjectLoader {
92110
* @param hit
93111
* @returns {hit.attributes} The modified hit.attributes object, with an id and url field.
94112
*/
95-
mapSavedObjectApiHits(hit) {
113+
mapSavedObjectApiHits(hit: any) {
96114
return this.mapHitSource(hit.attributes, hit.id);
97115
}
98116

@@ -102,32 +120,33 @@ export class SavedObjectLoader {
102120
*
103121
* @param searchString
104122
* @param size
123+
* @param fields
105124
* @returns {Promise}
106125
*/
107-
findAll(search = '', size = 100, fields) {
108-
return this.savedObjectsClient.find(
109-
{
126+
findAll(search: string = '', size = 100, fields?: string[]) {
127+
return this.savedObjectsClient
128+
.find({
110129
type: this.lowercaseType,
111130
search: search ? `${search}*` : undefined,
112131
perPage: size,
113132
page: 1,
114133
searchFields: ['title^3', 'description'],
115134
defaultSearchOperator: 'AND',
116135
fields,
117-
}).then((resp) => {
118-
return {
119-
total: resp.total,
120-
hits: resp.savedObjects
121-
.map((savedObject) => this.mapSavedObjectApiHits(savedObject))
122-
};
123-
});
136+
})
137+
.then(resp => {
138+
return {
139+
total: resp.total,
140+
hits: resp.savedObjects.map(savedObject => this.mapSavedObjectApiHits(savedObject)),
141+
};
142+
});
124143
}
125144

126145
find(search = '', size = 100) {
127146
return this.findAll(search, size).then(resp => {
128147
return {
129148
total: resp.total,
130-
hits: resp.hits.filter(savedObject => !savedObject.error)
149+
hits: resp.hits.filter(savedObject => !savedObject.error),
131150
};
132151
});
133152
}

src/legacy/ui/public/saved_objects/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import { SearchSource } from 'ui/courier';
2121
import { SavedObjectAttributes, SavedObjectReference } from 'kibana/server';
22+
import { IndexPattern } from '../../../core_plugins/data/public';
2223

2324
export interface SaveOptions {
2425
confirmOverwrite: boolean;
@@ -51,9 +52,24 @@ export interface SavedObject {
5152
isSaving: boolean;
5253
isTitleChanged: any;
5354
lastSavedTitle: string;
54-
migrationVersion: string;
55+
migrationVersion?: Record<string, any>;
5556
save: (saveOptions: SaveOptions) => Promise<string>;
5657
searchSource?: SearchSource;
5758
showInRecentlyAccessed: boolean;
5859
title: string;
5960
}
61+
62+
export interface SavedObjectConfig {
63+
afterESResp?: () => any;
64+
clearSavedIndexPattern?: boolean;
65+
defaults?: any;
66+
extractReferences?: any;
67+
id?: string;
68+
indexPattern?: IndexPattern;
69+
init?: () => any;
70+
injectReferences?: any;
71+
mapping?: any;
72+
migrationVersion?: Record<string, any>;
73+
searchSource?: any;
74+
type?: string;
75+
}

0 commit comments

Comments
 (0)