Skip to content

Commit

Permalink
Add missing AbortController to API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Mar 26, 2020
1 parent 535b933 commit 6a3d47f
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 19 deletions.
43 changes: 35 additions & 8 deletions x-pack/legacy/plugins/siem/public/containers/case/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
ServiceConnectorCaseResponse,
ActionTypeExecutorResult,
} from '../../../../../../plugins/case/common/api';

import { KibanaServices } from '../../lib/kibana';

import {
ActionLicense,
AllCases,
Expand All @@ -31,7 +33,9 @@ import {
SortFieldCase,
CaseUserActions,
} from './types';

import { CASES_URL } from './constants';

import {
convertToCamelCase,
convertAllCasesToCamel,
Expand All @@ -45,12 +49,17 @@ import {
decodeServiceConnectorCaseResponse,
} from './utils';

export const getCase = async (caseId: string, includeComments: boolean = true): Promise<Case> => {
export const getCase = async (
caseId: string,
includeComments: boolean = true,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(`${CASES_URL}/${caseId}`, {
method: 'GET',
query: {
includeComments,
},
signal,
});
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};
Expand All @@ -66,9 +75,10 @@ export const getCasesStatus = async (signal: AbortSignal): Promise<CasesStatus>
return convertToCamelCase<CasesStatusResponse, CasesStatus>(decodeCasesStatusResponse(response));
};

export const getTags = async (): Promise<string[]> => {
export const getTags = async (signal: AbortSignal): Promise<string[]> => {
const response = await KibanaServices.get().http.fetch<string[]>(`${CASES_URL}/tags`, {
method: 'GET',
signal,
});
return response ?? [];
};
Expand Down Expand Up @@ -108,6 +118,7 @@ export const getCases = async ({
sortField: SortFieldCase.createdAt,
sortOrder: 'desc',
},
signal,
}: FetchCasesProps): Promise<AllCases> => {
const query = {
reporters: filterOptions.reporters.map(r => r.username),
Expand All @@ -119,44 +130,57 @@ export const getCases = async ({
const response = await KibanaServices.get().http.fetch<CasesFindResponse>(`${CASES_URL}/_find`, {
method: 'GET',
query,
signal,
});
return convertAllCasesToCamel(decodeCasesFindResponse(response));
};

export const postCase = async (newCase: CaseRequest): Promise<Case> => {
export const postCase = async (newCase: CaseRequest, signal: AbortSignal): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(CASES_URL, {
method: 'POST',
body: JSON.stringify(newCase),
signal,
});
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

export const patchCase = async (
caseId: string,
updatedCase: Partial<CaseRequest>,
version: string
version: string,
signal: AbortSignal
): Promise<Case[]> => {
const response = await KibanaServices.get().http.fetch<CasesResponse>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases: [{ ...updatedCase, id: caseId, version }] }),
signal,
});
return convertToCamelCase<CasesResponse, Case[]>(decodeCasesResponse(response));
};

export const patchCasesStatus = async (cases: BulkUpdateStatus[]): Promise<Case[]> => {
export const patchCasesStatus = async (
cases: BulkUpdateStatus[],
signal: AbortSignal
): Promise<Case[]> => {
const response = await KibanaServices.get().http.fetch<CasesResponse>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases }),
signal,
});
return convertToCamelCase<CasesResponse, Case[]>(decodeCasesResponse(response));
};

export const postComment = async (newComment: CommentRequest, caseId: string): Promise<Comment> => {
export const postComment = async (
newComment: CommentRequest,
caseId: string,
signal: AbortSignal
): Promise<Comment> => {
const response = await KibanaServices.get().http.fetch<CommentResponse>(
`${CASES_URL}/${caseId}/comments`,
{
method: 'POST',
body: JSON.stringify(newComment),
signal,
}
);
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response));
Expand All @@ -166,22 +190,25 @@ export const patchComment = async (
caseId: string,
commentId: string,
commentUpdate: string,
version: string
version: string,
signal: AbortSignal
): Promise<Partial<Comment>> => {
const response = await KibanaServices.get().http.fetch<CommentResponse>(
`${CASES_URL}/${caseId}/comments`,
{
method: 'PATCH',
body: JSON.stringify({ comment: commentUpdate, id: commentId, version }),
signal,
}
);
return convertToCamelCase<CommentResponse, Comment>(decodeCommentResponse(response));
};

