import { useMemo } from 'react' ; import type { CreateExceptionListItemSchema , ExceptionListItemSchema , ExceptionListSchema , UpdateExceptionListItemSchema , ApiCallFindListsItemsMemoProps , ApiCallMemoProps , ApiListExportProps , } from '@kbn/securitysolution-io-ts-list-types' ; import * as Api from '@kbn/securitysolution-list-api' ; interface HttpStart { fetch : < T > ( ... args : any ) => any ; } import { getIdsAndNamespaces } from '@kbn/securitysolution-list-utils' ; import { transformInput , transformNewItemOutput , transformOutput } from '../transforms' ; export interface ExceptionsApi { addExceptionListItem : ( arg : { listItem : CreateExceptionListItemSchema ; } ) => Promise < ExceptionListItemSchema > ; updateExceptionListItem : ( arg : { listItem : UpdateExceptionListItemSchema ; } ) => Promise < ExceptionListItemSchema > ; deleteExceptionItem : ( arg : ApiCallMemoProps ) => Promise < void > ; deleteExceptionList : ( arg : ApiCallMemoProps ) => Promise < void > ; getExceptionItem : ( arg : ApiCallMemoProps & { onSuccess : ( arg : ExceptionListItemSchema ) => void ; } ) => Promise < void > ; getExceptionList : ( arg : ApiCallMemoProps & { onSuccess : ( arg : ExceptionListSchema ) => void ; } ) => Promise < void > ; getExceptionListsItems : ( arg : ApiCallFindListsItemsMemoProps ) => Promise < void > ; exportExceptionList : ( arg : ApiListExportProps ) => Promise < void > ; } export const useApi = ( http ) : ExceptionsApi => { return useMemo ( ( ) : ExceptionsApi => ( { async addExceptionListItem ( { listItem , } ) { const abortCtrl = new AbortController ( ) ; const sanitizedItem = transformNewItemOutput ( listItem ) ; return Api . addExceptionListItem ( { http , listItem : sanitizedItem , signal : abortCtrl . signal , } ) ; } , async deleteExceptionItem ( { id , namespaceType , onSuccess , onError , } ) { const abortCtrl = new AbortController ( ) ; try { await Api . deleteExceptionListItemById ( { http , id , namespaceType , signal : abortCtrl . signal , } ) ; onSuccess ( ) ; } catch ( error ) { onError ( error ) ; } } , async deleteExceptionList ( { id , namespaceType , onSuccess , onError , } ) { const abortCtrl = new AbortController ( ) ; try { await Api . deleteExceptionListById ( { http , id , namespaceType , signal : abortCtrl . signal , } ) ; onSuccess ( ) ; } catch ( error ) { onError ( error ) ; } } , async exportExceptionList ( { id , listId , namespaceType , onError , onSuccess , } ) { const abortCtrl = new AbortController ( ) ; try { const blob = await Api . exportExceptionList ( { http , id , listId , namespaceType , signal : abortCtrl . signal , } ) ; onSuccess ( blob ) ; } catch ( error ) { onError ( error ) ; } } , async getExceptionItem ( { id , namespaceType , onSuccess , onError , } ) { const abortCtrl = new AbortController ( ) ; try { const item = transformInput ( await Api . fetchExceptionListItemById ( { http , id , namespaceType , signal : abortCtrl . signal , } ) ) ; onSuccess ( item ) ; } catch ( error ) { onError ( error ) ; } } , async getExceptionList ( { id , namespaceType , onSuccess , onError , } ) { const abortCtrl = new AbortController ( ) ; try { const list = await Api . fetchExceptionListById ( { http , id , namespaceType , signal : abortCtrl . signal , } ) ; onSuccess ( list ) ; } catch ( error ) { onError ( error ) ; } } , async getExceptionListsItems ( { lists , filterOptions , pagination , showDetectionsListsOnly , showEndpointListsOnly , onSuccess , onError , } ) { const abortCtrl = new AbortController ( ) ; const { ids , namespaces } = getIdsAndNamespaces ( { lists , showDetection : showDetectionsListsOnly , showEndpoint : showEndpointListsOnly , } ) ; try { if ( ids . length > 0 && namespaces . length > 0 ) { const { data , page , per_page : perPage , total , } = await Api . fetchExceptionListsItemsByListIds ( { filterOptions , http , listIds : ids , namespaceTypes : namespaces , pagination , signal : abortCtrl . signal , } ) ; onSuccess ( { exceptions : data . map ( ( item ) => transformInput ( item ) ) , pagination : { page , perPage , total , } , } ) ; } else { onSuccess ( { exceptions : [ ] , pagination : { page : 0 , perPage : pagination . perPage != null ? pagination . perPage : 0 , total : 0 , } , } ) ; } } catch ( error ) { onError ( error ) ; } } , async updateExceptionListItem ( { listItem , } ) { const abortCtrl = new AbortController ( ) ; const sanitizedItem = transformOutput ( listItem ) ; return Api . updateExceptionListItem ( { http , listItem : sanitizedItem , signal : abortCtrl . signal , } ) ; } , } ) , [ http ] ) ; } ;