|
| 1 | +import { Config } from "./types/Config"; |
| 2 | +import { ObjectConfig } from "./types/ObjectConfig"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +type AuthOptions = { |
| 6 | + user?: string; |
| 7 | + pass?: string; |
| 8 | + personalAccessToken?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +export class ObjectConfigLoader { |
| 12 | + static async load(objectConfig: ObjectConfig): Promise<Config> { |
| 13 | + const authOptions = await ObjectConfigLoader.fetchCredentialsFromObject(objectConfig); |
| 14 | + return ObjectConfigLoader.createConfig(objectConfig, ObjectConfigLoader.createAuthorizationToken(authOptions)); |
| 15 | + } |
| 16 | + |
| 17 | + private static async fetchCredentialsFromObject(objectConfig: ObjectConfig): Promise<AuthOptions> { |
| 18 | + const hasUserPass = !!(objectConfig.user && objectConfig.pass); |
| 19 | + const hasPersonalAccessToken = !!objectConfig.personalAccessToken; |
| 20 | + if (!hasPersonalAccessToken && !hasUserPass) { |
| 21 | + throw new Error("Missing configuration! Config object does not provide a combination of your Confluence username and password or a personal access token."); |
| 22 | + } |
| 23 | + return { |
| 24 | + user: objectConfig.user, |
| 25 | + pass: objectConfig.pass, |
| 26 | + personalAccessToken: objectConfig.personalAccessToken, |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + private static createAuthorizationToken(authOptions: AuthOptions): string { |
| 31 | + if (authOptions.personalAccessToken) { |
| 32 | + return `Bearer ${authOptions.personalAccessToken}`; |
| 33 | + } |
| 34 | + |
| 35 | + if (authOptions.user && authOptions.user.length > 0 && authOptions.pass && authOptions.pass.length > 0) { |
| 36 | + const encodedBasicToken = Buffer.from(`${authOptions.user}:${authOptions.pass}`).toString("base64"); |
| 37 | + return `Basic ${encodedBasicToken}`; |
| 38 | + } |
| 39 | + |
| 40 | + throw new Error("Missing configuration! Config object does not provide a combination of your Confluence username and password or a personal access token."); |
| 41 | + } |
| 42 | + |
| 43 | + private static normalizeFilePaths(objectConfig: ObjectConfig): ObjectConfig { |
| 44 | + for (const i in objectConfig.pages) { |
| 45 | + objectConfig.pages[i].file = path.isAbsolute(objectConfig.pages[i].file) |
| 46 | + ? objectConfig.pages[i].file |
| 47 | + : path.resolve(path.dirname(objectConfig.fileRoot) + "/" + objectConfig.pages[i].file); |
| 48 | + } |
| 49 | + |
| 50 | + return objectConfig; |
| 51 | + } |
| 52 | + |
| 53 | + private static createConfig(objectConfig: ObjectConfig, authorizationToken: string): Config { |
| 54 | + const config = ObjectConfigLoader.normalizeFilePaths(objectConfig); |
| 55 | + return { |
| 56 | + baseUrl: config.baseUrl, |
| 57 | + cachePath: config.cachePath, |
| 58 | + prefix: config.prefix, |
| 59 | + pages: config.pages, |
| 60 | + configPath: config.fileRoot || process.cwd(), |
| 61 | + customRenderer: config.customRenderer, |
| 62 | + authorizationToken: authorizationToken, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments