Skip to content

Instantly share code, notes, and snippets.

@NoriSte
Last active May 9, 2022 08:17
Show Gist options
  • Save NoriSte/81c1ef9821fbd9eeb80b7e11b56b7cdf to your computer and use it in GitHub Desktop.
Save NoriSte/81c1ef9821fbd9eeb80b7e11b56b7cdf to your computer and use it in GitHub Desktop.
Reading env variables from the Hasura's Console with Cypress (with sample Hasura's Console HTML page)
<!doctype html>
<html lang="en-US" data-reactroot=""><head><link rel="icon" type="image/png" href="/rstatic/favicon_green.png"/><script>
window.__env = {
dataApiUrl: 'http://localhost:8080',
isAdminSecretSet: 'true',
consoleMode: 'server',
nodeEnv: 'development',
serverVersion: 'v1.0.0',
urlPrefix: '/',
consolePath: 'undefined',
enableTelemetry: true,
assetsPath: 'https://graphql-engine-cdn.hasura.io/console/assets',
assetsVersion: 'channel/stable/v1.0',
cdnAssets: true,
herokuOAuthClientId: '',
tenantID: '',
projectID: '',
cloudRootDomain: 'undefined',
consoleType: 'undefined',
};
</script></head><body><style>
.content {
display: 'none';
opacity: 0;
transition: opacity .20s linear;
}
.content.show {
display: 'block';
opacity: 1;
transition: opacity .20s linear;
}
</style><div id="loading"><div class="page-loading" style="
min-height: 100vh;
width: 100%;
display: flex;
align-items: center;
font-family: sans-serif;
justify-content: center;
">
<span class="" style="
font-size: 2em;
margin-top: -3em;
color: #848484;
">
Loading...
</span>
</div></div><div id="content" class="content"></div><script src="http://127.0.0.1:3001/rstatic/dist/main-bfccddeec0d6f4ae3695.js" charSet="UTF-8"></script></body></html>
function setMetaData() {
cy.log('// Read the window.__env object');
const baseUrl = Cypress.config().baseUrl;
if (!baseUrl) {
throw new Error('No baseUrl set');
}
cy.request<string>(baseUrl).then(response => {
const rawHtml = response.body;
// Extract window.__env object from the html
// see: https://regex101.com/r/JCosQQ/1
/*
window\.__env = # literally match 'window.__env = '
(?<envObject> # name envObject the all object
{ # the left brace
[\s\S] # everything, including line breaks
*? # any number of times, but greedy
# (greedy stopping at the first `from` occurrence)
}) # close the envObject group
*/
const windowEnvRegex = /window\.__env = (?<envObject>{[\s\S]*?})/gm;
const match = windowEnvRegex.exec(rawHtml);
if (!match) {
cy.log('No window.__env object found in page', rawHtml);
throw new Error('No window.__env object found in page');
}
if (!match.groups || !match.groups.envObject) {
throw new Error('No env object found in page');
}
const envObject = eval(`(${match.groups.envObject})`);
const isJsObject =
!!envObject && typeof envObject === 'object' && !Array.isArray(envObject);
if (!isJsObject) {
cy.log('window.__env is not a plain JavaScript object', envObject);
throw new Error('window.__env is not a plain JavaScript object');
}
// TODO: it would be better off signing it with the correct type
const envVariables = envObject as Record<string, any>;
return envVariables;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment