@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.
84 lines (83 loc) • 3.26 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.GraphQLRobotsService = void 0;
const constants_1 = require("../constants");
const debug_1 = __importDefault(require("../debug"));
// The default query for request robots.txt
const defaultQuery = /* GraphQL */ `
query RobotsQuery($siteName: String!) {
site {
siteInfo(site: $siteName) {
robots
}
}
}
`;
/**
* Service that fetch the robots.txt data using Sitecore's GraphQL API.
*/
class GraphQLRobotsService {
/**
* Creates an instance of graphQL robots.txt service with the provided options
* @param {GraphQLRobotsServiceConfig} options instance
*/
constructor(options) {
this.options = options;
this.graphQLClient = this.getGraphQLClient();
}
get query() {
return defaultQuery;
}
/**
* Fetch a data of robots.txt from API
* @returns text of robots.txt
* @throws {Error} if the siteName is empty.
*/
fetchRobots() {
return __awaiter(this, void 0, void 0, function* () {
const siteName = this.options.siteName;
if (!siteName) {
throw new Error(constants_1.siteNameError);
}
const robotsResult = this.graphQLClient.request(this.query, {
siteName,
});
try {
return robotsResult.then((result) => {
var _a, _b;
return (_b = (_a = result === null || result === void 0 ? void 0 : result.site) === null || _a === void 0 ? void 0 : _a.siteInfo) === null || _b === void 0 ? void 0 : _b.robots;
});
}
catch (e) {
return 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.robots,
});
}
}
exports.GraphQLRobotsService = GraphQLRobotsService;