UNPKG

@hhgtech/hhg-components

Version:
56 lines (52 loc) 2.18 kB
'use strict'; var constantsDomainLocales = require('./constantsDomainLocales.js'); var constantsIsProduction = require('./constantsIsProduction.js'); const isNumeric = (n) => !isNaN(parseFloat(n)) && isFinite(n); const defaultDomain = 'hellosehat.com'; class GAssets { constructor() { this.baseDomain = ''; this.subPath = ''; this.getAssetPath = (path, forceBaseDomain, forceSubPath) => { const fileUrl = 'https://hhg-common.' + [ forceBaseDomain || this.getBaseDomain(), forceSubPath || this.subPath, path, ].join('/'); if (!constantsIsProduction.isProduction && !process.env.JEST_WORKER_ID) { // don't add timestamp when running jest tests // add timestamp to avoid caching if not production return fileUrl + '?v=' + Math.floor(new Date().getTime() / 1000 / 60 / 3); } return fileUrl; }; } // constructor() {} getBaseDomain() { if (this.baseDomain) { return this.baseDomain; } if (typeof window !== 'undefined') { const { host } = window.location; const hostParts = host.split('.'); const includeDomain = constantsDomainLocales.domainLocales[Object.keys(constantsDomainLocales.domainLocales).find((k) => host.includes(constantsDomainLocales.domainLocales[k]))]; if (hostParts.includes('localhost') || hostParts.includes('hellohealthgroup') || hostParts.every((part) => isNumeric(part)) || // is IP address !includeDomain) { return defaultDomain; } return includeDomain; } return defaultDomain; } } const CommonGAssets = new GAssets(); CommonGAssets.subPath = 'common'; if (process.env.NEXT_PUBLIC_DEPLOY_LOCALE) { CommonGAssets.baseDomain = constantsDomainLocales.domainLocales[process.env.NEXT_PUBLIC_DEPLOY_LOCALE]; } exports.CommonGAssets = CommonGAssets; exports.GAssets = GAssets;