UNPKG

@backstage/integration

Version:

Helpers for managing integrations towards external systems

89 lines (86 loc) 2.8 kB
import { trimEnd } from 'lodash'; import { isValidHost, isValidUrl } from '../helpers.esm.js'; const GITLAB_HOST = "gitlab.com"; const GITLAB_API_BASE_URL = "https://gitlab.com/api/v4"; function readOptionalNumberArray(config, key) { const value = config.getOptional(key); if (value === void 0) { return void 0; } if (!Array.isArray(value)) { throw new Error( `Invalid ${key} config: expected an array, got ${typeof value}` ); } return value.map((item, index) => { if (typeof item !== "number") { throw new Error( `Invalid ${key} config: all values must be numbers, got ${typeof item} at index ${index}` ); } return item; }); } function readGitLabIntegrationConfig(config) { const host = config.getString("host"); let apiBaseUrl = config.getOptionalString("apiBaseUrl"); const token = config.getOptionalString("token")?.trim(); let baseUrl = config.getOptionalString("baseUrl"); if (apiBaseUrl) { apiBaseUrl = trimEnd(apiBaseUrl, "/"); } else if (host === GITLAB_HOST) { apiBaseUrl = GITLAB_API_BASE_URL; } if (baseUrl) { baseUrl = trimEnd(baseUrl, "/"); } else { baseUrl = `https://${host}`; } if (!isValidHost(host)) { throw new Error( `Invalid GitLab integration config, '${host}' is not a valid host` ); } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) { throw new Error( `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl` ); } else if (!isValidUrl(baseUrl)) { throw new Error( `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl` ); } const retryConfig = config.getOptionalConfig("retry"); const retry = retryConfig ? { maxRetries: retryConfig.getOptionalNumber("maxRetries") ?? 0, retryStatusCodes: readOptionalNumberArray(retryConfig, "retryStatusCodes") ?? [], maxApiRequestsPerMinute: retryConfig.getOptionalNumber("maxApiRequestsPerMinute") ?? -1 } : void 0; return { host, token, apiBaseUrl, baseUrl, commitSigningKey: config.getOptionalString("commitSigningKey"), retry }; } function readGitLabIntegrationConfigs(configs) { const result = configs.map(readGitLabIntegrationConfig); if (!result.some((c) => c.host === GITLAB_HOST)) { result.push({ host: GITLAB_HOST, apiBaseUrl: GITLAB_API_BASE_URL, baseUrl: `https://${GITLAB_HOST}` }); } return result; } function getGitLabIntegrationRelativePath(config) { let relativePath = ""; if (config.host !== GITLAB_HOST) { relativePath = new URL(config.baseUrl).pathname; } return trimEnd(relativePath, "/"); } export { getGitLabIntegrationRelativePath, readGitLabIntegrationConfig, readGitLabIntegrationConfigs }; //# sourceMappingURL=config.esm.js.map