Skip to content

Commit

Permalink
getting close maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jan 25, 2022
1 parent 9226fd4 commit e40651c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ interface BatchGetQueueEntry {
interface BatchResolveQueueEntry {
type: string;
id: string;
resolve: <T = unknown>(value: ResolvedSimpleSavedObject<T>) => void;
resolve: <T extends SavedObjectAttributes = SavedObjectAttributes>(
value: ResolvedSimpleSavedObject<T>
) => void;
reject: (reason?: any) => void;
}

Expand Down Expand Up @@ -504,7 +506,7 @@ export class SavedObjectsClient {
* API is unique to the public client, which batches individual calls with `bulkResolve` under the hood. We don't throw an error in that
* case for legacy compatibility reasons.
*/
public resolve = <T = unknown>(
public resolve = <T extends SavedObjectAttributes = SavedObjectAttributes>(
type: string,
id: string
): Promise<ResolvedSimpleSavedObject<T>> => {
Expand Down
6 changes: 4 additions & 2 deletions src/core/server/saved_objects/object_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*
* @internal
*/
export interface LegacyUrlAlias {
// type required for index signature
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type LegacyUrlAlias = {
/**
* The original ID of the object, before it was converted.
*/
Expand Down Expand Up @@ -52,4 +54,4 @@ export interface LegacyUrlAlias {
* `SavedObjectsClient.collectMultiNamespaceReferences()`.
*/
disabled?: boolean;
}
};
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,7 @@ export class SavedObjectsRepository {
* }
* ```
*/
createPointInTimeFinder<T = unknown, A = unknown>(
createPointInTimeFinder<T extends SavedObjectAttributes = SavedObjectAttributes, A = unknown>(
findOptions: SavedObjectsCreatePointInTimeFinderOptions,
dependencies?: SavedObjectsCreatePointInTimeFinderDependencies
): ISavedObjectsPointInTimeFinder<T, A> {
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/service/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import uuidv1 from 'uuid/v1';
import uuidv5 from 'uuid/v5';
import { SavedObjectsFindOptions } from '../../types';
import { SavedObjectsFindOptions, SavedObjectAttributes } from '../../types';
import { SavedObjectsFindResponse } from '..';

export const DEFAULT_NAMESPACE_STRING = 'default';
Expand Down Expand Up @@ -53,7 +53,7 @@ export class SavedObjectsUtils {
/**
* Creates an empty response for a find operation. This is only intended to be used by saved objects client wrappers.
*/
public static createEmptyFindResponse = <T, A>({
public static createEmptyFindResponse = <T extends SavedObjectAttributes, A>({
page = FIND_DEFAULT_PAGE,
perPage = FIND_DEFAULT_PER_PAGE,
}: SavedObjectsFindOptions): SavedObjectsFindResponse<T, A> => ({
Expand Down
6 changes: 4 additions & 2 deletions src/core/types/saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { Serializable } from '@kbn/utility-types';

/**
* Don't use this type, it's simply a helper type for {@link SavedObjectAttribute}
*
Expand All @@ -24,7 +26,7 @@ export type SavedObjectAttributeSingle =
*
* @public
*/
export type SavedObjectAttribute = SavedObjectAttributeSingle | SavedObjectAttributeSingle[];
export type SavedObjectAttribute = Serializable;

/**
* The data for a Saved Object is stored as an object in the `attributes`
Expand All @@ -33,7 +35,7 @@ export type SavedObjectAttribute = SavedObjectAttributeSingle | SavedObjectAttri
* @public
*/
export interface SavedObjectAttributes {
[key: string]: SavedObjectAttribute;
[key: string]: Serializable;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/data/common/search/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { SerializableRecord } from '@kbn/utility-types';
import { SearchSessionStatus } from './status';

export const SEARCH_SESSION_TYPE = 'search-session';
export interface SearchSessionSavedObjectAttributes {

// type required for index signature
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type SearchSessionSavedObjectAttributes = {
sessionId: string;
/**
* User-facing session name to be displayed in session management
Expand Down Expand Up @@ -76,9 +79,11 @@ export interface SearchSessionSavedObjectAttributes {
* Version information to display warnings when trying to restore a session from a different version
*/
version: string;
}
};

export interface SearchSessionRequestInfo {
// type required for index signature
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type SearchSessionRequestInfo = {
/**
* ID of the async search request
*/
Expand All @@ -95,7 +100,7 @@ export interface SearchSessionRequestInfo {
* An optional error. Set if status is set to error.
*/
error?: string;
}
};

export interface SearchSessionFindOptions {
page?: number;
Expand Down
25 changes: 18 additions & 7 deletions src/plugins/data_views/server/saved_objects_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { SavedObjectsClientContract, SavedObject } from 'src/core/server';
import { SavedObjectsClientContract, SavedObject, SavedObjectAttributes } from 'src/core/server';
import {
SavedObjectsClientCommon,
SavedObjectsClientCommonFindArgs,
Expand All @@ -15,32 +15,43 @@ import {

export class SavedObjectsClientServerToCommon implements SavedObjectsClientCommon {
private savedObjectClient: SavedObjectsClientContract;

constructor(savedObjectClient: SavedObjectsClientContract) {
this.savedObjectClient = savedObjectClient;
}
async find<T = unknown>(options: SavedObjectsClientCommonFindArgs) {

async find<T extends SavedObjectAttributes = SavedObjectAttributes>(
options: SavedObjectsClientCommonFindArgs
) {
const result = await this.savedObjectClient.find<T>(options);
return result.saved_objects;
}

async get<T = unknown>(type: string, id: string) {
async get<T extends SavedObjectAttributes = SavedObjectAttributes>(type: string, id: string) {
const response = await this.savedObjectClient.resolve<T>(type, id);
if (response.outcome === 'conflict') {
throw new DataViewSavedObjectConflictError(id);
}
return response.saved_object;
}
async update(

async update<T extends SavedObjectAttributes = SavedObjectAttributes>(
type: string,
id: string,
attributes: Record<string, any>,
attributes: Partial<T>,
options: Record<string, any>
) {
return (await this.savedObjectClient.update(type, id, attributes, options)) as SavedObject;
return (await this.savedObjectClient.update(type, id, attributes, options)) as SavedObject<T>;
}
async create(type: string, attributes: Record<string, any>, options: Record<string, any>) {

async create<T extends SavedObjectAttributes = SavedObjectAttributes>(
type: string,
attributes: T,
options: Record<string, any>
) {
return await this.savedObjectClient.create(type, attributes, options);
}

delete(type: string, id: string) {
return this.savedObjectClient.delete(type, id);
}
Expand Down

0 comments on commit e40651c

Please sign in to comment.