1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

convert api service to ts with fetch

This commit is contained in:
Kyle Spearrin
2017-11-03 11:59:45 -04:00
parent 9f9e3245de
commit 6ae7b2e035
16 changed files with 616 additions and 147 deletions

View File

@@ -1,70 +0,0 @@
window.CipherRequest = function (cipher) {
this.type = cipher.type;
this.folderId = cipher.folderId;
this.organizationId = cipher.organizationId;
this.name = cipher.name ? cipher.name.encryptedString : null;
this.notes = cipher.notes ? cipher.notes.encryptedString : null;
this.favorite = cipher.favorite;
var constantsService = chrome.extension.getBackgroundPage().bg_constantsService;
switch (this.type) {
case constantsService.cipherType.login:
this.login = {
uri: cipher.login.uri ? cipher.login.uri.encryptedString : null,
username: cipher.login.username ? cipher.login.username.encryptedString : null,
password: cipher.login.password ? cipher.login.password.encryptedString : null,
totp: cipher.login.totp ? cipher.login.totp.encryptedString : null
};
break;
case constantsService.cipherType.secureNote:
this.secureNote = {
type: cipher.secureNote.type
};
break;
case constantsService.cipherType.card:
this.card = {
cardholderName: cipher.card.cardholderName ? cipher.card.cardholderName.encryptedString : null,
brand: cipher.card.brand ? cipher.card.brand.encryptedString : null,
number: cipher.card.number ? cipher.card.number.encryptedString : null,
expMonth: cipher.card.expMonth ? cipher.card.expMonth.encryptedString : null,
expYear: cipher.card.expYear ? cipher.card.expYear.encryptedString : null,
code: cipher.card.code ? cipher.card.code.encryptedString : null
};
break;
case constantsService.cipherType.identity:
this.identity = {
title: cipher.identity.title ? cipher.identity.title.encryptedString : null,
firstName: cipher.identity.firstName ? cipher.identity.firstName.encryptedString : null,
middleName: cipher.identity.middleName ? cipher.identity.middleName.encryptedString : null,
lastName: cipher.identity.lastName ? cipher.identity.lastName.encryptedString : null,
address1: cipher.identity.address1 ? cipher.identity.address1.encryptedString : null,
address2: cipher.identity.address2 ? cipher.identity.address2.encryptedString : null,
address3: cipher.identity.address3 ? cipher.identity.address3.encryptedString : null,
city: cipher.identity.city ? cipher.identity.city.encryptedString : null,
state: cipher.identity.state ? cipher.identity.state.encryptedString : null,
postalCode: cipher.identity.postalCode ? cipher.identity.postalCode.encryptedString : null,
country: cipher.identity.country ? cipher.identity.country.encryptedString : null,
company: cipher.identity.company ? cipher.identity.company.encryptedString : null,
email: cipher.identity.email ? cipher.identity.email.encryptedString : null,
phone: cipher.identity.phone ? cipher.identity.phone.encryptedString : null,
ssn: cipher.identity.ssn ? cipher.identity.ssn.encryptedString : null,
username: cipher.identity.username ? cipher.identity.username.encryptedString : null,
passportNumber: cipher.identity.passportNumber ? cipher.identity.passportNumber.encryptedString : null,
licenseNumber: cipher.identity.licenseNumber ? cipher.identity.licenseNumber.encryptedString : null
};
break;
default:
break;
}
if (cipher.fields) {
this.fields = [];
for (var i = 0; i < cipher.fields.length; i++) {
this.fields.push({
type: cipher.fields[i].type,
name: cipher.fields[i].name ? cipher.fields[i].name.encryptedString : null,
value: cipher.fields[i].value ? cipher.fields[i].value.encryptedString : null,
});
}
}
};

View File

@@ -0,0 +1,5 @@
export default class EnvironmentUrls {
base: string;
api: string;
identity: string;
}

View File

