antora-confluence
Version:
A tool to convert and publish Antora documentation to Confluence
82 lines (81 loc) • 2.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfluenceClient = void 0;
const Logger_1 = require("../Logger");
const LOGGER = (0, Logger_1.getLogger)();
class ConfluenceClient {
fetch;
API_DEFAULT_CONTEXT = "wiki";
API_V1_IDENTIFIER = "/rest/api";
API_V2_IDENTIFIER = "/api/v2";
SPACE_KEY;
API_V1_PATH;
API_V2_PATH;
EDITOR_VERSION;
BASE_URL;
AUTHORIZATION_HEADER;
ANCESTOR_ID;
CAPTAIN_NAME;
constructor(config) {
this.BASE_URL = new URL(config.baseUrl.origin);
this.SPACE_KEY = config.spaceKey;
if (config.editorVersion === "v2") {
LOGGER.warn("ConfluenceClient: editorVersion v2 is not fully supported yet");
}
this.EDITOR_VERSION = config.editorVersion;
const apiContext = this.constructApiContext(config.baseUrl);
this.API_V1_PATH = apiContext + this.API_V1_IDENTIFIER;
this.API_V2_PATH = apiContext + this.API_V2_IDENTIFIER;
this.AUTHORIZATION_HEADER = this.buildAuthHeader();
this.ANCESTOR_ID = config.ancestorId;
this.CAPTAIN_NAME = config.captainName || "Captain State Page";
LOGGER.debug(`ConfluenceClient: constructor CAPTAIN_NAME ${this.CAPTAIN_NAME}`);
}
async init() {
this.fetch = await this.importFetch();
}
buildUrlWithPath(path) {
const url = this.BASE_URL;
url.pathname = path;
return url;
}
async importFetch() {
const nodeFetch = await import("node-fetch");
return nodeFetch.default || nodeFetch;
}
constructApiContext(baseUrl) {
const apiContext = this.determineApiContext(baseUrl.pathname);
if (apiContext.length > 0) {
return "/" + apiContext;
}
else {
return "";
}
}
determineApiContext(apiPath) {
if (apiPath === "/" || apiPath.length === 0) {
// no context has been set
return this.API_DEFAULT_CONTEXT;
}
// remove leading slash, remove api versions identifier from path
apiPath = apiPath
.substring(1)
.replace(this.API_V1_IDENTIFIER, "")
.replace(this.API_V2_IDENTIFIER, "");
const pathParts = apiPath.split("/");
if (pathParts.length == 1) {
// context has been set
return pathParts[0];
}
// assume that context has been omitted intentionally https://docs.atlassian.com/ConfluenceServer/rest/8.6.1/
return "";
}
buildAuthHeader() {
if (process.env.CONFLUENCE_PAT) {
return `Bearer ${process.env.CONFLUENCE_PAT}`;
}
const credentials = `${process.env.CONFLUENCE_USERNAME}:${process.env.CONFLUENCE_PASSWORD}`;
return `Basic ${Buffer.from(credentials).toString("base64")}`;
}
}
exports.ConfluenceClient = ConfluenceClient;