@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.
104 lines (103 loc) • 4 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLSitemapXmlService = void 0;
const constants_1 = require("../constants");
const debug_1 = __importDefault(require("../debug"));
const PREFIX_NAME_SITEMAP = 'sitemap';
// The default query for request sitemaps
const defaultQuery = /* GraphQL */ `
query SitemapQuery($siteName: String!) {
site {
siteInfo(site: $siteName) {
sitemap
}
}
}
`;
/**
* Service that fetch the sitemaps data using Sitecore's GraphQL API.
*/
class GraphQLSitemapXmlService {
/**
* Creates an instance of graphQL sitemaps service with the provided options
* @param {GraphQLSitemapXmlServiceConfig} options instance
*/
constructor(options) {
this.options = options;
this.graphQLClient = this.getGraphQLClient();
}
get query() {
return defaultQuery;
}
/**
* Fetch list of sitemaps for the site
* @returns {string[]} list of sitemap paths
* @throws {Error} if the siteName is empty.
*/
fetchSitemaps() {
return __awaiter(this, void 0, void 0, function* () {
const siteName = this.options.siteName;
if (!siteName) {
throw new Error(constants_1.siteNameError);
}
const sitemapResult = this.graphQLClient.request(this.query, {
siteName,
});
try {
return sitemapResult.then((result) => result.site.siteInfo.sitemap);
}
catch (e) {
return Promise.reject(e);
}
});
}
/**
* Get sitemap file path for sitemap id
* @param {string} id the sitemap id (can be empty for default 'sitemap.xml' file)
* @returns {string | undefined} the sitemap file path or undefined if one doesn't exist
*/
getSitemap(id) {
return __awaiter(this, void 0, void 0, function* () {
let searchSitemap;
if (id === undefined) {
return undefined;
}
else if (id === '') {
searchSitemap = `${PREFIX_NAME_SITEMAP}.xml`;
}
else {
const normalizedId = id.startsWith('-') ? id.slice(1) : id;
searchSitemap = `${PREFIX_NAME_SITEMAP}-${normalizedId}.xml`;
}
const sitemaps = yield this.fetchSitemaps();
return sitemaps.find((sitemap) => sitemap.includes(searchSitemap));
});
}
/**
* 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.options.clientFactory) {
throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
}
return this.options.clientFactory({
debugger: debug_1.default.sitemap,
});
}
}
exports.GraphQLSitemapXmlService = GraphQLSitemapXmlService;