UNPKG

@atlassian/wrm-troubleshooting

Version:

A tool that can help you with troubleshooting the configuration of webpack and Atlassian P2 project.

93 lines 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchWebResources = void 0; const chalk_1 = __importDefault(require("chalk")); const fetchWithTimeout_1 = require("./fetchWithTimeout"); const SUPER_BATCH_CONTEXT_KEY = '_super'; async function fetchWebResources(applicationUrl, resourceKeys, timeout) { const url = `${applicationUrl}/rest/webResources/1.0/resources`; process.env.DEBUG && console.debug(`Fetching web-resource from ${url}`); // https://hello.atlassian.net/wiki/spaces/SVRFE/pages/491199354/WRM+Front-end+API#RESOURCES const payload = { r: Array.isArray(resourceKeys) ? resourceKeys : [resourceKeys], c: [], xr: [], xc: [SUPER_BATCH_CONTEXT_KEY], // Ignore super batch }; let jsonResponse; try { const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(timeout, url, { method: 'post', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }); if (!response.ok) { // TODO: What should be the error here?! throw new Error(response.status.toString()); } jsonResponse = (await response.json()); } catch (err) { const error = err; if (error.name === 'AbortError') { return new fetchWithTimeout_1.FetchTimeoutError(`Fetching web-resource for "${resourceKeys}" timed out`); } return Error(`Failed to load the web-resource for "${chalk_1.default.bold(resourceKeys)}" resource key.\nError: ${error.message}`); } const { resources } = jsonResponse; const maybeContents = await loadWrmResources(applicationUrl, resources, timeout); return maybeContents.filter(notError); } exports.fetchWebResources = fetchWebResources; async function loadWrmResources(applicationUrl, resources, timeout) { const simplifiedResourcesWithoutContent = resources.map((resource) => { const { url: urlPathname, resourceType } = resource; // We need to build a new absolute URL const url = new URL(applicationUrl); url.pathname = urlPathname; return { url: url.toString(), type: resourceType, }; }); const simplifiedResources = []; for (const resource of simplifiedResourcesWithoutContent) { const content = await loadWrmResource(resource.url, timeout); simplifiedResources.push(content instanceof Error ? content : { ...resource, content, }); } return simplifiedResources; } async function loadWrmResource(resourceUrl, timeout) { try { const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(timeout, resourceUrl, { method: 'get', }); if (!response.ok) { // TODO: should we retry? Or based on error code? (do we only check if we know an instance is running?) // TODO: What should be the error here?! throw new Error(response.status.toString()); } return await response.text(); } catch (err) { const error = err; if (error.name === 'AbortError') { return new fetchWithTimeout_1.FetchTimeoutError(`Loading web-resource "${resourceUrl}" timed out`); } return Error(`Can't load the web-resource for "${resourceUrl}" resource key. Error: ${error.message}`); } } function notError(value) { return !(value instanceof Error); } //# sourceMappingURL=wrmRestApi.js.map