import { ParameterContract } from "@azure/arm-apimanagement/src/models" ; export function parseUrlTemplate ( uriTemplate ) { let cleanTemplate = "" ; const parameters = [ ] ; let templateStart = 0 ; let parameterStart = 0 ; let parameterDepth = 0 ; for ( let i = 0 ; i < uriTemplate . length ; i ++ ) { if ( uriTemplate [ i ] === "{" ) { if ( parameterDepth === 0 ) { parameterStart = i + 1 ; } parameterDepth ++ ; cleanTemplate += uriTemplate . substring ( templateStart , i ) ; templateStart = i ; } else if ( uriTemplate [ i ] === "}" && -- parameterDepth === 0 ) { if ( parameterStart < i ) { const parameter = parseParameter ( uriTemplate . substring ( parameterStart , i ) ) ; cleanTemplate += `{${ parameter . name }}` ; parameters . push ( parameter ) ; } templateStart = i + 1 ; } } cleanTemplate += uriTemplate . substring ( templateStart ) ; return { urlTemplate : cleanTemplate , parameters : parameters } ; } function parseParameter ( param ) { const nameAndType = param . split ( /:|=|\?/ , 3 ) ; const defaultValue = param . split ( "=" , 3 ) ; const parameter = { name : nameAndType [ 0 ] . startsWith ( "*" ) ? nameAndType [ 0 ] . substr ( 1 ) : nameAndType [ 0 ] , type : nameAndType . length > 1 ? mapParameterType ( nameAndType [ 1 ] ) : "" , required : ! param . endsWith ( "?" ) } ; if ( defaultValue . length > 1 ) { parameter . defaultValue = defaultValue [ 1 ] . endsWith ( "?" ) ? defaultValue [ 1 ] . substr ( 0 , defaultValue [ 1 ] . length - 1 ) : defaultValue [ 1 ] ; } return parameter ; } function mapParameterType ( type ) { switch ( type ) { case "alpha" : case "datetime" : case "guid" : return "string" ; case "decimal" : case "float" : case "double" : return "number" ; case "int" : case "long" : return "integer" ; case "bool" : return "boolean" ; default : if ( type . startsWith ( "length(" ) || type . startsWith ( "maxlength(" ) || type . startsWith ( "minlength(" ) || type . startsWith ( "regex(" ) ) { return "string" ; } if ( type . startsWith ( "min(" ) || type . startsWith ( "max(" ) || type . startsWith ( "range(" ) ) { return "integer" ; } return "" ; } }