Refactor & update API

This commit is contained in:
Andras Schmelczer 2024-12-10 22:25:00 +00:00
commit 3065edba37
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 42 additions and 33 deletions

View file

@ -1,27 +1,11 @@
import { Logger } from "src/logger"; import { Logger } from "src/logger";
import { DEFAULT_SETTINGS, SyncSettings } from "./sync-settings";
export type DocumentId = string; import {
export type DocumentVersionId = number; RelativePath,
export type RelativePath = string; DocumentMetadata,
DocumentId,
export interface SyncSettings { DocumentVersionId,
remoteUri: string; } from "./document-metadata";
token: string;
fullScanIntervalInSeconds: number;
fullScanEnabled: boolean;
}
export const DEFAULT_SETTINGS: SyncSettings = {
remoteUri: "",
token: "",
fullScanIntervalInSeconds: 60,
fullScanEnabled: true,
};
export interface DocumentMetadata {
documentId: DocumentId;
parentVersionId: DocumentVersionId;
}
interface StoredDatabase { interface StoredDatabase {
documents: Map<RelativePath, DocumentMetadata>; documents: Map<RelativePath, DocumentMetadata>;
@ -69,6 +53,10 @@ export class Database {
); );
} }
public getDocuments(): Map<RelativePath, DocumentMetadata> {
return this._documents;
}
public getSettings(): SyncSettings { public getSettings(): SyncSettings {
return this._settings; return this._settings;
} }

View file

@ -0,0 +1,8 @@
export type DocumentId = string;
export type DocumentVersionId = number;
export type RelativePath = string;
export interface DocumentMetadata {
documentId: DocumentId;
parentVersionId: DocumentVersionId;
}

View file

@ -0,0 +1,13 @@
export interface SyncSettings {
remoteUri: string;
token: string;
fullScanIntervalInSeconds: number;
fullScanEnabled: boolean;
}
export const DEFAULT_SETTINGS: SyncSettings = {
remoteUri: "",
token: "",
fullScanIntervalInSeconds: 60,
fullScanEnabled: true,
};

View file

@ -1,14 +1,14 @@
import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js";
import createClient, { Client } from "openapi-fetch"; import createClient, { Client } 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 { Logger } from "src/logger.js"; import { Logger } from "src/logger";
import { Database } from "src/database/database";
import { SyncSettings } from "src/database/sync-settings";
import { import {
Database,
DocumentId, DocumentId,
DocumentVersionId, DocumentVersionId,
SyncSettings, } from "src/database/document-metadata.js";
} from "src/database/database.js";
export class SyncServer { export class SyncServer {
private static VAULT_ID = "default"; private static VAULT_ID = "default";
@ -47,7 +47,7 @@ export class SyncServer {
content: ArrayBuffer; content: ArrayBuffer;
relativePath: string; relativePath: string;
createdDate: Date; createdDate: Date;
}): Promise<components["schemas"]["DocumentVersionWithoutContent"]> { }): Promise<components["schemas"]["DocumentVersion"]> {
let contentBytes = new Uint8Array(content); let contentBytes = new Uint8Array(content);
let response = await this.client.POST("/vaults/{vault_id}/documents", { let response = await this.client.POST("/vaults/{vault_id}/documents", {
params: { params: {
@ -88,7 +88,7 @@ export class SyncServer {
relativePath: string; relativePath: string;
content: ArrayBuffer; content: ArrayBuffer;
createdDate: Date; createdDate: Date;
}): Promise<components["schemas"]["DocumentVersionWithoutContent"]> { }): Promise<components["schemas"]["DocumentVersion"]> {
let contentBytes = new Uint8Array(content); let contentBytes = new Uint8Array(content);
let response = await this.client.PUT( let response = await this.client.PUT(

View file

@ -91,7 +91,7 @@ export interface paths {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["DocumentVersionWithoutContent"]; "application/json": components["schemas"]["DocumentVersion"];
}; };
}; };
}; };
@ -156,7 +156,7 @@ export interface paths {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["DocumentVersionWithoutContent"]; "application/json": components["schemas"]["DocumentVersion"];
}; };
}; };
}; };

View file

@ -9,7 +9,7 @@ import {
Setting, Setting,
} from "obsidian"; } from "obsidian";
import SyncPlugin from "src/plugin.js"; import SyncPlugin from "src/plugin";
import { Database } from "src/database/database"; import { Database } from "src/database/database";
import { SyncServer } from "src/services/sync_service"; import { SyncServer } from "src/services/sync_service";