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.

108 lines (107 loc) 4.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLSiteInfoService = void 0; const debug_1 = __importDefault(require("../debug")); const cache_client_1 = require("../cache-client"); const siteQuery = /* GraphQL */ ` query { site { siteInfoCollection { name hostName: hostname language } } } `; class GraphQLSiteInfoService { /** * Creates an instance of graphQL service to retrieve site configuration list from Sitecore * @param {GraphQLSiteInfoServiceConfig} config instance */ constructor(config) { this.config = config; this.graphQLClient = this.getGraphQLClient(); this.cache = this.getCacheClient(); } /** * site query is available on XM Cloud and XP 10.4+ */ get siteQuery() { return siteQuery; } fetchSiteInfo() { return __awaiter(this, void 0, void 0, function* () { const cachedResult = this.cache.getCacheValue(this.getCacheKey()); if (cachedResult) { return cachedResult; } if (process.env.SITECORE) { debug_1.default.multisite('Skipping site information fetch (building on XM Cloud)'); return []; } const results = yield this.fetchWithSiteQuery(); this.cache.setCacheValue(this.getCacheKey(), results); return results; }); } fetchWithSiteQuery() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const response = yield this.graphQLClient.request(this.siteQuery); const result = (_b = (_a = response === null || response === void 0 ? void 0 : response.site) === null || _a === void 0 ? void 0 : _a.siteInfoCollection) === null || _b === void 0 ? void 0 : _b.reduce((result, current) => { // filter out built in website current.name !== 'website' && result.push({ name: current.name, hostName: current.hostName, language: current.language, }); return result; }, []); return result; }); } /** * Gets cache client implementation * Override this method if custom cache needs to be used * @returns CacheClient instance */ getCacheClient() { var _a, _b; return new cache_client_1.MemoryCacheClient({ cacheEnabled: (_a = this.config.cacheEnabled) !== null && _a !== void 0 ? _a : true, cacheTimeout: (_b = this.config.cacheTimeout) !== null && _b !== void 0 ? _b : 10, }); } /** * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you * want to use something else. * @returns {GraphQLClient} implementation */ getGraphQLClient() { if (!this.config.clientFactory) { throw new Error('clientFactory needs to be provided when initializing GraphQL client.'); } return this.config.clientFactory({ debugger: debug_1.default.multisite, }); } getCacheKey() { return 'siteinfo-service-cache'; } } exports.GraphQLSiteInfoService = GraphQLSiteInfoService;