bitwarden-estensione-browser/libs/common/src/models/domain/field.ts

79 lines
1.5 KiB
TypeScript

import { Jsonify } from "type-fest";
import { FieldType } from "../../enums/fieldType";
import { LinkedIdType } from "../../enums/linkedIdType";
import { FieldData } from "../data/field.data";
import { FieldView } from "../view/field.view";
import Domain from "./domain-base";
import { EncString } from "./enc-string";
import { SymmetricCryptoKey } from "./symmetric-crypto-key";
export class Field extends Domain {
name: EncString;
value: EncString;
type: FieldType;
linkedId: LinkedIdType;
constructor(obj?: FieldData) {
super();
if (obj == null) {
return;
}
this.type = obj.type;
this.linkedId = obj.linkedId;
this.buildDomainModel(
this,
obj,
{
name: null,
value: null,
},
[]
);
}
decrypt(orgId: string, encKey?: SymmetricCryptoKey): Promise<FieldView> {
return this.decryptObj(
new FieldView(this),
{
name: null,
value: null,
},
orgId,
encKey
);
}
toFieldData(): FieldData {
const f = new FieldData();
this.buildDataModel(
this,
f,
{
name: null,
value: null,
type: null,
linkedId: null,
},
["type", "linkedId"]
);
return f;
}
static fromJSON(obj: Partial<Jsonify<Field>>): Field {
if (obj == null) {
return null;
}
const name = EncString.fromJSON(obj.name);
const value = EncString.fromJSON(obj.value);
return Object.assign(new Field(), obj, {
name,
value,
});
}
}