import axios from 'axios' ; import config from '@config' ; import featureConfig from '@config/feature.config' ; import To from '@app/common/to' ; interface Props { type ? : 'query' | 'mutation' ; headers ? : { 'accessToken' ? : string ; 'user-agent' ? : string ; 'Cache-Control' ? : string ; 'role' ? : 'admin' ; } ; cache ? : boolean ; apis : Array < { aliases ? : string ; api : string ; args ? : any ; fields ? : string ; } > ; multiple ? : boolean ; } export default async ( { type = 'query' , headers = { } , cache = false , apis , multiple = false } ) => { let sql = '' ; apis . map ( ( { aliases = '' , api , args = { } , fields = '' } ) => { args = convertParamsFormat ( args ) ; sql += `${ aliases ? aliases + ':' : '' }${ api }${ args }` ; if ( fields ) { sql += `{
        ${ fields }
      }
      ` ; } } ) ; return To ( new Promise ( async ( resolve , reject ) => { let url = config . api . graphql . client ; let option = { url , method : 'post' , headers , data : { operationName : null , variables : { } , query : `${ type }{
          ${ sql }
        }` } } ; let result = await new Promise ( resolve => { axios ( option ) . then ( resp => { if ( resp && resp . data ) { resolve ( resp . data ) ; } else { resolve ( { errors : [ { message : 'return none' } ] } ) ; } } ) . catch ( function ( error ) { console . log ( error ) ; if ( error . response && error . response . data ) { resolve ( error . response . data ) ; } else if ( error . message ) { resolve ( { errors : [ { message : error . message } ] } ) ; } else { resolve ( { errors : [ { message : 'return none' } ] } ) ; } } ) ; } ) ; if ( featureConfig . apiLog ) { console . log ( `${ type }{
        ${ sql }
      }` ) ; console . log ( result ) ; } if ( result && ! result . errors ) { if ( apis . length == 1 && ! multiple ) { resolve ( result . data [ apis [ 0 ] . aliases || apis [ 0 ] . api ] ) ; } else { resolve ( result . data ) ; } } else if ( result && result . errors ) { if ( result . errors . length != 0 ) { result . errors . map ( ( item ) => { item = converterErrorInfo ( item ) ; } ) ; reject ( result . errors [ 0 ] ) ; } else { if ( console . error ) { console . error ( 'GraphQL request error' ) ; console . error ( `${ type }{
            ${ sql }
          }` ) ; console . error ( result . errors ) ; } reject ( { 'message' : JSON . stringify ( result . errors ) } ) ; } } else { if ( console . error ) { console . error ( 'GraphQL API 请求发生未知的错误' ) ; console . error ( `${ type }{
          ${ sql }
        }` ) ; console . error ( result ) ; } } } ) ) ; } ; const StringAs = ( string ) => { return '"' + string . replace ( /(\\|\"|\n|\r)/g , "\\$1" ) + '"' ; } ; const convertParamsFormat = ( params ) => { let arr = [ ] ; for ( let i in params ) { let v = '' ; switch ( typeof params [ i ] ) { case 'string' : v = StringAs ( params [ i ] ) ; break ; case 'object' : v = JSON . stringify ( params [ i ] ) ; break ; default : v = params [ i ] ; } arr . push ( i + ':' + v ) ; } let str = '(' + arr . join ( ',' ) + ')' ; return arr . length > 0 ? str : '' ; } ; param { String } string param { String } key param { String } value return { String } const synthesis = ( string , key , value ) => { return string . replace ( new RegExp ( "({" + key + "})" , "g" ) , value ) ; } ; const converterErrorInfo = ( err ) => { if ( err . data && err . data . error_data ) { for ( let n in err . data . error_data ) { err . message = synthesis ( err . message , n , err . data . error_data [ n ] ) ; } } return err ; } ;