Skip to content

Commit edf2032

Browse files
committed
Create as code shared search schemas
1 parent 4cefefa commit edf2032

11 files changed

Lines changed: 132 additions & 96 deletions

File tree

src/platform/packages/shared/as-code/shared-schemas/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export {
1212
asCodeMetaSchema,
1313
getMeta,
1414
asCodeQuerySchema,
15+
asCodeSearchRequestPaginationParamsSchema,
16+
asCodeSearchResponseMetaSchema,
1517
type AsCodeMeta,
1618
type AsCodeQuery,
1719
} from './src/schemas';

src/platform/packages/shared/as-code/shared-schemas/src/schemas/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
export { asCodeIdSchema } from './id';
1111
export { asCodeMetaSchema, getMeta, type AsCodeMeta } from './meta';
1212
export { asCodeQuerySchema, type AsCodeQuery } from './query';
13+
export {
14+
asCodeSearchRequestPaginationParamsSchema,
15+
asCodeSearchResponseMetaSchema,
16+
} from './search';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
export {
11+
asCodeSearchRequestPaginationParamsSchema,
12+
asCodeSearchResponseMetaSchema,
13+
} from './search';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
import { schema } from '@kbn/config-schema';
11+
12+
export const asCodeSearchRequestPaginationParamsSchema = schema.object({
13+
page: schema.number({
14+
meta: {
15+
description: 'The page of results to return.',
16+
},
17+
min: 1,
18+
defaultValue: 1,
19+
}),
20+
per_page: schema.number({
21+
meta: {
22+
description: 'The number of results to return per page.',
23+
},
24+
defaultValue: 20,
25+
min: 1,
26+
max: 1000,
27+
}),
28+
});
29+
30+
export const asCodeSearchResponseMetaSchema = schema.object(
31+
{
32+
page: schema.number({
33+
meta: {
34+
description: 'The page of results to return.',
35+
},
36+
min: 1,
37+
defaultValue: 1,
38+
}),
39+
per_page: schema.number({
40+
meta: {
41+
description: 'The number of results to return per page.',
42+
},
43+
defaultValue: 20,
44+
min: 1,
45+
max: 1000,
46+
}),
47+
total: schema.number({
48+
meta: { description: 'The total number of results matching the query.' },
49+
}),
50+
},
51+
{ unknowns: 'forbid' }
52+
);

src/platform/plugins/shared/dashboard/server/api/search/schemas.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { schema } from '@kbn/config-schema';
1111
import { timeRangeSchema } from '@kbn/es-query-server';
12-
import { asCodeMetaSchema } from '@kbn/as-code-shared-schemas';
12+
import { asCodeMetaSchema, asCodeSearchResponseMetaSchema } from '@kbn/as-code-shared-schemas';
1313
import { accessControlSchema } from '../dashboard_state_schemas';
1414

1515
export const searchRequestParamsSchema = schema.object({
@@ -54,7 +54,7 @@ export const searchRequestParamsSchema = schema.object({
5454
});
5555

5656
export const searchResponseBodySchema = schema.object({
57-
dashboards: schema.arrayOf(
57+
data: schema.arrayOf(
5858
schema.object({
5959
id: schema.string({
6060
meta: { description: 'The dashboard ID.' },
@@ -82,10 +82,5 @@ export const searchResponseBodySchema = schema.object({
8282
},
8383
}
8484
),
85-
total: schema.number({
86-
meta: { description: 'The total number of dashboards matching the query.' },
87-
}),
88-
page: schema.number({
89-
meta: { description: 'The current page number.' },
90-
}),
85+
meta: asCodeSearchResponseMetaSchema,
9186
});

src/platform/plugins/shared/dashboard/server/api/search/search.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function search(
4747
});
4848

4949
return {
50-
dashboards: soResponse.saved_objects.map((so) => {
50+
data: soResponse.saved_objects.map((so) => {
5151
const {
5252
dashboardState: { description, tags, time_range, title },
5353
} = transformDashboardOut(so.attributes, so.references);
@@ -68,7 +68,6 @@ export async function search(
6868
meta: getMeta(so),
6969
};
7070
}),
71-
page: soResponse.page,
72-
total: soResponse.total,
71+
meta: { page: soResponse.page, per_page: soResponse.per_page, total: soResponse.total },
7372
};
7473
}

src/platform/plugins/shared/dashboard/test/scout/api/tests/search_dashboards.spec.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
5454
});
5555

5656
expect(response).toHaveStatusCode(200);
57-
expect(response.body.total).toBe(101);
58-
expect(response.body.dashboards).toHaveLength(20);
59-
expect(response.body.dashboards[0].id).toBe('test-dashboard-00');
57+
expect(response.body.meta.total).toBe(101);
58+
expect(response.body.meta.page).toBe(1);
59+
expect(response.body.meta.per_page).toBe(20);
60+
expect(response.body.data).toHaveLength(20);
61+
expect(response.body.data[0].id).toBe('test-dashboard-00');
6062
});
6163

6264
apiTest('should narrow results by query', async ({ apiClient }) => {
@@ -69,8 +71,10 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
6971
});
7072

7173
expect(response).toHaveStatusCode(200);
72-
expect(response.body.total).toBe(1);
73-
expect(response.body.dashboards).toHaveLength(1);
74+
expect(response.body.meta.total).toBe(1);
75+
expect(response.body.meta.page).toBe(1);
76+
expect(response.body.meta.per_page).toBe(20);
77+
expect(response.body.data).toHaveLength(1);
7478
});
7579

7680
apiTest('should allow users to set a per page limit', async ({ apiClient }) => {
@@ -83,8 +87,10 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
8387
});
8488

8589
expect(response).toHaveStatusCode(200);
86-
expect(response.body.total).toBe(101);
87-
expect(response.body.dashboards).toHaveLength(10);
90+
expect(response.body.meta.total).toBe(101);
91+
expect(response.body.meta.page).toBe(1);
92+
expect(response.body.meta.per_page).toBe(10);
93+
expect(response.body.data).toHaveLength(10);
8894
});
8995

