Skip to content

Commit c791b6c

Browse files
committed
PR feedback
1 parent 4162397 commit c791b6c

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

x-pack/plugins/cases/public/api/decoders.ts

+14
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import type {
1515
CasesFindResponse,
1616
CasesStatusResponse,
1717
CasesMetricsResponse,
18+
CasesBulkGetResponseCertainFields,
19+
CaseResponse,
1820
} from '../../common/api';
1921
import {
2022
CasesFindResponseRt,
2123
CasesStatusResponseRt,
24+
CasesResponseRt,
25+
getTypeForCertainFieldsFromArray,
2226
CasesMetricsResponseRt,
2327
} from '../../common/api';
2428

@@ -36,3 +40,13 @@ export const decodeCasesMetricsResponse = (metrics?: CasesMetricsResponse) =>
3640
CasesMetricsResponseRt.decode(metrics),
3741
fold(throwErrors(createToasterPlainError), identity)
3842
);
43+
44+
export const decodeCasesBulkGetResponse = <Field extends keyof CaseResponse = keyof CaseResponse>(
45+
res: CasesBulkGetResponseCertainFields<Field>,
46+
fields?: string[]
47+
) => {
48+
const typeToDecode = getTypeForCertainFieldsFromArray(CasesResponseRt, fields);
49+
pipe(typeToDecode.decode(res.cases), fold(throwErrors(createToasterPlainError), identity));
50+
51+
return res;
52+
};

x-pack/plugins/cases/public/api/index.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { httpServiceMock } from '@kbn/core/public/mocks';
99
import { bulkGetCases, getCases, getCasesMetrics } from '.';
10-
import { allCases, allCasesSnake } from '../containers/mock';
10+
import { allCases, allCasesSnake, casesSnake } from '../containers/mock';
1111

1212
describe('api', () => {
1313
beforeEach(() => {
@@ -50,11 +50,19 @@ describe('api', () => {
5050

5151
describe('bulkGetCases', () => {
5252
const http = httpServiceMock.createStartContract({ basePath: '' });
53-
http.post.mockResolvedValue({ cases: allCasesSnake, errors: [] });
53+
http.post.mockResolvedValue({ cases: [{ title: 'test' }], errors: [] });
5454

55-
it('should return the correct response', async () => {
55+
it('should return the correct cases with a subset of fields', async () => {
5656
expect(await bulkGetCases({ http, params: { ids: ['test'], fields: ['title'] } })).toEqual({
57-
cases: allCasesSnake,
57+
cases: [{ title: 'test' }],
58+
errors: [],
59+
});
60+
});
61+
62+
it('should return the correct cases with all fields', async () => {
63+
http.post.mockResolvedValueOnce({ cases: casesSnake, errors: [] });
64+
expect(await bulkGetCases({ http, params: { ids: ['test'] } })).toEqual({
65+
cases: casesSnake,
5866
errors: [],
5967
});
6068
});

x-pack/plugins/cases/public/api/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
} from '../../common/api';
2727
import { convertAllCasesToCamel, convertToCamelCase } from './utils';
2828
import {
29+
decodeCasesBulkGetResponse,
2930
decodeCasesFindResponse,
3031
decodeCasesMetricsResponse,
3132
decodeCasesStatusResponse,
@@ -82,5 +83,5 @@ export const bulkGetCases = async <Field extends keyof CaseResponse = keyof Case
8283
}
8384
);
8485

85-
return res;
86+
return decodeCasesBulkGetResponse(res, params.fields);
8687
};

x-pack/plugins/cases/public/client/api/index.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { httpServiceMock } from '@kbn/core/public/mocks';
99
import { createClientAPI } from '.';
10-
import { allCases, allCasesSnake } from '../../containers/mock';
10+
import { allCases, allCasesSnake, casesSnake } from '../../containers/mock';
1111

1212
describe('createClientAPI', () => {
1313
beforeEach(() => {
@@ -84,11 +84,19 @@ describe('createClientAPI', () => {
8484
describe('bulkGet', () => {
8585
const http = httpServiceMock.createStartContract({ basePath: '' });
8686
const api = createClientAPI({ http });
87-
http.post.mockResolvedValue({ cases: [], errors: [] });
87+
http.post.mockResolvedValue({ cases: [{ title: 'test' }], errors: [] });
8888

89-
it('should return the correct response', async () => {
89+
it('should return the correct cases with a subset of fields', async () => {
90+
expect(await api.cases.bulkGet({ ids: ['test'], fields: ['title'] })).toEqual({
91+
cases: [{ title: 'test' }],
92+
errors: [],
93+
});
94+
});
95+
96+
it('should return the correct cases with all fields', async () => {
97+
http.post.mockResolvedValueOnce({ cases: casesSnake, errors: [] });
9098
expect(await api.cases.bulkGet({ ids: ['test'], fields: ['title'] })).toEqual({
91-
cases: [],
99+
cases: casesSnake,
92100
errors: [],
93101
});
94102
});

x-pack/plugins/cases/server/client/cases/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export interface CasesSubClient {
6666
* Retrieves multiple cases with the specified IDs.
6767
*/
6868
bulkGet<Field extends keyof CaseResponse = keyof CaseResponse>(
69-
params: CasesBulkGetRequestCertainFields<Field | 'id' | 'version' | 'owner'>
70-
): Promise<CasesBulkGetResponseCertainFields<Field | 'id' | 'version' | 'owner'>>;
69+
params: CasesBulkGetRequestCertainFields<Field>
70+
): Promise<CasesBulkGetResponseCertainFields<Field>>;
7171
/**
7272
* Pushes a specific case to an external system.
7373
*/

0 commit comments

Comments
 (0)