@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.
74 lines (73 loc) • 3.81 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.languageError = exports.siteNameError = void 0;
exports.getAppRootId = getAppRootId;
const constants_1 = require("../constants");
/** @private */
exports.siteNameError = 'The site name must be a non-empty string';
/** @private */
exports.languageError = 'The language must be a non-empty string';
/*
* GraphQL query that returns the ID of the root item of the specified site and language
*/
const appRootQuery = /* GraphQL */ `
query AppRootQuery($jssAppTemplateId: String!, $siteName: String!, $language: String!) {
layout(site: $siteName, routePath: "/", language: $language) {
homePage: item {
rootItem: ancestors(includeTemplateIDs: [$jssAppTemplateId]) {
id
}
}
}
}
`;
/**
* Gets the ID of the JSS App root item for the specified site and language.
* @param {GraphQLClient} client that fetches data from a GraphQL endpoint.
* @param {string} siteName the name of the Sitecore site.
* @param {string} language the item language version.
* @param {string} [jssAppTemplateId] optional template ID of the app root item. If not
* specified, the ID of the "/sitecore/templates/Foundation/JavaScript Services/App"
* item is used.
* @returns the root item ID of the JSS App in Sitecore. Returns null if the app root item is not found.
* @throws {RangeError} if a valid site name value is not provided.
* @throws {RangeError} if a valid language value is not provided.
* @summary This function intentionally avoids throwing an error if a root item is not found,
* leaving that decision up to implementations.
*/
function getAppRootId(client, siteName, language, jssAppTemplateId) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f;
if (!siteName) {
throw new RangeError(exports.siteNameError);
}
if (!language) {
throw new RangeError(exports.languageError);
}
let fetchResponse = yield client.request(appRootQuery, {
jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
siteName,
language,
});
if (!((_c = (_b = (_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.layout) === null || _a === void 0 ? void 0 : _a.homePage) === null || _b === void 0 ? void 0 : _b.rootItem) === null || _c === void 0 ? void 0 : _c.length) && language !== 'en') {
fetchResponse = yield client.request(appRootQuery, {
jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
siteName,
language: 'en',
});
}
if (!((_f = (_e = (_d = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.layout) === null || _d === void 0 ? void 0 : _d.homePage) === null || _e === void 0 ? void 0 : _e.rootItem) === null || _f === void 0 ? void 0 : _f.length)) {
return null;
}
return fetchResponse.layout.homePage.rootItem[0].id;
});
}