import { computed } from '@vue/composition-api' ; import { Context , sharedRef , Logger , configureFactoryParams , FactoryParams , ComposableFunctionArgs , } from '@vue-storefront/core' ; import { PlatformApi } from '@vue-storefront/core/lib/src/types' ; import { UseConfig } from '../types/composables' ; export interface UseConfigFactoryParams < CONFIG , API extends PlatformApi = any > extends FactoryParams < API > { loadConfig : ( context : Context , params ? : ComposableFunctionArgs < { } > ) => Promise < CONFIG > ; } export function useConfigFactory < CONFIG , API extends PlatformApi = any > ( factoryParams ) { return function useConfig ( cacheId = '' ) : UseConfig < CONFIG , API > { const ssrKey = cacheId || 'useConfigFactory' ; const config = sharedRef < CONFIG > ( { } , `${ ssrKey }-config` ) ; const loading = sharedRef < boolean > ( false , `${ ssrKey }-loading` ) ; const _factoryParams = configureFactoryParams ( factoryParams ) ; const loadConfig = async ( params ? ) => { Logger . debug ( `useConfig/${ ssrKey }/loadConfig` ) ; loading . value = true ; try { config . value = await _factoryParams . loadConfig ( params ) ; } finally { loading . value = false ; } } ; return { loadConfig , config : computed ( ( ) => config . value ) , loading : computed ( ( ) => loading . value ) , } ; } ; }