@sitecore-content-sdk/core 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/LICENSE.txt +202 -0
  2. package/README.md +11 -0
  3. package/dist/cjs/cache-client.js +54 -0
  4. package/dist/cjs/constants.js +13 -0
  5. package/dist/cjs/data-fetcher.js +33 -0
  6. package/dist/cjs/debug.js +43 -0
  7. package/dist/cjs/editing/component-library.js +104 -0
  8. package/dist/cjs/editing/graphql-editing-service.js +186 -0
  9. package/dist/cjs/editing/index.js +23 -0
  10. package/dist/cjs/editing/models.js +23 -0
  11. package/dist/cjs/editing/rest-component-layout-service.js +76 -0
  12. package/dist/cjs/editing/utils.js +86 -0
  13. package/dist/cjs/graphql/app-root-query.js +73 -0
  14. package/dist/cjs/graphql/graphql-edge-proxy.js +21 -0
  15. package/dist/cjs/graphql/index.js +13 -0
  16. package/dist/cjs/graphql/search-service.js +61 -0
  17. package/dist/cjs/graphql-request-client.js +152 -0
  18. package/dist/cjs/i18n/dictionary-service.js +45 -0
  19. package/dist/cjs/i18n/graphql-dictionary-service.js +136 -0
  20. package/dist/cjs/i18n/index.js +7 -0
  21. package/dist/cjs/index.js +55 -0
  22. package/dist/cjs/layout/content-styles.js +70 -0
  23. package/dist/cjs/layout/graphql-layout-service.js +86 -0
  24. package/dist/cjs/layout/index.js +24 -0
  25. package/dist/cjs/layout/layout-service.js +9 -0
  26. package/dist/cjs/layout/models.js +35 -0
  27. package/dist/cjs/layout/themes.js +74 -0
  28. package/dist/cjs/layout/utils.js +110 -0
  29. package/dist/cjs/media/index.js +38 -0
  30. package/dist/cjs/media/media-api.js +96 -0
  31. package/dist/cjs/models.js +2 -0
  32. package/dist/cjs/native-fetcher.js +200 -0
  33. package/dist/cjs/personalize/graphql-personalize-service.js +114 -0
  34. package/dist/cjs/personalize/index.js +14 -0
  35. package/dist/cjs/personalize/layout-personalizer.js +97 -0
  36. package/dist/cjs/personalize/utils.js +136 -0
  37. package/dist/cjs/site/graphql-error-pages-service.js +89 -0
  38. package/dist/cjs/site/graphql-redirects-service.js +105 -0
  39. package/dist/cjs/site/graphql-robots-service.js +83 -0
  40. package/dist/cjs/site/graphql-siteinfo-service.js +107 -0
  41. package/dist/cjs/site/graphql-sitemap-service.js +93 -0
  42. package/dist/cjs/site/index.js +22 -0
  43. package/dist/cjs/site/site-resolver.js +79 -0
  44. package/dist/cjs/site/utils.js +43 -0
  45. package/dist/cjs/utils/env.js +26 -0
  46. package/dist/cjs/utils/index.js +20 -0
  47. package/dist/cjs/utils/is-server.js +10 -0
  48. package/dist/cjs/utils/timeout-promise.js +31 -0
  49. package/dist/cjs/utils/utils.js +222 -0
  50. package/dist/esm/cache-client.js +50 -0
  51. package/dist/esm/constants.js +10 -0
  52. package/dist/esm/data-fetcher.js +28 -0
  53. package/dist/esm/debug.js +36 -0
  54. package/dist/esm/editing/component-library.js +98 -0
  55. package/dist/esm/editing/graphql-editing-service.js +179 -0
  56. package/dist/esm/editing/index.js +5 -0
  57. package/dist/esm/editing/models.js +20 -0
  58. package/dist/esm/editing/rest-component-layout-service.js +72 -0
  59. package/dist/esm/editing/utils.js +76 -0
  60. package/dist/esm/graphql/app-root-query.js +69 -0
  61. package/dist/esm/graphql/graphql-edge-proxy.js +16 -0
  62. package/dist/esm/graphql/index.js +4 -0
  63. package/dist/esm/graphql/search-service.js +57 -0
  64. package/dist/esm/graphql-request-client.js +144 -0
  65. package/dist/esm/i18n/dictionary-service.js +41 -0
  66. package/dist/esm/i18n/graphql-dictionary-service.js +129 -0
  67. package/dist/esm/i18n/index.js +2 -0
  68. package/dist/esm/index.js +9 -0
  69. package/dist/esm/layout/content-styles.js +62 -0
  70. package/dist/esm/layout/graphql-layout-service.js +79 -0
  71. package/dist/esm/layout/index.js +6 -0
  72. package/dist/esm/layout/layout-service.js +5 -0
  73. package/dist/esm/layout/models.js +32 -0
  74. package/dist/esm/layout/themes.js +69 -0
  75. package/dist/esm/layout/utils.js +102 -0
  76. package/dist/esm/media/index.js +2 -0
  77. package/dist/esm/media/media-api.js +86 -0
  78. package/dist/esm/models.js +1 -0
  79. package/dist/esm/native-fetcher.js +193 -0
  80. package/dist/esm/personalize/graphql-personalize-service.js +107 -0
  81. package/dist/esm/personalize/index.js +3 -0
  82. package/dist/esm/personalize/layout-personalizer.js +92 -0
  83. package/dist/esm/personalize/utils.js +128 -0
  84. package/dist/esm/site/graphql-error-pages-service.js +82 -0
  85. package/dist/esm/site/graphql-redirects-service.js +98 -0
  86. package/dist/esm/site/graphql-robots-service.js +76 -0
  87. package/dist/esm/site/graphql-siteinfo-service.js +100 -0
  88. package/dist/esm/site/graphql-sitemap-service.js +86 -0
  89. package/dist/esm/site/index.js +7 -0
  90. package/dist/esm/site/site-resolver.js +75 -0
  91. package/dist/esm/site/utils.js +37 -0
  92. package/dist/esm/utils/env.js +22 -0
  93. package/dist/esm/utils/index.js +3 -0
  94. package/dist/esm/utils/is-server.js +8 -0
  95. package/dist/esm/utils/timeout-promise.js +28 -0
  96. package/dist/esm/utils/utils.js +207 -0
  97. package/editing.d.ts +1 -0
  98. package/editing.js +1 -0
  99. package/graphql.d.ts +1 -0
  100. package/graphql.js +1 -0
  101. package/i18n.d.ts +1 -0
  102. package/i18n.js +1 -0
  103. package/layout.d.ts +1 -0
  104. package/layout.js +1 -0
  105. package/media.d.ts +1 -0
  106. package/media.js +1 -0
  107. package/package.json +76 -0
  108. package/personalize.d.ts +1 -0
  109. package/personalize.js +1 -0
  110. package/site.d.ts +1 -0
  111. package/site.js +1 -0
  112. package/types/cache-client.d.ts +64 -0
  113. package/types/constants.d.ts +7 -0
  114. package/types/data-fetcher.d.ts +34 -0
  115. package/types/debug.d.ts +26 -0
  116. package/types/editing/component-library.d.ts +48 -0
  117. package/types/editing/graphql-editing-service.d.ts +90 -0
  118. package/types/editing/index.d.ts +6 -0
  119. package/types/editing/models.d.ts +52 -0
  120. package/types/editing/rest-component-layout-service.d.ts +100 -0
  121. package/types/editing/utils.d.ts +70 -0
  122. package/types/graphql/app-root-query.d.ts +32 -0
  123. package/types/graphql/graphql-edge-proxy.d.ts +15 -0
  124. package/types/graphql/index.d.ts +4 -0
  125. package/types/graphql/search-service.d.ts +95 -0
  126. package/types/graphql-request-client.d.ts +159 -0
  127. package/types/i18n/dictionary-service.d.ts +56 -0
  128. package/types/i18n/graphql-dictionary-service.d.ts +94 -0
  129. package/types/i18n/index.d.ts +2 -0
  130. package/types/index.d.ts +8 -0
  131. package/types/layout/content-styles.d.ts +18 -0
  132. package/types/layout/graphql-layout-service.d.ts +58 -0
  133. package/types/layout/index.d.ts +6 -0
  134. package/types/layout/layout-service.d.ts +19 -0
  135. package/types/layout/models.d.ts +145 -0
  136. package/types/layout/themes.d.ts +11 -0
  137. package/types/layout/utils.d.ts +40 -0
  138. package/types/media/index.d.ts +2 -0
  139. package/types/media/media-api.d.ts +55 -0
  140. package/types/models.d.ts +6 -0
  141. package/types/native-fetcher.d.ts +121 -0
  142. package/types/personalize/graphql-personalize-service.d.ts +80 -0
  143. package/types/personalize/index.d.ts +3 -0
  144. package/types/personalize/layout-personalizer.d.ts +27 -0
  145. package/types/personalize/utils.d.ts +69 -0
  146. package/types/site/graphql-error-pages-service.d.ts +57 -0
  147. package/types/site/graphql-redirects-service.d.ts +68 -0
  148. package/types/site/graphql-robots-service.d.ts +49 -0
  149. package/types/site/graphql-siteinfo-service.d.ts +70 -0
  150. package/types/site/graphql-sitemap-service.d.ts +55 -0
  151. package/types/site/index.d.ts +7 -0
  152. package/types/site/site-resolver.d.ts +27 -0
  153. package/types/site/utils.d.ts +24 -0
  154. package/types/utils/env.d.ts +7 -0
  155. package/types/utils/index.d.ts +3 -0
  156. package/types/utils/is-server.d.ts +6 -0
  157. package/types/utils/timeout-promise.d.ts +17 -0
  158. package/types/utils/utils.d.ts +71 -0
  159. package/utils.d.ts +1 -0
  160. package/utils.js +1 -0
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getComponentLibraryStatusEvent = exports.ComponentLibraryStatus = exports.addComponentUpdateHandler = exports.MetadataKind = exports.LayoutKind = exports.RestComponentLayoutService = exports.PAGES_EDITING_MARKER = exports.QUERY_PARAM_EDITING_SECRET = exports.EDITING_ALLOWED_ORIGINS = exports.getJssPagesClientData = exports.resetEditorChromes = exports.isEditorActive = exports.PagesEditor = exports.DEFAULT_PLACEHOLDER_UID = exports.GraphQLEditingService = void 0;
4
+ var graphql_editing_service_1 = require("./graphql-editing-service");
5
+ Object.defineProperty(exports, "GraphQLEditingService", { enumerable: true, get: function () { return graphql_editing_service_1.GraphQLEditingService; } });
6
+ var utils_1 = require("./utils");
7
+ Object.defineProperty(exports, "DEFAULT_PLACEHOLDER_UID", { enumerable: true, get: function () { return utils_1.DEFAULT_PLACEHOLDER_UID; } });
8
+ Object.defineProperty(exports, "PagesEditor", { enumerable: true, get: function () { return utils_1.PagesEditor; } });
9
+ Object.defineProperty(exports, "isEditorActive", { enumerable: true, get: function () { return utils_1.isEditorActive; } });
10
+ Object.defineProperty(exports, "resetEditorChromes", { enumerable: true, get: function () { return utils_1.resetEditorChromes; } });
11
+ Object.defineProperty(exports, "getJssPagesClientData", { enumerable: true, get: function () { return utils_1.getJssPagesClientData; } });
12
+ Object.defineProperty(exports, "EDITING_ALLOWED_ORIGINS", { enumerable: true, get: function () { return utils_1.EDITING_ALLOWED_ORIGINS; } });
13
+ Object.defineProperty(exports, "QUERY_PARAM_EDITING_SECRET", { enumerable: true, get: function () { return utils_1.QUERY_PARAM_EDITING_SECRET; } });
14
+ Object.defineProperty(exports, "PAGES_EDITING_MARKER", { enumerable: true, get: function () { return utils_1.PAGES_EDITING_MARKER; } });
15
+ var rest_component_layout_service_1 = require("./rest-component-layout-service");
16
+ Object.defineProperty(exports, "RestComponentLayoutService", { enumerable: true, get: function () { return rest_component_layout_service_1.RestComponentLayoutService; } });
17
+ var models_1 = require("./models");
18
+ Object.defineProperty(exports, "LayoutKind", { enumerable: true, get: function () { return models_1.LayoutKind; } });
19
+ Object.defineProperty(exports, "MetadataKind", { enumerable: true, get: function () { return models_1.MetadataKind; } });
20
+ var component_library_1 = require("./component-library");
21
+ Object.defineProperty(exports, "addComponentUpdateHandler", { enumerable: true, get: function () { return component_library_1.addComponentUpdateHandler; } });
22
+ Object.defineProperty(exports, "ComponentLibraryStatus", { enumerable: true, get: function () { return component_library_1.ComponentLibraryStatus; } });
23
+ Object.defineProperty(exports, "getComponentLibraryStatusEvent", { enumerable: true, get: function () { return component_library_1.getComponentLibraryStatusEvent; } });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MetadataKind = exports.LayoutKind = void 0;
4
+ /**
5
+ * Represents the Editing Layout variant.
6
+ * - shared - shared layout
7
+ * - final - final layout
8
+ */
9
+ var LayoutKind;
10
+ (function (LayoutKind) {
11
+ LayoutKind["Final"] = "final";
12
+ LayoutKind["Shared"] = "shared";
13
+ })(LayoutKind || (exports.LayoutKind = LayoutKind = {}));
14
+ /**
15
+ * Represents the kind of metadata element.
16
+ * - open - starting chrome element
17
+ * - close - closing chrome element
18
+ */
19
+ var MetadataKind;
20
+ (function (MetadataKind) {
21
+ MetadataKind["Open"] = "open";
22
+ MetadataKind["Close"] = "close";
23
+ })(MetadataKind || (exports.MetadataKind = MetadataKind = {}));
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RestComponentLayoutService = void 0;
4
+ const __1 = require("..");
5
+ const data_fetcher_1 = require("../data-fetcher");
6
+ /**
7
+ * REST service that enables Component Library functioality
8
+ * Makes a request to /sitecore/api/layout/component in 'library' mode in Pages.
9
+ * Returns layoutData for one single rendered component
10
+ */
11
+ class RestComponentLayoutService {
12
+ constructor(config) {
13
+ this.config = config;
14
+ this.getFetcher = (req, res) => {
15
+ return this.config.dataFetcherResolver
16
+ ? this.config.dataFetcherResolver(req, res)
17
+ : this.getDefaultFetcher(req);
18
+ };
19
+ /**
20
+ * Provides default @see NativeDataFetcher data fetcher
21
+ * @param {IncomingMessage} [req] Request instance
22
+ * @returns default fetcher
23
+ */
24
+ this.getDefaultFetcher = (req) => {
25
+ var _a;
26
+ const config = {
27
+ debugger: __1.debug.editing,
28
+ };
29
+ const nativeFetcher = new __1.NativeDataFetcher(config);
30
+ const headers = req && Object.assign(Object.assign({}, req.headers), (((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress) ? { 'X-Forwarded-For': req.socket.remoteAddress } : {}));
31
+ const fetcher = (url, data) => {
32
+ data = Object.assign(Object.assign({}, data), { headers: headers });
33
+ return nativeFetcher.fetch(url, data);
34
+ };
35
+ return fetcher;
36
+ };
37
+ }
38
+ fetchComponentData(params, req, res) {
39
+ params.siteName = params.siteName || this.config.siteName;
40
+ const querystringParams = this.getComponentFetchParams(params);
41
+ __1.debug.layout('fetching component with uid %s for %s %s %s', params.componentUid, params.itemId, params.language, params.siteName);
42
+ const fetcher = this.getFetcher(req, res);
43
+ const fetchUrl = this.resolveLayoutServiceUrl('component');
44
+ return (0, data_fetcher_1.fetchData)(fetchUrl, fetcher, querystringParams).catch((error) => {
45
+ var _a;
46
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
47
+ return error.response.data;
48
+ }
49
+ throw error;
50
+ });
51
+ }
52
+ /**
53
+ * Resolves layout service url
54
+ * @param {string} apiType which layout service API to call ('render' or 'placeholder')
55
+ * @returns the layout service url
56
+ */
57
+ resolveLayoutServiceUrl(apiType) {
58
+ const { apiHost = '', configurationName = 'jss' } = this.config;
59
+ return `${apiHost}/sitecore/api/layout/${apiType}/${configurationName}`;
60
+ }
61
+ getComponentFetchParams(params) {
62
+ // exclude undefined params with this one simple trick
63
+ return JSON.parse(JSON.stringify({
64
+ sc_apikey: this.config.apiKey,
65
+ item: params.itemId,
66
+ uid: params.componentUid,
67
+ dataSourceId: params.dataSourceId,
68
+ renderingItemId: params.renderingId,
69
+ version: params.version,
70
+ sc_site: params.siteName,
71
+ sc_lang: params.language || 'en',
72
+ sc_mode: params.editMode,
73
+ }));
74
+ }
75
+ }
76
+ exports.RestComponentLayoutService = RestComponentLayoutService;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getJssPagesClientData = exports.resetEditorChromes = exports.isEditorActive = exports.PagesEditor = exports.ChromeRediscoveryGlobalFunctionName = exports.EDITING_ALLOWED_ORIGINS = exports.PAGES_EDITING_MARKER = exports.QUERY_PARAM_EDITING_SECRET = exports.DEFAULT_PLACEHOLDER_UID = void 0;
7
+ const is_server_1 = __importDefault(require("../utils/is-server"));
8
+ /**
9
+ * Default value of uid for root placeholder when uid is not present.
10
+ */
11
+ exports.DEFAULT_PLACEHOLDER_UID = '00000000-0000-0000-0000-000000000000';
12
+ /**
13
+ * Query parameter for editing secret
14
+ */
15
+ exports.QUERY_PARAM_EDITING_SECRET = 'secret';
16
+ /**
17
+ * ID to be used as a marker for a script rendered in XMC Pages
18
+ * Should identify app is in XM Cloud Pages editing mode
19
+ */
20
+ exports.PAGES_EDITING_MARKER = 'jss-hrz-editing';
21
+ /**
22
+ * Default allowed origins for editing requests. This is used to enforce CORS, CSP headers.
23
+ */
24
+ exports.EDITING_ALLOWED_ORIGINS = ['https://pages.sitecorecloud.io'];
25
+ /**
26
+ * Copy of chrome rediscovery contract from Horizon (chrome-rediscovery.contract.ts)
27
+ */
28
+ exports.ChromeRediscoveryGlobalFunctionName = {
29
+ name: 'Sitecore.Horizon.ResetChromes',
30
+ };
31
+ /**
32
+ * Static utility class for Sitecore Pages Editor
33
+ */
34
+ class PagesEditor {
35
+ /**
36
+ * Determines whether the current execution context is within a Pages Editor.
37
+ * Pages Editor environment can be identified only in the browser
38
+ * @returns true if executing within a Pages Editor
39
+ */
40
+ static isActive() {
41
+ if ((0, is_server_1.default)()) {
42
+ return false;
43
+ }
44
+ // Check for Chromes mode
45
+ const chromesCheck = window.location.search.indexOf('sc_headless_mode=edit') > -1;
46
+ // JSS will render a jss-exclusive script element in Metadata mode to indicate edit mode in Pages
47
+ return chromesCheck || !!window.document.getElementById(exports.PAGES_EDITING_MARKER);
48
+ }
49
+ static resetChromes() {
50
+ if ((0, is_server_1.default)()) {
51
+ return;
52
+ }
53
+ // Reset chromes in Pages
54
+ window[exports.ChromeRediscoveryGlobalFunctionName.name] &&
55
+ window[exports.ChromeRediscoveryGlobalFunctionName.name]();
56
+ }
57
+ }
58
+ exports.PagesEditor = PagesEditor;
59
+ /**
60
+ * Determines whether the current execution context is within a Sitecore editor.
61
+ * Sitecore Editor environment can be identified only in the browser
62
+ * @returns true if executing within a Sitecore editor
63
+ */
64
+ const isEditorActive = () => {
65
+ return PagesEditor.isActive();
66
+ };
67
+ exports.isEditorActive = isEditorActive;
68
+ /**
69
+ * Resets Sitecore editor "chromes"
70
+ */
71
+ const resetEditorChromes = () => {
72
+ if (PagesEditor.isActive()) {
73
+ PagesEditor.resetChromes();
74
+ }
75
+ };
76
+ exports.resetEditorChromes = resetEditorChromes;
77
+ /**
78
+ * Gets extra JSS clientData scripts to render in XMC Pages in addition to clientData from Pages itself
79
+ * @returns {Record} collection of clientData
80
+ */
81
+ const getJssPagesClientData = () => {
82
+ const clientData = {};
83
+ clientData[exports.PAGES_EDITING_MARKER] = {};
84
+ return clientData;
85
+ };
86
+ exports.getJssPagesClientData = getJssPagesClientData;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.languageError = exports.siteNameError = void 0;
13
+ exports.getAppRootId = getAppRootId;
14
+ const constants_1 = require("../constants");
15
+ /** @private */
16
+ exports.siteNameError = 'The site name must be a non-empty string';
17
+ /** @private */
18
+ exports.languageError = 'The language must be a non-empty string';
19
+ /*
20
+ * GraphQL query that returns the ID of the root item of the specified site and language
21
+ */
22
+ const appRootQuery = /* GraphQL */ `
23
+ query AppRootQuery($jssAppTemplateId: String!, $siteName: String!, $language: String!) {
24
+ layout(site: $siteName, routePath: "/", language: $language) {
25
+ homePage: item {
26
+ rootItem: ancestors(includeTemplateIDs: [$jssAppTemplateId]) {
27
+ id
28
+ }
29
+ }
30
+ }
31
+ }
32
+ `;
33
+ /**
34
+ * Gets the ID of the JSS App root item for the specified site and language.
35
+ * @param {GraphQLClient} client that fetches data from a GraphQL endpoint.
36
+ * @param {string} siteName the name of the Sitecore site.
37
+ * @param {string} language the item language version.
38
+ * @param {string} [jssAppTemplateId] optional template ID of the app root item. If not
39
+ * specified, the ID of the "/sitecore/templates/Foundation/JavaScript Services/App"
40
+ * item is used.
41
+ * @returns the root item ID of the JSS App in Sitecore. Returns null if the app root item is not found.
42
+ * @throws {RangeError} if a valid site name value is not provided.
43
+ * @throws {RangeError} if a valid language value is not provided.
44
+ * @summary This function intentionally avoids throwing an error if a root item is not found,
45
+ * leaving that decision up to implementations.
46
+ */
47
+ function getAppRootId(client, siteName, language, jssAppTemplateId) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ var _a, _b, _c, _d, _e, _f;
50
+ if (!siteName) {
51
+ throw new RangeError(exports.siteNameError);
52
+ }
53
+ if (!language) {
54
+ throw new RangeError(exports.languageError);
55
+ }
56
+ let fetchResponse = yield client.request(appRootQuery, {
57
+ jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
58
+ siteName,
59
+ language,
60
+ });
61
+ if (!((_c = (_b = (_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.layout) === null || _a === void 0 ? void 0 : _a.homePage) === null || _b === void 0 ? void 0 : _b.rootItem) === null || _c === void 0 ? void 0 : _c.length) && language !== 'en') {
62
+ fetchResponse = yield client.request(appRootQuery, {
63
+ jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
64
+ siteName,
65
+ language: 'en',
66
+ });
67
+ }
68
+ if (!((_f = (_e = (_d = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.layout) === null || _d === void 0 ? void 0 : _d.homePage) === null || _e === void 0 ? void 0 : _e.rootItem) === null || _f === void 0 ? void 0 : _f.length)) {
69
+ return null;
70
+ }
71
+ return fetchResponse.layout.homePage.rootItem[0].id;
72
+ });
73
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEdgeProxyFormsUrl = exports.getEdgeProxyContentUrl = void 0;
4
+ const constants_1 = require("../constants");
5
+ /**
6
+ * Generates a URL for accessing Sitecore Edge Platform Content using the provided endpoint and context ID.
7
+ * @param {string} sitecoreEdgeContextId - The unique context id.
8
+ * @param {string} [sitecoreEdgeUrl] - The base endpoint URL for the Edge Platform. Default is https://edge-platform.sitecorecloud.io
9
+ * @returns {string} The complete URL for accessing content through the Edge Platform.
10
+ */
11
+ const getEdgeProxyContentUrl = (sitecoreEdgeContextId, sitecoreEdgeUrl = constants_1.SITECORE_EDGE_URL_DEFAULT) => `${sitecoreEdgeUrl}/v1/content/api/graphql/v1?sitecoreContextId=${sitecoreEdgeContextId}`;
12
+ exports.getEdgeProxyContentUrl = getEdgeProxyContentUrl;
13
+ /**
14
+ * Generates a URL for accessing Sitecore Edge Platform Forms using the provided form ID and context ID.
15
+ * @param {string} sitecoreEdgeContextId - The unique context id.
16
+ * @param {string} formId - The unique form id.
17
+ * @param {string} [sitecoreEdgeUrl] - The base endpoint URL for the Edge Platform. Default is https://edge-platform.sitecorecloud.io
18
+ * @returns {string} The complete URL for accessing forms through the Edge Platform.
19
+ */
20
+ const getEdgeProxyFormsUrl = (sitecoreEdgeContextId, formId, sitecoreEdgeUrl = constants_1.SITECORE_EDGE_URL_DEFAULT) => `${sitecoreEdgeUrl}/v1/forms/publisher/${formId}?sitecoreContextId=${sitecoreEdgeContextId}`;
21
+ exports.getEdgeProxyFormsUrl = getEdgeProxyFormsUrl;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEdgeProxyFormsUrl = exports.getEdgeProxyContentUrl = exports.SearchQueryService = exports.GraphQLRequestClient = exports.DefaultRetryStrategy = exports.getAppRootId = void 0;
4
+ var app_root_query_1 = require("./app-root-query");
5
+ Object.defineProperty(exports, "getAppRootId", { enumerable: true, get: function () { return app_root_query_1.getAppRootId; } });
6
+ var graphql_request_client_1 = require("../graphql-request-client");
7
+ Object.defineProperty(exports, "DefaultRetryStrategy", { enumerable: true, get: function () { return graphql_request_client_1.DefaultRetryStrategy; } });
8
+ Object.defineProperty(exports, "GraphQLRequestClient", { enumerable: true, get: function () { return graphql_request_client_1.GraphQLRequestClient; } });
9
+ var search_service_1 = require("./search-service");
10
+ Object.defineProperty(exports, "SearchQueryService", { enumerable: true, get: function () { return search_service_1.SearchQueryService; } });
11
+ var graphql_edge_proxy_1 = require("./graphql-edge-proxy");
12
+ Object.defineProperty(exports, "getEdgeProxyContentUrl", { enumerable: true, get: function () { return graphql_edge_proxy_1.getEdgeProxyContentUrl; } });
13
+ Object.defineProperty(exports, "getEdgeProxyFormsUrl", { enumerable: true, get: function () { return graphql_edge_proxy_1.getEdgeProxyFormsUrl; } });
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SearchQueryService = void 0;
13
+ /**
14
+ * @deprecated use GraphQLClient instead
15
+ * Provides functionality for performing GraphQL 'search' operations, including handling pagination.
16
+ * This class is meant to be extended or used as a mixin; it's not meant to be used directly.
17
+ * @template T The type of objects being requested.
18
+ * @mixin
19
+ */
20
+ class SearchQueryService {
21
+ /**
22
+ * Creates an instance of search query service.
23
+ * @param {GraphQLClient} client that fetches data from a GraphQL endpoint.
24
+ */
25
+ constructor(client) {
26
+ this.client = client;
27
+ }
28
+ /**
29
+ * 1. Validates mandatory search query arguments
30
+ * 2. Executes search query with pagination
31
+ * 3. Aggregates pagination results into a single result-set.
32
+ * @template T The type of objects being requested.
33
+ * @param {string | DocumentNode} query the search query.
34
+ * @param {SearchQueryVariables} args search query arguments.
35
+ * @returns {T[]} array of result objects.
36
+ * @throws {RangeError} if a valid root item ID is not provided.
37
+ * @throws {RangeError} if the provided language(s) is(are) not valid.
38
+ */
39
+ fetch(query, args) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ var _a;
42
+ if (!args.rootItemId) {
43
+ throw new RangeError('"rootItemId" and "language" must be non-empty strings');
44
+ }
45
+ if (!args.language) {
46
+ throw new RangeError('"rootItemId" and "language" must be non-empty strings');
47
+ }
48
+ let results = [];
49
+ let hasNext = true;
50
+ let after = '';
51
+ while (hasNext) {
52
+ const fetchResponse = yield this.client.request(query, Object.assign(Object.assign({}, args), { after }));
53
+ results = results.concat((_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.search) === null || _a === void 0 ? void 0 : _a.results);
54
+ hasNext = fetchResponse.search.pageInfo.hasNext;
55
+ after = fetchResponse.search.pageInfo.endCursor;
56
+ }
57
+ return results;
58
+ });
59
+ }
60
+ }
61
+ exports.SearchQueryService = SearchQueryService;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.GraphQLRequestClient = exports.DefaultRetryStrategy = void 0;
16
+ const graphql_request_1 = require("graphql-request");
17
+ const url_parse_1 = __importDefault(require("url-parse"));
18
+ const debug_1 = __importDefault(require("./debug"));
19
+ const timeout_promise_1 = __importDefault(require("./utils/timeout-promise"));
20
+ /**
21
+ * Represents a default retry strategy for handling retry attempts in case of specific HTTP status codes.
22
+ * This class implements the RetryStrategy interface and provides methods to determine whether a request
23
+ * should be retried and calculates the delay before the next retry attempt.
24
+ */
25
+ class DefaultRetryStrategy {
26
+ /**
27
+ * @param {object} options Configurable options for retry mechanism.
28
+ * @param {number[]} [options.statusCodes] HTTP status codes to trigger retries on. Default is [429].
29
+ * @param {string[]} [options.errorCodes] Node error codes to trigger retries. Default is ['ECONNRESET', 'ETIMEDOUT', 'EPROTO'].
30
+ * @param {number} [options.factor] Factor by which the delay increases with each retry attempt. Default is 2.
31
+ */
32
+ constructor(options = {}) {
33
+ this.statusCodes = options.statusCodes || [429];
34
+ this.errorCodes = options.errorCodes || ['ECONNRESET', 'ETIMEDOUT', 'EPROTO'];
35
+ this.factor = options.factor || 2;
36
+ }
37
+ shouldRetry(error, attempt, retries) {
38
+ var _a;
39
+ const isStatusCodeError = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== undefined && this.statusCodes.includes(error.response.status);
40
+ const isNodeErrorCode = error.code !== undefined && this.errorCodes.includes(error.code);
41
+ return retries > 0 && attempt <= retries && (isStatusCodeError || isNodeErrorCode);
42
+ }
43
+ getDelay(error, attempt) {
44
+ var _a;
45
+ const rawHeaders = (_a = error.response) === null || _a === void 0 ? void 0 : _a.headers;
46
+ const retryAfterHeader = rawHeaders === null || rawHeaders === void 0 ? void 0 : rawHeaders.get('Retry-After');
47
+ if (retryAfterHeader !== null &&
48
+ retryAfterHeader !== undefined &&
49
+ retryAfterHeader.trim() !== '') {
50
+ const delaySeconds = Number.parseFloat(retryAfterHeader);
51
+ return delaySeconds * 1000;
52
+ }
53
+ else {
54
+ return Math.pow(this.factor, attempt - 1) * 1000;
55
+ }
56
+ }
57
+ }
58
+ exports.DefaultRetryStrategy = DefaultRetryStrategy;
59
+ /**
60
+ * A GraphQL client for Sitecore APIs that uses the 'graphql-request' library.
61
+ * https://github.com/prisma-labs/graphql-request
62
+ */
63
+ class GraphQLRequestClient {
64
+ /**
65
+ * Provides ability to execute graphql query using given `endpoint`
66
+ * @param {string} endpoint The Graphql endpoint
67
+ * @param {GraphQLRequestClientConfig} [clientConfig] GraphQL request client configuration.
68
+ */
69
+ constructor(endpoint, clientConfig = {}) {
70
+ var _a;
71
+ this.endpoint = endpoint;
72
+ this.headers = {};
73
+ if (clientConfig.apiKey) {
74
+ this.headers.sc_apikey = clientConfig.apiKey;
75
+ }
76
+ if (clientConfig.headers) {
77
+ this.headers = Object.assign(Object.assign({}, this.headers), clientConfig.headers);
78
+ }
79
+ if (!endpoint || !(0, url_parse_1.default)(endpoint).hostname) {
80
+ throw new Error(`Invalid GraphQL endpoint '${endpoint}'. Verify that 'layoutServiceHost' property in 'scjssconfig.json' file or appropriate environment variable is set`);
81
+ }
82
+ this.timeout = clientConfig.timeout;
83
+ this.retries = (_a = clientConfig.retries) !== null && _a !== void 0 ? _a : 3;
84
+ this.retryStrategy =
85
+ clientConfig.retryStrategy ||
86
+ new DefaultRetryStrategy({ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524] });
87
+ this.client = new graphql_request_1.GraphQLClient(endpoint, {
88
+ headers: this.headers,
89
+ fetch: clientConfig.fetch,
90
+ });
91
+ this.debug = clientConfig.debugger || debug_1.default.http;
92
+ }
93
+ /**
94
+ * Factory method for creating a GraphQLRequestClientFactory.
95
+ * @param {object} config - client configuration options.
96
+ * @param {string} config.endpoint - endpoint
97
+ * @param {string} [config.apiKey] - apikey
98
+ */
99
+ static createClientFactory({ endpoint, apiKey, }) {
100
+ return (config = {}) => new GraphQLRequestClient(endpoint, Object.assign(Object.assign({}, config), { apiKey }));
101
+ }
102
+ /**
103
+ * Execute graphql request
104
+ * @param {string | DocumentNode} query graphql query
105
+ * @param {object} [variables] graphql variables
106
+ * @param {RequestOptions} [options] Options for configuring a GraphQL request.
107
+ */
108
+ request(query, variables, options) {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ let attempt = 1;
111
+ const retryer = () => __awaiter(this, void 0, void 0, function* () {
112
+ // Note we don't have access to raw request/response with graphql-request
113
+ // but we should log whatever we have.
114
+ this.debug('request: %o', {
115
+ url: this.endpoint,
116
+ headers: Object.assign(Object.assign({}, this.headers), options === null || options === void 0 ? void 0 : options.headers),
117
+ query,
118
+ variables,
119
+ });
120
+ const startTimestamp = Date.now();
121
+ const fetchWithOptionalTimeout = [this.client.request(query, variables, options === null || options === void 0 ? void 0 : options.headers)];
122
+ if (this.timeout) {
123
+ this.abortTimeout = new timeout_promise_1.default(this.timeout);
124
+ fetchWithOptionalTimeout.push(this.abortTimeout.start);
125
+ }
126
+ return Promise.race(fetchWithOptionalTimeout).then((data) => {
127
+ var _a;
128
+ (_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
129
+ this.debug('response in %dms: %o', Date.now() - startTimestamp, data);
130
+ return Promise.resolve(data);
131
+ }, (error) => __awaiter(this, void 0, void 0, function* () {
132
+ var _a, _b;
133
+ (_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
134
+ this.debug('response error: %o', error.response || error.message || error);
135
+ const status = ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) || error.code;
136
+ const shouldRetry = this.retryStrategy.shouldRetry(error, attempt, this.retries);
137
+ if (shouldRetry) {
138
+ const delayMs = this.retryStrategy.getDelay(error, attempt);
139
+ this.debug('Error: %s. Retrying in %dms (attempt %d).', status, delayMs, attempt);
140
+ attempt++;
141
+ return new Promise((resolve) => setTimeout(resolve, delayMs)).then(retryer);
142
+ }
143
+ else {
144
+ return Promise.reject(error);
145
+ }
146
+ }));
147
+ });
148
+ return retryer();
149
+ });
150
+ }
151
+ }
152
+ exports.GraphQLRequestClient = GraphQLRequestClient;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DictionaryServiceBase = void 0;
4
+ const cache_client_1 = require("../cache-client");
5
+ /**
6
+ * Base implementation of @see DictionaryService that handles caching dictionary values
7
+ */
8
+ class DictionaryServiceBase {
9
+ /**
10
+ * Initializes a new instance of @see DictionaryService using the provided @see CacheOptions
11
+ * @param {CacheOptions} options Configuration options
12
+ */
13
+ constructor(options) {
14
+ this.options = options;
15
+ this.cache = this.getCacheClient();
16
+ }
17
+ /**
18
+ * Caches a @see DictionaryPhrases value for the specified cache key.
19
+ * @param {string} key The cache key.
20
+ * @param {DictionaryPhrases} value The value to cache.
21
+ * @returns The value added to the cache.
22
+ * @mixes CacheClient<DictionaryPhrases>
23
+ */
24
+ setCacheValue(key, value) {
25
+ return this.cache.setCacheValue(key, value);
26
+ }
27
+ /**
28
+ * Retrieves a @see DictionaryPhrases value from the cache.
29
+ * @param {string} key The cache key.
30
+ * @returns The @see DictionaryPhrases value, or null if the specified key is not found in the cache.
31
+ */
32
+ getCacheValue(key) {
33
+ return this.cache.getCacheValue(key);
34
+ }
35
+ /**
36
+ * Gets a cache client that can cache data. Uses memory-cache as the default
37
+ * library for caching (@see MemoryCacheClient). Override this method if you
38
+ * want to use something else.
39
+ * @returns {CacheClient} implementation
40
+ */
41
+ getCacheClient() {
42
+ return new cache_client_1.MemoryCacheClient(this.options);
43
+ }
44
+ }
45
+ exports.DictionaryServiceBase = DictionaryServiceBase;