{"version":3,"file":"runtimeConfigurationService.mjs","sources":["../../lib/services/runtimeConfigurationService.ts"],"sourcesContent":["// @ts-nocheck\n/**\n * Vue.js plugin to consume configurations using environment variables\n */\n\nimport { InjectionKey, type App } from 'vue';\nimport merge from 'merge-anything';\nimport { configSchema, type ResolvedConfig } from '../configurations/config-schema';\n// The configuration object that contains symbols for overwriting during runtime\n// import config from '../../config/runtime-config';\n\ndeclare module '@vue/runtime-core' {\n  interface ComponentCustomProperties {\n    $env: ResolvedConfig;\n  }\n}\n\nexport const injectionKey: InjectionKey<ResolvedConfig> = Symbol('runtimeConfig');\n\n// Takes a base configuration (e.g., process.env) and a runtime configuration\n// and merges the runtime configuration over the base configuration to overwrite it.\n// Overwritten values are always of type string. Pass an empty string to model a falsy value.\n\nconst RuntimeConfiguration = {\n  install(app: App, runtimeConfig, options = {}) {\n    const defaultOptions = {\n      debug: true,\n      baseConfig: import.meta.env,\n      useExperimentalRuntimeParser: false,\n    };\n    const opts = Object.assign({}, defaultOptions, options);\n\n    // Custom merge rule to ignore values that start with $VITE_\n    // i.e., use this.$env property when environment variable is not set\n    const ignoreUnusedVariables = (originVal, newVal) => {\n      if (typeof newVal === 'string') {\n\n        // 1. Evaluate Arrays\n        if (newVal.startsWith('[') && newVal.endsWith(']')) {\n          // This looks like an array\n          // Use JSON.parse to transform it into a real array\n          try {\n            return JSON.parse(newVal);\n          } catch (error) {\n            console.error(\"Invalid JSON, please note that the JSON standard requires double quotes: \\n\" + newVal);\n            console.error(error);\n          }\n        }\n\n        // 2. Evaluate Objects\n        // TODO: Do we need this? It seems that no values are objects\n        if (newVal.startsWith('{') && newVal.endsWith('}')) {\n          // This looks like an object\n          // Use JSON.parse to transform it into a real object\n          try {\n            JSON.parse(newVal);\n            return JSON.parse(newVal);\n          } catch (error) {\n            console.error(\"Invalid JSON, please note that the JSON standard requires double quotes: \\n\" + newVal);\n            console.error(error);\n          }\n\n        }\n\n      }\n\n      // 3. Evaluate Boolean values\n      if (newVal === 'false') return false;\n      if (newVal === 'true') return true;\n\n      // 4. Evaluate undefined values\n      // Take originVal when env variable is not set\n      if (originVal !== undefined && typeof newVal === 'string') {\n        // Environment variable not set (e.g., development env)\n        if (newVal.startsWith('$VITE_')) {\n          return originVal;\n        }\n      }\n\n      // Use the new value\n      return newVal;\n    };\n\n    const mergeOptions = {\n      extensions: [ignoreUnusedVariables],\n    };\n\n    // const merged = merge(mergeOptions, opts.baseConfig, runtimeConfig);\n    const merged = clean(merge(mergeOptions, opts.baseConfig, runtimeConfig));\n    if (opts.debug) {\n      console.debug(`Runtime configuration = ${JSON.stringify(merged)}`); // eslint-disable-line\n    }\n\n    if (opts.useExperimentalRuntimeParser) {\n      // Validate the merged configuration\n      const { success, error } = configSchema.safeParse(merged)\n      if (!success) {\n        console.warn('Invalid runtime configuration detected. This application may not run as intended.'); // eslint-disable-line\n        console.warn(error); // eslint-disable-line\n      }\n    }\n\n    app.config.globalProperties.$env = merged; // eslint-disable-line\n    app.provide(injectionKey, merged);\n  },\n};\n\n/**\n * Removes all elements from the config that have a non-replaced runtime-config.js key\n * @param o\n * @param current\n */\nfunction clean(o, current = {}): ResolvedConfig {\n  Object.keys(o).forEach(key => {\n    const value = o[key];\n    if (value?.constructor === Object) {\n      current[key] = clean(value);\n    } else if (typeof value === 'string' && ! value.startsWith(\"$VITE_\")) {\n      current[key] = value;\n    } else if (typeof value !== 'string') {\n      current[key] = value;\n    }\n  });\n  return current as ResolvedConfig;\n}\n\nexport default RuntimeConfiguration;\n"],"names":["injectionKey","RuntimeConfiguration","app","runtimeConfig","options","opts","merged","clean","merge","originVal","newVal","error","success","configSchema","o","current","key","value"],"mappings":";;;;;;;;;;AAiBa,MAAAA,IAA6C,OAAO,eAAe,GAM1EC,IAAuB;AAAA,EAC3B,QAAQC,GAAUC,GAAeC,IAAU,CAAA,GAAI;AAM7C,UAAMC,IAAO,OAAO,OAAO,CAAA,GALJ;AAAA,MACrB,OAAO;AAAA,MACP,YAAY,EAAA,UAAA,KAA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