mirror of
https://github.com/bitwarden/browser
synced 2025-12-21 18:53:29 +00:00
convert data models to jslib
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import { Abstractions, Enums, Request, Response } from '@bitwarden/jslib';
|
||||
import { Abstractions, Data, Enums, Request, Response } from '@bitwarden/jslib';
|
||||
|
||||
import { Cipher } from '../models/domain/cipher';
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import { Field } from '../models/domain/field';
|
||||
import SymmetricCryptoKey from '../models/domain/symmetricCryptoKey';
|
||||
|
||||
import { CipherData } from '../models/data/cipherData';
|
||||
|
||||
import ApiService from './api.service';
|
||||
import ConstantsService from './constants.service';
|
||||
import CryptoService from './crypto.service';
|
||||
@@ -128,7 +126,7 @@ export default class CipherService {
|
||||
async get(id: string): Promise<Cipher> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const localData = await this.storageService.get<any>(Keys.localData);
|
||||
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
||||
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
if (ciphers == null || !ciphers.hasOwnProperty(id)) {
|
||||
return null;
|
||||
@@ -140,7 +138,7 @@ export default class CipherService {
|
||||
async getAll(): Promise<Cipher[]> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const localData = await this.storageService.get<any>(Keys.localData);
|
||||
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
||||
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
const response: Cipher[] = [];
|
||||
for (const id in ciphers) {
|
||||
@@ -292,7 +290,7 @@ export default class CipherService {
|
||||
}
|
||||
|
||||
const userId = await this.userService.getUserId();
|
||||
const data = new CipherData(response, userId, cipher.collectionIds);
|
||||
const data = new Data.Cipher(response, userId, cipher.collectionIds);
|
||||
await this.upsert(data);
|
||||
}
|
||||
|
||||
@@ -321,7 +319,7 @@ export default class CipherService {
|
||||
}
|
||||
|
||||
const userId = await self.userService.getUserId();
|
||||
const data = new CipherData(response, userId, cipher.collectionIds);
|
||||
const data = new Data.Cipher(response, userId, cipher.collectionIds);
|
||||
this.upsert(data);
|
||||
resolve(new Cipher(data));
|
||||
|
||||
@@ -333,19 +331,19 @@ export default class CipherService {
|
||||
});
|
||||
}
|
||||
|
||||
async upsert(cipher: CipherData | CipherData[]): Promise<any> {
|
||||
async upsert(cipher: Data.Cipher | Data.Cipher[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
let ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
||||
let ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
if (ciphers == null) {
|
||||
ciphers = {};
|
||||
}
|
||||
|
||||
if (cipher instanceof CipherData) {
|
||||
const c = cipher as CipherData;
|
||||
if (cipher instanceof Data.Cipher) {
|
||||
const c = cipher as Data.Cipher;
|
||||
ciphers[c.id] = c;
|
||||
} else {
|
||||
(cipher as CipherData[]).forEach((c) => {
|
||||
(cipher as Data.Cipher[]).forEach((c) => {
|
||||
ciphers[c.id] = c;
|
||||
});
|
||||
}
|
||||
@@ -354,7 +352,7 @@ export default class CipherService {
|
||||
this.decryptedCipherCache = null;
|
||||
}
|
||||
|
||||
async replace(ciphers: { [id: string]: CipherData; }): Promise<any> {
|
||||
async replace(ciphers: { [id: string]: Data.Cipher; }): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
|
||||
this.decryptedCipherCache = null;
|
||||
@@ -367,7 +365,7 @@ export default class CipherService {
|
||||
|
||||
async delete(id: string | string[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
||||
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
if (ciphers == null) {
|
||||
return;
|
||||
@@ -393,7 +391,7 @@ export default class CipherService {
|
||||
|
||||
async deleteAttachment(id: string, attachmentId: string): Promise<void> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
|
||||
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
|
||||
if (ciphers == null || !ciphers.hasOwnProperty(id) || ciphers[id].attachments == null) {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import { Collection } from '../models/domain/collection';
|
||||
|
||||
import { CollectionData } from '../models/data/collectionData';
|
||||
|
||||
import CryptoService from './crypto.service';
|
||||
import UserService from './user.service';
|
||||
|
||||
import { Abstractions } from '@bitwarden/jslib';
|
||||
import { Abstractions, Data } from '@bitwarden/jslib';
|
||||
|
||||
const Keys = {
|
||||
collectionsPrefix: 'collections_',
|
||||
@@ -25,7 +23,7 @@ export default class CollectionService {
|
||||
|
||||
async get(id: string): Promise<Collection> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
||||
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
if (collections == null || !collections.hasOwnProperty(id)) {
|
||||
return null;
|
||||
@@ -36,7 +34,7 @@ export default class CollectionService {
|
||||
|
||||
async getAll(): Promise<Collection[]> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
||||
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
const response: Collection[] = [];
|
||||
for (const id in collections) {
|
||||
@@ -71,19 +69,19 @@ export default class CollectionService {
|
||||
return this.decryptedCollectionCache;
|
||||
}
|
||||
|
||||
async upsert(collection: CollectionData | CollectionData[]): Promise<any> {
|
||||
async upsert(collection: Data.Collection | Data.Collection[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
let collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
||||
let collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
if (collections == null) {
|
||||
collections = {};
|
||||
}
|
||||
|
||||
if (collection instanceof CollectionData) {
|
||||
const c = collection as CollectionData;
|
||||
if (collection instanceof Data.Collection) {
|
||||
const c = collection as Data.Collection;
|
||||
collections[c.id] = c;
|
||||
} else {
|
||||
(collection as CollectionData[]).forEach((c) => {
|
||||
(collection as Data.Collection[]).forEach((c) => {
|
||||
collections[c.id] = c;
|
||||
});
|
||||
}
|
||||
@@ -92,7 +90,7 @@ export default class CollectionService {
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
|
||||
async replace(collections: { [id: string]: Data.Collection; }): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
||||
this.decryptedCollectionCache = null;
|
||||
@@ -105,7 +103,7 @@ export default class CollectionService {
|
||||
|
||||
async delete(id: string | string[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
||||
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
if (collections == null) {
|
||||
return;
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
|
||||
import { FolderData } from '../models/data/folderData';
|
||||
|
||||
import ApiService from './api.service';
|
||||
import CryptoService from './crypto.service';
|
||||
import UserService from './user.service';
|
||||
|
||||
import { Abstractions, Domain, Request, Response } from '@bitwarden/jslib';
|
||||
import { Abstractions, Data, Domain, Request, Response } from '@bitwarden/jslib';
|
||||
|
||||
const Keys = {
|
||||
foldersPrefix: 'folders_',
|
||||
@@ -33,7 +31,7 @@ export default class FolderService {
|
||||
|
||||
async get(id: string): Promise<Domain.Folder> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
|
||||
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
|
||||
Keys.foldersPrefix + userId);
|
||||
if (folders == null || !folders.hasOwnProperty(id)) {
|
||||
return null;
|
||||
@@ -44,7 +42,7 @@ export default class FolderService {
|
||||
|
||||
async getAll(): Promise<Domain.Folder[]> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
|
||||
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
|
||||
Keys.foldersPrefix + userId);
|
||||
const response: Domain.Folder[] = [];
|
||||
for (const id in folders) {
|
||||
@@ -95,23 +93,23 @@ export default class FolderService {
|
||||
}
|
||||
|
||||
const userId = await this.userService.getUserId();
|
||||
const data = new FolderData(response, userId);
|
||||
const data = new Data.Folder(response, userId);
|
||||
await this.upsert(data);
|
||||
}
|
||||
|
||||
async upsert(folder: FolderData | FolderData[]): Promise<any> {
|
||||
async upsert(folder: Data.Folder | Data.Folder[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
let folders = await this.storageService.get<{ [id: string]: FolderData; }>(
|
||||
let folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
|
||||
Keys.foldersPrefix + userId);
|
||||
if (folders == null) {
|
||||
folders = {};
|
||||
}
|
||||
|
||||
if (folder instanceof FolderData) {
|
||||
const f = folder as FolderData;
|
||||
if (folder instanceof Data.Folder) {
|
||||
const f = folder as Data.Folder;
|
||||
folders[f.id] = f;
|
||||
} else {
|
||||
(folder as FolderData[]).forEach((f) => {
|
||||
(folder as Data.Folder[]).forEach((f) => {
|
||||
folders[f.id] = f;
|
||||
});
|
||||
}
|
||||
@@ -120,7 +118,7 @@ export default class FolderService {
|
||||
this.decryptedFolderCache = null;
|
||||
}
|
||||
|
||||
async replace(folders: { [id: string]: FolderData; }): Promise<any> {
|
||||
async replace(folders: { [id: string]: Data.Folder; }): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
await this.storageService.save(Keys.foldersPrefix + userId, folders);
|
||||
this.decryptedFolderCache = null;
|
||||
@@ -133,7 +131,7 @@ export default class FolderService {
|
||||
|
||||
async delete(id: string | string[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
|
||||
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
|
||||
Keys.foldersPrefix + userId);
|
||||
if (folders == null) {
|
||||
return;
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
import { CipherData } from '../models/data/cipherData';
|
||||
import { CollectionData } from '../models/data/collectionData';
|
||||
import { FolderData } from '../models/data/folderData';
|
||||
|
||||
import ApiService from './api.service';
|
||||
import CipherService from './cipher.service';
|
||||
import CollectionService from './collection.service';
|
||||
@@ -10,7 +6,7 @@ import FolderService from './folder.service';
|
||||
import SettingsService from './settings.service';
|
||||
import UserService from './user.service';
|
||||
|
||||
import { Abstractions, Response } from '@bitwarden/jslib';
|
||||
import { Abstractions, Data, Response } from '@bitwarden/jslib';
|
||||
|
||||
const Keys = {
|
||||
lastSyncPrefix: 'lastSync_',
|
||||
@@ -132,25 +128,25 @@ export default class SyncService {
|
||||
}
|
||||
|
||||
private async syncFolders(userId: string, response: Response.Folder[]) {
|
||||
const folders: { [id: string]: FolderData; } = {};
|
||||
const folders: { [id: string]: Data.Folder; } = {};
|
||||
response.forEach((f) => {
|
||||
folders[f.id] = new FolderData(f, userId);
|
||||
folders[f.id] = new Data.Folder(f, userId);
|
||||
});
|
||||
return await this.folderService.replace(folders);
|
||||
}
|
||||
|
||||
private async syncCollections(response: Response.Collection[]) {
|
||||
const collections: { [id: string]: CollectionData; } = {};
|
||||
const collections: { [id: string]: Data.Collection; } = {};
|
||||
response.forEach((c) => {
|
||||
collections[c.id] = new CollectionData(c);
|
||||
collections[c.id] = new Data.Collection(c);
|
||||
});
|
||||
return await this.collectionService.replace(collections);
|
||||
}
|
||||
|
||||
private async syncCiphers(userId: string, response: Response.Cipher[]) {
|
||||
const ciphers: { [id: string]: CipherData; } = {};
|
||||
const ciphers: { [id: string]: Data.Cipher; } = {};
|
||||
response.forEach((c) => {
|
||||
ciphers[c.id] = new CipherData(c, userId);
|
||||
ciphers[c.id] = new Data.Cipher(c, userId);
|
||||
});
|
||||
return await this.cipherService.replace(ciphers);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user