export const deleteCases = async (caseIds: string[]): Promise<boolean> => {
export const deleteCases = async (caseIds: string[], signal: AbortSignal): Promise<boolean> => {
const response = await KibanaServices.get().http.fetch<string>(CASES_URL, {
method: 'DELETE',
query: { ids: JSON.stringify(caseIds) },
signal,
});
return response === 'true' ? true : false;
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/siem/public/containers/case/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface ElasticUser {
readonly username: string;
}

export interface FetchCasesProps {
export interface FetchCasesProps extends ApiProps {
queryParams?: QueryParams;
filterOptions?: FilterOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ export const useUpdateCases = (): UseUpdateCase => {

const dispatchUpdateCases = useCallback((cases: BulkUpdateStatus[]) => {
let cancel = false;
const abortCtrl = new AbortController();

const patchData = async () => {
try {
dispatch({ type: 'FETCH_INIT' });
await patchCasesStatus(cases);
await patchCasesStatus(cases, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: true });
}
Expand All @@ -87,6 +89,7 @@ export const useUpdateCases = (): UseUpdateCase => {
patchData();
return () => {
cancel = true;
abortCtrl.abort();
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export const useDeleteCases = (): UseDeleteCase => {

const dispatchDeleteCases = useCallback((caseIds: string[]) => {
let cancel = false;
const abortCtrl = new AbortController();

const deleteData = async () => {
try {
dispatch({ type: 'FETCH_INIT' });
await deleteCases(caseIds);
await deleteCases(caseIds, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: true });
}
Expand All @@ -94,6 +96,7 @@ export const useDeleteCases = (): UseDeleteCase => {
};
deleteData();
return () => {
abortCtrl.abort();
cancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ export const useGetCase = (caseId: string): CaseState => {

const callFetch = () => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT' });
try {
const response = await getCase(caseId);
const response = await getCase(caseId, true, abortCtrl.signal);
if (!didCancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: response });
}
Expand All @@ -98,6 +100,7 @@ export const useGetCase = (caseId: string): CaseState => {
fetchData();
return () => {
didCancel = true;
abortCtrl.abort();
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,15 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {

const fetchCases = useCallback((filterOptions: FilterOptions, queryParams: QueryParams) => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT', payload: 'cases' });
try {
const response = await getCases({
filterOptions,
queryParams,
signal: abortCtrl.signal,
});
if (!didCancel) {
dispatch({
Expand All @@ -175,6 +178,7 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
}, []);
Expand All @@ -187,13 +191,17 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
const dispatchUpdateCaseProperty = useCallback(
({ updateKey, updateValue, caseId, refetchCasesStatus, version }: UpdateCase) => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT', payload: 'caseUpdate' });
try {
await patchCase(
caseId,
{ [updateKey]: updateValue },
version ?? '' // saved object versions are typed as string | undefined, hope that's not true
// saved object versions are typed as string | undefined, hope that's not true
version ?? '',
abortCtrl.signal
);
if (!didCancel) {
dispatch({ type: 'FETCH_UPDATE_CASE_SUCCESS' });
Expand All @@ -209,6 +217,7 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export const useGetTags = (): TagsState => {

useEffect(() => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT' });
try {
const response = await getTags();
const response = await getTags(abortCtrl.signal);
if (!didCancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: response });
}
Expand All @@ -77,6 +79,7 @@ export const useGetTags = (): TagsState => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ export const usePostCase = (): UsePostCase => {

const postMyCase = useCallback(async (data: CaseRequest) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT' });
const response = await postCase({ ...data, status: 'open' });
const response = await postCase({ ...data, status: 'open' }, abortCtrl.signal);
if (!cancel) {
dispatch({
type: 'FETCH_SUCCESS',
Expand All @@ -81,6 +83,7 @@ export const usePostCase = (): UsePostCase => {
}
}
return () => {
abortCtrl.abort();
cancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export const usePostComment = (caseId: string): UsePostComment => {

const postMyComment = useCallback(async (data: CommentRequest) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT' });
const response = await postComment(data, state.caseId);
const response = await postComment(data, state.caseId, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: response });
}
Expand All @@ -89,6 +91,7 @@ export const usePostComment = (caseId: string): UsePostComment => {
}
}
return () => {
abortCtrl.abort();
cancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const usePostPushToService = (): UsePostPushToService => {
const abortCtrl = new AbortController();
try {
dispatch({ type: 'FETCH_INIT' });
const casePushData = await getCase(caseId);
const casePushData = await getCase(caseId, true, abortCtrl.signal);
const responseService = await pushToService(
connectorId,
formatServiceRequestData(casePushData),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ export const useUpdateCase = (caseId: string, initialData: Case): UseUpdateCase
const dispatchUpdateCaseProperty = useCallback(
async ({ fetchCaseUserActions, updateKey, updateValue }: UpdateByKey) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT', payload: updateKey });
const response = await patchCase(
caseId,
{ [updateKey]: updateValue },
state.caseData.version
state.caseData.version,
abortCtrl.signal
);
if (!cancel) {
if (fetchCaseUserActions != null) {
Expand All @@ -108,6 +111,7 @@ export const useUpdateCase = (caseId: string, initialData: Case): UseUpdateCase
}
return () => {
cancel = true;
abortCtrl.abort();
};
},
[state]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const useUpdateComment = (comments: Comment[]): UseUpdateComment => {
const dispatchUpdateComment = useCallback(
async ({ caseId, commentId, commentUpdate, fetchUserActions }: UpdateComment) => {
let cancel = false;
const abortCtrl = new AbortController();
try {
dispatch({ type: 'FETCH_INIT', payload: commentId });
const currentComment = state.comments.find(comment => comment.id === commentId) ?? {
Expand All @@ -102,7 +103,8 @@ export const useUpdateComment = (comments: Comment[]): UseUpdateComment => {
caseId,
commentId,
commentUpdate,
currentComment.version
currentComment.version,
abortCtrl.signal
);
if (!cancel) {
fetchUserActions();
Expand All @@ -120,6 +122,7 @@ export const useUpdateComment = (comments: Comment[]): UseUpdateComment => {
}
return () => {
cancel = true;
abortCtrl.abort();
};
},
[state]
Expand Down

0 comments on commit 6a3d47f

Please sign in to comment.