import { Base } from "gd-sprest-def" ; import { OData , RequestType } from "." ; /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ export class MethodInfo implements Base . IMethodInfo { /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ constructor ( methodName , methodInfo , args ) { this . methodInfo = methodInfo ; this . methodInfo . argValues = args ; this . methodInfo . name = typeof ( this . methodInfo . name ) === "string" ? this . methodInfo . name : methodName ; this . generateParams ( ) ; this . methodUrl = this . generateUrl ( ) ; } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ get appendEndpointFl ( ) : boolean { return this . methodInfo . appendEndpointFl ? true : false ; } get body ( ) : string { return this . methodData ; } get getAllItemsFl ( ) : boolean { return this . methodInfo . getAllItemsFl ; } get replaceEndpointFl ( ) : boolean { return this . methodInfo . replaceEndpointFl ? true : false ; } get requestMethod ( ) : string { if ( typeof ( this . methodInfo . requestMethod ) === "string" ) { return this . methodInfo . requestMethod ; } switch ( this . methodInfo . requestType ) { case RequestType . Delete : case RequestType . Post : case RequestType . PostBodyNoArgs : case RequestType . PostWithArgs : case RequestType . PostWithArgsAndData : case RequestType . PostWithArgsInBody : case RequestType . PostWithArgsInQS : case RequestType . PostWithArgsInQSAsVar : case RequestType . PostWithArgsValueOnly : case RequestType . PostReplace : return "POST" ; default : return "GET" ; } } get url ( ) : string { return this . methodUrl ; } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ private get passDataInBody ( ) : boolean { return this . methodInfo . requestType == RequestType . GetWithArgsInBody || this . methodInfo . requestType == RequestType . PostBodyNoArgs || this . methodInfo . requestType == RequestType . PostWithArgsInBody ; } private get passDataInQS ( ) : boolean { return this . methodInfo . requestType == RequestType . GetWithArgsInQS || this . methodInfo . requestType == RequestType . PostWithArgsInQS ; } private get passDataInQSAsVar ( ) : boolean { return this . methodInfo . requestType == RequestType . GetWithArgsInQSAsVar || this . methodInfo . requestType == RequestType . PostWithArgsInQSAsVar ; } private get isTemplate ( ) : boolean { return this . methodInfo . data ? true : false ; } private get replace ( ) : boolean { return this . methodInfo . requestType == RequestType . GetReplace || this . methodInfo . requestType == RequestType . PostReplace ; } private methodData : any ; private methodInfo : Base . IMethodInfo ; private methodParams : any ; private methodUrl : string ; /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ private generateParams ( ) { let maxArgNames = 0 ; let params = { } ; if ( this . methodInfo . argValues == null ) { return ; } if ( this . methodInfo . argNames ) { maxArgNames = this . methodInfo . argNames . length - ( this . methodInfo . requestType == RequestType . PostWithArgsAndData || this . methodInfo . requestType == RequestType . PostReplaceWithData ? 1 : 0 ) ; for ( let i = 0 ; i < maxArgNames && i < this . methodInfo . argValues . length ; i ++ ) { let name = this . methodInfo . argNames [ i ] ; let value = this . methodInfo . argValues [ i ] ; switch ( typeof ( this . methodInfo . argValues [ i ] ) ) { case "boolean" : params [ name ] = this . methodInfo . argValues [ i ] ? "true" : "false" ; break ; case "number" : params [ name ] = this . methodInfo . argValues [ i ] ; break ; default : params [ name ] = value ; break ; } } } let isEmpty = true ; for ( let k in params ) { isEmpty = false ; break ; } this . methodParams = isEmpty ? null : params ; if ( this . methodParams ) { if ( this . isTemplate ) { if ( typeof ( this . methodInfo . data ) !== "string" ) { this . methodInfo . data = JSON . stringify ( this . methodInfo . data ) ; } for ( let key in this . methodParams ) { this . methodInfo . data = this . methodInfo . data . replace ( "[[" + key + "]]" , this . methodParams [ key ] . replace ( /"/g , '\\"' ) . replace ( /\n/g , "" ) ) ; } this . methodData = JSON . parse ( this . methodInfo . data ) ; } } if ( this . methodInfo . argValues && this . methodInfo . argValues . length > 0 ) { if ( this . methodInfo . argNames == null || this . methodInfo . requestType == RequestType . PostBodyNoArgs ) { this . methodData = this . methodInfo . argValues [ 0 ] ; } else if ( this . methodInfo . argValues . length > maxArgNames ) { this . methodData = this . methodInfo . argValues [ maxArgNames ] ; } } if ( this . methodInfo . metadataType ) { if ( this . methodInfo . argNames && this . methodInfo . requestType != RequestType . PostBodyNoArgs && typeof ( ( this . methodData || this . methodParams ) [ this . methodInfo . argNames [ 0 ] ] ) !== "string" ) { ( this . methodData || this . methodParams ) [ this . methodInfo . argNames [ 0 ] ] [ "__metadata" ] = ( this . methodData || this . methodParams ) [ this . methodInfo . argNames [ 0 ] ] [ "__metadata" ] || { "type" : this . methodInfo . metadataType } ; } else { ( this . methodData || this . methodParams ) [ "__metadata" ] = ( this . methodData || this . methodParams ) [ "__metadata" ] || { "type" : this . methodInfo . metadataType } ; } } } private generateUrl ( ) { let url = this . methodInfo . name ; if ( this . methodInfo . requestType == RequestType . Delete ) { url = "deleteObject" ; } if ( this . passDataInBody ) { let data = this . methodData || this . methodParams ; this . methodData = JSON . stringify ( data ) ; } if ( this . passDataInQSAsVar ) { let data = this . methodParams || this . methodData ; url += "(@v)?@v=" + ( typeof ( data ) === "string" ? "'" + encodeURIComponent ( data ) + "'" : JSON . stringify ( data ) ) ; } if ( this . replace ) { for ( let key in this . methodParams ) { url = url . replace ( "[[" + key + "]]" , encodeURIComponent ( this . methodParams [ key ] ) ) ; } } else if ( this . methodInfo . requestType == RequestType . OData ) { let oData = new OData ( this . methodParams [ "oData" ] ) ; url = "?" + oData . QueryString ; this . methodInfo . getAllItemsFl = oData . GetAllItems ; } else if ( ! this . passDataInBody && ! this . passDataInQSAsVar ) { let params = "" ; let data = this . methodParams || this . methodData ; if ( data ) { data = data && typeof ( data ) === "object" ? data : { value : data } ; for ( let name in data ) { let value = data [ name ] ; value = typeof ( value ) === "string" ? "'" + value . replace ( /'/g , "''" ) + "'" : value ; switch ( this . methodInfo . requestType ) { case RequestType . GetWithArgsValueOnly : case RequestType . PostWithArgsValueOnly : params += value + ", " ; break ; default : params += name + "=" + value + ", " ; break ; } } } if ( this . passDataInQS ) { url += params . length > 0 ? "?" + params . replace ( /, $/ , "&" ) : "" ; } else { url += params . length > 0 ? "(" + params . replace ( /, $/ , "" ) + ")" : "" ; } } return url ; } }