import { ApiManagementModels } from "@azure/arm-apimanagement" ; import { ApiContract , OperationCollection , Protocol } from "@azure/arm-apimanagement/src/models" ; import { window } from "vscode" ; import { AzureParentTreeItem , DialogResponses , IOpenApiImportObject , IParsedError , parseError , UserCancelledError } from "../../extension.bundle" ; import * as Constants from "../constants" ; import { IServiceTreeRoot } from "../explorer/IServiceTreeRoot" ; import { ext } from "../extensionVariables" ; import { localize } from "../localize" ; export namespace apiUtil { export async function askApiName ( defaultName ? ) { const apiNamePrompt = localize ( 'apiNamePrompt' , 'Enter API Name.' ) ; return ( await ext . ui . showInputBox ( { prompt : apiNamePrompt , value : defaultName , validateInput : async ( value ) : Promise < string | undefined > => { value = value ? value . trim ( ) : '' ; return validateApiName ( value ) ; } } ) ) . trim ( ) ; } export function genApiId ( apiName ) { const identifier = displayNameToIdentifier ( apiName ) ; return `/apis/${ identifier }` ; } export function validateApiName ( apiName ) { if ( apiName . length > Constants . maxApiNameLength ) { return localize ( "apiNameMaxLength" , `API name cannot be more than ${ Constants . maxApiNameLength } characters long.` ) ; } if ( apiName . match ( /^[^*#&+:<>?]+$/ ) === null ) { return localize ( "apiNameInvalid" , 'Invalid API Name.' ) ; } return undefined ; } function removeAccents ( str ) { const accents = "ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøĎďDŽdžÈÉÊËèéêëðÇçČčÐÌÍÎÏìíîïÙÚÛÜùúûüĽĹľĺÑŇňñŔŕŠšŤťŸÝÿýŽž" ; const accentsOut = "AAAAAAaaaaaasOOOOOOOooooooDdDZdzEEEEeeeeeCcCcDIIIIiiiiUUUUuuuuLLllNNnnRrSsTtYYyyZz" ; const chars = str . split ( "" ) ; chars . forEach ( ( letter , index ) => { const i = accents . indexOf ( letter ) ; if ( i !== - 1 ) { chars [ index ] = accentsOut [ i ] ; } } ) ; return chars . join ( "" ) ; } export function displayNameToIdentifier ( value ) { const invalidIdCharsRegExp = new RegExp ( Constants . invalidIdCharRegEx , "ig" ) ; let identifier = value && value . replace ( invalidIdCharsRegExp , "-" ) . trim ( ) . replace ( /-+/g , "-" ) . substr ( 0 , Constants . maxApiNameLength ) . replace ( /(^-)|(-$)/g , "" ) . toLowerCase ( ) ; identifier = removeAccents ( identifier ) ; return identifier ; } export async function createOrUpdateApiWithSwaggerObject ( node , apiName , document ) { document . info . title = apiName ; await checkApiExist ( node , apiName ) ; const openApiImportPayload = { displayName : apiName , path : apiName , format : undefined , value : '' } ; if ( document . schemes === undefined ) { openApiImportPayload . protocols = [ "https" ] ; } else { const protocols = [ ] ; if ( document . schemes . indexOf ( "http" ) !== - 1 ) { protocols . push ( "http" ) ; } if ( document . schemes . indexOf ( "https" ) !== - 1 ) { protocols . push ( "https" ) ; } openApiImportPayload . protocols = protocols ; } openApiImportPayload . format = document . importFormat ; openApiImportPayload . value = JSON . stringify ( document . sourceDocument ) ; const options = { ifMatch : "*" } ; return await node . root . client . api . createOrUpdate ( node . root . resourceGroupName , node . root . serviceName , apiName , openApiImportPayload , options ) ; } export async function checkApiExist ( node , apiName ) { let apiExists = true ; try { await node . root . client . api . get ( node . root . resourceGroupName , node . root . serviceName , apiName ) ; } catch ( error ) { const err = parseError ( error ) ; if ( err . errorType . toLocaleLowerCase ( ) === 'notfound' || err . errorType . toLowerCase ( ) === 'resourcenotfound' ) { apiExists = false ; } } if ( apiExists ) { const overwriteFlag = await window . showWarningMessage ( localize ( "apiAlreadyExists" , `API "${ apiName }" already exists. Import will trigger an 'Override' of exisiting API. Do you want to continue?` ) , { modal : true } , DialogResponses . yes , DialogResponses . cancel ) ; if ( overwriteFlag !== DialogResponses . yes ) { throw new UserCancelledError ( ) ; } } } export async function getAllOperationsForApi ( root , apiId ) { let operations = await root . client . apiOperation . listByApi ( root . resourceGroupName , root . serviceName , apiId ) ; let nextLink = operations . nextLink ; while ( nextLink ) { const nOperations = await root . client . apiOperation . listByApiNext ( nextLink ) ; nextLink = nOperations . nextLink ; operations = operations . concat ( nOperations ) ; } return operations ; } export function isNotApiRevision ( api ) { return api . isCurrent !== undefined && api . isCurrent === true ; } }