Format files

This commit is contained in:
Andras Schmelczer 2025-01-05 15:35:51 +00:00
parent 02486d671e
commit 438caa96a6
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
19 changed files with 557 additions and 585 deletions

View file

@ -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
});
}
}

View file

@ -4,380 +4,382 @@
*/
export interface paths {
"/ping": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header?: {
authorization?: string;
};
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["PingResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/vaults/{vault_id}/documents": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: {
since_update_id?: number | null;
};
header: {
authorization: string;
};
path: {
vault_id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["FetchLatestDocumentsResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put?: never;
post: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["CreateDocumentVersion"];
};
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentUpdateResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/vaults/{vault_id}/documents/{document_id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentVersion"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["UpdateDocumentVersion"];
};
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentUpdateResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
post?: never;
delete: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["DeleteDocumentVersion"];
};
};
responses: {
/** @description no content */
200: {
headers: {
[name: string]: unknown;
};
content?: never;
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/ping": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header?: {
authorization?: string;
};
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["PingResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/vaults/{vault_id}/documents": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: {
since_update_id?: number | null;
};
header: {
authorization: string;
};
path: {
vault_id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["FetchLatestDocumentsResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put?: never;
post: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["CreateDocumentVersion"];
};
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentUpdateResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/vaults/{vault_id}/documents/{document_id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentVersion"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
put: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["UpdateDocumentVersion"];
};
};
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["DocumentUpdateResponse"];
};
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
post?: never;
delete: {
parameters: {
query?: never;
header: {
authorization: string;
};
path: {
document_id: string;
vault_id: string;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["DeleteDocumentVersion"];
};
};
responses: {
/** @description no content */
200: {
headers: {
[name: string]: unknown;
};
content?: never;
};
default: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["SerializedError"];
};
};
};
};
options?: never;
head?: never;
patch?: never;
trace?: never;
};
}
export type webhooks = Record<string, never>;
export interface components {
schemas: {
CreateDocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
relativePath: string;
};
DeleteDocumentVersion: {
/** Format: date-time */
createdDate: string;
relativePath: string;
};
/** @description Response to a create/update document request. */
DocumentUpdateResponse: {
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** @enum {string} */
type: "FastForwardUpdate";
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
} | {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** @enum {string} */
type: "MergingUpdate";
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
DocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
DocumentVersionWithoutContent: {
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
/** @description Response to a fetch latest documents request. */
FetchLatestDocumentsResponse: {
/**
* Format: int64
* @description The update ID of the latest document in the response.
*/
lastUpdateId: number;
latestDocuments: components["schemas"]["DocumentVersionWithoutContent"][];
};
PathParams: {
vault_id: string;
};
PathParams2: {
vault_id: string;
};
PathParams3: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
PathParams4: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
PathParams5: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
/** @description Response to a ping request. */
PingResponse: {
/** @description Whether the client is authenticated based on the sent Authorization header. */
isAuthenticated: boolean;
/** @description Semantic version of the server. */
serverVersion: string;
};
QueryParams: {
/** Format: int64 */
since_update_id?: number | null;
};
SerializedError: {
causes: string[];
message: string;
};
UpdateDocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: int64 */
parentVersionId: number;
relativePath: string;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
schemas: {
CreateDocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
relativePath: string;
};
DeleteDocumentVersion: {
/** Format: date-time */
createdDate: string;
relativePath: string;
};
/** @description Response to a create/update document request. */
DocumentUpdateResponse:
| {
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** @enum {string} */
type: "FastForwardUpdate";
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
}
| {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** @enum {string} */
type: "MergingUpdate";
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
DocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
DocumentVersionWithoutContent: {
/** Format: date-time */
createdDate: string;
/** Format: uuid */
documentId: string;
isDeleted: boolean;
relativePath: string;
/** Format: date-time */
updatedDate: string;
vaultId: string;
/** Format: int64 */
vaultUpdateId: number;
};
/** @description Response to a fetch latest documents request. */
FetchLatestDocumentsResponse: {
/**
* Format: int64
* @description The update ID of the latest document in the response.
*/
lastUpdateId: number;
latestDocuments: components["schemas"]["DocumentVersionWithoutContent"][];
};
PathParams: {
vault_id: string;
};
PathParams2: {
vault_id: string;
};
PathParams3: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
PathParams4: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
PathParams5: {
/** Format: uuid */
document_id: string;
vault_id: string;
};
/** @description Response to a ping request. */
PingResponse: {
/** @description Whether the client is authenticated based on the sent Authorization header. */
isAuthenticated: boolean;
/** @description Semantic version of the server. */
serverVersion: string;
};
QueryParams: {
/** Format: int64 */
since_update_id?: number | null;
};
SerializedError: {
causes: string[];
message: string;
};
UpdateDocumentVersion: {
contentBase64: string;
/** Format: date-time */
createdDate: string;
/** Format: int64 */
parentVersionId: number;
relativePath: string;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}
export type $defs = Record<string, never>;
export type operations = Record<string, never>;