@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.
90 lines (89 loc) • 3.46 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.GraphQLErrorPagesService = void 0;
const constants_1 = require("../constants");
const debug_1 = __importDefault(require("../debug"));
// The default query for request error handling
const defaultQuery = /* GraphQL */ `
query ErrorPagesQuery($siteName: String!, $language: String!) {
site {
siteInfo(site: $siteName) {
errorHandling(language: $language) {
notFoundPage {
rendered
}
notFoundPagePath
serverErrorPage {
rendered
}
serverErrorPagePath
}
}
}
}
`;
/**
* Service that fetch the error pages data using Sitecore's GraphQL API.
*/
class GraphQLErrorPagesService {
/**
* Creates an instance of graphQL error pages service with the provided options
* @param {GraphQLErrorPagesServiceConfig} options instance
*/
constructor(options) {
this.options = options;
this.graphQLClient = this.getGraphQLClient();
}
get query() {
return defaultQuery;
}
/**
* Fetch list of error pages for the site
* @returns {ErrorPages} list of url's error pages
* @throws {Error} if the siteName is empty.
*/
fetchErrorPages() {
return __awaiter(this, void 0, void 0, function* () {
const siteName = this.options.siteName;
const language = this.options.language;
if (!siteName) {
throw new Error(constants_1.siteNameError);
}
return this.graphQLClient.request(this.query, {
siteName,
language,
})
.then((result) => result.site.siteInfo ? result.site.siteInfo.errorHandling : null)
.catch((e) => Promise.reject(e));
});
}
/**
* 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.errorpages,
retries: this.options.retries,
retryStrategy: this.options.retryStrategy,
});
}
}
exports.GraphQLErrorPagesService = GraphQLErrorPagesService;