Skip to content

Commit 2f9d2db

Browse files
committed
Use correct types for config and accounts
1 parent 33c42d2 commit 2f9d2db

File tree

7 files changed

+67
-79
lines changed

7 files changed

+67
-79
lines changed

src/lib/commands/account.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { commands, ExtensionContext, Uri } from 'vscode';
2+
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
23
import { COMMANDS } from '../constants';
3-
import { Portal } from '../types';
44

55
export const registerCommands = (context: ExtensionContext) => {
66
context.subscriptions.push(
77
commands.registerCommand(
88
COMMANDS.ACCOUNT.VIEW_PERSONAL_ACCESS_KEY,
9-
async (hubspotAccount: Portal) => {
9+
async (hubspotAccount: CLIAccount_DEPRECATED) => {
1010
const pakUrl = `https://app.hubspot${
1111
hubspotAccount.env === 'qa' ? 'qa' : ''
1212
}.com/personal-access-key/${hubspotAccount.portalId}`;
@@ -18,7 +18,7 @@ export const registerCommands = (context: ExtensionContext) => {
1818
context.subscriptions.push(
1919
commands.registerCommand(
2020
COMMANDS.ACCOUNT.OPEN_DESIGN_MANAGER,
21-
async (hubspotAccount: Portal) => {
21+
async (hubspotAccount: CLIAccount_DEPRECATED) => {
2222
const designManagerUrl = `https://app.hubspot${
2323
hubspotAccount.env === 'qa' ? 'qa' : ''
2424
}.com/design-manager/${hubspotAccount.portalId}`;

src/lib/commands/config.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { commands, window, ExtensionContext } from 'vscode';
22
import { updateStatusBarItems } from '../statusBar';
33
import { COMMANDS, TRACKED_EVENTS } from '../constants';
44
import { getDisplayedHubspotPortalInfo } from '../helpers';
5-
import { Portal } from '../types';
65
import { portalNameInvalid } from '../validation';
76
import { trackEvent } from '../tracking';
87
import { showAutoDismissedStatusBarMessage } from '../messaging';
@@ -13,24 +12,31 @@ import {
1312
renameAccount,
1413
updateDefaultAccount,
1514
} from '@hubspot/local-dev-lib/config';
15+
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
16+
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
1617

17-
const showRenameAccountPrompt = (accountToRename: Portal) => {
18+
const showRenameAccountPrompt = (accountToRename: CLIAccount_DEPRECATED) => {
1819
window
1920
.showInputBox({
2021
placeHolder: 'Enter a new name for the account',
2122
})
2223
.then(async (newName: string | undefined) => {
2324
if (newName) {
2425
const oldName = accountToRename.name || accountToRename.portalId;
25-
const config = getConfig();
26+
const config: CLIConfig | null = getConfig();
2627
let invalidReason = '';
2728
if (config) {
28-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
2929
invalidReason = portalNameInvalid(newName, config);
3030
}
3131

3232
if (!invalidReason) {
33-
renameAccount(oldName, newName);
33+
if (!oldName) {
34+
window.showErrorMessage(
35+
'Could not determine account name to rename'
36+
);
37+
return;
38+
}
39+
renameAccount(String(oldName), newName);
3440
showAutoDismissedStatusBarMessage(
3541
`Successfully renamed default account from ${oldName} to ${newName}.`
3642
);
@@ -76,19 +82,16 @@ export const registerCommands = (context: ExtensionContext) => {
7682
commands.registerCommand(
7783
COMMANDS.CONFIG.SELECT_DEFAULT_ACCOUNT,
7884
async () => {
79-
const config = getConfig();
80-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
81-
if (config && config.portals) {
85+
const config: CLIConfig | null = getConfig();
86+
87+
if (config && 'portals' in config && config.portals) {
8288
window
8389
.showQuickPick(
84-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
85-
config.portals.map((p: Portal) => {
90+
config.portals.map((p: CLIAccount_DEPRECATED) => {
8691
return {
8792
label: getDisplayedHubspotPortalInfo(p),
8893
description:
89-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
9094
config.defaultPortal === p.portalId ||
91-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
9295
config.defaultPortal === p.name
9396
? '(default)'
9497
: '',
@@ -102,8 +105,13 @@ export const registerCommands = (context: ExtensionContext) => {
102105
.then(async (selection) => {
103106
if (selection) {
104107
const newDefaultAccount =
105-
// @ts-ignore selection is an object
106108
selection.portal.name || selection.portal.portalId;
109+
if (!newDefaultAccount) {
110+
window.showErrorMessage(
111+
'No account selected; Choose an account to set as default'
112+
);
113+
return;
114+
}
107115
await trackEvent(TRACKED_EVENTS.SELECT_DEFAULT_ACCOUNT);
108116
updateDefaultAccount(newDefaultAccount);
109117
showAutoDismissedStatusBarMessage(
@@ -142,8 +150,11 @@ export const registerCommands = (context: ExtensionContext) => {
142150
)
143151
.then(async (answer) => {
144152
if (answer === 'Yes') {
145-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
146-
if (config && config.portals.length === 1) {
153+
if (
154+
config &&
155+
'portals' in config &&
156+
config.portals.length === 1
157+
) {
147158
deleteConfigFile();
148159
showAutoDismissedStatusBarMessage(
149160
`Successfully deleted account ${accountIdentifier}. The config file has been deleted because there are no more authenticated accounts.`

src/lib/helpers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { window, commands, workspace, StatusBarAlignment } from 'vscode';
33
import { getAccountId } from '@hubspot/local-dev-lib/config';
44

55
import { COMMANDS } from './constants';
6-
import { HubspotConfig, Portal } from './types';
6+
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
7+
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
78

89
const { exec } = require('node:child_process');
910

@@ -16,18 +17,21 @@ export const getRootPath = () => {
1617
return workspaceFolders[0].uri.fsPath;
1718
};
1819

19-
export const getDefaultPortalFromConfig = (config: HubspotConfig) => {
20+
export const getDefaultPortalFromConfig = (config: CLIConfig) => {
2021
return (
2122
config &&
23+
'portals' in config &&
2224
config.portals &&
2325
config.portals.find(
24-
(p: Portal) =>
26+
(p: CLIAccount_DEPRECATED) =>
2527
p.portalId === config.defaultPortal || p.name === config.defaultPortal
2628
)
2729
);
2830
};
2931

30-
export const getDisplayedHubspotPortalInfo = (portalData: Portal) => {
32+
export const getDisplayedHubspotPortalInfo = (
33+
portalData: CLIAccount_DEPRECATED
34+
) => {
3135
return portalData.name
3236
? `${portalData.name} - ${portalData.portalId}`
3337
: `${portalData.portalId}`;

src/lib/providers/treedata/accounts.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ import {
77
TreeItemCollapsibleState,
88
} from 'vscode';
99
import { getDisplayedHubspotPortalInfo } from '../../helpers';
10-
import { HubspotConfig, Portal } from '../../types';
1110

1211
import { getConfig } from '@hubspot/local-dev-lib/config';
12+
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
13+
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
1314

14-
const isDefaultPortal = (portal: Portal, config: HubspotConfig) => {
15+
const isDefaultPortal = (
16+
portal: CLIAccount_DEPRECATED,
17+
config: CLIConfig | null
18+
) => {
1519
return (
16-
config.defaultPortal === portal.portalId ||
17-
config.defaultPortal === portal.name
20+
config &&
21+
'defaultPortal' in config &&
22+
(config.defaultPortal === portal.portalId ||
23+
config.defaultPortal === portal.name)
1824
);
1925
};
2026

21-
const getAccountIdentifiers = (portal: Portal) => {
27+
const getAccountIdentifiers = (portal: CLIAccount_DEPRECATED) => {
2228
let accountIdentifiers = '';
2329

2430
if (portal.env === 'qa') {
@@ -28,10 +34,11 @@ const getAccountIdentifiers = (portal: Portal) => {
2834
return accountIdentifiers;
2935
};
3036

31-
export class AccountsProvider implements TreeDataProvider<Portal> {
32-
private config: HubspotConfig | null;
37+
export class AccountsProvider
38+
implements TreeDataProvider<CLIAccount_DEPRECATED>
39+
{
40+
private config: CLIConfig | null;
3341
constructor() {
34-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
3542
this.config = getConfig();
3643
}
3744

@@ -43,24 +50,22 @@ export class AccountsProvider implements TreeDataProvider<Portal> {
4350
this._onDidChangeTreeData.fire(undefined);
4451
}
4552

46-
getTreeItem(p: Portal): TreeItem {
53+
getTreeItem(p: CLIAccount_DEPRECATED): TreeItem {
4754
const identifiers = getAccountIdentifiers(p);
4855
const name = `${getDisplayedHubspotPortalInfo(p)} ${identifiers}`;
4956
return new AccountTreeItem(
5057
name,
5158
p,
52-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
53-
{ isDefault: isDefaultPortal(p, this.config) },
59+
{ isDefault: isDefaultPortal(p, this.config) ?? false },
5460
TreeItemCollapsibleState.None
5561
);
5662
}
5763

58-
getChildren(): Thenable<Portal[] | undefined> {
64+
getChildren(): Thenable<CLIAccount_DEPRECATED[] | undefined> {
5965
console.log('AccountsProvider:getChildren');
60-
// @ts-expect-error TODO: Fix this when updating local-dev-lib
6166
this.config = getConfig();
6267

63-
if (this.config && this.config.portals) {
68+
if (this.config && 'portals' in this.config && this.config.portals) {
6469
return Promise.resolve(this.config.portals);
6570
}
6671

@@ -71,7 +76,7 @@ export class AccountsProvider implements TreeDataProvider<Portal> {
7176
export class AccountTreeItem extends TreeItem {
7277
constructor(
7378
public readonly name: string,
74-
public readonly portalData: Portal,
79+
public readonly portalData: CLIAccount_DEPRECATED,
7580
public readonly options: { isDefault: boolean },
7681
public readonly collapsibleState: TreeItemCollapsibleState,
7782
public iconPath: ThemeIcon = new ThemeIcon('account'),

src/lib/types.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
11
import { Command } from 'vscode';
22

3-
export interface Portal {
4-
name: string;
5-
portalId: string;
6-
authType: string;
7-
auth: {
8-
clientId: string;
9-
clientSecret: string;
10-
scopes: string[];
11-
tokenInfo: {
12-
accessToken: string;
13-
expiresAt: number;
14-
refreshToken: string;
15-
};
16-
};
17-
personalAccessKey: string;
18-
env?: string;
19-
sandboxAccountType?: string;
20-
parentAccountId?: number;
21-
}
22-
23-
export interface HubspotConfig {
24-
defaultPortal: string;
25-
portals: Array<Portal>;
26-
defaultMode?: 'draft' | 'published' | undefined;
27-
httpTimeout?: number | undefined;
28-
allowUsageTracking?: boolean | undefined;
29-
useCustomObjectHubFile?: boolean | undefined;
30-
}
31-
323
export interface Link {
334
label: string;
345
url: string;

src/lib/uri.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ import {
2020
createEmptyConfigFile,
2121
setConfigPath,
2222
} from '@hubspot/local-dev-lib/config';
23-
24-
// TODO: Remove these once we've updated local-dev-lib
25-
const ENVIRONMENTS = {
26-
PROD: 'prod',
27-
QA: 'qa',
28-
} as const;
29-
type ValueOf<T> = T[keyof T];
23+
import { ENVIRONMENTS } from '@hubspot/local-dev-lib/constants/environments';
24+
import { Environment } from '@hubspot/local-dev-lib/types/Config';
3025

3126
const getQueryObject = (uri: Uri) => {
3227
return new URLSearchParams(uri.query);
@@ -35,7 +30,7 @@ const getQueryObject = (uri: Uri) => {
3530
const handleAuthRequest = async (authParams: URLSearchParams) => {
3631
const personalAccessKeyResp = authParams.get('personalAccessKeyResp') || '';
3732
const envParam = authParams.get('env');
38-
const env: ValueOf<typeof ENVIRONMENTS> =
33+
const env: Environment =
3934
envParam === ENVIRONMENTS.QA ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD;
4035
const name = authParams.get('name') || undefined;
4136
const portalId = authParams.get('portalId');

src/lib/validation.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
Diagnostic,
88
Range,
99
} from 'vscode';
10-
import { HubspotConfig } from './types';
10+
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
11+
import { CLIAccount } from '@hubspot/local-dev-lib/types/Accounts';
1112
import { HubLValidationError } from '@hubspot/local-dev-lib/types/HublValidation';
1213
import { validateHubl } from '@hubspot/local-dev-lib/api/validateHubl';
1314
import { getAccountId } from '@hubspot/local-dev-lib/config';
@@ -174,7 +175,7 @@ export const triggerValidate = (
174175

175176
export const portalNameInvalid = (
176177
portalName: string,
177-
config: HubspotConfig
178+
config: CLIConfig | null
178179
) => {
179180
if (typeof portalName !== 'string') {
180181
return 'Portal name must be a string';
@@ -184,9 +185,10 @@ export const portalNameInvalid = (
184185
return 'Portal name cannot contain spaces';
185186
}
186187
return config &&
188+
'portals' in config &&
187189
(config.portals || [])
188-
.map((p) => p.name)
189-
.find((name) => name === portalName)
190+
.map((p: CLIAccount) => p.name)
191+
.find((name: string | undefined) => name === portalName)
190192
? `${portalName} already exists in config.`
191-
: false;
193+
: '';
192194
};

0 commit comments

Comments
 (0)