Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typings for saved object client #29951

Merged
merged 18 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/server/saved_objects/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/

export {
MigrationVersion,
SavedObject,
SavedObjectAttributes,
SavedObjectsClient,
SavedObjectsClientWrapperFactory,
SavedObjectReference,
SavedObjectsService,
} from './service';
12 changes: 11 additions & 1 deletion src/server/saved_objects/service/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@

export { SavedObjectsService } from './create_saved_objects_service';
export { SavedObjectsClientWrapperFactory } from './lib';
export { SavedObject, SavedObjectsClient } from './saved_objects_client';
export {
FindOptions,
GetResponse,
UpdateResponse,
CreateResponse,
MigrationVersion,
SavedObject,
SavedObjectAttributes,
SavedObjectsClient,
SavedObjectReference,
} from './saved_objects_client';
17 changes: 14 additions & 3 deletions src/server/saved_objects/service/saved_objects_client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface BaseOptions {
export interface CreateOptions extends BaseOptions {
id?: string;
override?: boolean;
references?: SavedObjectReference[];
}

export interface BulkCreateObject<T extends SavedObjectAttributes = any> {
Expand All @@ -48,6 +49,7 @@ export interface FindOptions extends BaseOptions {
fields?: string[];
search?: string;
searchFields?: string[];
hasReference?: { type: string; id: string };
}

export interface FindResponse<T extends SavedObjectAttributes = any> {
Expand All @@ -71,6 +73,10 @@ export interface BulkGetResponse<T extends SavedObjectAttributes = any> {
saved_objects: Array<SavedObject<T>>;
}

export interface MigrationVersion {
[pluginName: string]: string;
}

export interface SavedObjectAttributes {
[key: string]: SavedObjectAttributes | string | number | boolean | null;
}
Expand All @@ -85,6 +91,7 @@ export interface SavedObject<T extends SavedObjectAttributes = any> {
};
attributes: T;
references: SavedObjectReference[];
migrationVersion?: MigrationVersion;
}

export interface SavedObjectReference {
Expand All @@ -93,6 +100,10 @@ export interface SavedObjectReference {
id: string;
}

export type GetResponse<T extends SavedObjectAttributes = any> = SavedObject<T>;
export type CreateResponse<T extends SavedObjectAttributes = any> = SavedObject<T>;
export type UpdateResponse<T extends SavedObjectAttributes = any> = SavedObject<T>;

export declare class SavedObjectsClient {
public static errors: typeof errors;
public errors: typeof errors;
Expand All @@ -103,7 +114,7 @@ export declare class SavedObjectsClient {
type: string,
attributes: T,
options?: CreateOptions
): Promise<SavedObject<T>>;
): Promise<CreateResponse<T>>;
public bulkCreate<T extends SavedObjectAttributes = any>(
objects: Array<BulkCreateObject<T>>,
options?: CreateOptions
Expand All @@ -120,11 +131,11 @@ export declare class SavedObjectsClient {
type: string,
id: string,
options?: BaseOptions
): Promise<SavedObject<T>>;
): Promise<GetResponse<T>>;
public update<T extends SavedObjectAttributes = any>(
type: string,
id: string,
attributes: Partial<T>,
options?: UpdateOptions
): Promise<SavedObject<T>>;
): Promise<UpdateResponse<T>>;
}
2 changes: 2 additions & 0 deletions src/ui/public/chrome/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { Brand } from '../../../core/public/chrome';
import { SavedObjectsClient } from '../saved_objects';
import { BreadcrumbsApi } from './api/breadcrumbs';
import { HelpExtensionApi } from './api/help_extension';
import { ChromeNavLinks } from './api/nav';
Expand All @@ -34,6 +35,7 @@ declare interface Chrome extends ChromeNavLinks {
getBasePath(): string;
getXsrfToken(): string;
getKibanaVersion(): string;
getSavedObjectsClient(): SavedObjectsClient;
getUiSettingsClient(): any;
setVisible(visible: boolean): any;
getInjected(key: string, defaultValue?: any): any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ import uiRoutes from '../routes';

import template from './error_auto_create_index.html';

uiRoutes
.when('/error/action.auto_create_index', {
template,
k7Breadcrumbs: () => [{ text: i18n.translate('common.ui.errorAutoCreateIndex.breadcrumbs.errorText', { defaultMessage: 'Error' }) }],
});
uiRoutes.when('/error/action.auto_create_index', {
template,
k7Breadcrumbs: () => [
{
text: i18n.translate('common.ui.errorAutoCreateIndex.breadcrumbs.errorText', {
defaultMessage: 'Error',
}),
},
],
});

export function isAutoCreateIndexError(error) {
export function isAutoCreateIndexError(error: object) {
return (
get(error, 'res.status') === 503 &&
get(error, 'body.code') === 'ES_AUTO_CREATE_INDEX_ERROR'
get(error, 'res.status') === 503 && get(error, 'body.code') === 'ES_AUTO_CREATE_INDEX_ERROR'
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/kfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
* under the License.
*/

export { kfetch, addInterceptor, KFetchOptions } from './kfetch';
export { kfetch, addInterceptor, KFetchOptions, KFetchQuery } from './kfetch';
export { kfetchAbortable } from './kfetch_abortable';
2 changes: 1 addition & 1 deletion src/ui/public/kfetch/kfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import url from 'url';
import chrome from '../chrome';
import { KFetchError } from './kfetch_error';

interface KFetchQuery {
export interface KFetchQuery {
[key: string]: string | number | boolean | undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/

import { find } from 'lodash';
import { SavedObjectAttributes } from 'src/server/saved_objects';
weltenwort marked this conversation as resolved.
Show resolved Hide resolved
import { SavedObject } from './saved_object';
import { SavedObjectsClient } from './saved_objects_client';

/**
* Returns an object matching a given title
Expand All @@ -27,22 +30,30 @@ import { find } from 'lodash';
* @param title {string}
* @returns {Promise<SavedObject|undefined>}
*/
export function findObjectByTitle(savedObjectsClient, type, title) {
if (!title) return Promise.resolve();
export function findObjectByTitle<T extends SavedObjectAttributes>(
savedObjectsClient: SavedObjectsClient,
type: string,
title: string
): Promise<SavedObject<T> | void> {
if (!title) {
return Promise.resolve();
}

// Elastic search will return the most relevant results first, which means exact matches should come
// first, and so we shouldn't need to request everything. Using 10 just to be on the safe side.
return savedObjectsClient.find({
type,
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title']
}).then(response => {
const match = find(response.savedObjects, (obj) => {
return obj.get('title').toLowerCase() === title.toLowerCase();
});
return savedObjectsClient
.find({
type,
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title'],
})
.then(response => {
const match = find(response.savedObjects, obj => {
return obj.get('title').toLowerCase() === title.toLowerCase();
});

return match;
});
return match;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/

export { SavedObjectsClient } from './saved_objects_client';
// @ts-ignore
export { SavedObjectRegistryProvider } from './saved_object_registry';
// @ts-ignore
weltenwort marked this conversation as resolved.
Show resolved Hide resolved
export { SavedObjectsClientProvider } from './saved_objects_client_provider';
export { SavedObject } from './saved_object';
export { findObjectByTitle } from './find_object_by_title';
67 changes: 0 additions & 67 deletions src/ui/public/saved_objects/saved_object.js

This file was deleted.

81 changes: 81 additions & 0 deletions src/ui/public/saved_objects/saved_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { get, has, set } from 'lodash';
import {
SavedObject as SavedObjectType,
SavedObjectAttributes,
} from '../../../server/saved_objects';
import { SavedObjectsClient } from './saved_objects_client';

export class SavedObject<T extends SavedObjectAttributes> {
public attributes: T;
// tslint:disable-next-line variable-name We want to use the same interface this class had in JS
public _version?: SavedObjectType<T>['version'];
public id: SavedObjectType<T>['id'];
public type: SavedObjectType<T>['type'];
public migrationVersion: SavedObjectType<T>['migrationVersion'];
public error: SavedObjectType<T>['error'];
public references: SavedObjectType<T>['references'];

constructor(
private client: SavedObjectsClient,
{ id, type, version, attributes, error, references, migrationVersion }: SavedObjectType<T>
) {
this.id = id;
this.type = type;
this.attributes = attributes || {};
this.references = references || [];
this._version = version;
this.migrationVersion = migrationVersion;
if (error) {
this.error = error;
}
}

public get(key: string): any {
return get(this.attributes, key);
}

public set(key: string, value: any): T {
return set(this.attributes, key, value);
}

public has(key: string): boolean {
return has(this.attributes, key);
}

public save() {
if (this.id) {
return this.client.update(this.type, this.id, this.attributes, {
migrationVersion: this.migrationVersion,
references: this.references,
});
} else {
return this.client.create(this.type, this.attributes, {
migrationVersion: this.migrationVersion,
references: this.references,
});
}
}

public delete() {
return this.client.delete(this.type, this.id);
}
}
Loading