Skip to content

Commit

Permalink
feat: lightning url check from sfdx-core (#955)
Browse files Browse the repository at this point in the history
* feat: use lighting domain definitions from sfdx-core

* feat: use shared lightning domain detection from sfdx-core
  • Loading branch information
mshanemc authored Mar 25, 2024
1 parent 23dcc5e commit c5921ec
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@ const messages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

const resolveLoginUrl = async (instanceUrl?: string): Promise<string> => {
const logger = await Logger.child('Common', { tag: 'resolveLoginUrl' });
if (instanceUrl) {
throwIfLightning(instanceUrl);
return instanceUrl;
}
let loginUrl: string;
const loginUrl = instanceUrl ?? (await getLoginUrl(logger));
throwIfLightning(loginUrl);
logger.debug(`loginUrl: ${loginUrl}`);
return loginUrl;
};

/** try to get url from project if there is one, otherwise use the default production URL */
const getLoginUrl = async (logger: Logger): Promise<string> => {
try {
const project = await SfProject.resolve();
const projectJson = await project.resolveProjectConfig();
loginUrl = getString(projectJson, 'sfdcLoginUrl', SfdcUrl.PRODUCTION);
return getString(projectJson, 'sfdcLoginUrl', SfdcUrl.PRODUCTION);
} catch (err) {
const message: string = (isObject(err) ? Reflect.get(err, 'message') ?? err : err) as string;
logger.debug(`error occurred while trying to determine loginUrl: ${message}`);
loginUrl = SfdcUrl.PRODUCTION;
return SfdcUrl.PRODUCTION;
}
throwIfLightning(loginUrl);

logger.debug(`loginUrl: ${loginUrl}`);
return loginUrl;
};

const throwIfLightning = (urlString?: string): void => {
if (urlString?.match(/\.lightning\..*force\.com/)) {
const throwIfLightning = (urlString: string): void => {
const url = new SfdcUrl(urlString);
if (url.isLightningDomain() || (url.isInternalUrl() && url.origin.includes('.lightning.'))) {
throw new SfError(messages.getMessage('lightningInstanceUrl'), 'LightningDomain', [
messages.getMessage('flags.instance-url.description'),
]);
Expand Down

0 comments on commit c5921ec

Please sign in to comment.