9096
apiTest(
@@ -99,9 +105,11 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
99105
});
100106

101107
expect(response).toHaveStatusCode(200);
102-
expect(response.body.total).toBe(101);
103-
expect(response.body.dashboards).toHaveLength(10);
104-
expect(response.body.dashboards[0].id).toBe('test-dashboard-40');
108+
expect(response.body.meta.total).toBe(101);
109+
expect(response.body.meta.page).toBe(5);
110+
expect(response.body.meta.per_page).toBe(10);
111+
expect(response.body.data).toHaveLength(10);
112+
expect(response.body.data[0].id).toBe('test-dashboard-40');
105113
}
106114
);
107115

@@ -115,10 +123,12 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
115123
});
116124

117125
expect(response).toHaveStatusCode(200);
118-
expect(response.body.total).toBe(1);
119-
expect(response.body.dashboards).toHaveLength(1);
120-
expect(response.body.dashboards[0].id).toBe('8d66658a-f5b7-4482-84dc-f41d317473b8');
121-
expect(response.body.dashboards[0].data.tags).toStrictEqual(['tag-2', 'tag-3']);
126+
expect(response.body.meta.total).toBe(1);
127+
expect(response.body.meta.page).toBe(1);
128+
expect(response.body.meta.per_page).toBe(20);
129+
expect(response.body.data).toHaveLength(1);
130+
expect(response.body.data[0].id).toBe('8d66658a-f5b7-4482-84dc-f41d317473b8');
131+
expect(response.body.data[0].data.tags).toStrictEqual(['tag-2', 'tag-3']);
122132
});
123133

124134
apiTest('should narrow results by tags with multiple values', async ({ apiClient }) => {
@@ -131,9 +141,11 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
131141
});
132142

133143
expect(response).toHaveStatusCode(200);
134-
expect(response.body.total).toBe(1);
135-
expect(response.body.dashboards).toHaveLength(1);
136-
expect(response.body.dashboards[0].id).toBe('8d66658a-f5b7-4482-84dc-f41d317473b8');
144+
expect(response.body.meta.total).toBe(1);
145+
expect(response.body.meta.page).toBe(1);
146+
expect(response.body.meta.per_page).toBe(20);
147+
expect(response.body.data).toHaveLength(1);
148+
expect(response.body.data[0].id).toBe('8d66658a-f5b7-4482-84dc-f41d317473b8');
137149
});
138150

139151
apiTest('should narrow results by excluded_tags', async ({ apiClient }) => {
@@ -146,9 +158,11 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
146158
});
147159

148160
expect(response).toHaveStatusCode(200);
149-
expect(response.body.total).toBe(100);
150-
expect(response.body.dashboards).toHaveLength(20);
151-
expect(response.body.dashboards.map((dashboard: { id: string }) => dashboard.id)).not.toContain(
161+
expect(response.body.meta.total).toBe(100);
162+
expect(response.body.meta.page).toBe(1);
163+
expect(response.body.meta.per_page).toBe(20);
164+
expect(response.body.data).toHaveLength(20);
165+
expect(response.body.data.map((dashboard: { id: string }) => dashboard.id)).not.toContain(
152166
'8d66658a-f5b7-4482-84dc-f41d317473b8'
153167
);
154168
});
@@ -166,7 +180,9 @@ apiTest.describe('dashboards - search', { tag: tags.deploymentAgnostic }, () =>
166180
);
167181

