import { Provider } from '@liquality/provider' ; import { NodeError } from '@liquality/errors' ; import axios , { AxiosInstance , AxiosRequestConfig } from 'axios' ; import { get } from 'lodash' ; export default class NodeProvider extends Provider { _node : AxiosInstance ; constructor ( config ) { super ( ) ; this . _node = axios . create ( config ) ; } _handleNodeError ( e , context ) { const { name , message , ... attrs } = e ; const data = get ( e , 'response.data' ) ; const errorMessage = data || message ; throw new NodeError ( errorMessage , { ... context , ... attrs } ) ; } nodeGet ( url , params = { } ) { return this . _node . get ( url , { params } ) . then ( ( response ) => response . data ) . catch ( ( e ) => this . _handleNodeError ( e , { url , params } ) ) ; } nodePost ( url , data ) { return this . _node . post ( url , data ) . then ( ( response ) => response . data ) . catch ( ( e ) => this . _handleNodeError ( e , { url , data } ) ) ; } }