@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.
77 lines (76 loc) • 2.91 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());
});
};
import { siteNameError } from '../constants';
import debug from '../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.
*/
export 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(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.robots,
});
}
}