E2E workflow for creating docs
This commit is contained in:
parent
f9dafdfc88
commit
0db1380d27
1 changed files with 106 additions and 47 deletions
60
plugin/src/syncer/syncer.ts
Normal file
60
plugin/src/syncer/syncer.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { SettingsContainer, SyncSettings } from "src/settings/settings";
|
||||
|
||||
import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||
|
||||
import createClient, { Client } from "openapi-fetch";
|
||||
import type { components, paths } from "./types"; // generated by openapi-typescript
|
||||
import { Logger } from "src/logger.js";
|
||||
|
||||
export class Syncer {
|
||||
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 onCreate(
|
||||
relativePath: string,
|
||||
content: string
|
||||
): Promise<
|
||||
components["schemas"]["DocumentVersionWithoutContent"] | undefined
|
||||
> {
|
||||
let response = await this.client.POST("/vaults/{vault_id}/documents", {
|
||||
params: {
|
||||
path: { vault_id: Syncer.vault_id },
|
||||
header: {
|
||||
authorization:
|
||||
"Bearer " + this.settings.getSettings().token,
|
||||
},
|
||||
},
|
||||
body: {
|
||||
contentBase64: plugin.string_to_base64(content),
|
||||
createdDate: new Date().toISOString(),
|
||||
isBinary: true,
|
||||
relativePath,
|
||||
},
|
||||
});
|
||||
|
||||
Logger.getInstance().info(
|
||||
"Created document " + JSON.stringify(response.data)
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
private uri(path: string) {
|
||||
let uri = this.settings.getSettings().remoteUri;
|
||||
if (!uri.endsWith("/")) {
|
||||
uri += "/";
|
||||
}
|
||||
return uri + path;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue