mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-06 09:20:43 +01:00
send api helper
This commit is contained in:
parent
0d2cd4c482
commit
79fd5d9832
@ -121,647 +121,194 @@ export class ApiService implements ApiServiceAbstraction {
|
|||||||
|
|
||||||
// Two Factor APIs
|
// Two Factor APIs
|
||||||
|
|
||||||
async postTwoFactorEmail(request: TwoFactorEmailRequest): Promise<any> {
|
postTwoFactorEmail(request: TwoFactorEmailRequest): Promise<any> {
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/two-factor/send-email-login', {
|
return this.send('POST', '/two-factor/send-email-login', request, false, false);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account APIs
|
// Account APIs
|
||||||
|
|
||||||
async getProfile(): Promise<ProfileResponse> {
|
async getProfile(): Promise<ProfileResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('GET', '/accounts/profile', null, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/profile', {
|
return new ProfileResponse(r);
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new ProfileResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putProfile(request: UpdateProfileRequest): Promise<ProfileResponse> {
|
async putProfile(request: UpdateProfileRequest): Promise<ProfileResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('PUT', '/accounts/profile', request, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/profile', {
|
return new ProfileResponse(r);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new ProfileResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postEmailToken(request: EmailTokenRequest): Promise<any> {
|
postEmailToken(request: EmailTokenRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/accounts/email-token', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/email-token', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postEmail(request: EmailRequest): Promise<any> {
|
postEmail(request: EmailRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/accounts/email', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/email', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postPassword(request: PasswordRequest): Promise<any> {
|
postPassword(request: PasswordRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/accounts/password', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/password', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postSecurityStamp(request: PasswordVerificationRequest): Promise<any> {
|
postSecurityStamp(request: PasswordVerificationRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/accounts/security-stamp', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/security-stamp', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postDeleteAccount(request: PasswordVerificationRequest): Promise<any> {
|
postDeleteAccount(request: PasswordVerificationRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/accounts/delete', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/delete', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAccountRevisionDate(): Promise<number> {
|
async getAccountRevisionDate(): Promise<number> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('GET', '/accounts/revision-date', null, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/revision-date', {
|
return r as number;
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
return (await response.json() as number);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postPasswordHint(request: PasswordHintRequest): Promise<any> {
|
postPasswordHint(request: PasswordHintRequest): Promise<any> {
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/password-hint', {
|
return this.send('POST', '/accounts/password-hint', request, false, false);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postRegister(request: RegisterRequest): Promise<any> {
|
postRegister(request: RegisterRequest): Promise<any> {
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/accounts/register', {
|
return this.send('POST', '/accounts/register', request, false, false);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Folder APIs
|
// Folder APIs
|
||||||
|
|
||||||
async postFolder(request: FolderRequest): Promise<FolderResponse> {
|
async postFolder(request: FolderRequest): Promise<FolderResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('POST', '/folders', request, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/folders', {
|
return new FolderResponse(r);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new FolderResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putFolder(id: string, request: FolderRequest): Promise<FolderResponse> {
|
async putFolder(id: string, request: FolderRequest): Promise<FolderResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('PUT', '/folders/' + id, request, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/folders/' + id, {
|
return new FolderResponse(r);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new FolderResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteFolder(id: string): Promise<any> {
|
deleteFolder(id: string): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('DELETE', '/folders/' + id, null, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/folders/' + id, {
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'DELETE',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cipher APIs
|
// Cipher APIs
|
||||||
|
|
||||||
async postCipher(request: CipherRequest): Promise<CipherResponse> {
|
async postCipher(request: CipherRequest): Promise<CipherResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('POST', '/ciphers', request, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers', {
|
return new CipherResponse(r);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new CipherResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putCipher(id: string, request: CipherRequest): Promise<CipherResponse> {
|
async putCipher(id: string, request: CipherRequest): Promise<CipherResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('PUT', '/ciphers/' + id, request, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id, {
|
return new CipherResponse(r);
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new CipherResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteCipher(id: string): Promise<any> {
|
deleteCipher(id: string): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('DELETE', '/ciphers/' + id, null, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id, {
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'DELETE',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteManyCiphers(request: CipherBulkDeleteRequest): Promise<any> {
|
deleteManyCiphers(request: CipherBulkDeleteRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('DELETE', '/ciphers', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'DELETE',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putMoveCiphers(request: CipherBulkMoveRequest): Promise<any> {
|
putMoveCiphers(request: CipherBulkMoveRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('PUT', '/ciphers/move', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/move', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putShareCipher(id: string, request: CipherShareRequest): Promise<any> {
|
putShareCipher(id: string, request: CipherShareRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('PUT', '/ciphers/' + id + '/share', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id + '/share', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putShareCiphers(request: CipherBulkShareRequest): Promise<any> {
|
putShareCiphers(request: CipherBulkShareRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('PUT', '/ciphers/share', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/share', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putCipherCollections(id: string, request: CipherCollectionsRequest): Promise<any> {
|
putCipherCollections(id: string, request: CipherCollectionsRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('PUT', '/ciphers/' + id + '/collections', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id + '/collections', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'PUT',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postPurgeCiphers(request: PasswordVerificationRequest): Promise<any> {
|
postPurgeCiphers(request: PasswordVerificationRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/ciphers/purge', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/purge', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postImportCiphers(request: ImportCiphersRequest): Promise<any> {
|
postImportCiphers(request: ImportCiphersRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/ciphers/import', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/import', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postImportOrganizationCiphers(request: ImportOrganizationCiphersRequest): Promise<any> {
|
postImportOrganizationCiphers(request: ImportOrganizationCiphersRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/ciphers/import-organization', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/import-organization', {
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attachments APIs
|
// Attachments APIs
|
||||||
|
|
||||||
async postCipherAttachment(id: string, data: FormData): Promise<CipherResponse> {
|
async postCipherAttachment(id: string, data: FormData): Promise<CipherResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('POST', '/ciphers/' + id + '/attachment', data, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id + '/attachment', {
|
return new CipherResponse(r);
|
||||||
body: data,
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new CipherResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteCipherAttachment(id: string, attachmentId: string): Promise<any> {
|
deleteCipherAttachment(id: string, attachmentId: string): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('DELETE', '/ciphers/' + id + '/attachment/' + attachmentId, null, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id + '/attachment/' + attachmentId, {
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'DELETE',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postShareCipherAttachment(id: string, attachmentId: string, data: FormData,
|
postShareCipherAttachment(id: string, attachmentId: string, data: FormData,
|
||||||
organizationId: string): Promise<any> {
|
organizationId: string): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/ciphers/' + id + '/attachment/' +
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/' + id + '/attachment/' +
|
attachmentId + '/share?organizationId=' + organizationId, data, true, false);
|
||||||
attachmentId + '/share?organizationId=' + organizationId, {
|
|
||||||
body: data,
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync APIs
|
// Sync APIs
|
||||||
|
|
||||||
async getSync(): Promise<SyncResponse> {
|
async getSync(): Promise<SyncResponse> {
|
||||||
const authHeader = await this.handleTokenState();
|
const r = await this.send('GET', '/sync', null, true, true);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/sync', {
|
return new SyncResponse(r);
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: this.getCredentials(),
|
|
||||||
headers: new Headers({
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Authorization': authHeader,
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
const responseJson = await response.json();
|
|
||||||
return new SyncResponse(responseJson);
|
|
||||||
} else {
|
|
||||||
const error = await this.handleError(response, false);
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async postImportDirectory(organizationId: string, request: ImportDirectoryRequest): Promise<any> {
|
async postImportDirectory(organizationId: string, request: ImportDirectoryRequest): Promise<any> {
|
||||||
const authHeader = await this.handleTokenState();
|
return this.send('POST', '/organizations/' + organizationId + '/import', request, true, false);
|
||||||
const response = await fetch(new Request(this.apiBaseUrl + '/organizations/' + organizationId + '/import', {
|
}
|
||||||
body: JSON.stringify(request),
|
|
||||||
|
// Helpers
|
||||||
|
|
||||||
|
private async send(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body: any,
|
||||||
|
authed: boolean, hasResponse: boolean): Promise<any> {
|
||||||
|
const headers = new Headers({
|
||||||
|
'Device-Type': this.deviceType,
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestInit: RequestInit = {
|
||||||
cache: 'no-cache',
|
cache: 'no-cache',
|
||||||
credentials: this.getCredentials(),
|
credentials: this.getCredentials(),
|
||||||
headers: new Headers({
|
method: method,
|
||||||
'Accept': 'application/json',
|
};
|
||||||
'Authorization': authHeader,
|
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
|
||||||
'Device-Type': this.deviceType,
|
|
||||||
}),
|
|
||||||
method: 'POST',
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
if (authed) {
|
||||||
|
const authHeader = await this.handleTokenState();
|
||||||
|
headers.set('Authorization', authHeader);
|
||||||
|
}
|
||||||
|
if (body != null) {
|
||||||
|
if (typeof body === 'string') {
|
||||||
|
requestInit.body = body;
|
||||||
|
headers.set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
|
||||||
|
} else if (typeof body === 'object') {
|
||||||
|
if (body instanceof FormData) {
|
||||||
|
requestInit.body = body;
|
||||||
|
} else {
|
||||||
|
headers.set('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
requestInit.body = JSON.stringify(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasResponse) {
|
||||||
|
headers.set('Accept', 'application/json');
|
||||||
|
}
|
||||||
|
|
||||||
|
requestInit.headers = headers;
|
||||||
|
const response = await fetch(new Request(this.apiBaseUrl + path, requestInit));
|
||||||
|
|
||||||
|
if (hasResponse && response.status === 200) {
|
||||||
|
const responseJson = await response.json();
|
||||||
|
return responseJson;
|
||||||
|
} else if (response.status !== 200) {
|
||||||
const error = await this.handleError(response, false);
|
const error = await this.handleError(response, false);
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helpers
|
|
||||||
|
|
||||||
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
|
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
|
||||||
if ((tokenError && response.status === 400) || response.status === 401 || response.status === 403) {
|
if ((tokenError && response.status === 400) || response.status === 401 || response.status === 403) {
|
||||||
await this.logoutCallback(true);
|
await this.logoutCallback(true);
|
||||||
|
Loading…
Reference in New Issue
Block a user