168182
expect(response).toHaveStatusCode(200);
169-
expect(response.body.total).toBe(0);
170-
expect(response.body.dashboards).toHaveLength(0);
183+
expect(response.body.meta.total).toBe(0);
184+
expect(response.body.meta.page).toBe(1);
185+
expect(response.body.meta.per_page).toBe(20);
186+
expect(response.body.data).toHaveLength(0);
171187
});
172188
});

src/platform/plugins/shared/dashboard_markdown/server/api/search/schemas.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
*/
99

1010
import { schema } from '@kbn/config-schema';
11-
import { asCodeMetaSchema } from '@kbn/as-code-shared-schemas';
12-
13-
const MAX_PER_PAGE = 10000;
11+
import {
12+
asCodeMetaSchema,
13+
asCodeSearchRequestPaginationParamsSchema,
14+
asCodeSearchResponseMetaSchema,
15+
} from '@kbn/as-code-shared-schemas';
1416

1517
export const searchRequestQuerySchema = schema.object({
1618
query: schema.maybe(
@@ -21,20 +23,7 @@ export const searchRequestQuerySchema = schema.object({
2123
},
2224
})
2325
),
24-
page: schema.maybe(
25-
schema.number({
26-
meta: {
27-
description: 'The page of results to return. Defaults to `1`.',
28-
},
29-
})
30-
),
31-
per_page: schema.maybe(
32-
schema.number({
33-
meta: {
34-
description: 'The number of results to return per page. Defaults to `20`.',
35-
},
36-
})
37-
),
26+
...asCodeSearchRequestPaginationParamsSchema.getPropSchemas(),
3827
});
3928

4029
export const searchResponseBodySchema = schema.object({
@@ -50,20 +39,8 @@ export const searchResponseBodySchema = schema.object({
5039
meta: asCodeMetaSchema,
5140
}),
5241
{
53-
meta: { description: 'The markdown library items matching the search query.' },
54-
minSize: 0,
55-
maxSize: MAX_PER_PAGE,
42+
meta: { description: 'List of markdown library items matching the query.' },
5643
}
5744
),
58-
meta: schema.object({
59-
page: schema.number({
60-
meta: { description: 'The current page number.' },
61-
}),
62-
per_page: schema.number({
63-
meta: { description: 'The number of markdown library items returned in the current page.' },
64-
}),
65-
total: schema.number({
66-
meta: { description: 'The total number of markdown library items matching the query.' },
67-
}),
68-
}),
45+
meta: asCodeSearchResponseMetaSchema,
6946
});

src/platform/plugins/shared/dashboard_markdown/server/api/search/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function search(
4343
}),
4444
meta: {
4545
page: soResponse.page,
46-
per_page: soResponse.saved_objects.length,
46+
per_page: soResponse.per_page,
4747
total: soResponse.total,
4848
},
4949
};

src/platform/plugins/shared/dashboard_markdown/test/scout/api/tests/search_markdowns.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ apiTest.describe('markdown - search', { tag: tags.deploymentAgnostic }, () => {
4242
expect(response).toHaveStatusCode(200);
4343
expect(response.body.meta.total).toBe(TOTAL_MARKDOWNS);
4444
expect(response.body.meta.page).toBe(1);
45-
expect(response.body.meta.per_page).toBe(TOTAL_MARKDOWNS);
46-
expect(response.body.data.length).toBeGreaterThan(1);
45+
expect(response.body.meta.per_page).toBe(20);
46+
expect(response.body.data).toHaveLength(20);
4747
});
4848

4949
apiTest('should narrow results by query', async ({ apiClient }) => {
@@ -58,7 +58,7 @@ apiTest.describe('markdown - search', { tag: tags.deploymentAgnostic }, () => {
5858
expect(response).toHaveStatusCode(200);
5959
expect(response.body.meta.total).toBe(1);
6060
expect(response.body.meta.page).toBe(1);
61-
expect(response.body.meta.per_page).toBe(1);
61+
expect(response.body.meta.per_page).toBe(20);
6262
expect(response.body.data).toHaveLength(1);
6363
});
6464

0 commit comments

Comments
 (0)