@jorsek/ezd-client
Version:
55 lines (44 loc) • 1.94 kB
text/typescript
import Axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import { Resource, IResourceArgs } from "./Resource";
import { TopicTemplate } from "./TopicTemplate";
import { API_Template } from "../Types";
export interface API_Document {
uuid: string
title: string
owner: string
}
export class Contribute extends Resource {
private availableTemplates: TopicTemplate[];
private _resource_uuid: string;
public async initContribution(template_path: string): Promise<string> {
const response = await this.axios.post("/content/v1/initContribution", {}, { params: { "template_uri": template_path } });
const uuid = response.data.initContribution.resourceUUID;
if (uuid) {
this._resource_uuid = uuid;
} else {
console.log("OH NO!");
}
return this._resource_uuid;
}
public resource_uuid() {
return this._resource_uuid;
}
public commit(_doc_id: string) {
this.axios.get("/content/v1/commitContribution", { params: { resourceUUID: this.resource_uuid() } });
}
public cancel(_doc_id: string) {
console.log(`THIS IS THE DOC ID: ${_doc_id}`);
this.axios.get("/content/v1/cancelContribution", { params: { resourceUUID: _doc_id } });
}
public async templates(): Promise<Array<API_Template>> {
return (await this.config.get()).templates;
}
public async getContributions(): Promise<Array<API_Document>> {
const response = await this.axios.get("/content/v1/contributions");
// TODO: validate this response once API is written in java, not xquery
// xquery returns null instead of empty array
// xwquery also returns single object instead of array of 1
const return_me = response.data ? response.data["resources"] ? response.data["resources"] : [] : [];
return Array.isArray(return_me) ? return_me : [return_me];
}
}