import * as fs from 'fs' ; import { hub , RequestContext , TypeDef , ProviderDef , NamedObjectDef } from './index' ; export function listApplications ( request ) { return hub ( ) . applicationInstanceNames ; } const DEFAULT_RANK_USER_ENTRYPOINT = 700 ; export function listEntryPoints ( request , input = { detail : false , allowGlobal : true , allowPrivate : false } ) { let entryPointDefs = [ ] ; request . app . objectContext . forEach ( namedObject => { let def = namedObject . def ; if ( def . value . _type === 'EntryPoint' && ( input . allowPrivate || ! namedObject . def . private ) && ( input . allowGlobal || namedObject . scope !== "global" ) ) { entryPointDefs . push ( namedObject . def ) ; } } ) ; entryPointDefs = entryPointDefs . sort ( ( a , b ) : number => { let rankA = isNaN ( a . value [ 'displayRank' ] ) ? DEFAULT_RANK_USER_ENTRYPOINT : a . value [ 'displayRank' ] ; let rankB = isNaN ( b . value [ 'displayRank' ] ) ? DEFAULT_RANK_USER_ENTRYPOINT : b . value [ 'displayRank' ] ; if ( rankA != rankB ) { return rankA - rankB ; } return a . name < b . name ? - 1 : 1 ; } ) ; return input . detail ? entryPointDefs : entryPointDefs . map ( ( def ) => { return def . name ; } ) ; } export function listNamedObjects ( request , input = { allowPrivate : false , scopes : [ 'request' , './application' ] } ) { let objectNames = [ ] ; request . app . objectContext . forEach ( namedObject => { if ( ( input . allowPrivate || ! namedObject . def . private ) && ( namedObject . scope in input . scopes ) ) { objectNames . push ( namedObject . def . name ) ; } } ) ; return objectNames ; } export function getNamedObject ( request , input ) { if ( input == null || input . name == null ) { throw new Error ( "'name' property must be specified under 'input' object of request." ) ; } let object = request . getNamedObject ( input . name ) ; if ( object == null || object . def == null ) { return null ; } return object . def ; } export function listTypes ( request ) { let appDef = request . app . settings ; let typeNames = [ ] ; for ( let typeDef of appDef . objectContextDef . typeDefs ) { typeNames . push ( typeDef . typeName ) ; } return typeNames ; } export function getType ( request , input ) { if ( input == null || input . typeName == null ) { throw new Error ( "'typeName' property must be specified under 'input' object of request." ) ; } let appDef = request . app . settings ; let types = appDef . objectContextDef . typeDefs ; for ( let i = 0 ; i < types . length ; i ++ ) { if ( types [ i ] . typeName . toLowerCase ( ) == input . typeName . toLowerCase ( ) ) { return types [ i ] ; } } throw new Error ( "Type name '" + input . typeName + "' is not supported in current application." ) ; } export function listProviders ( request ) { let appDef = request . app . settings ; let protocolNames = [ ] ; for ( let providerDef of appDef . objectContextDef . providerDefs ) { protocolNames . push ( providerDef . protocol ) ; } return protocolNames ; } export function getProvider ( request , input ) { if ( input == null || input . protocolName == null ) { throw new Error ( "'protocolName' property must be specified under 'input' object of request." ) ; } let appDef = request . app . settings ; let providers = appDef . objectContextDef . providerDefs ; for ( let provider of providers ) { if ( provider . protocol . toLowerCase ( ) === input . protocolName . toLowerCase ( ) ) { return provider ; } } throw new Error ( "Protocol name '" + input . protocolName + "' is not supported in current application." ) ; }