Skip to content

Commit

Permalink
Make type option required for SavedObjects.find
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolf committed Aug 6, 2019
1 parent 6cd3a67 commit 0313b50
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Search for objects
<b>Signature:</b>

```typescript
find: <T extends SavedObjectAttributes>(options?: Pick<SavedObjectFindOptionsServer, "search" | "type" | "defaultSearchOperator" | "searchFields" | "sortField" | "hasReference" | "page" | "perPage" | "fields">) => Promise<SavedObjectsFindResponsePublic<T>>;
find: <T extends SavedObjectAttributes>(options: Pick<SavedObjectFindOptionsServer, "search" | "type" | "defaultSearchOperator" | "searchFields" | "sortField" | "hasReference" | "page" | "perPage" | "fields">) => Promise<SavedObjectsFindResponsePublic<T>>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export declare class SavedObjectsClient
| [bulkGet](./kibana-plugin-public.savedobjectsclient.bulkget.md) | | <code>(objects?: {</code><br/><code> id: string;</code><br/><code> type: string;</code><br/><code> }[]) =&gt; Promise&lt;SavedObjectsBatchResponse&lt;SavedObjectAttributes&gt;&gt;</code> | Returns an array of objects by id |
| [create](./kibana-plugin-public.savedobjectsclient.create.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(type: string, attributes: T, options?: SavedObjectsCreateOptions) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Persists an object |
| [delete](./kibana-plugin-public.savedobjectsclient.delete.md) | | <code>(type: string, id: string) =&gt; Promise&lt;{}&gt;</code> | Deletes an object |
| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(options?: Pick&lt;SavedObjectFindOptionsServer, &quot;search&quot; &#124; &quot;type&quot; &#124; &quot;defaultSearchOperator&quot; &#124; &quot;searchFields&quot; &#124; &quot;sortField&quot; &#124; &quot;hasReference&quot; &#124; &quot;page&quot; &#124; &quot;perPage&quot; &#124; &quot;fields&quot;&gt;) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T&gt;&gt;</code> | Search for objects |
| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(options: Pick&lt;SavedObjectFindOptionsServer, &quot;search&quot; &#124; &quot;type&quot; &#124; &quot;defaultSearchOperator&quot; &#124; &quot;searchFields&quot; &#124; &quot;sortField&quot; &#124; &quot;hasReference&quot; &#124; &quot;page&quot; &#124; &quot;perPage&quot; &#124; &quot;fields&quot;&gt;) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T&gt;&gt;</code> | Search for objects |
| [get](./kibana-plugin-public.savedobjectsclient.get.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(type: string, id: string) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Fetches a single object |

## Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
type?: string | string[];
type: string | string[];
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
type?: string | string[];
type: string | string[];
```
4 changes: 2 additions & 2 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export class SavedObjectsClient {
}[]) => Promise<SavedObjectsBatchResponse<SavedObjectAttributes>>;
create: <T extends SavedObjectAttributes>(type: string, attributes: T, options?: SavedObjectsCreateOptions) => Promise<SimpleSavedObject<T>>;
delete: (type: string, id: string) => Promise<{}>;
find: <T extends SavedObjectAttributes>(options?: Pick<SavedObjectsFindOptions, "search" | "type" | "defaultSearchOperator" | "searchFields" | "sortField" | "hasReference" | "page" | "perPage" | "fields">) => Promise<SavedObjectsFindResponsePublic<T>>;
find: <T extends SavedObjectAttributes>(options: Pick<SavedObjectsFindOptions, "search" | "type" | "defaultSearchOperator" | "searchFields" | "sortField" | "hasReference" | "page" | "perPage" | "fields">) => Promise<SavedObjectsFindResponsePublic<T>>;
get: <T extends SavedObjectAttributes>(type: string, id: string) => Promise<SimpleSavedObject<T>>;
update<T extends SavedObjectAttributes>(type: string, id: string, attributes: T, { version, migrationVersion, references }?: SavedObjectsUpdateOptions): Promise<SimpleSavedObject<T>>;
}
Expand Down Expand Up @@ -714,7 +714,7 @@ export interface SavedObjectsFindOptions extends SavedObjectsBaseOptions {
// (undocumented)
sortOrder?: string;
// (undocumented)
type?: string | string[];
type: string | string[];
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class SavedObjectsClient {
* @returns A find result with objects matching the specified search.
*/
public find = <T extends SavedObjectAttributes>(
options: SavedObjectsFindOptions = {}
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponsePublic<T>> => {
const path = this.getPath(['_find']);
const renameMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function fetchObjectsToExport({
savedObjectsClient: SavedObjectsClientContract;
namespace?: string;
}) {
if (objects) {
if (objects && objects.length > 0) {
if (objects.length > exportSizeLimit) {
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
}
Expand All @@ -66,18 +66,21 @@ async function fetchObjectsToExport({
throw err;
}
return bulkGetResult.saved_objects;
} else if (types && types.length > 0) {
const findResponse = await savedObjectsClient.find({
type: types,
sortField: '_id',
sortOrder: 'asc',
perPage: exportSizeLimit,
namespace,
});
if (findResponse.total > exportSizeLimit) {
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
}
return findResponse.saved_objects;
} else {
throw Boom.badRequest('Either `type` or `objects` are required.');
}
const findResponse = await savedObjectsClient.find({
type: types,
sortField: '_id',
sortOrder: 'asc',
perPage: exportSizeLimit,
namespace,
});
if (findResponse.total > exportSizeLimit) {
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
}
return findResponse.saved_objects;
}

export async function getSortedObjectsForExport({
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface SavedObjectReference {
* @public
*/
export interface SavedObjectsFindOptions extends SavedObjectsBaseOptions {
type?: string | string[];
type: string | string[];
page?: number;
perPage?: number;
sortField?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export interface SavedObjectsFindOptions extends SavedObjectsBaseOptions {
// (undocumented)
sortOrder?: string;
// (undocumented)
type?: string | string[];
type: string | string[];
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class EncryptedSavedObjectsClientWrapper implements SavedObjectsClientCon
return await this.options.baseClient.delete(type, id, options);
}

public async find(options: SavedObjectsFindOptions = {}) {
public async find(options: SavedObjectsFindOptions) {
return this.stripEncryptedAttributesFromBulkResponse(
await this.options.baseClient.find(options)
);
Expand Down
9 changes: 5 additions & 4 deletions x-pack/legacy/plugins/siem/server/lib/note/saved_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class Note {

public async deleteNoteByTimelineId(request: FrameworkRequest, timelineId: string) {
const options: SavedObjectsFindOptions = {
type: noteSavedObjectType,
search: timelineId,
searchFields: ['timelineId'],
};
Expand All @@ -69,6 +70,7 @@ export class Note {
eventId: string
): Promise<NoteSavedObject[]> {
const options: SavedObjectsFindOptions = {
type: noteSavedObjectType,
search: eventId,
searchFields: ['eventId'],
};
Expand All @@ -81,6 +83,7 @@ export class Note {
timelineId: string
): Promise<NoteSavedObject[]> {
const options: SavedObjectsFindOptions = {
type: noteSavedObjectType,
search: timelineId,
searchFields: ['timelineId'],
};
Expand All @@ -95,6 +98,7 @@ export class Note {
sort: SortNote | null
): Promise<ResponseNotes> {
const options: SavedObjectsFindOptions = {
type: noteSavedObjectType,
perPage: pageInfo != null ? pageInfo.pageSize : undefined,
page: pageInfo != null ? pageInfo.pageIndex : undefined,
search: search != null ? search : undefined,
Expand Down Expand Up @@ -196,10 +200,7 @@ export class Note {
request[internalFrameworkRequest]
);

const savedObjects = await savedObjectsClient.find({
type: noteSavedObjectType,
...options,
});
const savedObjects = await savedObjectsClient.find(options);

return {
totalCount: savedObjects.total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class PinnedEvent {

public async deleteAllPinnedEventsOnTimeline(request: FrameworkRequest, timelineId: string) {
const options: SavedObjectsFindOptions = {
type: pinnedEventSavedObjectType,
search: timelineId,
searchFields: ['timelineId'],
};
Expand All @@ -68,6 +69,7 @@ export class PinnedEvent {
timelineId: string
): Promise<PinnedEventSavedObject[]> {
const options: SavedObjectsFindOptions = {
type: pinnedEventSavedObjectType,
search: timelineId,
searchFields: ['timelineId'],
};
Expand All @@ -81,6 +83,7 @@ export class PinnedEvent {
sort: SortNote | null
): Promise<PinnedEventSavedObject[]> {
const options: SavedObjectsFindOptions = {
type: pinnedEventSavedObjectType,
perPage: pageInfo != null ? pageInfo.pageSize : undefined,
page: pageInfo != null ? pageInfo.pageIndex : undefined,
search: search != null ? search : undefined,
Expand Down Expand Up @@ -181,10 +184,7 @@ export class PinnedEvent {
request[internalFrameworkRequest]
);

const savedObjects = await savedObjectsClient.find({
type: pinnedEventSavedObjectType,
...options,
});
const savedObjects = await savedObjectsClient.find(options);

return savedObjects.saved_objects.map(savedObject =>
convertSavedObjectToSavedPinnedEvent(savedObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class Timeline {
sort: SortTimeline | null
): Promise<ResponseTimelines> {
const options: SavedObjectsFindOptions = {
type: timelineSavedObjectType,
perPage: pageInfo != null ? pageInfo.pageSize : undefined,
page: pageInfo != null ? pageInfo.pageIndex : undefined,
search: search != null ? search : undefined,
Expand Down Expand Up @@ -266,10 +267,7 @@ export class Timeline {
}`;
}

const savedObjects = await savedObjectsClient.find({
type: timelineSavedObjectType,
...options,
});
const savedObjects = await savedObjectsClient.find(options);

const timelinesWithNotesAndPinnedEvents = await Promise.all(
savedObjects.saved_objects.map(async savedObject => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ const createSpacesService = async (spaceId: string) => {
types,
});

await expect(client.find({ namespace: 'bar' })).rejects.toThrowErrorMatchingSnapshot();
await expect(
client.find({ type: 'foo', namespace: 'bar' })
).rejects.toThrowErrorMatchingSnapshot();
});

test(`passes options.type to baseClient if valid singular type specified`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class SpacesSavedObjectsClient implements SavedObjectsClientContract {
* @property {object} [options.hasReference] - { type, id }
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }], total, per_page, page }
*/
public async find(options: SavedObjectsFindOptions = {}) {
public async find(options: SavedObjectsFindOptions) {
throwErrorIfNamespaceSpecified(options);

return await this.client.find({
Expand Down

0 comments on commit 0313b50

Please sign in to comment.