Skip to content

Commit b9f1d47

Browse files
pvdzjdalton
authored andcommitted
Migrate apiKey to apiToken if it occurs (#387)
1 parent 9548309 commit b9f1d47

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/utils/config.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const { LOCALAPPDATA, SOCKET_APP_DIR, XDG_DATA_HOME } = constants
1313

1414
export interface LocalConfig {
1515
apiBaseUrl?: string | null | undefined
16+
// @deprecated ; use apiToken. when loading a config, if this prop exists it
17+
// is deleted and set to apiToken instead, and then persisted.
18+
// should only happen once for legacy users.
19+
apiKey?: string | null | undefined
1620
apiProxy?: string | null | undefined
1721
apiToken?: string | null | undefined
1822
defaultOrg?: string
@@ -41,6 +45,11 @@ let _readOnlyConfig = false
4145
export function overrideCachedConfig(config: unknown) {
4246
_cachedConfig = config as LocalConfig
4347
_readOnlyConfig = true
48+
// Normalize apiKey to apiToken.
49+
if (_cachedConfig['apiKey']) {
50+
_cachedConfig['apiToken'] = _cachedConfig['apiKey']
51+
delete _cachedConfig['apiKey']
52+
}
4453
}
4554

4655
function getConfigValues(): LocalConfig {
@@ -59,6 +68,13 @@ function getConfigValues(): LocalConfig {
5968
} catch {
6069
logger.warn(`Failed to parse config at ${configPath}`)
6170
}
71+
// Normalize apiKey to apiToken and persist it.
72+
// This is a one time migration per user.
73+
if (_cachedConfig['apiKey']) {
74+
const token = _cachedConfig['apiKey']
75+
delete _cachedConfig['apiKey']
76+
updateConfigValue('apiToken', token)
77+
}
6278
} else {
6379
fs.mkdirSync(path.dirname(configPath), { recursive: true })
6480
}
@@ -111,12 +127,13 @@ function getConfigPath(): string | undefined {
111127
}
112128

113129
function normalizeConfigKey(key: keyof LocalConfig): keyof LocalConfig {
114-
const externalKey = (key as string) === 'apiKey' ? 'apiToken' : key
115-
if (!supportedConfigKeys.has(externalKey)) {
116-
throw new Error(`Invalid config key: ${key}`)
130+
// Note: apiKey was the old name of the token. When we load a config with
131+
// property apiKey, we'll copy that to apiToken and delete the old property.
132+
const normalizedKey = key === 'apiKey' ? 'apiToken' : key
133+
if (!supportedConfigKeys.has(normalizedKey)) {
134+
throw new Error(`Invalid config key: ${normalizedKey}`)
117135
}
118-
const internalKey = key === 'apiToken' ? 'apiKey' : key
119-
return internalKey as keyof LocalConfig
136+
return normalizedKey
120137
}
121138

122139
export function findSocketYmlSync() {

0 commit comments

Comments
 (0)