Format files
This commit is contained in:
parent
02486d671e
commit
438caa96a6
19 changed files with 557 additions and 585 deletions
|
|
@ -1,17 +1,16 @@
|
|||
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||
|
||||
import type { Client } from "openapi-fetch";
|
||||
import createClient from "openapi-fetch";
|
||||
import type { components, paths } from "./types.js"; // Generated by openapi-typescript
|
||||
import type { components, paths } from "./types"; // Generated by openapi-typescript
|
||||
import type { Database } from "src/database/database";
|
||||
import type { SyncSettings } from "src/database/sync-settings";
|
||||
import type {
|
||||
DocumentId,
|
||||
RelativePath,
|
||||
VaultUpdateId,
|
||||
VaultUpdateId
|
||||
} from "src/database/document-metadata";
|
||||
import { Logger } from "src/tracing/logger.js";
|
||||
import { retriedFetch } from "src/utils/retried-fetch.js";
|
||||
import { Logger } from "src/tracing/logger";
|
||||
import { retriedFetch } from "src/utils/retried-fetch";
|
||||
import { bytesToBase64 } from "sync_lib";
|
||||
|
||||
export interface CheckConnectionResult {
|
||||
isSuccessful: boolean;
|
||||
|
|
@ -45,11 +44,9 @@ export class SyncService {
|
|||
const response = await this.clientWithoutRetries.GET("/ping", {
|
||||
params: {
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
},
|
||||
},
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Logger.getInstance().debug(
|
||||
|
|
@ -58,9 +55,7 @@ export class SyncService {
|
|||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to ping server: ${SyncService.formatError(
|
||||
response.error
|
||||
)}`
|
||||
`Failed to ping server: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +65,7 @@ export class SyncService {
|
|||
public async create({
|
||||
relativePath,
|
||||
contentBytes,
|
||||
createdDate,
|
||||
createdDate
|
||||
}: {
|
||||
relativePath: RelativePath;
|
||||
contentBytes: Uint8Array;
|
||||
|
|
@ -81,27 +76,23 @@ export class SyncService {
|
|||
{
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
vault_id: this.database.getSettings().vaultName
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
},
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
body: {
|
||||
contentBase64: lib.bytesToBase64(contentBytes),
|
||||
contentBase64: bytesToBase64(contentBytes),
|
||||
createdDate: createdDate.toISOString(),
|
||||
relativePath,
|
||||
},
|
||||
relativePath
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to create document: ${SyncService.formatError(
|
||||
response.error
|
||||
)}`
|
||||
`Failed to create document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +110,7 @@ export class SyncService {
|
|||
documentId,
|
||||
relativePath,
|
||||
contentBytes,
|
||||
createdDate,
|
||||
createdDate
|
||||
}: {
|
||||
parentVersionId: VaultUpdateId;
|
||||
documentId: DocumentId;
|
||||
|
|
@ -133,28 +124,24 @@ export class SyncService {
|
|||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
},
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
body: {
|
||||
parentVersionId,
|
||||
contentBase64: lib.bytesToBase64(contentBytes),
|
||||
contentBase64: bytesToBase64(contentBytes),
|
||||
createdDate: createdDate.toISOString(),
|
||||
relativePath,
|
||||
},
|
||||
relativePath
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to update document: ${SyncService.formatError(
|
||||
response.error
|
||||
)}`
|
||||
`Failed to update document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +155,7 @@ export class SyncService {
|
|||
public async delete({
|
||||
documentId,
|
||||
relativePath,
|
||||
createdDate,
|
||||
createdDate
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
relativePath: RelativePath;
|
||||
|
|
@ -180,18 +167,16 @@ export class SyncService {
|
|||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
},
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
},
|
||||
body: {
|
||||
createdDate: createdDate.toISOString(),
|
||||
relativePath,
|
||||
},
|
||||
relativePath
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
|
@ -207,7 +192,7 @@ export class SyncService {
|
|||
}
|
||||
|
||||
public async get({
|
||||
documentId,
|
||||
documentId
|
||||
}: {
|
||||
documentId: DocumentId;
|
||||
}): Promise<components["schemas"]["DocumentVersion"]> {
|
||||
|
|
@ -217,22 +202,18 @@ export class SyncService {
|
|||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
document_id: documentId,
|
||||
document_id: documentId
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
},
|
||||
},
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
throw new Error(
|
||||
`Failed to get document: ${SyncService.formatError(
|
||||
response.error
|
||||
)}`
|
||||
`Failed to get document: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -249,25 +230,21 @@ export class SyncService {
|
|||
const response = await this.client.GET("/vaults/{vault_id}/documents", {
|
||||
params: {
|
||||
path: {
|
||||
vault_id: this.database.getSettings().vaultName,
|
||||
vault_id: this.database.getSettings().vaultName
|
||||
},
|
||||
header: {
|
||||
authorization: `Bearer ${
|
||||
this.database.getSettings().token
|
||||
}`,
|
||||
authorization: `Bearer ${this.database.getSettings().token}`
|
||||
},
|
||||
query: {
|
||||
since_update_id: since,
|
||||
},
|
||||
},
|
||||
since_update_id: since
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const { error } = response;
|
||||
if (error) {
|
||||
throw new Error(
|
||||
`Failed to get documents: ${SyncService.formatError(
|
||||
response.error
|
||||
)}`
|
||||
`Failed to get documents: ${SyncService.formatError(response.error)}`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -284,18 +261,18 @@ export class SyncService {
|
|||
if (result.isAuthenticated) {
|
||||
return {
|
||||
isSuccessful: true,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) and authenticated.`,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) and authenticated.`
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
isSuccessful: false,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) but failed to authenticate.`,
|
||||
message: `Successfully connected to server (version: ${result.serverVersion}) but failed to authenticate.`
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
isSuccessful: false,
|
||||
message: `Failed to connect to server: ${e}`,
|
||||
message: `Failed to connect to server: ${e}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -303,11 +280,11 @@ export class SyncService {
|
|||
private createClient(settings: SyncSettings): void {
|
||||
this.client = createClient<paths>({
|
||||
baseUrl: settings.remoteUri,
|
||||
fetch: retriedFetch,
|
||||
fetch: retriedFetch
|
||||
});
|
||||
|
||||
this.clientWithoutRetries = createClient<paths>({
|
||||
baseUrl: settings.remoteUri,
|
||||
baseUrl: settings.remoteUri
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue