@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.
188 lines (187 loc) • 7.58 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.GraphQLEditingService = exports.dictionaryQuery = exports.query = void 0;
const debug_1 = __importDefault(require("../debug"));
const layout_1 = require("../layout");
const models_1 = require("./models");
/**
* The dictionary query default page size.
*/
const PAGE_SIZE = 1000;
/**
* GraphQL query for fetching editing data.
*/
exports.query = `
query EditingQuery(
$siteName: String!
$itemId: String!
$language: String!
$version: String
$after: String
$pageSize: Int = ${PAGE_SIZE}
) {
item(path: $itemId, language: $language, version: $version) {
rendered
}
site {
siteInfo(site: $siteName) {
dictionary(language: $language, first: $pageSize, after: $after) {
results {
key
value
}
pageInfo {
endCursor
hasNext
}
}
}
}
}
`;
/**
* GraphQL query for fetching dictionary data.
* This query is used when the dictionary data is paginated.
*/
exports.dictionaryQuery = `
query EditingDictionaryQuery(
$siteName: String!
$language: String!
$after: String
$pageSize: Int = ${PAGE_SIZE}
) {
site {
siteInfo(site: $siteName) {
dictionary(language: $language, first: $pageSize, after: $after) {
results {
key
value
}
pageInfo {
endCursor
hasNext
}
}
}
}
}
`;
/**
* Service for fetching editing data from Sitecore using the Sitecore's GraphQL API.
* Expected to be used in XMCloud Pages preview (editing) Metadata Edit Mode.
*/
class GraphQLEditingService {
/**
* Fetch layout data using the Sitecore GraphQL endpoint.
* @param {GraphQLLayoutServiceConfig} serviceConfig configuration
*/
constructor(serviceConfig) {
this.serviceConfig = serviceConfig;
this.graphQLClient = this.getGraphQLClient();
}
/**
* Fetches editing data. Provides the layout data and dictionary phrases
* @param {object} variables - The parameters for fetching editing data.
* @param {string} variables.siteName - The site name.
* @param {string} variables.itemId - The item id (path) to fetch layout data for.
* @param {string} variables.language - The language to fetch layout data for.
* @param {string} [variables.version] - The version of the item (optional).
* @param {LayoutKind} [variables.layoutKind] - The final or shared layout variant.
* @param {string} [variables.mode] - The editing mode to fetch layout data for.
* @returns {Promise} The layout data and dictionary phrases.
*/
fetchEditingData(_a) {
return __awaiter(this, arguments, void 0, function* ({ siteName, itemId, language, version, layoutKind = models_1.LayoutKind.Final, mode = layout_1.LayoutServicePageState.Edit, }) {
var _b, _c, _d;
debug_1.default.editing('fetching editing data for %s %s %s %s', siteName, itemId, language, version, layoutKind);
if (!siteName) {
throw new RangeError('The site name must be a non-empty string');
}
if (!language) {
throw new RangeError('The language must be a non-empty string');
}
let dictionaryResults = [];
let hasNext = true;
let after = '';
const editModeHeader = mode === 'edit' ? 'true' : 'false';
const editingData = yield this.graphQLClient.request(exports.query, {
siteName,
itemId,
version,
language,
}, {
headers: {
sc_layoutKind: layoutKind,
sc_editMode: editModeHeader,
},
});
if ((_c = (_b = editingData === null || editingData === void 0 ? void 0 : editingData.site) === null || _b === void 0 ? void 0 : _b.siteInfo) === null || _c === void 0 ? void 0 : _c.dictionary) {
dictionaryResults = editingData.site.siteInfo.dictionary.results;
hasNext = editingData.site.siteInfo.dictionary.pageInfo.hasNext;
after = editingData.site.siteInfo.dictionary.pageInfo.endCursor;
}
else {
hasNext = false;
}
const dictionary = yield this.fetchDictionaryData({ siteName, language }, dictionaryResults, hasNext, after);
return {
layoutData: ((_d = editingData === null || editingData === void 0 ? void 0 : editingData.item) === null || _d === void 0 ? void 0 : _d.rendered) || {
sitecore: {
context: { pageEditing: true, language, editMode: layout_1.EditMode.Metadata },
route: null,
},
},
dictionary,
};
});
}
fetchDictionaryData(_a) {
return __awaiter(this, arguments, void 0, function* ({ siteName, language, }, initDictionary = [], hasNext = true, after) {
var _b, _c;
let dictionaryResults = initDictionary;
const dictionary = {};
while (hasNext) {
const data = yield this.graphQLClient.request(exports.dictionaryQuery, {
siteName,
language,
after,
});
if ((_c = (_b = data === null || data === void 0 ? void 0 : data.site) === null || _b === void 0 ? void 0 : _b.siteInfo) === null || _c === void 0 ? void 0 : _c.dictionary) {
dictionaryResults = dictionaryResults.concat(data.site.siteInfo.dictionary.results);
hasNext = data.site.siteInfo.dictionary.pageInfo.hasNext;
after = data.site.siteInfo.dictionary.pageInfo.endCursor;
}
else {
hasNext = false;
}
}
dictionaryResults.forEach((item) => (dictionary[item.key] = item.value));
return dictionary;
});
}
/**
* Gets a GraphQL client that can make requests to the API.
* @returns {GraphQLClient} implementation
*/
getGraphQLClient() {
if (!this.serviceConfig.clientFactory) {
throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
}
return this.serviceConfig.clientFactory({
debugger: debug_1.default.editing,
});
}
}
exports.GraphQLEditingService = GraphQLEditingService;