Skip to content

Commit 76e4870

Browse files
authored
PM-22143 Refactor TS enums to be const objects (Import only) (#16770)
* Import related changes from PR #16399
1 parent 2858d12 commit 76e4870

File tree

9 files changed

+180
-137
lines changed

9 files changed

+180
-137
lines changed

libs/importer/src/importers/fsecure/fsecure-fsk-importer.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// FIXME: Update this file to be type safe and remove this and next line
2-
// @ts-strict-ignore
31
import { CipherType } from "@bitwarden/common/vault/enums";
42
import { CardView } from "@bitwarden/common/vault/models/view/card.view";
53
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
@@ -8,7 +6,7 @@ import { ImportResult } from "../../models/import-result";
86
import { BaseImporter } from "../base-importer";
97
import { Importer } from "../importer";
108

11-
import { FskEntry, FskEntryTypesEnum, FskFile } from "./fsecure-fsk-types";
9+
import { FskEntry, FskEntryType, FskFile } from "./fsecure-fsk-types";
1210

1311
export class FSecureFskImporter extends BaseImporter implements Importer {
1412
parse(data: string): Promise<ImportResult> {
@@ -19,37 +17,32 @@ export class FSecureFskImporter extends BaseImporter implements Importer {
1917
return Promise.resolve(result);
2018
}
2119

22-
for (const key in results.data) {
23-
// eslint-disable-next-line
24-
if (!results.data.hasOwnProperty(key)) {
25-
continue;
26-
}
27-
28-
const value = results.data[key];
20+
for (const [, value] of Object.entries(results.data)) {
2921
const cipher = this.parseEntry(value);
30-
result.ciphers.push(cipher);
22+
if (cipher != undefined) {
23+
result.ciphers.push(cipher);
24+
}
3125
}
3226

3327
result.success = true;
3428
return Promise.resolve(result);
3529
}
3630

37-
private parseEntry(entry: FskEntry): CipherView {
31+
private parseEntry(entry: FskEntry): CipherView | undefined {
3832
const cipher = this.initLoginCipher();
3933
cipher.name = this.getValueOrDefault(entry.service);
4034
cipher.notes = this.getValueOrDefault(entry.notes);
4135
cipher.favorite = entry.favorite > 0;
4236

4337
switch (entry.type) {
44-
case FskEntryTypesEnum.Login:
38+
case FskEntryType.Login:
4539
this.handleLoginEntry(entry, cipher);
4640
break;
47-
case FskEntryTypesEnum.CreditCard:
41+
case FskEntryType.CreditCard:
4842
this.handleCreditCardEntry(entry, cipher);
4943
break;
5044
default:
51-
return;
52-
break;
45+
return undefined;
5346
}
5447

5548
this.convertToNoteIfNeeded(cipher);

libs/importer/src/importers/fsecure/fsecure-fsk-types.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ export interface Data {
66
[key: string]: FskEntry;
77
}
88

9-
// FIXME: update to use a const object instead of a typescript enum
10-
// eslint-disable-next-line @bitwarden/platform/no-enums
11-
export enum FskEntryTypesEnum {
12-
Login = 1,
13-
CreditCard = 2,
14-
}
9+
/**
10+
* Represents the different types of FSK entries.
11+
*/
12+
export const FskEntryType = Object.freeze({
13+
Login: 1,
14+
CreditCard: 2,
15+
});
16+
17+
/**
18+
* Type representing valid FSK entry type values.
19+
*/
20+
export type FskEntryType = (typeof FskEntryType)[keyof typeof FskEntryType];
1521

1622
export interface FskEntry {
1723
color: string;
@@ -26,7 +32,7 @@ export interface FskEntry {
2632
rev: string | number;
2733
service: string;
2834
style: string;
29-
type: FskEntryTypesEnum;
35+
type: FskEntryType;
3036
url: string;
3137
username: string;
3238
createdDate: number; // UNIX timestamp
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
// FIXME: update to use a const object instead of a typescript enum
2-
// eslint-disable-next-line @bitwarden/platform/no-enums
3-
export enum IdpProvider {
4-
Azure = 0,
5-
OktaAuthServer = 1,
6-
OktaNoAuthServer = 2,
7-
Google = 3,
8-
PingOne = 4,
9-
OneLogin = 5,
10-
}
1+
/**
2+
* Represents the different identity providers supported for authentication.
3+
*/
4+
export const IdpProvider = Object.freeze({
5+
Azure: 0,
6+
OktaAuthServer: 1,
7+
OktaNoAuthServer: 2,
8+
Google: 3,
9+
PingOne: 4,
10+
OneLogin: 5,
11+
} as const);
12+
13+
/**
14+
* Type representing valid identity provider values.
15+
*/
16+
export type IdpProvider = (typeof IdpProvider)[keyof typeof IdpProvider];
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
// FIXME: update to use a const object instead of a typescript enum
2-
// eslint-disable-next-line @bitwarden/platform/no-enums
3-
export enum LastpassLoginType {
4-
MasterPassword = 0,
1+
/**
2+
* Represents LastPass login types.
3+
*/
4+
export const LastpassLoginType = Object.freeze({
5+
MasterPassword: 0,
56
// Not sure what Types 1 and 2 are?
6-
Federated = 3,
7-
}
7+
Federated: 3,
8+
} as const);
9+
10+
/**
11+
* Type representing valid LastPass login type values.
12+
*/
13+
export type LastpassLoginType = (typeof LastpassLoginType)[keyof typeof LastpassLoginType];
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
// FIXME: update to use a const object instead of a typescript enum
2-
// eslint-disable-next-line @bitwarden/platform/no-enums
3-
export enum OtpMethod {
4-
GoogleAuth,
5-
MicrosoftAuth,
6-
Yubikey,
7-
}
1+
/**
2+
* Represents OTP authentication methods.
3+
*/
4+
export const OtpMethod = Object.freeze({
5+
GoogleAuth: 0,
6+
MicrosoftAuth: 1,
7+
Yubikey: 2,
8+
} as const);
9+
10+
/**
11+
* Type representing valid OTP method values.
12+
*/
13+
export type OtpMethod = (typeof OtpMethod)[keyof typeof OtpMethod];
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
// FIXME: update to use a const object instead of a typescript enum
2-
// eslint-disable-next-line @bitwarden/platform/no-enums
3-
export enum Platform {
4-
Desktop,
5-
Mobile,
6-
}
1+
/**
2+
* Platform types representing different device categories.
3+
*/
4+
export const Platform = Object.freeze({
5+
Desktop: 0,
6+
Mobile: 1,
7+
} as const);
8+
9+
/**
10+
* Type representing valid platform values.
11+
*/
12+
export type Platform = (typeof Platform)[keyof typeof Platform];

libs/importer/src/importers/onepassword/onepassword-1pux-importer.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { BaseImporter } from "../base-importer";
1414
import { Importer } from "../importer";
1515

1616
import {
17-
CategoryEnum,
17+
Category,
1818
Details,
1919
ExportData,
2020
FieldsEntity,
2121
Item,
22-
LoginFieldTypeEnum,
22+
LoginFieldType,
2323
Overview,
2424
PasswordHistoryEntity,
2525
SectionsEntity,
@@ -45,38 +45,38 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
4545

4646
const cipher = this.initLoginCipher();
4747

48-
const category = item.categoryUuid as CategoryEnum;
48+
const category = item.categoryUuid as Category;
4949
switch (category) {
50-
case CategoryEnum.Login:
51-
case CategoryEnum.Database:
52-
case CategoryEnum.Password:
53-
case CategoryEnum.WirelessRouter:
54-
case CategoryEnum.Server:
55-
case CategoryEnum.API_Credential:
50+
case Category.Login:
51+
case Category.Database:
52+
case Category.Password:
53+
case Category.WirelessRouter:
54+
case Category.Server:
55+
case Category.API_Credential:
5656
cipher.type = CipherType.Login;
5757
cipher.login = new LoginView();
5858
break;
59-
case CategoryEnum.CreditCard:
60-
case CategoryEnum.BankAccount:
59+
case Category.CreditCard:
60+
case Category.BankAccount:
6161
cipher.type = CipherType.Card;
6262
cipher.card = new CardView();
6363
break;
64-
case CategoryEnum.SecureNote:
65-
case CategoryEnum.SoftwareLicense:
66-
case CategoryEnum.EmailAccount:
67-
case CategoryEnum.MedicalRecord:
64+
case Category.SecureNote:
65+
case Category.SoftwareLicense:
66+
case Category.EmailAccount:
67+
case Category.MedicalRecord:
6868
// case CategoryEnum.Document:
6969
cipher.type = CipherType.SecureNote;
7070
cipher.secureNote = new SecureNoteView();
7171
cipher.secureNote.type = SecureNoteType.Generic;
7272
break;
73-
case CategoryEnum.Identity:
74-
case CategoryEnum.DriversLicense:
75-
case CategoryEnum.OutdoorLicense:
76-
case CategoryEnum.Membership:
77-
case CategoryEnum.Passport:
78-
case CategoryEnum.RewardsProgram:
79-
case CategoryEnum.SocialSecurityNumber:
73+
case Category.Identity:
74+
case Category.DriversLicense:
75+
case Category.OutdoorLicense:
76+
case Category.Membership:
77+
case Category.Passport:
78+
case Category.RewardsProgram:
79+
case Category.SocialSecurityNumber:
8080
cipher.type = CipherType.Identity;
8181
cipher.identity = new IdentityView();
8282
break;
@@ -166,10 +166,10 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
166166
let fieldValue = loginField.value;
167167
let fieldType: FieldType = FieldType.Text;
168168
switch (loginField.fieldType) {
169-
case LoginFieldTypeEnum.Password:
169+
case LoginFieldType.Password:
170170
fieldType = FieldType.Hidden;
171171
break;
172-
case LoginFieldTypeEnum.CheckBox:
172+
case LoginFieldType.CheckBox:
173173
fieldValue = loginField.value !== "" ? "true" : "false";
174174
fieldType = FieldType.Boolean;
175175
break;
@@ -180,8 +180,8 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
180180
});
181181
}
182182

183-
private processDetails(category: CategoryEnum, details: Details, cipher: CipherView) {
184-
if (category !== CategoryEnum.Password) {
183+
private processDetails(category: Category, details: Details, cipher: CipherView) {
184+
if (category !== Category.Password) {
185185
return;
186186
}
187187

@@ -191,7 +191,7 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
191191
cipher.login.password = details.password;
192192
}
193193

194-
private processSections(category: CategoryEnum, sections: SectionsEntity[], cipher: CipherView) {
194+
private processSections(category: Category, sections: SectionsEntity[], cipher: CipherView) {
195195
if (sections == null || sections.length === 0) {
196196
return;
197197
}
@@ -206,7 +206,7 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
206206
}
207207

208208
private parseSectionFields(
209-
category: CategoryEnum,
209+
category: Category,
210210
fields: FieldsEntity[],
211211
cipher: CipherView,
212212
sectionTitle: string,
@@ -232,20 +232,20 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
232232
}
233233

234234
switch (category) {
235-
case CategoryEnum.Login:
236-
case CategoryEnum.Database:
237-
case CategoryEnum.EmailAccount:
238-
case CategoryEnum.WirelessRouter:
235+
case Category.Login:
236+
case Category.Database:
237+
case Category.EmailAccount:
238+
case Category.WirelessRouter:
239239
break;
240240

241-
case CategoryEnum.Server:
241+
case Category.Server:
242242
if (this.isNullOrWhitespace(cipher.login.uri) && field.id === "url") {
243243
cipher.login.uris = this.makeUriArray(fieldValue);
244244
return;
245245
}
246246
break;
247247

248-
case CategoryEnum.API_Credential:
248+
case Category.API_Credential:
249249
if (this.fillApiCredentials(field, fieldValue, cipher)) {
250250
return;
251251
}
@@ -258,7 +258,7 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
258258
return;
259259
}
260260

261-
if (category === CategoryEnum.BankAccount) {
261+
if (category === Category.BankAccount) {
262262
if (this.fillBankAccount(field, fieldValue, cipher)) {
263263
return;
264264
}
@@ -281,34 +281,34 @@ export class OnePassword1PuxImporter extends BaseImporter implements Importer {
281281
}
282282

283283
switch (category) {
284-
case CategoryEnum.Identity:
284+
case Category.Identity:
285285
break;
286-
case CategoryEnum.DriversLicense:
286+
case Category.DriversLicense:
287287
if (this.fillDriversLicense(field, fieldValue, cipher)) {
288288
return;
289289
}
290290
break;
291-
case CategoryEnum.OutdoorLicense:
291+
case Category.OutdoorLicense:
292292
if (this.fillOutdoorLicense(field, fieldValue, cipher)) {
293293
return;
294294
}
295295
break;
296-
case CategoryEnum.Membership:
296+
case Category.Membership:
297297
if (this.fillMembership(field, fieldValue, cipher)) {
298298
return;
299299
}
300300
break;
301-
case CategoryEnum.Passport:
301+
case Category.Passport:
302302
if (this.fillPassport(field, fieldValue, cipher)) {
303303
return;
304304
}
305305
break;
306-
case CategoryEnum.RewardsProgram:
306+
case Category.RewardsProgram:
307307
if (this.fillRewardsProgram(field, fieldValue, cipher)) {
308308
return;
309309
}
310310
break;
311-
case CategoryEnum.SocialSecurityNumber:
311+
case Category.SocialSecurityNumber:
312312
if (this.fillSSN(field, fieldValue, cipher)) {
313313
return;
314314
}

0 commit comments

Comments
 (0)