@availity/env-var
Advanced tools
Comparing version 1.9.20 to 1.10.0
@@ -6,2 +6,13 @@ # Change Log | ||
# 1.10.0 (2020-05-20) | ||
### Features | ||
* **env-var:** add env support for cloud urls ([2534563](https://github.com/availity/sdk-js/commit/2534563e7ced303d6095101733e5d64a22210ef6)) | ||
## 1.9.20 (2020-05-01) | ||
@@ -8,0 +19,0 @@ |
105
lib/index.js
@@ -7,11 +7,58 @@ "use strict"; | ||
exports.setEnvironments = setEnvironments; | ||
exports.setSpecificEnvironments = setSpecificEnvironments; | ||
exports.getLocation = getLocation; | ||
exports.getCurrentEnv = getCurrentEnv; | ||
exports.getSpecificEnv = getSpecificEnv; | ||
exports["default"] = _default; | ||
// Cloud domains are in the format <team>.<cloud provider><zone>.availity.com | ||
// where <cloud provider> is a two character abbreviation for the cloud provider | ||
// and zone is a one character abbreviation for prod vs non-prod. | ||
// Cloud URIs are in the format /<namespace>/<environment>/... | ||
// where <namespace> is a three character abbreviation, e.g., cdn or api | ||
// and <environment> is a three character abbreviation, e.g., prd or t01 | ||
var getCloudEnv = function getCloudEnv(options) { | ||
var subdomain = options.subdomain, | ||
pathname = options.pathname; | ||
if (!(subdomain && pathname)) return null; | ||
var subMatch = subdomain.match(/.*?\.(?:av|aw|gc)(n|p)$/); | ||
if (!subMatch) return null; | ||
var pathMatch = pathname.match(/^\/[a-z]{3}\/([a-z0-9]{3})\/.*/); | ||
if (!pathMatch) return null; // ??p domains must be prod, ??n domains can't be prod | ||
var isProdPath = pathMatch[1] === 'prd'; | ||
switch (subMatch[1]) { | ||
case 'p': | ||
return isProdPath ? pathMatch[1] : null; | ||
case 'n': | ||
return isProdPath ? null : pathMatch[1]; | ||
default: | ||
return null; | ||
} | ||
}; | ||
var environments = { | ||
local: ['127.0.0.1', 'localhost'], | ||
test: [/^t(?:(?:\d\d)|(?:est))-apps$/], | ||
qa: [/^q(?:(?:\d\d)|(?:ap?))-apps$/], | ||
prod: [/^apps$/] | ||
test: [/^t(?:(?:\d\d)|(?:est))-apps$/, function (options) { | ||
return /^t(?:(?:\d\d)|(?:st))$/.test(getCloudEnv(options)); | ||
}], | ||
qa: [/^q(?:(?:\d\d)|(?:ap?))-apps$/, function (options) { | ||
return /^(stg|q(?:(?:\d\d)|(?:ua)|(?:ap)))$/.test(getCloudEnv(options)); | ||
}], | ||
prod: [/^apps$/, function (options) { | ||
return getCloudEnv(options) === 'prd'; | ||
}] | ||
}; | ||
var specificEnvironments = [{ | ||
regex: /^(?:(.*)-)?apps$/, | ||
fn: function fn(options) { | ||
return options.match[1] || 'prod'; | ||
} | ||
}, { | ||
regex: /.*?\.(?:av|aw|gc)(n|p)$/, | ||
fn: getCloudEnv | ||
}]; | ||
@@ -26,2 +73,10 @@ function setEnvironments(envs, override) { | ||
function setSpecificEnvironments(envs, override) { | ||
if (override) { | ||
specificEnvironments = envs; | ||
} else { | ||
Object.assign(specificEnvironments, envs); | ||
} | ||
} | ||
function getLocation(href) { | ||
@@ -33,9 +88,21 @@ var anchor = document.createElement('a'); | ||
function getLocationComponents(windowOverride) { | ||
var _ref = windowOverride === null || typeof windowOverride === 'string' ? getLocation(windowOverride) : windowOverride.location, | ||
hostname = _ref.hostname, | ||
pathname = _ref.pathname; | ||
var subdomain = hostname.split('.availity')[0]; | ||
return { | ||
subdomain: subdomain, | ||
pathname: pathname | ||
}; | ||
} | ||
function getCurrentEnv() { | ||
var windowOverride = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window; | ||
var _ref = windowOverride === null || typeof windowOverride === 'string' ? getLocation(windowOverride) : windowOverride.location, | ||
hostname = _ref.hostname; | ||
var _getLocationComponent = getLocationComponents(windowOverride), | ||
subdomain = _getLocationComponent.subdomain, | ||
pathname = _getLocationComponent.pathname; | ||
var subdomain = hostname.split('.availity')[0]; | ||
return Object.keys(environments).reduce(function (prev, env) { | ||
@@ -59,3 +126,6 @@ if (prev) return prev; | ||
// eslint-disable-next-line jest/no-disabled-tests | ||
return test(); | ||
return test({ | ||
subdomain: subdomain, | ||
pathname: pathname | ||
}); | ||
@@ -67,2 +137,23 @@ default: | ||
}, ''); | ||
} // Returns the specific environment, e.g. t01, not test | ||
function getSpecificEnv() { | ||
var windowOverride = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window; | ||
var _getLocationComponent2 = getLocationComponents(windowOverride), | ||
subdomain = _getLocationComponent2.subdomain, | ||
pathname = _getLocationComponent2.pathname; | ||
return specificEnvironments.reduce(function (prev, env) { | ||
if (prev) return prev; | ||
var regex = env.regex, | ||
fn = env.fn; | ||
var match = subdomain.match(regex); | ||
return match ? fn({ | ||
match: match, | ||
subdomain: subdomain, | ||
pathname: pathname | ||
}) : null; | ||
}, null) || 'local'; | ||
} | ||
@@ -69,0 +160,0 @@ |
{ | ||
"name": "@availity/env-var", | ||
"version": "1.9.20", | ||
"version": "1.10.0", | ||
"description": "Availity-specific way to determine variables based on the current environment the code is running in", | ||
@@ -28,3 +28,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "a15f93a4b6ffb150feddcbb0ea1113b9ade628bb" | ||
"gitHead": "1045201776b1cac4238b5e70972c871a5455f43d" | ||
} |
@@ -0,7 +1,53 @@ | ||
// Cloud domains are in the format <team>.<cloud provider><zone>.availity.com | ||
// where <cloud provider> is a two character abbreviation for the cloud provider | ||
// and zone is a one character abbreviation for prod vs non-prod. | ||
// Cloud URIs are in the format /<namespace>/<environment>/... | ||
// where <namespace> is a three character abbreviation, e.g., cdn or api | ||
// and <environment> is a three character abbreviation, e.g., prd or t01 | ||
const getCloudEnv = options => { | ||
const { subdomain, pathname } = options; | ||
if (!(subdomain && pathname)) return null; | ||
const subMatch = subdomain.match(/.*?\.(?:av|aw|gc)(n|p)$/); | ||
if (!subMatch) return null; | ||
const pathMatch = pathname.match(/^\/[a-z]{3}\/([a-z0-9]{3})\/.*/); | ||
if (!pathMatch) return null; | ||
// ??p domains must be prod, ??n domains can't be prod | ||
const isProdPath = pathMatch[1] === 'prd'; | ||
switch (subMatch[1]) { | ||
case 'p': | ||
return isProdPath ? pathMatch[1] : null; | ||
case 'n': | ||
return isProdPath ? null : pathMatch[1]; | ||
default: | ||
return null; | ||
} | ||
}; | ||
let environments = { | ||
local: ['127.0.0.1', 'localhost'], | ||
test: [/^t(?:(?:\d\d)|(?:est))-apps$/], | ||
qa: [/^q(?:(?:\d\d)|(?:ap?))-apps$/], | ||
prod: [/^apps$/], | ||
test: [ | ||
/^t(?:(?:\d\d)|(?:est))-apps$/, | ||
options => /^t(?:(?:\d\d)|(?:st))$/.test(getCloudEnv(options)), | ||
], | ||
qa: [ | ||
/^q(?:(?:\d\d)|(?:ap?))-apps$/, | ||
options => /^(stg|q(?:(?:\d\d)|(?:ua)|(?:ap)))$/.test(getCloudEnv(options)), | ||
], | ||
prod: [/^apps$/, options => getCloudEnv(options) === 'prd'], | ||
}; | ||
let specificEnvironments = [ | ||
{ | ||
regex: /^(?:(.*)-)?apps$/, | ||
fn: options => options.match[1] || 'prod', | ||
}, | ||
{ | ||
regex: /.*?\.(?:av|aw|gc)(n|p)$/, | ||
fn: getCloudEnv, | ||
}, | ||
]; | ||
export function setEnvironments(envs, override) { | ||
@@ -14,2 +60,11 @@ if (override) { | ||
} | ||
export function setSpecificEnvironments(envs, override) { | ||
if (override) { | ||
specificEnvironments = envs; | ||
} else { | ||
Object.assign(specificEnvironments, envs); | ||
} | ||
} | ||
export function getLocation(href) { | ||
@@ -21,4 +76,4 @@ const anchor = document.createElement('a'); | ||
export function getCurrentEnv(windowOverride = window) { | ||
const { hostname } = | ||
function getLocationComponents(windowOverride) { | ||
const { hostname, pathname } = | ||
windowOverride === null || typeof windowOverride === 'string' | ||
@@ -28,3 +83,11 @@ ? getLocation(windowOverride) | ||
const subdomain = hostname.split('.availity')[0]; | ||
return { | ||
subdomain, | ||
pathname, | ||
}; | ||
} | ||
export function getCurrentEnv(windowOverride = window) { | ||
const { subdomain, pathname } = getLocationComponents(windowOverride); | ||
return Object.keys(environments).reduce((prev, env) => { | ||
@@ -46,3 +109,3 @@ if (prev) return prev; | ||
// eslint-disable-next-line jest/no-disabled-tests | ||
return test(); | ||
return test({ subdomain, pathname }); | ||
default: | ||
@@ -56,2 +119,16 @@ return false; | ||
// Returns the specific environment, e.g. t01, not test | ||
export function getSpecificEnv(windowOverride = window) { | ||
const { subdomain, pathname } = getLocationComponents(windowOverride); | ||
return ( | ||
specificEnvironments.reduce((prev, env) => { | ||
if (prev) return prev; | ||
const { regex, fn } = env; | ||
const match = subdomain.match(regex); | ||
return match ? fn({ match, subdomain, pathname }) : null; | ||
}, null) || 'local' | ||
); | ||
} | ||
export default function(varObj, windowOverride, defaultVar) { | ||
@@ -58,0 +135,0 @@ const env = getCurrentEnv(windowOverride); |
@@ -1,2 +0,2 @@ | ||
import envVar, { setEnvironments } from '../src'; | ||
import envVar, { setEnvironments, getSpecificEnv } from '../src'; | ||
@@ -10,4 +10,4 @@ const setHostname = hostname => { | ||
const getFakeWindowLocation = hostname => ({ | ||
location: { hostname }, | ||
const getFakeWindowLocation = (hostname, pathname) => ({ | ||
location: { hostname, pathname }, | ||
}); | ||
@@ -42,2 +42,29 @@ | ||
['apps.availity.com', 'prod'], | ||
['digital.awp.availity.com/cdn/prd/spaces/index.html', 'prod'], | ||
['digital.avp.availity.com/cdn/prd/spaces/index.html', 'prod'], | ||
['digital.gcp.availity.com/cdn/prd/spaces/index.html', 'prod'], | ||
['digital.awn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.avn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.gcn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.awp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.avp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gcp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.awp.availity.com/api/prd/spaces/index.html', 'prod'], | ||
['digital.avp.availity.com/api/prd/spaces/index.html', 'prod'], | ||
['digital.gcp.availity.com/api/prd/spaces/index.html', 'prod'], | ||
['digital.awn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.avn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.gcn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.awp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.avp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gcp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gap.availity.com/api/prd/spaces/index.html', 'local'], // Unrecognized cloud | ||
['digital.avp.availity.com/apic/prd/spaces/index.html', 'local'], // Unrecognized namespace | ||
['digital.avp.availity.com/api/prod/spaces/index.html', 'local'], // Unrecognized environment | ||
['digital.awn.availity.com/cdn/stg/spaces/index.html', 'qa'], | ||
['digital.avn.availity.com/cdn/qua/spaces/index.html', 'qa'], | ||
['digital.gcn.availity.com/cdn/qap/spaces/index.html', 'qa'], | ||
['digital.awn.availity.com/cdn/tst/spaces/index.html', 'test'], | ||
['digital.avn.availity.com/cdn/t01/spaces/index.html', 'test'], | ||
['digital.gcn.availity.com/cdn/t25/spaces/index.html', 'test'], | ||
].forEach(args => { | ||
@@ -91,2 +118,17 @@ generateTest(...args); | ||
], | ||
[ | ||
'apps.availity.com', | ||
'prod', | ||
getFakeWindowLocation('bar.awp.availity.com', '/cdn/prd/index.html'), | ||
], | ||
[ | ||
'apps.availity.com', | ||
'qa', | ||
getFakeWindowLocation('bar.awn.availity.com', '/cdn/q01/index.html'), | ||
], | ||
[ | ||
'apps.availity.com', | ||
'test', | ||
getFakeWindowLocation('bar.awn.availity.com', '/cdn/tst/index.html'), | ||
], | ||
].forEach(args => { | ||
@@ -109,2 +151,37 @@ generateTest(...args); | ||
['apps.availity.com', 'qa', 'https://q01-apps.availity.com'], | ||
[ | ||
'foo.awn.availity.com/cdn/tst/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awn.availity.com/cdn/t01/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awn.availity.com/cdn/t14/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awn.availity.com/cdn/stg/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awn.availity.com/cdn/qap/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awn.availity.com/cdn/q01/spaces/index.html', | ||
'prod', | ||
'https://foo.awp.availity.com/api/prd/spaces/index.html', | ||
], | ||
[ | ||
'foo.awp.availity.com/cdn/prd/spaces/index.html', | ||
'qa', | ||
'https://foo.awn.availity.com/api/stg/spaces/index.html', | ||
], | ||
].forEach(args => { | ||
@@ -242,2 +319,56 @@ generateTest(...args); | ||
}); | ||
const generateSpecificTest = (url, env, overrideWindow) => { | ||
test(`should return specific environment ${env} for ${url}`, () => { | ||
setHostname(url); | ||
expect(getSpecificEnv(overrideWindow)).toBe(env); | ||
}); | ||
}; | ||
describe('Specific environments', () => { | ||
[ | ||
['fallback-apps.availity.com', 'fallback'], | ||
['localhost', 'local'], | ||
['127.0.0.1', 'local'], | ||
['test-apps.availity.com', 'test'], | ||
['t01-apps.availity.com', 't01'], | ||
['t14-apps.availity.com', 't14'], | ||
['qa-apps.availity.com', 'qa'], | ||
['qap-apps.availity.com', 'qap'], | ||
['q01-apps.availity.com', 'q01'], | ||
['apps.availity.com', 'prod'], | ||
['digital.awp.availity.com/cdn/prd/spaces/index.html', 'prd'], | ||
['digital.avp.availity.com/cdn/prd/spaces/index.html', 'prd'], | ||
['digital.gcp.availity.com/cdn/prd/spaces/index.html', 'prd'], | ||
['digital.awn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.avn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.gcn.availity.com/cdn/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.awp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.avp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gcp.availity.com/cdn/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.awp.availity.com/api/prd/spaces/index.html', 'prd'], | ||
['digital.avp.availity.com/api/prd/spaces/index.html', 'prd'], | ||
['digital.gcp.availity.com/api/prd/spaces/index.html', 'prd'], | ||
['digital.awn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.avn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.gcn.availity.com/api/prd/spaces/index.html', 'local'], // Non-prod domain, prod URI | ||
['digital.awp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.avp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gcp.availity.com/api/tst/spaces/index.html', 'local'], // Prod domain, non-prod URI | ||
['digital.gap.availity.com/api/prd/spaces/index.html', 'local'], // Unrecognized cloud | ||
['digital.avp.availity.com/apic/prd/spaces/index.html', 'local'], // Unrecognized namespace | ||
['digital.avp.availity.com/api/prod/spaces/index.html', 'local'], // Unrecognized environment | ||
['digital.awn.availity.com/cdn/stg/spaces/index.html', 'stg'], | ||
['digital.avn.availity.com/cdn/qua/spaces/index.html', 'qua'], | ||
['digital.gcn.availity.com/cdn/qap/spaces/index.html', 'qap'], | ||
['digital.awn.availity.com/cdn/tst/spaces/index.html', 'tst'], | ||
['digital.avn.availity.com/cdn/t01/spaces/index.html', 't01'], | ||
['digital.gcn.availity.com/cdn/t25/spaces/index.html', 't25'], | ||
['digital.box.availity.com/cdn/t25/spaces/index.html', 'local'], | ||
['digital.awn.availity.com/cdn/apple/spaces/index.html', 'local'], | ||
['digital.awn.availity.com/nahfam/stg/spaces/index.html', 'local'], | ||
].forEach(args => { | ||
generateSpecificTest(...args); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29496
625