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' ;
2023import { 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 */
3033export 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 }
0 commit comments