Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure app config variables have values at build-time #1296

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class MultisitePlugin implements ConfigPlugin {

async exec(config: JssConfig) {
let sites: SiteInfo[] = [];
const endpoint = process.env.GRAPH_QL_ENDPOINT || config.graphQLEndpoint;
const apiKey = process.env.SITECORE_API_KEY || config.sitecoreApiKey;

const endpoint = config.graphQLEndpoint;
const apiKey = config.sitecoreApiKey;

if (!endpoint || !apiKey) {
console.warn(
chalk.yellow('Skipping site information fetch (missing GraphQL connection details)')
chalk.yellow('Skipping site information fetch (missing GraphQL endpoint or API key).')
);
} else {
console.log(`Fetching site information from ${endpoint}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ComputedPlugin implements ConfigPlugin {

async exec(config: JssConfig) {
return Object.assign({}, config, {
graphQLEndpoint: `${config.sitecoreApiHost}${config.graphQLEndpointPath}`,
graphQLEndpoint: config.graphQLEndpoint || `${config.sitecoreApiHost}${config.graphQLEndpointPath}`,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ConfigPlugin, JssConfig } from '..';

/**
* This config will set fallback values for properties that were left empty
* If neither env, nor other places had a proper value, this will ensure a fallback is set
*/
class FallbackPlugin implements ConfigPlugin {
// should always comes last
order = 99;

async exec(config: JssConfig) {
return Object.assign({}, config, {
defaultLanguage: config.defaultLanguage || 'en',
sitecoreApiKey: config.sitecoreApiKey || 'no-api-key-set'
});
}
}

export const fallbackPlugin = new FallbackPlugin();
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class PackageJsonPlugin implements ConfigPlugin {
if (!packageConfig.config) return config;

return Object.assign({}, config, {
jssAppName: packageConfig.config.appName,
graphQLEndpointPath: packageConfig.config.graphQLEndpointPath,
defaultLanguage: packageConfig.config.language,
jssAppName: config.jssAppName || packageConfig.config.appName,
graphQLEndpointPath: config.graphQLEndpointPath || packageConfig.config.graphQLEndpointPath,
defaultLanguage: config.defaultLanguage || packageConfig.config.language,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class ScJssConfigPlugin implements ConfigPlugin {
if (!scJssConfig) return config;

return Object.assign({}, config, {
sitecoreApiKey: scJssConfig.sitecore?.apiKey,
sitecoreApiHost: scJssConfig.sitecore?.layoutServiceHost,
sitecoreApiKey: config.sitecoreApiKey || scJssConfig.sitecore?.apiKey,
sitecoreApiHost: config.sitecoreApiHost || scJssConfig.sitecore?.layoutServiceHost,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { JssConfig, jssConfigFactory } from './config';
*/

const defaultConfig: JssConfig = {
sitecoreApiKey: 'no-api-key-set',
sitecoreApiHost: '',
jssAppName: 'Unknown',
graphQLEndpointPath: '',
defaultLanguage: 'en',
sitecoreApiKey: process.env[`${constantCase('sitecoreApiKey')}`],
sitecoreApiHost: process.env[`${constantCase('sitecoreApiHost')}`],
jssAppName: process.env[`${constantCase('jssAppName')}`],
graphQLEndpointPath: process.env[`${constantCase('graphQLEndpointPath')}`],
defaultLanguage: process.env[`${constantCase('defaultLanguage')}`],
};

generateConfig(defaultConfig);
Expand Down