diff --git a/plugin/src/services/sync_service.ts b/plugin/src/services/sync_service.ts new file mode 100644 index 00000000..c2916b33 --- /dev/null +++ b/plugin/src/services/sync_service.ts @@ -0,0 +1,192 @@ +import { + SettingsContainer, + SyncSettings, +} from "src/database/settings/settings.js"; + +import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js"; + +import createClient, { Client } from "openapi-fetch"; +import type { components, paths } from "./types.js"; // generated by openapi-typescript +import { Logger } from "src/logger.js"; +import { DocumentId, DocumentVersionId } from "src/database/database.js"; + +export class SyncServer { + private static VAULT_ID = "default"; + private client: Client; + + public constructor(private settings: SettingsContainer) { + this.createClient(settings.getSettings()); + settings.onChange((s) => this.createClient(s)); + } + + private createClient(settings: SyncSettings) { + this.client = createClient({ + baseUrl: settings.remoteUri, + }); + } + + public async create({ + relativePath, + content, + createdDate, + }: { + content: ArrayBuffer; + relativePath: string; + createdDate: Date; + }): Promise< + components["schemas"]["DocumentVersionWithoutContent"] | undefined + > { + let contentBytes = new Uint8Array(content); + let response = await this.client.POST("/vaults/{vault_id}/documents", { + params: { + path: { vaultId: SyncServer.VAULT_ID }, + header: { + authorization: + "Bearer " + this.settings.getSettings().token, + }, + }, + body: { + contentBase64: plugin.bytes_to_base64(contentBytes), + createdDate: createdDate.toISOString(), + isBinary: plugin.is_binary(contentBytes), + relativePath, + }, + }); + + Logger.getInstance().info( + "Created document " + JSON.stringify(response.data) + ); + + return response.data; + } + + public async update({ + documentId, + parentVersionId, + relativePath, + content, + createdDate, + }: { + documentId: DocumentId; + parentVersionId: DocumentVersionId; + relativePath: string; + content: ArrayBuffer; + createdDate: Date; + }): Promise< + components["schemas"]["DocumentVersionWithoutContent"] | undefined + > { + let contentBytes = new Uint8Array(content); + + let response = await this.client.PUT( + "/vaults/{vault_id}/documents/{document_id}", + { + params: { + path: { + vaultId: SyncServer.VAULT_ID, + documentId, + }, + header: { + authorization: + "Bearer " + this.settings.getSettings().token, + }, + }, + body: { + parentVersionId, + contentBase64: plugin.bytes_to_base64(contentBytes), + createdDate: createdDate.toISOString(), + isBinary: plugin.is_binary(contentBytes), + relativePath, + }, + } + ); + + Logger.getInstance().info( + "Updated document " + JSON.stringify(response.data) + ); + + return response.data; + } + + public async delete({ + documentId, + createdDate, + }: { + documentId: DocumentId; + createdDate: Date; + }): Promise { + const response = await this.client.DELETE( + "/vaults/{vault_id}/documents/{document_id}", + { + params: { + path: { + vaultId: SyncServer.VAULT_ID, + documentId, + }, + header: { + authorization: + "Bearer " + this.settings.getSettings().token, + }, + }, + body: { + createdDate: createdDate.toISOString(), + }, + } + ); + + Logger.getInstance().info( + "Updated document " + JSON.stringify(response.data) + ); + + return response.data; + } + + public async get({ + documentId, + }: { + documentId: DocumentId; + }): Promise { + const response = await this.client.GET( + "/vaults/{vault_id}/documents/{document_id}", + { + params: { + path: { + vaultId: SyncServer.VAULT_ID, + documentId, + }, + header: { + authorization: + "Bearer " + this.settings.getSettings().token, + }, + }, + } + ); + + Logger.getInstance().info( + "Get document " + JSON.stringify(response.data) + ); + + return response.data; + } + + public async getAll(): Promise< + components["schemas"]["DocumentVersionWithoutContent"][] | undefined + > { + const response = await this.client.GET("/vaults/{vault_id}/documents", { + params: { + path: { + vaultId: SyncServer.VAULT_ID, + }, + header: { + authorization: + "Bearer " + this.settings.getSettings().token, + }, + }, + }); + + Logger.getInstance().info( + "Get document " + JSON.stringify(response.data) + ); + + return response.data; + } +} diff --git a/plugin/src/syncer/types.ts b/plugin/src/services/types.ts similarity index 92% rename from plugin/src/syncer/types.ts rename to plugin/src/services/types.ts index b543710a..e85559d6 100644 --- a/plugin/src/syncer/types.ts +++ b/plugin/src/services/types.ts @@ -18,7 +18,7 @@ export interface paths { authorization: string; }; path: { - vault_id: string; + vaultId: string; }; cookie?: never; }; @@ -42,7 +42,7 @@ export interface paths { authorization: string; }; path: { - vault_id: string; + vaultId: string; }; cookie?: never; }; @@ -82,8 +82,8 @@ export interface paths { authorization: string; }; path: { - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; cookie?: never; }; @@ -106,8 +106,8 @@ export interface paths { authorization: string; }; path: { - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; cookie?: never; }; @@ -135,8 +135,8 @@ export interface paths { authorization: string; }; path: { - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; cookie?: never; }; @@ -242,25 +242,25 @@ export interface components { versionId: number; }; PathParams: { - vault_id: string; + vaultId: string; }; PathParams2: { - vault_id: string; + vaultId: string; }; PathParams3: { /** Format: uuid */ - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; PathParams4: { /** Format: uuid */ - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; PathParams5: { /** Format: uuid */ - document_id: string; - vault_id: string; + documentId: string; + vaultId: string; }; UpdateDocumentVersion: { contentBase64: string;