refactor req/res models

This commit is contained in:
Kyle Spearrin 2018-05-15 12:18:47 -04:00
parent 1d3ed93bff
commit 07cc64c0b8
25 changed files with 363 additions and 229 deletions

View File

@ -5,8 +5,8 @@ import { FolderService } from 'jslib/services/folder.service';
import { Response } from '../models/response';
import { CipherRequest } from '../models/request/cipherRequest';
import { FolderRequest } from '../models/request/folderRequest';
import { Cipher } from '../models/cipher';
import { Folder } from '../models/folder';
export class CreateCommand {
constructor(private cipherService: CipherService, private folderService: FolderService) { }
@ -30,8 +30,8 @@ export class CreateCommand {
}
}
private async createCipher(req: CipherRequest) {
const cipher = await this.cipherService.encrypt(CipherRequest.toView(req));
private async createCipher(req: Cipher) {
const cipher = await this.cipherService.encrypt(Cipher.toView(req));
try {
await this.cipherService.saveWithServer(cipher);
return Response.success();
@ -40,8 +40,8 @@ export class CreateCommand {
}
}
private async createFolder(req: FolderRequest) {
const folder = await this.folderService.encrypt(FolderRequest.toView(req));
private async createFolder(req: Folder) {
const folder = await this.folderService.encrypt(Folder.toView(req));
try {
await this.folderService.saveWithServer(folder);
return Response.success();

View File

@ -5,8 +5,8 @@ import { FolderService } from 'jslib/services/folder.service';
import { Response } from '../models/response';
import { CipherRequest } from '../models/request/cipherRequest';
import { FolderRequest } from '../models/request/folderRequest';
import { Cipher } from '../models/cipher';
import { Folder } from '../models/folder';
export class EditCommand {
constructor(private cipherService: CipherService, private folderService: FolderService) { }
@ -30,14 +30,14 @@ export class EditCommand {
}
}
private async editCipher(id: string, req: CipherRequest) {
private async editCipher(id: string, req: Cipher) {
const cipher = await this.cipherService.get(id);
if (cipher == null) {
return Response.notFound();
}
let cipherView = await cipher.decrypt();
cipherView = CipherRequest.toView(req, cipherView);
cipherView = Cipher.toView(req, cipherView);
const encCipher = await this.cipherService.encrypt(cipherView);
try {
await this.cipherService.saveWithServer(encCipher);
@ -47,14 +47,14 @@ export class EditCommand {
}
}
private async editFolder(id: string, req: FolderRequest) {
private async editFolder(id: string, req: Folder) {
const folder = await this.folderService.get(id);
if (folder == null) {
return Response.notFound();
}
let folderView = await folder.decrypt();
folderView = FolderRequest.toView(req, folderView);
folderView = Folder.toView(req, folderView);
const encFolder = await this.folderService.encrypt(folderView);
try {
await this.folderService.saveWithServer(encFolder);

View File

@ -14,13 +14,13 @@ import { FolderResponse } from '../models/response/folderResponse';
import { StringResponse } from '../models/response/stringResponse';
import { TemplateResponse } from '../models/response/templateResponse';
import { CardRequest } from '../models/request/cardRequest';
import { CipherRequest } from '../models/request/cipherRequest';
import { FieldRequest } from '../models/request/fieldRequest';
import { IdentityRequest } from '../models/request/identityRequest';
import { LoginRequest } from '../models/request/loginRequest';
import { LoginUriRequest } from '../models/request/loginUriRequest';
import { SecureNoteRequest } from '../models/request/secureNoteRequest';
import { Card } from '../models/card';
import { Cipher } from '../models/cipher';
import { Field } from '../models/field';
import { Identity } from '../models/identity';
import { Login } from '../models/login';
import { LoginUri } from '../models/loginUri';
import { SecureNote } from '../models/secureNote';
export class GetCommand {
constructor(private cipherService: CipherService, private folderService: FolderService,
@ -104,25 +104,25 @@ export class GetCommand {
let template: any = null;
switch (id.toLowerCase()) {
case 'item':
template = CipherRequest.template();
template = Cipher.template();
break;
case 'field':
template = FieldRequest.template();
template = Field.template();
break;
case 'login':
template = LoginRequest.template();
template = Login.template();
break;
case 'loginuri':
template = LoginUriRequest.template();
template = LoginUri.template();
break;
case 'card':
template = CardRequest.template();
template = Card.template();
break;
case 'identity':
template = IdentityRequest.template();
template = Identity.template();
break;
case 'securenote':
template = SecureNoteRequest.template();
template = SecureNote.template();
break;
default:
return Response.badRequest('Unknown template object.');

View File

@ -1,8 +1,8 @@
import { CardView } from 'jslib/models/view';
import { CardView } from 'jslib/models/view/cardView';
export class CardRequest {
static template(): CardRequest {
const req = new CardRequest();
export class Card {
static template(): Card {
const req = new Card();
req.cardholderName = 'John Doe';
req.brand = 'visa';
req.number = '4242424242424242';
@ -12,7 +12,7 @@ export class CardRequest {
return req;
}
static toView(req: CardRequest, view = new CardView()) {
static toView(req: Card, view = new CardView()) {
view.cardholderName = req.cardholderName;
view.brand = req.brand;
view.number = req.number;
@ -28,4 +28,17 @@ export class CardRequest {
expMonth: string;
expYear: string;
code: string;
constructor(o?: CardView) {
if (o == null) {
return;
}
this.cardholderName = o.cardholderName;
this.brand = o.brand;
this.number = o.number;
this.expMonth = o.expMonth;
this.expYear = o.expYear;
this.code = o.code;
}
}

104
src/models/cipher.ts Normal file
View File

@ -0,0 +1,104 @@
import { CipherType } from 'jslib/enums/cipherType';
import { CipherRequest } from 'jslib/models/request/cipherRequest';
import { CipherView } from 'jslib/models/view/cipherView';
import { Card } from './card';
import { Field } from './field';
import { Identity } from './identity';
import { Login } from './login';
import { SecureNote } from './secureNote';
export class Cipher {
static template(): Cipher {
const req = new Cipher();
req.type = CipherType.Login;
req.folderId = null;
req.organizationId = null;
req.name = 'Item name';
req.notes = 'Some notes about this item.';
req.favorite = false;
req.fields = [];
req.login = null;
req.secureNote = null;
req.card = null;
req.identity = null;
return req;
}
static toView(req: CipherRequest, view = new CipherView()) {
view.type = req.type;
view.folderId = req.folderId;
if (view.organizationId == null) {
view.organizationId = req.organizationId;
}
view.name = req.name;
view.notes = req.notes;
view.favorite = req.favorite;
if (req.fields != null) {
view.fields = req.fields.map((f) => Field.toView(f));
}
switch (req.type) {
case CipherType.Login:
view.login = Login.toView(req.login);
break;
case CipherType.SecureNote:
view.secureNote = SecureNote.toView(req.secureNote);
break;
case CipherType.Card:
view.card = Card.toView(req.card);
break;
case CipherType.Identity:
view.identity = Identity.toView(req.identity);
break;
}
return view;
}
type: CipherType;
folderId: string;
organizationId: string;
name: string;
notes: string;
favorite: boolean;
fields: Field[];
login: Login;
secureNote: SecureNote;
card: Card;
identity: Identity;
constructor(o?: CipherView) {
if (o == null) {
return;
}
this.organizationId = o.organizationId;
this.folderId = o.folderId;
this.type = o.type;
this.name = o.name;
this.notes = o.notes;
this.favorite = o.favorite;
if (o.fields != null) {
this.fields = o.fields.map((f) => new Field(f));
}
switch (o.type) {
case CipherType.Login:
this.login = new Login(o.login);
break;
case CipherType.SecureNote:
this.secureNote = new SecureNote(o.secureNote);
break;
case CipherType.Card:
this.card = new Card(o.card);
break;
case CipherType.Identity:
this.identity = new Identity(o.identity);
break;
}
}
}

23
src/models/collection.ts Normal file
View File

@ -0,0 +1,23 @@
import { CollectionView } from 'jslib/models/view/collectionView';
export class Collection {
static toView(req: Collection, view = new CollectionView()) {
view.name = req.name;
if (view.organizationId == null) {
view.organizationId = req.organizationId;
}
return view;
}
name: string;
organizationId: string;
constructor(o?: CollectionView) {
if (o == null) {
return;
}
this.name = o.name;
this.organizationId = o.organizationId;
}
}

34
src/models/field.ts Normal file
View File

@ -0,0 +1,34 @@
import { FieldType } from 'jslib/enums/fieldType';
import { FieldView } from 'jslib/models/view/fieldView';
export class Field {
static template(): Field {
const req = new Field();
req.name = 'Field name';
req.value = 'Some value';
req.type = FieldType.Text;
return req;
}
static toView(req: Field, view = new FieldView()) {
view.type = req.type;
view.value = req.value;
view.name = req.name;
return view;
}
name: string;
value: string;
type: FieldType;
constructor(o?: FieldView) {
if (o == null) {
return;
}
this.name = o.name;
this.value = o.value;
this.type = o.type;
}
}

18
src/models/folder.ts Normal file
View File

@ -0,0 +1,18 @@
import { FolderView } from 'jslib/models/view/folderView';
export class Folder {
static toView(req: Folder, view = new FolderView()) {
view.name = req.name;
return view;
}
name: string;
constructor(o?: FolderView) {
if (o == null) {
return;
}
this.name = o.name;
}
}

View File

@ -1,8 +1,8 @@
import { IdentityView } from 'jslib/models/view';
import { IdentityView } from 'jslib/models/view/identityView';
export class IdentityRequest {
static template(): IdentityRequest {
const req = new IdentityRequest();
export class Identity {
static template(): Identity {
const req = new Identity();
req.title = 'Mr';
req.firstName = 'John';
req.middleName = 'William';
@ -24,7 +24,7 @@ export class IdentityRequest {
return req;
}
static toView(req: IdentityRequest, view = new IdentityView()) {
static toView(req: Identity, view = new IdentityView()) {
view.title = req.title;
view.firstName = req.firstName;
view.middleName = req.middleName;
@ -64,4 +64,29 @@ export class IdentityRequest {
username: string;
passportNumber: string;
licenseNumber: string;
constructor(o?: IdentityView) {
if (o == null) {
return;
}
this.title = o.title;
this.firstName = o.firstName;
this.middleName = o.middleName;
this.lastName = o.lastName;
this.address1 = o.address1;
this.address2 = o.address2;
this.address3 = o.address3;
this.city = o.city;
this.state = o.state;
this.postalCode = o.postalCode;
this.country = o.country;
this.company = o.company;
this.email = o.email;
this.phone = o.phone;
this.ssn = o.ssn;
this.username = o.username;
this.passportNumber = o.passportNumber;
this.licenseNumber = o.licenseNumber;
}
}

43
src/models/login.ts Normal file
View File

@ -0,0 +1,43 @@
import { LoginUri } from './loginUri';
import { LoginView } from 'jslib/models/view/loginView';
export class Login {
static template(): Login {
const req = new Login();
req.uris = [];
req.username = 'jdoe';
req.password = 'myp@ssword123';
req.totp = 'JBSWY3DPEHPK3PXP';
return req;
}
static toView(req: Login, view = new LoginView()) {
if (req.uris != null) {
view.uris = req.uris.map((u) => LoginUri.toView(u));
}
view.username = req.username;
view.password = req.password;
view.totp = req.totp;
return view;
}
uris: LoginUri[];
username: string;
password: string;
totp: string;
constructor(o?: LoginView) {
if (o == null) {
return;
}
if (o.uris != null) {
this.uris = o.uris.map((u) => new LoginUri(u));
}
this.username = o.username;
this.password = o.password;
this.totp = o.totp;
}
}

View File

@ -1,15 +1,16 @@
import { UriMatchType } from 'jslib/enums/uriMatchType';
import { LoginUriView } from 'jslib/models/view/loginUriView';
export class LoginUriRequest {
static template(): LoginUriRequest {
const req = new LoginUriRequest();
export class LoginUri {
static template(): LoginUri {
const req = new LoginUri();
req.uri = 'https://google.com';
req.match = null;
return req;
}
static toView(req: LoginUriRequest, view = new LoginUriView()) {
static toView(req: LoginUri, view = new LoginUriView()) {
view.uri = req.uri;
view.match = req.match;
return view;
@ -17,4 +18,13 @@ export class LoginUriRequest {
uri: string;
match: UriMatchType = null;
constructor(o?: LoginUriView) {
if (o == null) {
return;
}
this.uri = o.uri;
this.match = o.match;
}
}

View File

@ -1,69 +0,0 @@
import { CipherType } from 'jslib/enums/cipherType';
import { CardRequest } from './cardRequest';
import { FieldRequest } from './fieldRequest';
import { IdentityRequest } from './identityRequest';
import { LoginRequest } from './loginRequest';
import { SecureNoteRequest } from './secureNoteRequest';
import { CipherView } from 'jslib/models/view/cipherView';
export class CipherRequest {
static template(): CipherRequest {
const req = new CipherRequest();
req.type = CipherType.Login;
req.folderId = null;
req.organizationId = null;
req.name = 'Item name';
req.notes = 'Some notes about this item.';
req.favorite = false;
req.fields = [];
req.login = null;
req.secureNote = null;
req.card = null;
req.identity = null;
return req;
}
static toView(req: CipherRequest, view = new CipherView()) {
view.type = req.type;
view.folderId = req.folderId;
view.organizationId = req.organizationId;
view.name = req.name;
view.notes = req.notes;
view.favorite = req.favorite;
if (req.fields != null) {
view.fields = req.fields.map((f) => FieldRequest.toView(f));
}
switch (req.type) {
case CipherType.Login:
view.login = LoginRequest.toView(req.login);
break;
case CipherType.SecureNote:
view.secureNote = SecureNoteRequest.toView(req.secureNote);
break;
case CipherType.Card:
view.card = CardRequest.toView(req.card);
break;
case CipherType.Identity:
view.identity = IdentityRequest.toView(req.identity);
break;
}
return view;
}
type: CipherType;
folderId: string;
organizationId: string;
name: string;
notes: string;
favorite: boolean;
fields: FieldRequest[];
login: LoginRequest;
secureNote: SecureNoteRequest;
card: CardRequest;
identity: IdentityRequest;
}

View File

@ -1,3 +0,0 @@
export class CollectionRequest {
name: string;
}

View File

@ -1,23 +0,0 @@
import { FieldType } from 'jslib/enums/fieldType';
import { FieldView } from 'jslib/models/view';
export class FieldRequest {
static template(): FieldRequest {
const req = new FieldRequest();
req.name = 'Field name';
req.value = 'Some value';
req.type = FieldType.Text;
return req;
}
static toView(req: FieldRequest, view = new FieldView()) {
view.type = req.type;
view.value = req.value;
view.name = req.name;
return view;
}
name: string;
value: string;
type: FieldType;
}

View File

@ -1,10 +0,0 @@
import { FolderView } from 'jslib/models/view/folderView';
export class FolderRequest {
static toView(req: FolderRequest, view = new FolderView()) {
view.name = req.name;
return view;
}
name: string;
}

View File

@ -1,29 +0,0 @@
import { LoginUriRequest } from './loginUriRequest';
import { LoginView } from 'jslib/models/view';
export class LoginRequest {
static template(): LoginRequest {
const req = new LoginRequest();
req.uris = [];
req.username = 'jdoe';
req.password = 'myp@ssword123';
req.totp = 'JBSWY3DPEHPK3PXP';
return req;
}
static toView(req: LoginRequest, view = new LoginView()) {
if (req.uris != null) {
view.uris = req.uris.map((u) => LoginUriRequest.toView(u));
}
view.username = req.username;
view.password = req.password;
view.totp = req.totp;
return view;
}
uris: LoginUriRequest[];
username: string;
password: string;
totp: string;
}

View File

@ -1,17 +0,0 @@
import { SecureNoteType } from 'jslib/enums/secureNoteType';
import { SecureNoteView } from 'jslib/models/view/secureNoteView';
export class SecureNoteRequest {
static template(): SecureNoteRequest {
const req = new SecureNoteRequest();
req.type = SecureNoteType.Generic;
return req;
}
static toView(req: SecureNoteRequest, view = new SecureNoteView()) {
view.type = req.type;
return view;
}
type: SecureNoteType;
}

View File

@ -1,9 +1,3 @@
export abstract class BaseResponse {
export interface BaseResponse {
object: string;
constructor(object?: string) {
if (object != null) {
this.object = object;
}
}
}

View File

@ -2,21 +2,15 @@ import { CipherView } from 'jslib/models/view/cipherView';
import { BaseResponse } from './baseResponse';
import { CipherType } from 'jslib/enums';
import { Cipher } from '../cipher';
export class CipherResponse extends BaseResponse {
export class CipherResponse extends Cipher implements BaseResponse {
object: string;
id: string;
organizationId: string;
type: CipherType;
name: string;
notes: string;
constructor(o: CipherView) {
super('item');
super(o);
this.object = 'item';
this.id = o.id;
this.organizationId = o.organizationId;
this.type = o.type;
this.name = o.name;
this.notes = o.notes;
}
}

View File

@ -2,15 +2,15 @@ import { CollectionView } from 'jslib/models/view/collectionView';
import { BaseResponse } from './baseResponse';
export class CollectionResponse extends BaseResponse {
import { Collection } from '../collection';
export class CollectionResponse extends Collection implements BaseResponse {
object: string;
id: string;
organizationId: string;
name: string;
constructor(o: CollectionView) {
super('collection');
super(o);
this.object = 'collection';
this.id = o.id;
this.organizationId = o.organizationId;
this.name = o.name;
}
}

View File

@ -2,13 +2,15 @@ import { FolderView } from 'jslib/models/view/folderView';
import { BaseResponse } from './baseResponse';
export class FolderResponse extends BaseResponse {
import { Folder } from '../folder';
export class FolderResponse extends Folder implements BaseResponse {
object: string;
id: string;
name: string;
constructor(o: FolderView) {
super('folder');
super(o);
this.object = 'folder';
this.id = o.id;
this.name = o.name;
}
}

View File

@ -1,10 +1,11 @@
import { BaseResponse } from './baseResponse';
export class ListResponse extends BaseResponse {
export class ListResponse implements BaseResponse {
object: string;
data: BaseResponse[];
constructor(data: BaseResponse[]) {
super('list');
this.object = 'list';
this.data = data;
}
}

View File

@ -1,14 +1,11 @@
import { CipherView } from 'jslib/models/view/cipherView';
import { BaseResponse } from './baseResponse';
import { CipherType } from 'jslib/enums';
export class StringResponse extends BaseResponse {
export class StringResponse implements BaseResponse {
object: string;
data: string;
constructor(data: string) {
super('string');
this.object = 'string';
this.data = data;
}
}

View File

@ -1,10 +1,11 @@
import { BaseResponse } from './baseResponse';
export class TemplateResponse extends BaseResponse {
export class TemplateResponse implements BaseResponse {
object: string;
template: any;
constructor(template: any) {
super('template');
this.object = 'template';
this.template = template;
}
}

26
src/models/secureNote.ts Normal file
View File

@ -0,0 +1,26 @@
import { SecureNoteType } from 'jslib/enums/secureNoteType';
import { SecureNoteView } from 'jslib/models/view/secureNoteView';
export class SecureNote {
static template(): SecureNote {
const req = new SecureNote();
req.type = SecureNoteType.Generic;
return req;
}
static toView(req: SecureNote, view = new SecureNoteView()) {
view.type = req.type;
return view;
}
type: SecureNoteType;
constructor(o?: SecureNoteView) {
if (o == null) {
return;
}
this.type = o.type;
}
}