antora-confluence
Version:
A tool to convert and publish Antora documentation to Confluence
176 lines (175 loc) • 6.74 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfluenceClientV1 = void 0;
const ConfluenceClient_1 = require("./ConfluenceClient");
const types_1 = require("../types");
const form_data_1 = __importDefault(require("form-data"));
const Logger_1 = require("../Logger");
const LOGGER = (0, Logger_1.getLogger)();
class ConfluenceClientV1 extends ConfluenceClient_1.ConfluenceClient {
createPage(page, status) {
LOGGER.info(`Creating page ${page.title}`);
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content`);
return this.fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: this.AUTHORIZATION_HEADER,
},
body: JSON.stringify({
type: "page",
title: page.title,
status,
space: {
key: this.SPACE_KEY,
},
metadata: {
properties: {
editor: {
value: this.EDITOR_VERSION,
},
"content-appearance-draft": {
value: "full-width",
},
"content-appearance-published": {
value: "full-width",
},
},
},
ancestors: page.parentPageId || this.ANCESTOR_ID
? [
{
id: page.parentPageId || this.ANCESTOR_ID,
},
]
: undefined,
body: {
storage: {
value: page.content,
representation: "storage",
},
},
}),
});
}
updatePage(page, pageId, newVersion, status) {
LOGGER.info(`Updating page ${page.title} with new version ${newVersion}`);
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content/${pageId}`);
return this.fetch(apiUrl, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: this.AUTHORIZATION_HEADER,
},
body: JSON.stringify({
type: "page",
version: {
number: newVersion,
},
title: page.title,
status,
metadata: {
properties: {
editor: {
value: this.EDITOR_VERSION,
},
"content-appearance-draft": {
value: "full-width",
},
"content-appearance-published": {
value: "full-width",
},
},
},
body: {
storage: {
value: page.content,
representation: "storage",
},
},
}),
});
}
createAttachment(attachment) {
const formData = new form_data_1.default();
formData.append("file", attachment.file, {
filename: attachment.fileName,
});
formData.append("comment", attachment.comment);
formData.append("minorEdit", "true");
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content/${attachment.pageId}/child/attachment`);
return this.fetch(apiUrl, {
method: "POST",
body: formData,
headers: {
...formData.getHeaders(),
Accept: "application/json",
"X-Atlassian-Token": "nocheck",
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
fetchPageIdByName(title, status) {
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content`);
apiUrl.searchParams.set("spaceKey", this.SPACE_KEY);
apiUrl.searchParams.set("title", title);
apiUrl.searchParams.set("status", status || types_1.ConfluencePageStatus.CURRENT);
apiUrl.searchParams.set("expand", "body.storage,version");
return this.fetch(apiUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
deletePage(pageId) {
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content/${pageId}`);
return this.fetch(apiUrl, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
getAttachment(pageId, fileName) {
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content/${pageId}/child/attachment`);
apiUrl.searchParams.set("filename", fileName);
return this.fetch(apiUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
updateAttachment(attachment) {
const formData = new form_data_1.default();
formData.append("file", attachment.file, {
filename: attachment.fileName,
});
formData.append("comment", attachment.comment);
formData.append("minorEdit", "true");
const apiUrl = this.buildUrlWithPath(`${this.API_V1_PATH}/content/${attachment.pageId}/child/attachment/${attachment.attachmentId}/data`);
return this.fetch(apiUrl, {
method: "POST",
body: formData,
headers: {
...formData.getHeaders(),
Accept: "application/json",
"X-Atlassian-Token": "nocheck",
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
}
exports.ConfluenceClientV1 = ConfluenceClientV1;