Proxy all endpoints
This commit is contained in:
parent
161d2c7ee4
commit
66fbdbf368
2 changed files with 208 additions and 16 deletions
192
plugin/src/services/sync_service.ts
Normal file
192
plugin/src/services/sync_service.ts
Normal file
|
|
@ -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<paths>;
|
||||||
|
|
||||||
|
public constructor(private settings: SettingsContainer) {
|
||||||
|
this.createClient(settings.getSettings());
|
||||||
|
settings.onChange((s) => this.createClient(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
private createClient(settings: SyncSettings) {
|
||||||
|
this.client = createClient<paths>({
|
||||||
|
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<void> {
|
||||||
|
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<components["schemas"]["DocumentVersion"] | undefined> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,7 +18,7 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
};
|
};
|
||||||
|
|
@ -42,7 +42,7 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
};
|
};
|
||||||
|
|
@ -82,8 +82,8 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
};
|
};
|
||||||
|
|
@ -106,8 +106,8 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
};
|
};
|
||||||
|
|
@ -135,8 +135,8 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
};
|
};
|
||||||
|
|
@ -242,25 +242,25 @@ export interface components {
|
||||||
versionId: number;
|
versionId: number;
|
||||||
};
|
};
|
||||||
PathParams: {
|
PathParams: {
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
PathParams2: {
|
PathParams2: {
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
PathParams3: {
|
PathParams3: {
|
||||||
/** Format: uuid */
|
/** Format: uuid */
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
PathParams4: {
|
PathParams4: {
|
||||||
/** Format: uuid */
|
/** Format: uuid */
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
PathParams5: {
|
PathParams5: {
|
||||||
/** Format: uuid */
|
/** Format: uuid */
|
||||||
document_id: string;
|
documentId: string;
|
||||||
vault_id: string;
|
vaultId: string;
|
||||||
};
|
};
|
||||||
UpdateDocumentVersion: {
|
UpdateDocumentVersion: {
|
||||||
contentBase64: string;
|
contentBase64: string;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue