-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommon.ts
43 lines (39 loc) · 1.58 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Logger, SfdcUrl, SfProject, Messages, SfError } from '@salesforce/core';
import { getString, isObject } from '@salesforce/ts-types';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');
export class Common {
public static async resolveLoginUrl(instanceUrl?: string): Promise<string> {
const logger = await Logger.child('Common', { tag: 'resolveLoginUrl' });
if (instanceUrl) {
throwIfLightning(instanceUrl);
return instanceUrl;
}
let loginUrl: string;
try {
const project = await SfProject.resolve();
const projectJson = await project.resolveProjectConfig();
loginUrl = 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;
}
throwIfLightning(loginUrl);
logger.debug(`loginUrl: ${loginUrl}`);
return loginUrl;
}
}
const throwIfLightning = (urlString?: string): void => {
if (urlString?.match(/\.lightning\..*force\.com/)) {
throw new SfError(messages.getMessage('lightningInstanceUrl'), 'LightningDomain', [
messages.getMessage('flags.instance-url.description'),
]);
}
};