-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
find.ts
106 lines (93 loc) · 3.7 KB
/
find.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { type Type, schema } from '@kbn/config-schema';
import type { IRouter } from '@kbn/core/server';
import type { v1 } from '../../common';
import { injectMetaAttributes, toSavedObjectWithMeta } from '../lib';
import type { ISavedObjectsManagement } from '../services';
export const registerFindRoute = (
router: IRouter,
managementServicePromise: Promise<ISavedObjectsManagement>
) => {
const referenceSchema = schema.object({
type: schema.string(),
id: schema.string(),
});
const searchOperatorSchema = schema.oneOf([schema.literal('OR'), schema.literal('AND')], {
defaultValue: 'OR',
});
const sortFieldSchema: Type<keyof v1.SavedObjectWithMetadata> = schema.oneOf([
schema.literal('created_at'),
schema.literal('updated_at'),
schema.literal('type'),
]);
router.get(
{
path: '/api/kibana/management/saved_objects/_find',
security: {
authz: {
enabled: false,
reason: 'This route is opted out from authorization',
},
},
validate: {
query: schema.object({
perPage: schema.number({ min: 0, defaultValue: 20 }),
page: schema.number({ min: 0, defaultValue: 1 }),
type: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
search: schema.maybe(schema.string()),
defaultSearchOperator: searchOperatorSchema,
sortField: schema.maybe(sortFieldSchema),
sortOrder: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
hasReference: schema.maybe(
schema.oneOf([referenceSchema, schema.arrayOf(referenceSchema)])
),
hasReferenceOperator: searchOperatorSchema,
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { query } = req;
const managementService = await managementServicePromise;
const { getClient, typeRegistry } = (await context.core).savedObjects;
const searchTypes = Array.isArray(query.type) ? query.type : [query.type];
const importAndExportableTypes = searchTypes.filter((type) =>
typeRegistry.isImportableAndExportable(type)
);
const includedHiddenTypes = importAndExportableTypes.filter((type) =>
typeRegistry.isHidden(type)
);
const client = getClient({ includedHiddenTypes });
const searchFields = new Set<string>();
importAndExportableTypes.forEach((type) => {
const searchField = managementService.getDefaultSearchField(type);
if (searchField) {
searchFields.add(searchField);
}
});
const findResponse = await client.find<any>({
...query,
fields: undefined,
searchFields: [...searchFields],
});
const savedObjects = findResponse.saved_objects.map(toSavedObjectWithMeta);
const response: v1.FindResponseHTTP = {
saved_objects: savedObjects.map((so) => {
const obj = injectMetaAttributes(so, managementService);
const result = { ...obj, attributes: {} as Record<string, unknown> };
return result;
}),
total: findResponse.total,
per_page: findResponse.per_page,
page: findResponse.page,
};
return res.ok({ body: response });
})
);
};