import { chain , fromEither , map , tryCatch } from 'fp-ts/lib/TaskEither' ; import { flow } from 'fp-ts/lib/function' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { validateEither } from '@kbn/securitysolution-io-ts-utils' ; import { AcknowledgeSchema , DeleteListSchemaEncoded , ExportListItemQuerySchemaEncoded , FindListSchemaEncoded , FoundListSchema , ImportListItemQuerySchemaEncoded , ImportListItemSchemaEncoded , ListItemIndexExistSchema , ListSchema , acknowledgeSchema , deleteListSchema , exportListItemQuerySchema , findListSchema , foundListSchema , importListItemQuerySchema , importListItemSchema , listItemIndexExistSchema , listSchema , } from '@kbn/securitysolution-io-ts-list-types' ; import { LIST_INDEX , LIST_ITEM_URL , LIST_PRIVILEGES_URL , LIST_URL , } from '@kbn/securitysolution-list-constants' ; import { toError , toPromise } from '../fp_utils' ; import { ApiParams , DeleteListParams , ExportListParams , FindListsParams , ImportListParams , } from './types' ; export type { ApiParams , DeleteListParams , ExportListParams , FindListsParams , ImportListParams , } from './types' ; const findLists = async ( { http , cursor , page , per_page , signal , } ) : Promise < FoundListSchema > => { return http . fetch ( `${ LIST_URL }/_find` , { method : 'GET' , query : { cursor , page , per_page , } , signal , } ) ; } ; const findListsWithValidation = async ( { cursor , http , pageIndex , pageSize , signal , } ) : Promise < FoundListSchema > => pipe ( { cursor : cursor != null ? cursor . toString ( ) : undefined , page : pageIndex != null ? pageIndex . toString ( ) : undefined , per_page : pageSize != null ? pageSize . toString ( ) : undefined , } , ( payload ) => fromEither ( validateEither ( findListSchema , payload ) ) , chain ( ( payload ) => tryCatch ( ( ) => findLists ( { http , signal , ... payload } ) , toError ) ) , chain ( ( response ) => fromEither ( validateEither ( foundListSchema , response ) ) ) , flow ( toPromise ) ) ; export { findListsWithValidation as findLists } ; const importList = async ( { file , http , list_id , type , signal , } ) : Promise < ListSchema > => { const formData = new FormData ( ) ; formData . append ( 'file' , file as Blob ) ; return http . fetch < ListSchema > ( `${ LIST_ITEM_URL }/_import` , { body : formData , headers : { 'Content-Type' : undefined } , method : 'POST' , query : { list_id , type } , signal , } ) ; } ; const importListWithValidation = async ( { file , http , listId , type , signal , } ) : Promise < ListSchema > => pipe ( { list_id : listId , type , } , ( query ) => fromEither ( validateEither ( importListItemQuerySchema , query ) ) , chain ( ( query ) => pipe ( fromEither ( validateEither ( importListItemSchema , { file } ) ) , map ( ( body ) => ( { ... body , ... query } ) ) ) ) , chain ( ( payload ) => tryCatch ( ( ) => importList ( { http , signal , ... payload } ) , toError ) ) , chain ( ( response ) => fromEither ( validateEither ( listSchema , response ) ) ) , toPromise ) ; export { importListWithValidation as importList } ; const deleteList = async ( { deleteReferences = false , http , id , ignoreReferences = false , signal , } ) : Promise < ListSchema > => http . fetch < ListSchema > ( LIST_URL , { method : 'DELETE' , query : { deleteReferences , id , ignoreReferences } , signal , } ) ; const deleteListWithValidation = async ( { deleteReferences , http , id , ignoreReferences , signal , } ) : Promise < ListSchema > => pipe ( { deleteReferences , id , ignoreReferences } , ( payload ) => fromEither ( validateEither ( deleteListSchema , payload ) ) , chain ( ( payload ) => tryCatch ( ( ) => deleteList ( { http , signal , ... payload } ) , toError ) ) , chain ( ( response ) => fromEither ( validateEither ( listSchema , response ) ) ) , flow ( toPromise ) ) ; export { deleteListWithValidation as deleteList } ; const exportList = async ( { http , list_id , signal , } ) : Promise < Blob > => http . fetch < Blob > ( `${ LIST_ITEM_URL }/_export` , { method : 'POST' , query : { list_id } , signal , } ) ; const exportListWithValidation = async ( { http , listId , signal , } ) : Promise < Blob > => pipe ( { list_id : listId } , ( payload ) => fromEither ( validateEither ( exportListItemQuerySchema , payload ) ) , chain ( ( payload ) => tryCatch ( ( ) => exportList ( { http , signal , ... payload } ) , toError ) ) , flow ( toPromise ) ) ; export { exportListWithValidation as exportList } ; const readListIndex = async ( { http , signal } ) : Promise < ListItemIndexExistSchema > => http . fetch < ListItemIndexExistSchema > ( LIST_INDEX , { method : 'GET' , signal , } ) ; const readListIndexWithValidation = async ( { http , signal , } ) : Promise < ListItemIndexExistSchema > => flow ( ( ) => tryCatch ( ( ) => readListIndex ( { http , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( listItemIndexExistSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { readListIndexWithValidation as readListIndex } ; export const readListPrivileges = async ( { http , signal } ) : Promise < unknown > => http . fetch < unknown > ( LIST_PRIVILEGES_URL , { method : 'GET' , signal , } ) ; const createListIndex = async ( { http , signal } ) : Promise < AcknowledgeSchema > => http . fetch < AcknowledgeSchema > ( LIST_INDEX , { method : 'POST' , signal , } ) ; const createListIndexWithValidation = async ( { http , signal , } ) : Promise < AcknowledgeSchema > => flow ( ( ) => tryCatch ( ( ) => createListIndex ( { http , signal } ) , toError ) , chain ( ( response ) => fromEither ( validateEither ( acknowledgeSchema , response ) ) ) , flow ( toPromise ) ) ( ) ; export { createListIndexWithValidation as createListIndex } ;