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

force savedObject API consumers to define SO type explicitly #58022

Merged
merged 5 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 11 additions & 25 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { resolve as resolveUrl } from 'url';

import {
SavedObject,
SavedObjectAttributes,
SavedObjectReference,
SavedObjectsClientContract as SavedObjectsApi,
SavedObjectsFindOptions as SavedObjectFindOptionsServer,
Expand Down Expand Up @@ -61,9 +60,7 @@ export interface SavedObjectsCreateOptions {
*
* @public
*/
export interface SavedObjectsBulkCreateObject<
T extends SavedObjectAttributes = SavedObjectAttributes
> extends SavedObjectsCreateOptions {
export interface SavedObjectsBulkCreateObject<T = unknown> extends SavedObjectsCreateOptions {
type: string;
attributes: T;
}
Expand All @@ -75,9 +72,7 @@ export interface SavedObjectsBulkCreateOptions {
}

/** @public */
export interface SavedObjectsBulkUpdateObject<
T extends SavedObjectAttributes = SavedObjectAttributes
> {
export interface SavedObjectsBulkUpdateObject<T = unknown> {
type: string;
id: string;
attributes: T;
Expand All @@ -99,9 +94,7 @@ export interface SavedObjectsUpdateOptions {
}

/** @public */
export interface SavedObjectsBatchResponse<
T extends SavedObjectAttributes = SavedObjectAttributes
> {
export interface SavedObjectsBatchResponse<T = unknown> {
savedObjects: Array<SimpleSavedObject<T>>;
}

Expand All @@ -113,9 +106,7 @@ export interface SavedObjectsBatchResponse<
*
* @public
*/
export interface SavedObjectsFindResponsePublic<
T extends SavedObjectAttributes = SavedObjectAttributes
> extends SavedObjectsBatchResponse<T> {
export interface SavedObjectsFindResponsePublic<T = unknown> extends SavedObjectsBatchResponse<T> {
total: number;
perPage: number;
page: number;
Expand All @@ -124,7 +115,7 @@ export interface SavedObjectsFindResponsePublic<
interface BatchQueueEntry {
type: string;
id: string;
resolve: <T extends SavedObjectAttributes>(value: SimpleSavedObject<T> | SavedObject<T>) => void;
resolve: <T = unknown>(value: SimpleSavedObject<T> | SavedObject<T>) => void;
reject: (reason?: any) => void;
}

Expand Down Expand Up @@ -207,7 +198,7 @@ export class SavedObjectsClient {
* @param options
* @returns
*/
public create = <T extends SavedObjectAttributes>(
public create = <T = unknown>(
type: string,
attributes: T,
options: SavedObjectsCreateOptions = {}
Expand Down Expand Up @@ -300,7 +291,7 @@ export class SavedObjectsClient {
* @property {object} [options.hasReference] - { type, id }
* @returns A find result with objects matching the specified search.
*/
public find = <T extends SavedObjectAttributes>(
public find = <T = unknown>(
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponsePublic<T>> => {
const path = this.getPath(['_find']);
Expand Down Expand Up @@ -348,10 +339,7 @@ export class SavedObjectsClient {
* @param {string} id
* @returns The saved object for the given type and id.
*/
public get = <T extends SavedObjectAttributes>(
type: string,
id: string
): Promise<SimpleSavedObject<T>> => {
public get = <T = unknown>(type: string, id: string): Promise<SimpleSavedObject<T>> => {
if (!type || !id) {
return Promise.reject(new Error('requires type and id'));
}
Expand Down Expand Up @@ -402,7 +390,7 @@ export class SavedObjectsClient {
* @prop {object} options.migrationVersion - The optional migrationVersion of this document
* @returns
*/
public update<T extends SavedObjectAttributes>(
public update<T = unknown>(
type: string,
id: string,
attributes: T,
Expand Down Expand Up @@ -434,7 +422,7 @@ export class SavedObjectsClient {
* @param {array} objects - [{ type, id, attributes, options: { version, references } }]
* @returns The result of the update operation containing both failed and updated saved objects.
*/
public bulkUpdate<T extends SavedObjectAttributes>(objects: SavedObjectsBulkUpdateObject[] = []) {
public bulkUpdate<T = unknown>(objects: SavedObjectsBulkUpdateObject[] = []) {
const path = this.getPath(['_bulk_update']);

return this.savedObjectsFetch(path, {
Expand All @@ -449,9 +437,7 @@ export class SavedObjectsClient {
});
}

private createSavedObject<T extends SavedObjectAttributes>(
options: SavedObject<T>
): SimpleSavedObject<T> {
private createSavedObject<T = unknown>(options: SavedObject<T>): SimpleSavedObject<T> {
return new SimpleSavedObject(this, options);
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/public/saved_objects/simple_saved_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

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

/**
Expand All @@ -30,7 +30,7 @@ import { SavedObjectsClientContract } from './saved_objects_client';
*
* @public
*/
export class SimpleSavedObject<T extends SavedObjectAttributes> {
export class SimpleSavedObject<T = unknown> {
public attributes: T;
// We want to use the same interface this class had in JS
public _version?: SavedObjectType<T>['version'];
Expand All @@ -46,7 +46,7 @@ export class SimpleSavedObject<T extends SavedObjectAttributes> {
) {
this.id = id;
this.type = type;
this.attributes = attributes || {};
this.attributes = attributes || ({} as T);
this.references = references || [];
this._version = version;
this.migrationVersion = migrationVersion;
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/import/collect_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export async function collectSavedObjects({
supportedTypes,
}: CollectSavedObjectsOptions) {
const errors: SavedObjectsImportError[] = [];
const collectedObjects: SavedObject[] = await createPromiseFromStreams([
const collectedObjects: Array<SavedObject<{ title: string }>> = await createPromiseFromStreams([
readStream,
createLimitStream(objectLimit),
createFilterStream<SavedObject>(obj => {
createFilterStream<SavedObject<{ title: string }>>(obj => {
if (supportedTypes.includes(obj.type)) {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/import/extract_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { SavedObject } from '../types';
import { SavedObjectsImportError } from './types';

export function extractErrors(
savedObjectResults: SavedObject[],
savedObjectsToImport: SavedObject[]
savedObjectResults: Array<SavedObject<any>>,
savedObjectsToImport: Array<SavedObject<any>>
) {
const errors: SavedObjectsImportError[] = [];
const originalSavedObjectsMap = new Map<string, SavedObject>();
const originalSavedObjectsMap = new Map<string, SavedObject<{ title: string }>>();
for (const savedObject of savedObjectsToImport) {
originalSavedObjectsMap.set(`${savedObject.type}:${savedObject.id}`, savedObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function getNonExistingReferenceAsKeys(
}

export async function validateReferences(
savedObjects: SavedObject[],
savedObjects: Array<SavedObject<{ title?: string }>>,
savedObjectsClient: SavedObjectsClientContract,
namespace?: string
) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/management/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ interface SavedObjectsManagementTypeDefinition {
isImportableAndExportable?: boolean;
defaultSearchField?: string;
icon?: string;
getTitle?: (savedObject: SavedObject) => string;
getEditUrl?: (savedObject: SavedObject) => string;
getInAppUrl?: (savedObject: SavedObject) => { path: string; uiCapabilitiesPath: string };
getTitle?: (savedObject: SavedObject<any>) => string;
getEditUrl?: (savedObject: SavedObject<any>) => string;
getInAppUrl?: (savedObject: SavedObject<any>) => { path: string; uiCapabilitiesPath: string };
}

export interface SavedObjectsManagementDefinition {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/serialization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface SavedObjectsRawDocSource {
* scenario out of the box.
*/
interface SavedObjectDoc {
attributes: object;
attributes: unknown;
id?: string; // NOTE: SavedObjectDoc is used for uncreated objects where `id` is optional
type: string;
namespace?: string;
Expand Down
19 changes: 9 additions & 10 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
} from '../saved_objects_client';
import {
SavedObject,
SavedObjectAttributes,
SavedObjectsBaseOptions,
SavedObjectsFindOptions,
SavedObjectsMigrationVersion,
Expand Down Expand Up @@ -213,7 +212,7 @@ export class SavedObjectsRepository {
* @property {array} [options.references=[]] - [{ name, type, id }]
* @returns {promise} - { id, type, version, attributes }
*/
public async create<T extends SavedObjectAttributes>(
public async create<T = unknown>(
type: string,
attributes: T,
options: SavedObjectsCreateOptions = {}
Expand Down Expand Up @@ -254,7 +253,7 @@ export class SavedObjectsRepository {
body: raw._source,
});

return this._rawToSavedObject({
return this._rawToSavedObject<T>({
...raw,
...response,
});
Expand All @@ -277,7 +276,7 @@ export class SavedObjectsRepository {
* @property {string} [options.namespace]
* @returns {promise} - {saved_objects: [[{ id, type, version, references, attributes, error: { message } }]}
*/
async bulkCreate<T extends SavedObjectAttributes = any>(
async bulkCreate<T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options: SavedObjectsCreateOptions = {}
): Promise<SavedObjectsBulkResponse<T>> {
Expand Down Expand Up @@ -464,7 +463,7 @@ export class SavedObjectsRepository {
* @property {object} [options.hasReference] - { type, id }
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }], total, per_page, page }
*/
async find<T extends SavedObjectAttributes = any>({
async find<T = unknown>({
search,
defaultSearchOperator = 'OR',
searchFields,
Expand Down Expand Up @@ -577,7 +576,7 @@ export class SavedObjectsRepository {
* { id: 'foo', type: 'index-pattern' }
* ])
*/
async bulkGet<T extends SavedObjectAttributes = any>(
async bulkGet<T = unknown>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> {
Expand Down Expand Up @@ -648,7 +647,7 @@ export class SavedObjectsRepository {
* @property {string} [options.namespace]
* @returns {promise} - { id, type, version, attributes }
*/
async get<T extends SavedObjectAttributes = any>(
async get<T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
Expand Down Expand Up @@ -696,7 +695,7 @@ export class SavedObjectsRepository {
* @property {array} [options.references] - [{ name, type, id }]
* @returns {promise}
*/
async update<T extends SavedObjectAttributes = any>(
async update<T = unknown>(
type: string,
id: string,
attributes: Partial<T>,
Expand Down Expand Up @@ -753,7 +752,7 @@ export class SavedObjectsRepository {
* @property {string} [options.namespace]
* @returns {promise} - {saved_objects: [[{ id, type, version, references, attributes, error: { message } }]}
*/
async bulkUpdate<T extends SavedObjectAttributes = any>(
async bulkUpdate<T = unknown>(
objects: Array<SavedObjectsBulkUpdateObject<T>>,
options: SavedObjectsBulkUpdateOptions = {}
): Promise<SavedObjectsBulkUpdateResponse<T>> {
Expand Down Expand Up @@ -972,7 +971,7 @@ export class SavedObjectsRepository {
// includes the namespace, and we use this for migrating documents. However, we don't
// want the namespace to be returned from the repository, as the repository scopes each
// method transparently to the specified namespace.
private _rawToSavedObject(raw: SavedObjectsRawDoc): SavedObject {
private _rawToSavedObject<T = unknown>(raw: SavedObjectsRawDoc): SavedObject<T> {
const savedObject = this._serializer.rawToSavedObject(raw);
return omit(savedObject, 'namespace');
}
Expand Down
Loading