Remove helper methods

This commit is contained in:
Thomas Rittson 2022-06-29 10:23:01 +10:00
parent d9f2dc2b90
commit 2115c5c76a
11 changed files with 139 additions and 160 deletions

View File

@ -70,50 +70,4 @@ describe("Utils Service", () => {
expect(Utils.newGuid()).toMatch(validGuid);
});
});
describe("copyToObject", () => {
const source = {
sourceProp1: "value1",
sourceProp2: "value2",
sourceProp3: "value3",
};
it("Copies properties with different target names", () => {
const map = {
sourceProp1: "targetProp1",
sourceProp3: "targetProp3",
};
const target = Utils.copyToObject(source, map, {});
expect(target).toEqual({
targetProp1: "value1",
targetProp3: "value3",
});
});
it("Copies properties with the same names", () => {
const map: any = {
sourceProp1: null,
sourceProp3: null,
};
const target = Utils.copyToObject(source, map, {});
expect(target).toEqual({
sourceProp1: "value1",
sourceProp3: "value3",
});
});
});
describe("CopyToNewObject", () => {
it("Instantiates a new object type if provided", () => {
class TestClass {}
const target = Utils.copyToNewObject({}, {}, TestClass);
expect(target).toBeInstanceOf(TestClass);
});
});
});

View File

@ -348,35 +348,6 @@ export class Utils {
return s.charAt(0).toUpperCase() + s.slice(1);
}
/**
* Copies properties from a source object to a new object according to a mapping
* Same as Utils.copyToObject but will instantiate the new object for you according to a given type
*/
static copyToNewObject<T = Record<string, unknown>>(
source: any,
map: { [sourcePropName: string]: string },
targetType?: new () => T
): T {
const target: any = targetType != null ? new targetType() : {};
return Utils.copyToObject(source, map, target);
}
/**
* Copies properties from a source object to a target object according to a mapping
* @param source The source to copy from
* @param map The mapping of source property names to target property names (target names are optional)
* @param target The target to copy to
*/
static copyToObject(source: any, map: { [sourcePropName: string]: string }, target: any): any {
Object.keys(map).forEach((sourcePropName) => {
const targetPropName = map[sourcePropName] ?? sourcePropName;
const value = source[sourcePropName];
target[targetPropName] = value;
});
return target;
}
private static validIpAddress(ipString: string): boolean {
const ipRegex =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

View File

@ -1,5 +1,3 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { Attachment } from "../domain/attachment";
import { SymmetricCryptoKey } from "../domain/symmetricCryptoKey";
@ -36,19 +34,15 @@ export class AttachmentView implements View {
}
static fromJSON(obj: any): AttachmentView {
const view = Utils.copyToNewObject(
obj,
{
id: null,
url: null,
size: null,
sizeName: null,
fileName: null,
},
AttachmentView
);
const view = new AttachmentView();
view.id = obj.id;
view.url = obj.url;
view.size = obj.size;
view.sizeName = obj.sizeName;
view.fileName = obj.fileName;
view.key = obj.key == null ? null : SymmetricCryptoKey.fromJSON(obj.key);
return view;
}
}

View File

@ -1,5 +1,3 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { CardLinkedId as LinkedId } from "../../enums/linkedIdType";
import { linkedFieldOption } from "../../misc/linkedFieldOption.decorator";
@ -93,10 +91,25 @@ export class CardView extends ItemView {
toJSON() {
// Needed to serialize getters which are not included by JSON.stringify
return Utils.copyToNewObject(this, serializedProperties);
return {
cardholderName: this.cardholderName,
brand: this.brand,
number: this.number,
expMonth: this.expMonth,
expYear: this.expYear,
code: this.code,
};
}
static fromJSON(obj: any): CardView {
return Utils.copyToNewObject(obj, serializedProperties, CardView);
const view = new CardView();
view.cardholderName = obj.cardholderName;
view.brand = obj.brand;
view.number = obj.number;
view.expMonth = obj.expMonth;
view.expYear = obj.expYear;
view.code = obj.code;
return view;
}
}

View File

