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.

56 lines (55 loc) 2.21 kB
import { NativeDataFetcher } from '../native-fetcher'; import debug from '../debug'; import { SITECORE_EDGE_URL_DEFAULT } from '../constants'; import { resolveUrl } from '../utils'; import { DesignLibraryMode } from './models'; /** * REST service that enables design Library functionality * Returns layoutData for one single rendered component */ export class RestComponentLayoutService { constructor(config) { this.config = config; } fetchComponentData(params) { const config = { debugger: debug.layout }; const fetcher = new NativeDataFetcher(config); debug.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 === 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 resolveUrl(`${this.config.sitecoreEdgeUrl || SITECORE_EDGE_URL_DEFAULT}/layout/component`, this.getComponentFetchParams(params)); } }