import { chain , fromEither , tryCatch } from 'fp-ts/lib/TaskEither' ; import { flow } from 'fp-ts/lib/function' ; import { validateEither } from '@kbn/securitysolution-io-ts-utils' ; import { CreateEndpointListSchema , ExceptionListItemSchema , ExceptionListSchema , FoundExceptionListItemSchema , FoundExceptionListSchema , createEndpointListSchema , exceptionListItemSchema , exceptionListSchema , foundExceptionListItemSchema , foundExceptionListSchema , AddEndpointExceptionListProps , AddExceptionListItemProps , AddExceptionListProps , ApiCallByIdProps , ApiCallByListIdProps , ApiCallFetchExceptionListsProps , ExportExceptionListProps , UpdateExceptionListItemProps , UpdateExceptionListProps , } from '@kbn/securitysolution-io-ts-list-types' ; import { ENDPOINT_LIST_URL , EXCEPTION_LIST_ITEM_URL , EXCEPTION_LIST_NAMESPACE , EXCEPTION_LIST_NAMESPACE_AGNOSTIC , EXCEPTION_LIST_URL , } from '@kbn/securitysolution-list-constants' ; import { toError , toPromise } from '../fp_utils' ; param http param list param signal throws const addExceptionList = async ( { http , list , signal , } ) : Promise < ExceptionListSchema > => http . fetch < ExceptionListSchema > ( EXCEPTION_LIST_URL , { body : JSON . stringify ( list ) , method : 'POST' , signal , } ) ; const addExceptionListWithValidation = async ( { http , list , signal , } ) : Promise < ExceptionListSchema > => flow ( ( ) => tryCatch ( ( ) => addExceptionList ( { http , list , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { addExceptionListWithValidation as addExceptionList } ; param http param listItem param signal throws const addExceptionListItem = async ( { http , listItem , signal , } ) : Promise < ExceptionListItemSchema > => http . fetch < ExceptionListItemSchema > ( EXCEPTION_LIST_ITEM_URL , { body : JSON . stringify ( listItem ) , method : 'POST' , signal , } ) ; const addExceptionListItemWithValidation = async ( { http , listItem , signal , } ) : Promise < ExceptionListItemSchema > => flow ( ( ) => tryCatch ( ( ) => addExceptionListItem ( { http , listItem , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListItemSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { addExceptionListItemWithValidation as addExceptionListItem } ; param http param list param signal throws const updateExceptionList = async ( { http , list , signal , } ) : Promise < ExceptionListSchema > => http . fetch < ExceptionListSchema > ( EXCEPTION_LIST_URL , { body : JSON . stringify ( list ) , method : 'PUT' , signal , } ) ; const updateExceptionListWithValidation = async ( { http , list , signal , } ) : Promise < ExceptionListSchema > => flow ( ( ) => tryCatch ( ( ) => updateExceptionList ( { http , list , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { updateExceptionListWithValidation as updateExceptionList } ; param http param listItem param signal throws const updateExceptionListItem = async ( { http , listItem , signal , } ) : Promise < ExceptionListItemSchema > => http . fetch < ExceptionListItemSchema > ( EXCEPTION_LIST_ITEM_URL , { body : JSON . stringify ( listItem ) , method : 'PUT' , signal , } ) ; const updateExceptionListItemWithValidation = async ( { http , listItem , signal , } ) : Promise < ExceptionListItemSchema > => flow ( ( ) => tryCatch ( ( ) => updateExceptionListItem ( { http , listItem , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListItemSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { updateExceptionListItemWithValidation as updateExceptionListItem } ; param http param namespaceTypes param filters param pagination param signal throws const fetchExceptionLists = async ( { http , filters , namespaceTypes , pagination , signal , } ) : Promise < FoundExceptionListSchema > => { const query = { filter : filters , namespace_type : namespaceTypes , page : pagination . page ? `${ pagination . page }` : '1' , per_page : pagination . perPage ? `${ pagination . perPage }` : '20' , sort_field : 'exception-list.created_at' , sort_order : 'desc' , } ; return http . fetch < FoundExceptionListSchema > ( `${ EXCEPTION_LIST_URL }/_find` , { method : 'GET' , query , signal , } ) ; } ; const fetchExceptionListsWithValidation = async ( { filters , http , namespaceTypes , pagination , signal , } ) : Promise < FoundExceptionListSchema > => flow ( ( ) => tryCatch ( ( ) => fetchExceptionLists ( { filters , http , namespaceTypes , pagination , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( foundExceptionListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { fetchExceptionListsWithValidation as fetchExceptionLists } ; param http param id param namespaceType param signal throws const fetchExceptionListById = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListSchema > => http . fetch < ExceptionListSchema > ( EXCEPTION_LIST_URL , { method : 'GET' , query : { id , namespace_type : namespaceType } , signal , } ) ; const fetchExceptionListByIdWithValidation = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListSchema > => flow ( ( ) => tryCatch ( ( ) => fetchExceptionListById ( { http , id , namespaceType , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { fetchExceptionListByIdWithValidation as fetchExceptionListById } ; param http param listIds param namespaceTypes param filterOptions param pagination param signal throws const fetchExceptionListsItemsByListIds = async ( { http , listIds , namespaceTypes , filterOptions , pagination , signal , } ) : Promise < FoundExceptionListItemSchema > => { const filters = filterOptions . map < string > ( ( filter , index ) => { const namespace = namespaceTypes [ index ] ; const filterNamespace = namespace === 'agnostic' ? EXCEPTION_LIST_NAMESPACE_AGNOSTIC : EXCEPTION_LIST_NAMESPACE ; const formattedFilters = [ ... ( filter . filter . length ? [ `${ filterNamespace }.attributes.entries.field:${ filter . filter }*` ] : [ ] ) , ... ( filter . tags . length ? filter . tags . map ( ( t ) => `${ filterNamespace }.attributes.tags:${ t }` ) : [ ] ) , ] ; return formattedFilters . join ( ' AND ' ) ; } ) . join ( ',' ) ; const query = { list_id : listIds . join ( ',' ) , namespace_type : namespaceTypes . join ( ',' ) , page : pagination . page ? `${ pagination . page }` : '1' , per_page : pagination . perPage ? `${ pagination . perPage }` : '20' , sort_field : 'exception-list.created_at' , sort_order : 'desc' , ... ( filters . trim ( ) !== '' ? { filter : filters } : { } ) , } ; return http . fetch < FoundExceptionListItemSchema > ( `${ EXCEPTION_LIST_ITEM_URL }/_find` , { method : 'GET' , query , signal , } ) ; } ; const fetchExceptionListsItemsByListIdsWithValidation = async ( { filterOptions , http , listIds , namespaceTypes , pagination , signal , } ) : Promise < FoundExceptionListItemSchema > => flow ( ( ) => tryCatch ( ( ) => fetchExceptionListsItemsByListIds ( { filterOptions , http , listIds , namespaceTypes , pagination , signal , } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( foundExceptionListItemSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { fetchExceptionListsItemsByListIdsWithValidation as fetchExceptionListsItemsByListIds } ; param http param id param namespaceType param signal throws const fetchExceptionListItemById = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListItemSchema > => http . fetch < ExceptionListItemSchema > ( EXCEPTION_LIST_ITEM_URL , { method : 'GET' , query : { id , namespace_type : namespaceType } , signal , } ) ; const fetchExceptionListItemByIdWithValidation = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListItemSchema > => flow ( ( ) => tryCatch ( ( ) => fetchExceptionListItemById ( { http , id , namespaceType , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListItemSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { fetchExceptionListItemByIdWithValidation as fetchExceptionListItemById } ; param http param id param namespaceType param signal throws const deleteExceptionListById = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListSchema > => http . fetch < ExceptionListSchema > ( EXCEPTION_LIST_URL , { method : 'DELETE' , query : { id , namespace_type : namespaceType } , signal , } ) ; const deleteExceptionListByIdWithValidation = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListSchema > => flow ( ( ) => tryCatch ( ( ) => deleteExceptionListById ( { http , id , namespaceType , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { deleteExceptionListByIdWithValidation as deleteExceptionListById } ; param http param id param namespaceType param signal throws const deleteExceptionListItemById = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListItemSchema > => http . fetch < ExceptionListItemSchema > ( EXCEPTION_LIST_ITEM_URL , { method : 'DELETE' , query : { id , namespace_type : namespaceType } , signal , } ) ; const deleteExceptionListItemByIdWithValidation = async ( { http , id , namespaceType , signal , } ) : Promise < ExceptionListItemSchema > => flow ( ( ) => tryCatch ( ( ) => deleteExceptionListItemById ( { http , id , namespaceType , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( exceptionListItemSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { deleteExceptionListItemByIdWithValidation as deleteExceptionListItemById } ; param http param signal throws const addEndpointExceptionList = async ( { http , signal , } ) : Promise < CreateEndpointListSchema > => http . fetch < ExceptionListItemSchema > ( ENDPOINT_LIST_URL , { method : 'POST' , signal , } ) ; const addEndpointExceptionListWithValidation = async ( { http , signal , } ) : Promise < CreateEndpointListSchema > => flow ( ( ) => tryCatch ( ( ) => addEndpointExceptionList ( { http , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( createEndpointListSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { addEndpointExceptionListWithValidation as addEndpointExceptionList } ; param http param id param listId param namespaceType param signal throws export const exportExceptionList = async ( { http , id , listId , namespaceType , signal , } ) : Promise < Blob > => http . fetch < Blob > ( `${ EXCEPTION_LIST_URL }/_export` , { method : 'POST' , query : { id , list_id : listId , namespace_type : namespaceType } , signal , } ) ;