@@ -1,4 +1,4 @@
var Cipher = function (obj, alreadyEncrypted, localData) {
var Cipher = window.Cipher = function (obj, alreadyEncrypted, localData) {
this.constantsService = chrome.extension.getBackgroundPage().bg_constantsService;
this.utilsService = chrome.extension.getBackgroundPage().bg_utilsService;
@@ -55,7 +55,7 @@ var Cipher = function (obj, alreadyEncrypted, localData) {
}
};
var Login2 = function (obj, alreadyEncrypted) {
var Login2 = window.Login2 = function (obj, alreadyEncrypted) {
buildDomainModel(this, obj, {
uri: null,
username: null,
@@ -64,7 +64,7 @@ var Login2 = function (obj, alreadyEncrypted) {
}, alreadyEncrypted, []);
};
var Identity = function (obj, alreadyEncrypted) {
var Identity = window.Identity = function (obj, alreadyEncrypted) {
buildDomainModel(this, obj, {
title: null,
firstName: null,
@@ -87,7 +87,7 @@ var Identity = function (obj, alreadyEncrypted) {
}, alreadyEncrypted, []);
};
var Card = function (obj, alreadyEncrypted) {
var Card = window.Card = function (obj, alreadyEncrypted) {
buildDomainModel(this, obj, {
cardholderName: null,
brand: null,
@@ -98,11 +98,11 @@ var Card = function (obj, alreadyEncrypted) {
}, alreadyEncrypted, []);
};
var SecureNote = function (obj, alreadyEncrypted) {
var SecureNote = window.SecureNote = function (obj, alreadyEncrypted) {
this.type = obj.type;
};
var Field = function (obj, alreadyEncrypted) {
var Field = window.Field = function (obj, alreadyEncrypted) {
this.type = obj.type;
buildDomainModel(this, obj, {
name: null,
@@ -110,7 +110,7 @@ var Field = function (obj, alreadyEncrypted) {
}, alreadyEncrypted, []);
};
var Attachment = function (obj, alreadyEncrypted) {
var Attachment = window.Attachment = function (obj, alreadyEncrypted) {
this.size = obj.size;
buildDomainModel(this, obj, {
id: null,
@@ -120,7 +120,7 @@ var Attachment = function (obj, alreadyEncrypted) {
}, alreadyEncrypted, ['id', 'url', 'sizeName']);
};
var Folder = function (obj, alreadyEncrypted) {
var Folder = window.Folder = function (obj, alreadyEncrypted) {
buildDomainModel(this, obj, {
id: null,
name: null

View File

@@ -0,0 +1,89 @@
import { CipherType } from '../../enums/cipherType.enum';
class CipherRequest {
type: CipherType;
folderId: string;
organizationId: string;
name: string;
notes: string;
favorite: boolean;
login: any;
secureNote: any;
card: any;
identity: any;
fields: any[];
constructor(cipher: any) {
this.type = cipher.type;
this.folderId = cipher.folderId;
this.organizationId = cipher.organizationId;
this.name = cipher.name ? cipher.name.encryptedString : null;
this.notes = cipher.notes ? cipher.notes.encryptedString : null;
this.favorite = cipher.favorite;
switch (this.type) {
case CipherType.Login:
this.login = {
uri: cipher.login.uri ? cipher.login.uri.encryptedString : null,
username: cipher.login.username ? cipher.login.username.encryptedString : null,
password: cipher.login.password ? cipher.login.password.encryptedString : null,
totp: cipher.login.totp ? cipher.login.totp.encryptedString : null,
};
break;
case CipherType.SecureNote:
this.secureNote = {
type: cipher.secureNote.type,
};
break;
case CipherType.Card:
this.card = {
cardholderName: cipher.card.cardholderName ? cipher.card.cardholderName.encryptedString : null,
brand: cipher.card.brand ? cipher.card.brand.encryptedString : null,
number: cipher.card.number ? cipher.card.number.encryptedString : null,
expMonth: cipher.card.expMonth ? cipher.card.expMonth.encryptedString : null,
expYear: cipher.card.expYear ? cipher.card.expYear.encryptedString : null,
code: cipher.card.code ? cipher.card.code.encryptedString : null,
};
break;
case CipherType.Identity:
this.identity = {
title: cipher.identity.title ? cipher.identity.title.encryptedString : null,
firstName: cipher.identity.firstName ? cipher.identity.firstName.encryptedString : null,
middleName: cipher.identity.middleName ? cipher.identity.middleName.encryptedString : null,
lastName: cipher.identity.lastName ? cipher.identity.lastName.encryptedString : null,
address1: cipher.identity.address1 ? cipher.identity.address1.encryptedString : null,
address2: cipher.identity.address2 ? cipher.identity.address2.encryptedString : null,
address3: cipher.identity.address3 ? cipher.identity.address3.encryptedString : null,
city: cipher.identity.city ? cipher.identity.city.encryptedString : null,
state: cipher.identity.state ? cipher.identity.state.encryptedString : null,
postalCode: cipher.identity.postalCode ? cipher.identity.postalCode.encryptedString : null,
country: cipher.identity.country ? cipher.identity.country.encryptedString : null,
company: cipher.identity.company ? cipher.identity.company.encryptedString : null,
email: cipher.identity.email ? cipher.identity.email.encryptedString : null,
phone: cipher.identity.phone ? cipher.identity.phone.encryptedString : null,
ssn: cipher.identity.ssn ? cipher.identity.ssn.encryptedString : null,
username: cipher.identity.username ? cipher.identity.username.encryptedString : null,
passportNumber: cipher.identity.passportNumber ?
cipher.identity.passportNumber.encryptedString : null,
licenseNumber: cipher.identity.licenseNumber ? cipher.identity.licenseNumber.encryptedString : null,
};
break;
default:
break;
}
if (cipher.fields) {
this.fields = [];
for (const field of cipher.fields) {
this.fields.push({
type: field.type,
name: field.name ? field.name.encryptedString : null,
value: field.value ? field.value.encryptedString : null,
});
}
}
}
}
export { CipherRequest };
(window as any).CipherRequest = CipherRequest;

View File

@@ -3,21 +3,21 @@ class ErrorResponse {
validationErrors: { [key: string]: string[]; };
statusCode: number;
constructor(response: any, identityResponse?: boolean) {
constructor(response: any, status: number, identityResponse?: boolean) {
let errorModel = null;
if (identityResponse && response.responseJSON && response.responseJSON.ErrorModel) {
errorModel = response.responseJSON.ErrorModel;
} else if (response.responseJSON) {
errorModel = response.responseJSON;
} else if (response.responseText && response.responseText.indexOf('{') === 0) {
errorModel = JSON.parse(response.responseText);
if (identityResponse && response && response.ErrorModel) {
errorModel = response.ErrorModel;
} else if (response) {
errorModel = response;
//} else if (response.responseText && response.responseText.indexOf('{') === 0) {
// errorModel = JSON.parse(response.responseText);
}
if (errorModel) {
this.message = errorModel.Message;
this.validationErrors = errorModel.ValidationErrors;
}
this.statusCode = response.status;
this.statusCode = status;
}
}