import { notification } from 'antd' ; import { either as Either , taskEither as TaskEither } from 'fp-ts' ; import { pipe } from 'fp-ts/function' ; export type CoreCommand = 'export' | 'list' | 'tags' | 'names' | 'collections' | 'abis' | 'rm' | 'status' | 'help' | 'scraper' | 'blocks' | 'transactions' | 'receipts' | 'logs' | 'traces' | 'when' | 'state' | 'tokens' | 'quotes' | 'slurp' | 'pins' ; export type CommandParams = Record < string , string | boolean | number > ; export type JsonResponse = Record < string , any > ; function turnResponseIntoJson ( response ) { return TaskEither . tryCatch ( ( ) : Promise < JsonResponse > => response . json ( ) , Either . toError ) ; } function validateStatus ( response ) { return pipe ( response , TaskEither . fromPredicate ( ( ) => response . ok , ( ) => Either . toError ( response . statusText ) ) ) ; } export function runCommand ( command , params ? ) { const url = new URL ( command , process . env . CORE_URL ) ; url . search = new URLSearchParams ( params as Record < string , string > ) . toString ( ) ; return pipe ( TaskEither . tryCatch ( ( ) => fetch ( url . toString ( ) ) , ( error ) => { notification [ 'warning' ] ( { message : `Error running "${ command }"` , description : `${ error }` , } ) ; return Either . toError ( error ) ; } ) , TaskEither . chain ( validateStatus ) , TaskEither . chain ( turnResponseIntoJson ) ) ( ) ; }