import { WebSiteManagementModels } from '@azure/arm-appservice' ; import * as url from 'url' ; import { AzExtTreeItem , IActionContext , TreeItemIconPath } from 'vscode-azureextensionui' ; import { HttpAuthLevel , ParsedFunctionJson } from '../funcConfig/function' ; import { IParsedHostJson } from '../funcConfig/host' ; import { FuncVersion } from '../FuncVersion' ; import { localize } from '../localize' ; import { treeUtils } from '../utils/treeUtils' ; import { FunctionsTreeItemBase } from './FunctionsTreeItemBase' ; import { ApplicationSettings , FuncHostRequest } from './IProjectTreeItem' ; import { getProjectContextValue , ProjectResource } from './projectContextValues' ; export abstract class FunctionTreeItemBase extends AzExtTreeItem { public readonly parent : FunctionsTreeItemBase ; public readonly name : string ; protected readonly _config : ParsedFunctionJson ; private _disabled : boolean ; private _func : WebSiteManagementModels . FunctionEnvelope | undefined ; protected constructor ( parent , config , name , func ) { super ( parent ) ; this . _config = config ; this . name = name ; this . _func = func ; this . commandId = 'azureFunctions.viewProperties' ; } public get id ( ) : string { return this . name ; } public get label ( ) : string { return this . name ; } public get isHttpTrigger ( ) : boolean { return ! ! this . _func ?. invokeUrlTemplate || this . _config . isHttpTrigger ; } public get isTimerTrigger ( ) : boolean { return this . _config . isTimerTrigger ; } public get isAnonymous ( ) : boolean { return this . _config . authLevel === HttpAuthLevel . anonymous ; } public get rawConfig ( ) : { } { return this . _config . data ; } public get triggerBindingType ( ) : string | undefined { return this . _config . triggerBinding ?. type ; } public get contextValue ( ) : string { let triggerType ; if ( this . isHttpTrigger ) { triggerType = 'Http' ; } else if ( this . isTimerTrigger ) { triggerType = 'Timer' ; } else { triggerType = 'Unknown' ; } const state = this . _disabled ? 'Disabled' : 'Enabled' ; return getProjectContextValue ( this . parent . parent . source , this . parent . access , ProjectResource . Function , triggerType , state ) ; } public get description ( ) : string | undefined { const descriptions = [ ] ; if ( this . isHttpTrigger ) { descriptions . push ( localize ( 'http' , 'HTTP' ) ) ; } else if ( this . isTimerTrigger ) { descriptions . push ( localize ( 'timer' , 'Timer' ) ) ; } if ( this . _disabled ) { descriptions . push ( localize ( 'disabledFunction' , 'Disabled' ) ) ; } return descriptions . join ( ' - ' ) ; } public get iconPath ( ) : TreeItemIconPath { return treeUtils . getIconPath ( 'azFuncFunction' ) ; } public get disabledStateKey ( ) : string { return `AzureWebJobs.${ this . name }.Disabled` ; } public abstract getKey ( context ) ; public async initAsync ( context ) { await this . refreshDisabledState ( context ) ; } public async getTriggerRequest ( context ) { if ( ! this . isHttpTrigger ) { return undefined ; } else { const funcHostReq = await this . parent . parent . getHostRequest ( context ) ; const hostUrl = new url . URL ( funcHostReq . url ) ; let triggerUrl ; if ( this . _func ?. invokeUrlTemplate ) { triggerUrl = new url . URL ( this . _func ?. invokeUrlTemplate ) ; triggerUrl . protocol = hostUrl . protocol ; } else { triggerUrl = hostUrl ; const route = ( this . _config . triggerBinding && this . _config . triggerBinding . route ) || this . name ; const hostJson = await this . parent . parent . getHostJson ( context ) ; triggerUrl . pathname = `${ hostJson . routePrefix }/${ route }` ; } const key = await this . getKey ( context ) ; if ( key ) { triggerUrl . searchParams . set ( 'code' , key ) ; } return { url : triggerUrl . toString ( ) , rejectUnauthorized : funcHostReq . rejectUnauthorized } ; } } /**
     * https://docs.microsoft.com/azure/azure-functions/disable-function
     */ private async refreshDisabledState ( context ) { if ( this . _func ) { this . _disabled = ! ! this . _func . isDisabled ; } else { const version = await this . parent . parent . getVersion ( context ) ; if ( version === FuncVersion . v1 ) { this . _disabled = this . _config . disabled ; } else { const appSettings = await this . parent . parent . getApplicationSettings ( context ) ; /**
                 * The docs only officially mentioned 'true' and 'false', but here is what I found:
                 * The following resulted in a disabled function:
                 * true, tRue, 1
                 * The following resulted in an enabled function:
                 * false, fAlse, 0, fdsaf, 2, undefined
                 */ this . _disabled = /^(1|true)$/i . test ( appSettings [ this . disabledStateKey ] || '' ) ; } } } }