UNPKG

@sitecore-jss/sitecore-jss

Version:

This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.

63 lines (62 loc) 2.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestComponentLayoutService = void 0; const native_fetcher_1 = require("../native-fetcher"); const debug_1 = __importDefault(require("../debug")); const constants_1 = require("../constants"); const utils_1 = require("../utils"); const models_1 = require("./models"); /** * REST service that enables design Library functionality * Returns layoutData for one single rendered component */ class RestComponentLayoutService { constructor(config) { this.config = config; } fetchComponentData(params) { const config = { debugger: debug_1.default.layout }; const fetcher = new native_fetcher_1.NativeDataFetcher(config); debug_1.default.layout('fetching component with uid %s for %s %s %s %s', params.componentUid, params.itemId, params.language, params.siteName, params.dataSourceId); const fetchUrl = this.getFetchUrl(params); return fetcher .get(fetchUrl, { headers: { sc_editMode: `${params.mode === models_1.DesignLibraryMode.Metadata}`, }, }) .then((response) => response.data) .catch((error) => { var _a; if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) { return error.response.data; } throw error; }); } getComponentFetchParams(params) { // exclude undefined params with this one simple trick return JSON.parse(JSON.stringify({ sitecoreContextId: this.config.sitecoreEdgeContextId, item: params.itemId, uid: params.componentUid, dataSourceId: params.dataSourceId, renderingItemId: params.renderingId, version: params.version, sc_site: params.siteName, sc_lang: params.language || 'en', })); } /** * Get the fetch URL for the partial layout data endpoint * @param {ComponentLayoutRequestParams} params - The parameters for the request * @returns {string} The fetch URL for the component data */ getFetchUrl(params) { return (0, utils_1.resolveUrl)(`${this.config.sitecoreEdgeUrl || constants_1.SITECORE_EDGE_URL_DEFAULT}/layout/component`, this.getComponentFetchParams(params)); } } exports.RestComponentLayoutService = RestComponentLayoutService;