@ -153,36 +153,65 @@ export class CipherView implements View {
return this.linkedFieldOptions.get(id)?.i18nKey;
}
toJSON() {
const propertiesToCopy = {
attachments: null,
fields: null,
passwordHistory: null,
...serializedProperties,
toJSON(): any {
const result: any = {
id: this.id,
organizationId: this.organizationId,
folderId: this.folderId,
name: this.name,
notes: this.notes,
type: this.type,
favorite: this.favorite,
organizationUseTotp: this.organizationUseTotp,
edit: this.edit,
viewPassword: this.viewPassword,
localData: this.localData,
collectionIds: this.collectionIds,
reprompt: this.reprompt,
attachments: this.attachments,
fields: this.fields,
passwordHistory: this.passwordHistory,
revisionDate: this.revisionDate,
deletedDate: this.deletedDate,
};
switch (this.type) {
case CipherType.Card:
propertiesToCopy.card = null;
result.card = this.card;
break;
case CipherType.Identity:
propertiesToCopy.identity = null;
result.identity = this.identity;
break;
case CipherType.Login:
propertiesToCopy.login = null;
result.login = this.login;
break;
case CipherType.SecureNote:
propertiesToCopy.secureNote = null;
result.secureNote = this.secureNote;
break;
default:
break;
}
return Utils.copyToNewObject(this, propertiesToCopy);
return result;
}
static fromJSON(obj: any): CipherView {
const view = Utils.copyToNewObject(obj, serializedProperties, CipherView);
const view = new CipherView();
view.id = obj.id;
view.organizationId = obj.organizationId;
view.folderId = obj.folderId;
view.name = obj.name;
view.notes = obj.notes;
view.type = obj.type;
view.favorite = obj.favorite;
view.organizationUseTotp = obj.organizationUseTotp;
view.edit = obj.edit;
view.viewPassword = obj.viewPassword;
view.localData = obj.localData;
view.collectionIds = obj.collectionIds;
view.reprompt = obj.reprompt;
// Dates
view.revisionDate = obj.revisionDate == null ? null : new Date(obj.revisionDate);

View File

@ -1,5 +1,3 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { FieldType } from "../../enums/fieldType";
import { LinkedIdType } from "../../enums/linkedIdType";
import { Field } from "../domain/field";
@ -29,18 +27,15 @@ export class FieldView implements View {
}
static fromJSON(obj: any): FieldView {
return Utils.copyToNewObject(
obj,
{
name: null,
value: null,
type: null,
newField: null,
showValue: null,
showCount: null,
linkedId: null,
},
FieldView
);
const view = new FieldView();
view.name = obj.name;
view.value = obj.value;
view.type = obj.type;
view.newField = obj.newField;
view.showValue = obj.showValue;
view.showCount = obj.showCount;
view.linkedId = obj.linkedId;
return view;
}
}

View File

@ -163,10 +163,49 @@ export class IdentityView extends ItemView {
toJSON() {
// Needed to serialize getters which are not included by JSON.stringify
return Utils.copyToNewObject(this, serializedProperties);
return {
title: this.title,
firstName: this.firstName,
middleName: this.middleName,
lastName: this.lastName,
address1: this.address1,
address2: this.address2,
address3: this.address3,
city: this.city,
state: this.state,
postalCode: this.postalCode,
country: this.country,
company: this.company,
email: this.email,
phone: this.phone,
ssn: this.ssn,
username: this.username,
passportNumber: this.passportNumber,
licenseNumber: this.licenseNumber,
};
}
static fromJSON(obj: any): IdentityView {
return Utils.copyToNewObject(obj, serializedProperties, IdentityView);
const view = new IdentityView();
view.title = obj.title;
view.firstName = obj.firstName;
view.middleName = obj.middleName;
view.lastName = obj.lastName;
view.address1 = obj.address1;
view.address2 = obj.address2;
view.address3 = obj.address3;
view.city = obj.city;
view.state = obj.state;
view.postalCode = obj.postalCode;
view.country = obj.country;
view.company = obj.company;
view.email = obj.email;
view.phone = obj.phone;
view.ssn = obj.ssn;
view.username = obj.username;
view.passportNumber = obj.passportNumber;
view.licenseNumber = obj.licenseNumber;
return view;
}
}

View File

@ -1,5 +1,3 @@
import { stringify } from "querystring";
import { UriMatchType } from "../../enums/uriMatchType";
import { Utils } from "../../misc/utils";
import { LoginUri } from "../domain/loginUri";
@ -22,10 +20,7 @@ const CanLaunchWhitelist = [
"androidapp://",
];
const serializedProperties: any = {
match: null,
uri: null,
};
const serializedProperties: any = {};
export class LoginUriView implements View {
match: UriMatchType = null;
@ -134,10 +129,16 @@ export class LoginUriView implements View {
toJSON(): any {
// Needed to serialize getters which are not included by JSON.stringify
return Utils.copyToNewObject(this, serializedProperties);
return {
match: this.match,
uri: this.uri,
};
}
static fromJSON(obj: any): LoginUriView {
return Utils.copyToNewObject(obj, serializedProperties, LoginUriView);
const view = new LoginUriView();
view.match = obj.match;
view.uri = obj.uri;
return view;
}
}

View File

@ -6,15 +6,6 @@ import { Login } from "../domain/login";
import { ItemView } from "./itemView";
import { LoginUriView } from "./loginUriView";
const serializedProperties: any = {
username: null,
password: null,
totp: null,
autofillOnPageLoad: null,
passwordRevisionDate: null,
uris: null,
};
export class LoginView extends ItemView {
@linkedFieldOption(LinkedId.Username)
username: string = null;
@ -71,10 +62,16 @@ export class LoginView extends ItemView {
}
static fromJSON(obj: any): LoginView {
const view = Utils.copyToNewObject(obj, serializedProperties, LoginView);
const view = new LoginView();
view.username = obj.username;
view.password = obj.password;
view.totp = obj.totp;
view.autofillOnPageLoad = obj.autofillOnPageLoad;
view.passwordRevisionDate =
obj.passwordRevisionDate == null ? null : new Date(obj.passwordRevisionDate);
view.uris = obj.uris?.map((uri: any) => LoginUriView.fromJSON(uri));
return view;
}
}

View File

@ -1,5 +1,3 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { Password } from "../domain/password";
import { View } from "./view";
@ -17,16 +15,10 @@ export class PasswordHistoryView implements View {
}
static fromJSON(obj: any): PasswordHistoryView {
const ph = Utils.copyToNewObject(
obj,
{
password: null,
},
PasswordHistoryView
);
const view = new PasswordHistoryView();
view.password = obj.password;
view.lastUsedDate = obj.lastUsedDate == null ? null : new Date(obj.lastUsedDate);
ph.lastUsedDate = obj.lastUsedDate == null ? null : new Date(obj.lastUsedDate);
return ph;
return view;
}
}

View File

@ -1,5 +1,3 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { SecureNoteType } from "../../enums/secureNoteType";
import { SecureNote } from "../domain/secureNote";
@ -22,12 +20,8 @@ export class SecureNoteView extends ItemView {
}
static fromJSON(obj: any): SecureNoteView {
return Utils.copyToNewObject(
obj,
{
type: null,
},
SecureNoteView
);
const view = new SecureNoteView();
view.type = obj.type;
return view;
}
}