mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 09:13:33 +00:00
Ps 1291/apply to from json pattern to state (#3425)
* Clean up dangling behaviorSubject
* Handle null in utils
* fix null check
* Await promises, even in async functions
* Add to/fromJSON methods to State and Accounts
This is needed since all storage in manifest v3 is key-value-pair-based
and session storage of most data is actually serialized into an
encrypted string.
* Simplify AccountKeys json parsing
* Fix account key (de)serialization
* Remove unused DecodedToken state
* Correct filename typo
* Simplify keys `toJSON` tests
* Explain AccountKeys `toJSON` return type
* Remove unnecessary `any`s
* Remove unique ArrayBuffer serialization
* Initialize items in MemoryStorageService
* Revert "Fix account key (de)serialization"
This reverts commit b1dffb5c2c, which was breaking serializations
* Move fromJSON to owning object
* Add DeepJsonify type
* Use Records for storage
* Add new Account Settings to serialized data
* Fix failing serialization tests
* Extract complex type conversion to helper methods
* Remove unnecessary decorator
* Return null from json deserializers
* Remove unnecessary decorators
* Remove obsolete test
* Use type-fest `Jsonify` formatting rules for external library
* Update jsonify comment
Co-authored-by: @eliykat
* Remove erroneous comment
* Fix unintended deep-jsonify changes
* Fix prettierignore
* Fix formatting of deep-jsonify.ts
Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com>
This commit is contained in:
55
libs/common/src/models/data/server-config.data.spec.ts
Normal file
55
libs/common/src/models/data/server-config.data.spec.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
EnvironmentServerConfigData,
|
||||
ServerConfigData,
|
||||
ThirdPartyServerConfigData,
|
||||
} from "./server-config.data";
|
||||
|
||||
describe("ServerConfigData", () => {
|
||||
describe("fromJSON", () => {
|
||||
it("should create a ServerConfigData from a JSON object", () => {
|
||||
const serverConfigData = ServerConfigData.fromJSON({
|
||||
version: "1.0.0",
|
||||
gitHash: "1234567890",
|
||||
server: {
|
||||
name: "test",
|
||||
url: "https://test.com",
|
||||
},
|
||||
environment: {
|
||||
vault: "https://vault.com",
|
||||
api: "https://api.com",
|
||||
identity: "https://identity.com",
|
||||
notifications: "https://notifications.com",
|
||||
sso: "https://sso.com",
|
||||
},
|
||||
utcDate: "2020-01-01T00:00:00.000Z",
|
||||
});
|
||||
|
||||
expect(serverConfigData.version).toEqual("1.0.0");
|
||||
expect(serverConfigData.gitHash).toEqual("1234567890");
|
||||
expect(serverConfigData.server.name).toEqual("test");
|
||||
expect(serverConfigData.server.url).toEqual("https://test.com");
|
||||
expect(serverConfigData.environment.vault).toEqual("https://vault.com");
|
||||
expect(serverConfigData.environment.api).toEqual("https://api.com");
|
||||
expect(serverConfigData.environment.identity).toEqual("https://identity.com");
|
||||
expect(serverConfigData.environment.notifications).toEqual("https://notifications.com");
|
||||
expect(serverConfigData.environment.sso).toEqual("https://sso.com");
|
||||
expect(serverConfigData.utcDate).toEqual("2020-01-01T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("should be an instance of ServerConfigData", () => {
|
||||
const serverConfigData = ServerConfigData.fromJSON({} as any);
|
||||
|
||||
expect(serverConfigData).toBeInstanceOf(ServerConfigData);
|
||||
});
|
||||
|
||||
it("should deserialize sub objects", () => {
|
||||
const serverConfigData = ServerConfigData.fromJSON({
|
||||
server: {},
|
||||
environment: {},
|
||||
} as any);
|
||||
|
||||
expect(serverConfigData.server).toBeInstanceOf(ThirdPartyServerConfigData);
|
||||
expect(serverConfigData.environment).toBeInstanceOf(EnvironmentServerConfigData);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,5 @@
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import {
|
||||
ServerConfigResponse,
|
||||
ThirdPartyServerConfigResponse,
|
||||
@@ -11,27 +13,38 @@ export class ServerConfigData {
|
||||
environment?: EnvironmentServerConfigData;
|
||||
utcDate: string;
|
||||
|
||||
constructor(serverConfigReponse: ServerConfigResponse) {
|
||||
this.version = serverConfigReponse?.version;
|
||||
this.gitHash = serverConfigReponse?.gitHash;
|
||||
this.server = serverConfigReponse?.server
|
||||
? new ThirdPartyServerConfigData(serverConfigReponse.server)
|
||||
constructor(serverConfigResponse: Partial<ServerConfigResponse>) {
|
||||
this.version = serverConfigResponse?.version;
|
||||
this.gitHash = serverConfigResponse?.gitHash;
|
||||
this.server = serverConfigResponse?.server
|
||||
? new ThirdPartyServerConfigData(serverConfigResponse.server)
|
||||
: null;
|
||||
this.utcDate = new Date().toISOString();
|
||||
this.environment = serverConfigReponse?.environment
|
||||
? new EnvironmentServerConfigData(serverConfigReponse.environment)
|
||||
this.environment = serverConfigResponse?.environment
|
||||
? new EnvironmentServerConfigData(serverConfigResponse.environment)
|
||||
: null;
|
||||
}
|
||||
|
||||
static fromJSON(obj: Jsonify<ServerConfigData>): ServerConfigData {
|
||||
return Object.assign(new ServerConfigData({}), obj, {
|
||||
server: obj?.server ? ThirdPartyServerConfigData.fromJSON(obj.server) : null,
|
||||
environment: obj?.environment ? EnvironmentServerConfigData.fromJSON(obj.environment) : null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ThirdPartyServerConfigData {
|
||||
name: string;
|
||||
url: string;
|
||||
|
||||
constructor(response: ThirdPartyServerConfigResponse) {
|
||||
constructor(response: Partial<ThirdPartyServerConfigResponse>) {
|
||||
this.name = response.name;
|
||||
this.url = response.url;
|
||||
}
|
||||
|
||||
static fromJSON(obj: Jsonify<ThirdPartyServerConfigData>): ThirdPartyServerConfigData {
|
||||
return Object.assign(new ThirdPartyServerConfigData({}), obj);
|
||||
}
|
||||
}
|
||||
|
||||
export class EnvironmentServerConfigData {
|
||||
@@ -41,11 +54,15 @@ export class EnvironmentServerConfigData {
|
||||
notifications: string;
|
||||
sso: string;
|
||||
|
||||
constructor(response: EnvironmentServerConfigResponse) {
|
||||
constructor(response: Partial<EnvironmentServerConfigResponse>) {
|
||||
this.vault = response.vault;
|
||||
this.api = response.api;
|
||||
this.identity = response.identity;
|
||||
this.notifications = response.notifications;
|
||||
this.sso = response.sso;
|
||||
}
|
||||
|
||||
static fromJSON(obj: Jsonify<EnvironmentServerConfigData>): EnvironmentServerConfigData {
|
||||
return Object.assign(new EnvironmentServerConfigData({}), obj);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user