@sitecore-content-sdk/content 1.5.0-canary.5

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 (258) hide show
  1. package/LICENSE.txt +202 -0
  2. package/README.md +7 -0
  3. package/client.d.ts +1 -0
  4. package/codegen.d.ts +1 -0
  5. package/config-cli.d.ts +1 -0
  6. package/config.d.ts +1 -0
  7. package/dist/cjs/client/edge-proxy.js +24 -0
  8. package/dist/cjs/client/index.js +14 -0
  9. package/dist/cjs/client/models.js +2 -0
  10. package/dist/cjs/client/sitecore-client.js +420 -0
  11. package/dist/cjs/client/utils.js +53 -0
  12. package/dist/cjs/config/define-config.js +195 -0
  13. package/dist/cjs/config/index.js +7 -0
  14. package/dist/cjs/config/models.js +12 -0
  15. package/dist/cjs/config-cli/define-cli-config.js +23 -0
  16. package/dist/cjs/config-cli/index.js +7 -0
  17. package/dist/cjs/config-cli/models.js +8 -0
  18. package/dist/cjs/constants.js +12 -0
  19. package/dist/cjs/debug.js +21 -0
  20. package/dist/cjs/editing/codegen/index.js +14 -0
  21. package/dist/cjs/editing/codegen/preview.js +277 -0
  22. package/dist/cjs/editing/component-layout-service.js +62 -0
  23. package/dist/cjs/editing/design-library.js +184 -0
  24. package/dist/cjs/editing/editing-service.js +81 -0
  25. package/dist/cjs/editing/index.js +33 -0
  26. package/dist/cjs/editing/models.js +44 -0
  27. package/dist/cjs/editing/utils.js +105 -0
  28. package/dist/cjs/form/form.js +81 -0
  29. package/dist/cjs/form/index.js +7 -0
  30. package/dist/cjs/i18n/dictionary-service.js +144 -0
  31. package/dist/cjs/i18n/index.js +7 -0
  32. package/dist/cjs/i18n/utils.js +16 -0
  33. package/dist/cjs/index.js +47 -0
  34. package/dist/cjs/layout/content-styles.js +73 -0
  35. package/dist/cjs/layout/index.js +24 -0
  36. package/dist/cjs/layout/layout-service.js +68 -0
  37. package/dist/cjs/layout/models.js +39 -0
  38. package/dist/cjs/layout/themes.js +77 -0
  39. package/dist/cjs/layout/utils.js +117 -0
  40. package/dist/cjs/media/index.js +38 -0
  41. package/dist/cjs/media/media-api.js +100 -0
  42. package/dist/cjs/models.js +2 -0
  43. package/dist/cjs/personalize/index.js +15 -0
  44. package/dist/cjs/personalize/layout-personalizer.js +98 -0
  45. package/dist/cjs/personalize/personalize-service.js +109 -0
  46. package/dist/cjs/personalize/utils.js +143 -0
  47. package/dist/cjs/site/error-pages-service.js +82 -0
  48. package/dist/cjs/site/index.js +26 -0
  49. package/dist/cjs/site/models.js +2 -0
  50. package/dist/cjs/site/redirects-service.js +109 -0
  51. package/dist/cjs/site/robots-service.js +74 -0
  52. package/dist/cjs/site/site-resolver.js +73 -0
  53. package/dist/cjs/site/siteinfo-service.js +94 -0
  54. package/dist/cjs/site/sitemap-xml-service.js +92 -0
  55. package/dist/cjs/site/sitepath-service.js +201 -0
  56. package/dist/cjs/site/utils.js +55 -0
  57. package/dist/cjs/sitecore-service-base.js +33 -0
  58. package/dist/cjs/tools/codegen/component-generation.js +49 -0
  59. package/dist/cjs/tools/codegen/extract-files.js +105 -0
  60. package/dist/cjs/tools/codegen/import-map.js +411 -0
  61. package/dist/cjs/tools/codegen/utils.js +418 -0
  62. package/dist/cjs/tools/generate-map.js +2 -0
  63. package/dist/cjs/tools/generateSites.js +59 -0
  64. package/dist/cjs/tools/index.js +30 -0
  65. package/dist/cjs/tools/scaffold.js +62 -0
  66. package/dist/cjs/tools/templating/components.js +96 -0
  67. package/dist/cjs/tools/templating/index.js +6 -0
  68. package/dist/esm/client/edge-proxy.js +19 -0
  69. package/dist/esm/client/index.js +4 -0
  70. package/dist/esm/client/models.js +1 -0
  71. package/dist/esm/client/sitecore-client.js +416 -0
  72. package/dist/esm/client/utils.js +49 -0
  73. package/dist/esm/config/define-config.js +189 -0
  74. package/dist/esm/config/index.js +2 -0
  75. package/dist/esm/config/models.js +9 -0
  76. package/dist/esm/config-cli/define-cli-config.js +19 -0
  77. package/dist/esm/config-cli/index.js +2 -0
  78. package/dist/esm/config-cli/models.js +5 -0
  79. package/dist/esm/constants.js +9 -0
  80. package/dist/esm/debug.js +19 -0
  81. package/dist/esm/editing/codegen/index.js +1 -0
  82. package/dist/esm/editing/codegen/preview.js +263 -0
  83. package/dist/esm/editing/component-layout-service.js +55 -0
  84. package/dist/esm/editing/design-library.js +172 -0
  85. package/dist/esm/editing/editing-service.js +74 -0
  86. package/dist/esm/editing/index.js +6 -0
  87. package/dist/esm/editing/models.js +41 -0
  88. package/dist/esm/editing/utils.js +98 -0
  89. package/dist/esm/form/form.js +72 -0
  90. package/dist/esm/form/index.js +1 -0
  91. package/dist/esm/i18n/dictionary-service.js +137 -0
  92. package/dist/esm/i18n/index.js +2 -0
  93. package/dist/esm/i18n/utils.js +13 -0
  94. package/dist/esm/index.js +5 -0
  95. package/dist/esm/layout/content-styles.js +65 -0
  96. package/dist/esm/layout/index.js +6 -0
  97. package/dist/esm/layout/layout-service.js +61 -0
  98. package/dist/esm/layout/models.js +36 -0
  99. package/dist/esm/layout/themes.js +72 -0
  100. package/dist/esm/layout/utils.js +109 -0
  101. package/dist/esm/media/index.js +2 -0
  102. package/dist/esm/media/media-api.js +90 -0
  103. package/dist/esm/models.js +1 -0
  104. package/dist/esm/personalize/index.js +3 -0
  105. package/dist/esm/personalize/layout-personalizer.js +93 -0
  106. package/dist/esm/personalize/personalize-service.js +102 -0
  107. package/dist/esm/personalize/utils.js +135 -0
  108. package/dist/esm/site/error-pages-service.js +75 -0
  109. package/dist/esm/site/index.js +8 -0
  110. package/dist/esm/site/models.js +1 -0
  111. package/dist/esm/site/redirects-service.js +102 -0
  112. package/dist/esm/site/robots-service.js +67 -0
  113. package/dist/esm/site/site-resolver.js +69 -0
  114. package/dist/esm/site/siteinfo-service.js +87 -0
  115. package/dist/esm/site/sitemap-xml-service.js +85 -0
  116. package/dist/esm/site/sitepath-service.js +193 -0
  117. package/dist/esm/site/utils.js +49 -0
  118. package/dist/esm/sitecore-service-base.js +29 -0
  119. package/dist/esm/tools/codegen/component-generation.js +44 -0
  120. package/dist/esm/tools/codegen/extract-files.js +99 -0
  121. package/dist/esm/tools/codegen/import-map.js +368 -0
  122. package/dist/esm/tools/codegen/utils.js +373 -0
  123. package/dist/esm/tools/generate-map.js +1 -0
  124. package/dist/esm/tools/generateSites.js +52 -0
  125. package/dist/esm/tools/index.js +6 -0
  126. package/dist/esm/tools/scaffold.js +54 -0
  127. package/dist/esm/tools/templating/components.js +59 -0
  128. package/dist/esm/tools/templating/index.js +1 -0
  129. package/editing.d.ts +1 -0
  130. package/i18n.d.ts +1 -0
  131. package/layout.d.ts +1 -0
  132. package/media.d.ts +1 -0
  133. package/package.json +157 -0
  134. package/personalize.d.ts +1 -0
  135. package/site.d.ts +1 -0
  136. package/tools.d.ts +1 -0
  137. package/types/client/edge-proxy.d.ts +17 -0
  138. package/types/client/edge-proxy.d.ts.map +1 -0
  139. package/types/client/index.d.ts +7 -0
  140. package/types/client/index.d.ts.map +1 -0
  141. package/types/client/models.d.ts +21 -0
  142. package/types/client/models.d.ts.map +1 -0
  143. package/types/client/sitecore-client.d.ts +338 -0
  144. package/types/client/sitecore-client.d.ts.map +1 -0
  145. package/types/client/utils.d.ts +15 -0
  146. package/types/client/utils.d.ts.map +1 -0
  147. package/types/config/define-config.d.ts +20 -0
  148. package/types/config/define-config.d.ts.map +1 -0
  149. package/types/config/index.d.ts +3 -0
  150. package/types/config/index.d.ts.map +1 -0
  151. package/types/config/models.d.ts +287 -0
  152. package/types/config/models.d.ts.map +1 -0
  153. package/types/config-cli/define-cli-config.d.ts +9 -0
  154. package/types/config-cli/define-cli-config.d.ts.map +1 -0
  155. package/types/config-cli/index.d.ts +3 -0
  156. package/types/config-cli/index.d.ts.map +1 -0
  157. package/types/config-cli/models.d.ts +6 -0
  158. package/types/config-cli/models.d.ts.map +1 -0
  159. package/types/constants.d.ts +10 -0
  160. package/types/constants.d.ts.map +1 -0
  161. package/types/debug.d.ts +19 -0
  162. package/types/debug.d.ts.map +1 -0
  163. package/types/editing/codegen/index.d.ts +2 -0
  164. package/types/editing/codegen/index.d.ts.map +1 -0
  165. package/types/editing/codegen/preview.d.ts +256 -0
  166. package/types/editing/codegen/preview.d.ts.map +1 -0
  167. package/types/editing/component-layout-service.d.ts +84 -0
  168. package/types/editing/component-layout-service.d.ts.map +1 -0
  169. package/types/editing/design-library.d.ts +111 -0
  170. package/types/editing/design-library.d.ts.map +1 -0
  171. package/types/editing/editing-service.d.ts +71 -0
  172. package/types/editing/editing-service.d.ts.map +1 -0
  173. package/types/editing/index.d.ts +7 -0
  174. package/types/editing/index.d.ts.map +1 -0
  175. package/types/editing/models.d.ts +103 -0
  176. package/types/editing/models.d.ts.map +1 -0
  177. package/types/editing/utils.d.ts +82 -0
  178. package/types/editing/utils.d.ts.map +1 -0
  179. package/types/form/form.d.ts +25 -0
  180. package/types/form/form.d.ts.map +1 -0
  181. package/types/form/index.d.ts +2 -0
  182. package/types/form/index.d.ts.map +1 -0
  183. package/types/i18n/dictionary-service.d.ts +133 -0
  184. package/types/i18n/dictionary-service.d.ts.map +1 -0
  185. package/types/i18n/index.d.ts +3 -0
  186. package/types/i18n/index.d.ts.map +1 -0
  187. package/types/i18n/utils.d.ts +9 -0
  188. package/types/i18n/utils.d.ts.map +1 -0
  189. package/types/index.d.ts +7 -0
  190. package/types/index.d.ts.map +1 -0
  191. package/types/layout/content-styles.d.ts +20 -0
  192. package/types/layout/content-styles.d.ts.map +1 -0
  193. package/types/layout/index.d.ts +6 -0
  194. package/types/layout/index.d.ts.map +1 -0
  195. package/types/layout/layout-service.d.ts +45 -0
  196. package/types/layout/layout-service.d.ts.map +1 -0
  197. package/types/layout/models.d.ts +174 -0
  198. package/types/layout/models.d.ts.map +1 -0
  199. package/types/layout/themes.d.ts +13 -0
  200. package/types/layout/themes.d.ts.map +1 -0
  201. package/types/layout/utils.d.ts +56 -0
  202. package/types/layout/utils.d.ts.map +1 -0
  203. package/types/media/index.d.ts +3 -0
  204. package/types/media/index.d.ts.map +1 -0
  205. package/types/media/media-api.d.ts +60 -0
  206. package/types/media/media-api.d.ts.map +1 -0
  207. package/types/models.d.ts +32 -0
  208. package/types/models.d.ts.map +1 -0
  209. package/types/personalize/index.d.ts +4 -0
  210. package/types/personalize/index.d.ts.map +1 -0
  211. package/types/personalize/layout-personalizer.d.ts +29 -0
  212. package/types/personalize/layout-personalizer.d.ts.map +1 -0
  213. package/types/personalize/personalize-service.d.ts +89 -0
  214. package/types/personalize/personalize-service.d.ts.map +1 -0
  215. package/types/personalize/utils.d.ts +78 -0
  216. package/types/personalize/utils.d.ts.map +1 -0
  217. package/types/site/error-pages-service.d.ts +64 -0
  218. package/types/site/error-pages-service.d.ts.map +1 -0
  219. package/types/site/index.d.ts +10 -0
  220. package/types/site/index.d.ts.map +1 -0
  221. package/types/site/models.d.ts +23 -0
  222. package/types/site/models.d.ts.map +1 -0
  223. package/types/site/redirects-service.d.ts +91 -0
  224. package/types/site/redirects-service.d.ts.map +1 -0
  225. package/types/site/robots-service.d.ts +57 -0
  226. package/types/site/robots-service.d.ts.map +1 -0
  227. package/types/site/site-resolver.d.ts +28 -0
  228. package/types/site/site-resolver.d.ts.map +1 -0
  229. package/types/site/siteinfo-service.d.ts +64 -0
  230. package/types/site/siteinfo-service.d.ts.map +1 -0
  231. package/types/site/sitemap-xml-service.d.ts +63 -0
  232. package/types/site/sitemap-xml-service.d.ts.map +1 -0
  233. package/types/site/sitepath-service.d.ts +137 -0
  234. package/types/site/sitepath-service.d.ts.map +1 -0
  235. package/types/site/utils.d.ts +41 -0
  236. package/types/site/utils.d.ts.map +1 -0
  237. package/types/sitecore-service-base.d.ts +31 -0
  238. package/types/sitecore-service-base.d.ts.map +1 -0
  239. package/types/tools/codegen/component-generation.d.ts +50 -0
  240. package/types/tools/codegen/component-generation.d.ts.map +1 -0
  241. package/types/tools/codegen/extract-files.d.ts +24 -0
  242. package/types/tools/codegen/extract-files.d.ts.map +1 -0
  243. package/types/tools/codegen/import-map.d.ts +103 -0
  244. package/types/tools/codegen/import-map.d.ts.map +1 -0
  245. package/types/tools/codegen/utils.d.ts +76 -0
  246. package/types/tools/codegen/utils.d.ts.map +1 -0
  247. package/types/tools/generate-map.d.ts +36 -0
  248. package/types/tools/generate-map.d.ts.map +1 -0
  249. package/types/tools/generateSites.d.ts +25 -0
  250. package/types/tools/generateSites.d.ts.map +1 -0
  251. package/types/tools/index.d.ts +8 -0
  252. package/types/tools/index.d.ts.map +1 -0
  253. package/types/tools/scaffold.d.ts +27 -0
  254. package/types/tools/scaffold.d.ts.map +1 -0
  255. package/types/tools/templating/components.d.ts +104 -0
  256. package/types/tools/templating/components.d.ts.map +1 -0
  257. package/types/tools/templating/index.d.ts +2 -0
  258. package/types/tools/templating/index.d.ts.map +1 -0
@@ -0,0 +1,109 @@
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.PersonalizeService = void 0;
7
+ const core_1 = require("@sitecore-content-sdk/core");
8
+ const tools_1 = require("@sitecore-content-sdk/core/tools");
9
+ const debug_1 = __importDefault(require("../debug"));
10
+ /**
11
+ * Fetch personalize data using the Sitecore GraphQL endpoint.
12
+ * @public
13
+ */
14
+ class PersonalizeService {
15
+ /**
16
+ * Fetch personalize data using the Sitecore GraphQL endpoint.
17
+ * @param {PersonalizeServiceConfig} config
18
+ */
19
+ constructor(config) {
20
+ this.config = config;
21
+ this.config.timeout = config.timeout || 400;
22
+ this.graphQLClient = this.getGraphQLClient();
23
+ this.cache = this.getCacheClient();
24
+ }
25
+ get query() {
26
+ return /* GraphQL */ `
27
+ query ($siteName: String!, $language: String!, $itemPath: String!) {
28
+ layout(site: $siteName, routePath: $itemPath, language: $language) {
29
+ item {
30
+ id
31
+ version
32
+ personalization {
33
+ variantIds
34
+ }
35
+ }
36
+ }
37
+ }
38
+ `;
39
+ }
40
+ /**
41
+ * Get personalize information for a route
42
+ * @param {string} itemPath page route
43
+ * @param {string} language language
44
+ * @param {string} siteName site name
45
+ * @returns {Promise<PersonalizeInfo | undefined>} the personalize information or undefined (if itemPath / language not found)
46
+ */
47
+ async getPersonalizeInfo(itemPath, language, siteName) {
48
+ var _a;
49
+ // while other graphql services can use fetchOptions, personalize is more sensitive
50
+ // we don't allow retries in it since we need to be fast
51
+ debug_1.default.personalize('fetching personalize info for %s %s %s', siteName, itemPath, language);
52
+ const cacheKey = this.getCacheKey(itemPath, language, siteName);
53
+ let data = this.cache.getCacheValue(cacheKey);
54
+ if (!data) {
55
+ try {
56
+ data = await this.graphQLClient.request(this.query, {
57
+ siteName,
58
+ itemPath,
59
+ language,
60
+ });
61
+ this.cache.setCacheValue(cacheKey, data);
62
+ }
63
+ catch (error) {
64
+ if ((0, tools_1.isTimeoutError)(error)) {
65
+ return undefined;
66
+ }
67
+ throw error;
68
+ }
69
+ }
70
+ return ((_a = data === null || data === void 0 ? void 0 : data.layout) === null || _a === void 0 ? void 0 : _a.item)
71
+ ? {
72
+ pageId: data.layout.item.id,
73
+ variantIds: data.layout.item.personalization.variantIds,
74
+ }
75
+ : undefined;
76
+ }
77
+ /**
78
+ * Gets cache client implementation
79
+ * Override this method if custom cache needs to be used
80
+ * @returns CacheClient instance
81
+ */
82
+ getCacheClient() {
83
+ var _a, _b;
84
+ return new core_1.MemoryCacheClient({
85
+ cacheEnabled: (_a = this.config.cacheEnabled) !== null && _a !== void 0 ? _a : true,
86
+ cacheTimeout: (_b = this.config.cacheTimeout) !== null && _b !== void 0 ? _b : 10,
87
+ });
88
+ }
89
+ getCacheKey(itemPath, language, siteName) {
90
+ return `${siteName}-${itemPath}-${language}`;
91
+ }
92
+ /**
93
+ * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
94
+ * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
95
+ * want to use something else.
96
+ * @returns {GraphQLClient} implementation
97
+ */
98
+ getGraphQLClient() {
99
+ if (!this.config.clientFactory) {
100
+ throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
101
+ }
102
+ return this.config.clientFactory({
103
+ debugger: debug_1.default.personalize,
104
+ fetch: this.config.fetch,
105
+ timeout: this.config.timeout,
106
+ });
107
+ }
108
+ }
109
+ exports.PersonalizeService = PersonalizeService;
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CdpHelper = exports.VARIANT_PREFIX = exports.DEFAULT_VARIANT = void 0;
4
+ exports.getPersonalizedRewrite = getPersonalizedRewrite;
5
+ exports.getPersonalizedRewriteData = getPersonalizedRewriteData;
6
+ exports.getGroomedVariantIds = getGroomedVariantIds;
7
+ exports.normalizePersonalizedRewrite = normalizePersonalizedRewrite;
8
+ /** @internal */
9
+ exports.DEFAULT_VARIANT = '_default';
10
+ /** @internal */
11
+ exports.VARIANT_PREFIX = '_variantId_';
12
+ /**
13
+ * Get a personalized rewrite path for given pathname
14
+ * @param {string} pathname the pathname
15
+ * @param {string[]} variantIds the variantIds to include in the rewrite
16
+ * @returns {string} the rewrite path
17
+ * @public
18
+ */
19
+ function getPersonalizedRewrite(pathname, variantIds) {
20
+ const path = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
21
+ return `${path}${variantIds.map((variantId) => `/${exports.VARIANT_PREFIX}${variantId}`).join('')}`;
22
+ }
23
+ /**
24
+ * Get personalize data from the rewrite path
25
+ * @param {string} pathname the pathname
26
+ * @returns {PersonalizedRewriteData} the personalize data from the rewrite
27
+ * @public
28
+ */
29
+ function getPersonalizedRewriteData(pathname) {
30
+ const segments = pathname.split('/');
31
+ const variantIds = [];
32
+ segments.forEach((segment) => {
33
+ const result = segment.match(`${exports.VARIANT_PREFIX}(.*$)`);
34
+ if (result) {
35
+ variantIds.push(result[1]);
36
+ }
37
+ });
38
+ return getGroomedVariantIds(variantIds);
39
+ }
40
+ /**
41
+ * Parses a list of variantIds and divides into layout and component variants
42
+ * @param {string[]} variantIds the list of variant IDs for a page
43
+ * @returns {PersonalizedRewriteData} object with variant IDs sorted
44
+ * @public
45
+ */
46
+ function getGroomedVariantIds(variantIds) {
47
+ const data = {
48
+ variantId: exports.DEFAULT_VARIANT,
49
+ componentVariantIds: [],
50
+ };
51
+ variantIds.forEach((variantId) => {
52
+ var _a;
53
+ if (variantId.includes('_')) {
54
+ // Component-level personalization in format "<ComponentID>_<VariantID>"
55
+ // There can be multiple
56
+ (_a = data.componentVariantIds) === null || _a === void 0 ? void 0 : _a.push(variantId);
57
+ }
58
+ else {
59
+ // Embedded (page-level) personalization in format "<VariantID>"
60
+ // There should be only one
61
+ data.variantId = variantId;
62
+ }
63
+ });
64
+ return data;
65
+ }
66
+ /**
67
+ * Normalize a personalized rewrite path (remove personalize data)
68
+ * @param {string} pathname the pathname
69
+ * @returns {string} the pathname with personalize data removed
70
+ * @public
71
+ */
72
+ function normalizePersonalizedRewrite(pathname) {
73
+ if (!pathname.includes(exports.VARIANT_PREFIX)) {
74
+ return pathname;
75
+ }
76
+ let segments = pathname.split('/');
77
+ segments = segments.filter((segment) => !segment.includes(exports.VARIANT_PREFIX));
78
+ const result = segments.join('/');
79
+ // return root path if all segments were personalize data
80
+ return result ? result : '/';
81
+ }
82
+ /**
83
+ * Static utility class for Sitecore CDP
84
+ * @public
85
+ */
86
+ class CdpHelper {
87
+ /**
88
+ * Gets the page variant id for CDP in the required format
89
+ * @param {string} pageId the page id
90
+ * @param {string} language the language
91
+ * @param {string} variantId the variant id
92
+ * @param {string} [scope] the scope value
93
+ * @returns {string} the formatted page variant id
94
+ */
95
+ static getPageVariantId(pageId, language, variantId, scope) {
96
+ const formattedPageId = pageId.replace(/[{}-]/g, '');
97
+ const formattedLanguage = language.replace('-', '_');
98
+ const scopeId = scope ? `${this.normalizeScope(scope)}_` : '';
99
+ let formattedVariantId = variantId;
100
+ if (!variantId || variantId === exports.DEFAULT_VARIANT) {
101
+ formattedVariantId = 'default';
102
+ }
103
+ return `${scopeId}${formattedPageId}_${formattedLanguage}_${formattedVariantId}`.toLowerCase();
104
+ }
105
+ /**
106
+ * Gets the friendly id for (page-level) Embedded Personalization in the required format `embedded_[<scope>_]<id>_<lang>`
107
+ * @param {string} pageId the page id
108
+ * @param {string} language the language
109
+ * @param {string} [scope] the scope value
110
+ * @returns {string} the friendly id
111
+ */
112
+ static getPageFriendlyId(pageId, language, scope) {
113
+ const formattedPageId = pageId.replace(/[{}-]/g, '');
114
+ const formattedLanguage = language.replace('-', '_');
115
+ const scopeId = scope ? `${this.normalizeScope(scope)}_` : '';
116
+ return `embedded_${scopeId}${formattedPageId}_${formattedLanguage}`.toLowerCase();
117
+ }
118
+ /**
119
+ * Gets the friendly id for Component A/B Testing in the required format `component_[<scope>_]<pageId>_<componentId>_<language>*`
120
+ * @param {string} pageId the page id
121
+ * @param {string} componentId the component id
122
+ * @param {string} language the language
123
+ * @param {string} [scope] the scope value
124
+ * @returns {string} the friendly id
125
+ */
126
+ static getComponentFriendlyId(pageId, componentId, language, scope) {
127
+ const formattedPageId = pageId.replace(/[{}-]/g, '');
128
+ const formattedComponentId = componentId.replace(/[{}-]/g, '');
129
+ const formattedLanguage = language.replace('-', '_');
130
+ const scopeId = scope ? `${this.normalizeScope(scope)}_` : '';
131
+ return `component_${scopeId}${formattedPageId}_${formattedComponentId}_${formattedLanguage}*`.toLowerCase();
132
+ }
133
+ /**
134
+ * Normalizes the scope from the given string value
135
+ * Removes all non-alphanumeric characters
136
+ * @param {string} [scope] the scope value
137
+ * @returns {string} normalized scope value
138
+ */
139
+ static normalizeScope(scope) {
140
+ return (scope === null || scope === void 0 ? void 0 : scope.replace(/[^a-zA-Z0-9]+/g, '')) || '';
141
+ }
142
+ }
143
+ exports.CdpHelper = CdpHelper;
@@ -0,0 +1,82 @@
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.ErrorPagesService = void 0;
7
+ const debug_1 = __importDefault(require("../debug"));
8
+ const constants_1 = require("../constants");
9
+ // The default query for request error handling
10
+ const defaultQuery = /* GraphQL */ `
11
+ query ErrorPagesQuery($siteName: String!, $language: String!) {
12
+ site {
13
+ siteInfo(site: $siteName) {
14
+ errorHandling(language: $language) {
15
+ notFoundPage {
16
+ rendered
17
+ }
18
+ notFoundPagePath
19
+ serverErrorPage {
20
+ rendered
21
+ }
22
+ serverErrorPagePath
23
+ }
24
+ }
25
+ }
26
+ }
27
+ `;
28
+ /**
29
+ * Service that fetch the error pages data using Sitecore's GraphQL API.
30
+ * @public
31
+ */
32
+ class ErrorPagesService {
33
+ /**
34
+ * Creates an instance of graphQL error pages service with the provided options
35
+ * @param {ErrorPagesServiceConfig} options instance
36
+ */
37
+ constructor(options) {
38
+ this.options = options;
39
+ this.graphQLClient = this.getGraphQLClient();
40
+ }
41
+ get query() {
42
+ return defaultQuery;
43
+ }
44
+ /**
45
+ * Fetch list of error pages for the site
46
+ * @param {string} siteName The site name
47
+ * @param {string} locale The language
48
+ * @param {FetchOptions} [fetchOptions] Options to override graphQL client details like retries and fetch implementation
49
+ * @returns {ErrorPages} list of url's error pages
50
+ * @throws {Error} if the siteName is empty.
51
+ */
52
+ async fetchErrorPages(siteName, locale, fetchOptions) {
53
+ const language = locale || this.options.language;
54
+ if (!siteName) {
55
+ throw new Error(constants_1.siteNameError);
56
+ }
57
+ return this.graphQLClient.request(this.query, {
58
+ siteName,
59
+ language,
60
+ }, fetchOptions)
61
+ .then((result) => result.site.siteInfo ? result.site.siteInfo.errorHandling : null)
62
+ .catch((e) => Promise.reject(e));
63
+ }
64
+ /**
65
+ * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
66
+ * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
67
+ * want to use something else.
68
+ * @returns {GraphQLClient} implementation
69
+ */
70
+ getGraphQLClient() {
71
+ var _a, _b;
72
+ if (!this.options.clientFactory) {
73
+ throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
74
+ }
75
+ return this.options.clientFactory({
76
+ debugger: debug_1.default.errorpages,
77
+ retries: (_a = this.options.retries) === null || _a === void 0 ? void 0 : _a.count,
78
+ retryStrategy: (_b = this.options.retries) === null || _b === void 0 ? void 0 : _b.retryStrategy,
79
+ });
80
+ }
81
+ }
82
+ exports.ErrorPagesService = ErrorPagesService;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SiteResolver = exports.SITE_KEY = exports.SITE_PREFIX = exports.normalizeSiteRewrite = exports.getSiteRewriteData = exports.getSiteRewrite = exports.SitePathService = exports.SiteInfoService = exports.ErrorPagesService = exports.SitemapXmlService = exports.RedirectsService = exports.REDIRECT_TYPE_SERVER_TRANSFER = exports.REDIRECT_TYPE_302 = exports.REDIRECT_TYPE_301 = exports.RobotsService = void 0;
4
+ var robots_service_1 = require("./robots-service");
5
+ Object.defineProperty(exports, "RobotsService", { enumerable: true, get: function () { return robots_service_1.RobotsService; } });
6
+ var redirects_service_1 = require("./redirects-service");
7
+ Object.defineProperty(exports, "REDIRECT_TYPE_301", { enumerable: true, get: function () { return redirects_service_1.REDIRECT_TYPE_301; } });
8
+ Object.defineProperty(exports, "REDIRECT_TYPE_302", { enumerable: true, get: function () { return redirects_service_1.REDIRECT_TYPE_302; } });
9
+ Object.defineProperty(exports, "REDIRECT_TYPE_SERVER_TRANSFER", { enumerable: true, get: function () { return redirects_service_1.REDIRECT_TYPE_SERVER_TRANSFER; } });
10
+ Object.defineProperty(exports, "RedirectsService", { enumerable: true, get: function () { return redirects_service_1.RedirectsService; } });
11
+ var sitemap_xml_service_1 = require("./sitemap-xml-service");
12
+ Object.defineProperty(exports, "SitemapXmlService", { enumerable: true, get: function () { return sitemap_xml_service_1.SitemapXmlService; } });
13
+ var error_pages_service_1 = require("./error-pages-service");
14
+ Object.defineProperty(exports, "ErrorPagesService", { enumerable: true, get: function () { return error_pages_service_1.ErrorPagesService; } });
15
+ var siteinfo_service_1 = require("./siteinfo-service");
16
+ Object.defineProperty(exports, "SiteInfoService", { enumerable: true, get: function () { return siteinfo_service_1.SiteInfoService; } });
17
+ var sitepath_service_1 = require("./sitepath-service");
18
+ Object.defineProperty(exports, "SitePathService", { enumerable: true, get: function () { return sitepath_service_1.SitePathService; } });
19
+ var utils_1 = require("./utils");
20
+ Object.defineProperty(exports, "getSiteRewrite", { enumerable: true, get: function () { return utils_1.getSiteRewrite; } });
21
+ Object.defineProperty(exports, "getSiteRewriteData", { enumerable: true, get: function () { return utils_1.getSiteRewriteData; } });
22
+ Object.defineProperty(exports, "normalizeSiteRewrite", { enumerable: true, get: function () { return utils_1.normalizeSiteRewrite; } });
23
+ Object.defineProperty(exports, "SITE_PREFIX", { enumerable: true, get: function () { return utils_1.SITE_PREFIX; } });
24
+ Object.defineProperty(exports, "SITE_KEY", { enumerable: true, get: function () { return utils_1.SITE_KEY; } });
25
+ var site_resolver_1 = require("./site-resolver");
26
+ Object.defineProperty(exports, "SiteResolver", { enumerable: true, get: function () { return site_resolver_1.SiteResolver; } });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,109 @@
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.RedirectsService = exports.REDIRECT_TYPE_SERVER_TRANSFER = exports.REDIRECT_TYPE_302 = exports.REDIRECT_TYPE_301 = void 0;
7
+ const core_1 = require("@sitecore-content-sdk/core");
8
+ const constants_1 = require("../constants");
9
+ const debug_1 = __importDefault(require("../debug"));
10
+ /**
11
+ * Redirect type for 301 redirects
12
+ * @public
13
+ */
14
+ exports.REDIRECT_TYPE_301 = 'REDIRECT_301';
15
+ /**
16
+ * Redirect type for 302 redirects
17
+ * @public
18
+ */
19
+ exports.REDIRECT_TYPE_302 = 'REDIRECT_302';
20
+ /**
21
+ * Redirect type for server transfer
22
+ * @public
23
+ */
24
+ exports.REDIRECT_TYPE_SERVER_TRANSFER = 'SERVER_TRANSFER';
25
+ // The default query for request redirects of site
26
+ const defaultQuery = /* GraphQL */ `
27
+ query RedirectsQuery($siteName: String!) {
28
+ site {
29
+ siteInfo(site: $siteName) {
30
+ redirects {
31
+ pattern
32
+ target
33
+ redirectType
34
+ isQueryStringPreserved
35
+ isLanguagePreserved
36
+ locale
37
+ }
38
+ }
39
+ }
40
+ }
41
+ `;
42
+ /**
43
+ * The RedirectsService class is used to query the Content SDK redirects using Graphql endpoint
44
+ * @public
45
+ */
46
+ class RedirectsService {
47
+ /**
48
+ * Creates an instance of graphQL redirects service with the provided options
49
+ * @param {RedirectsServiceConfig} options instance
50
+ */
51
+ constructor(options) {
52
+ this.options = options;
53
+ this.graphQLClient = this.getGraphQLClient();
54
+ this.cache = this.getCacheClient();
55
+ }
56
+ get query() {
57
+ return defaultQuery;
58
+ }
59
+ /**
60
+ * Fetch an array of redirects from API
61
+ * @param {string} siteName site name
62
+ * @returns Promise<RedirectInfo[]>
63
+ * @param {FetchOptions} [fetchOptions] Options to override graphQL client details like retries and fetch implementation
64
+ * @throws {Error} if the siteName is empty.
65
+ */
66
+ async fetchRedirects(siteName, fetchOptions) {
67
+ var _a, _b;
68
+ if (!siteName) {
69
+ throw new Error(constants_1.siteNameError);
70
+ }
71
+ const cacheKey = `redirects-${siteName}`;
72
+ let data = this.cache.getCacheValue(cacheKey);
73
+ if (!data) {
74
+ data = await this.graphQLClient.request(this.query, {
75
+ siteName,
76
+ }, fetchOptions);
77
+ this.cache.setCacheValue(cacheKey, data);
78
+ }
79
+ return ((_b = (_a = data === null || data === void 0 ? void 0 : data.site) === null || _a === void 0 ? void 0 : _a.siteInfo) === null || _b === void 0 ? void 0 : _b.redirects) || [];
80
+ }
81
+ /**
82
+ * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
83
+ * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
84
+ * want to use something else.
85
+ * @returns {GraphQLClient} implementation
86
+ */
87
+ getGraphQLClient() {
88
+ if (!this.options.clientFactory) {
89
+ throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
90
+ }
91
+ return this.options.clientFactory({
92
+ debugger: debug_1.default.redirects,
93
+ fetch: this.options.fetch,
94
+ });
95
+ }
96
+ /**
97
+ * Gets cache client implementation
98
+ * Override this method if custom cache needs to be used
99
+ * @returns CacheClient instance
100
+ */
101
+ getCacheClient() {
102
+ var _a, _b;
103
+ return new core_1.MemoryCacheClient({
104
+ cacheEnabled: (_a = this.options.cacheEnabled) !== null && _a !== void 0 ? _a : true,
105
+ cacheTimeout: (_b = this.options.cacheTimeout) !== null && _b !== void 0 ? _b : 10,
106
+ });
107
+ }
108
+ }
109
+ exports.RedirectsService = RedirectsService;
@@ -0,0 +1,74 @@
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.RobotsService = void 0;
7
+ const debug_1 = __importDefault(require("../debug"));
8
+ const constants_1 = require("../constants");
9
+ // The default query for request robots.txt
10
+ const defaultQuery = /* GraphQL */ `
11
+ query RobotsQuery($siteName: String!) {
12
+ site {
13
+ siteInfo(site: $siteName) {
14
+ robots
15
+ }
16
+ }
17
+ }
18
+ `;
19
+ /**
20
+ * Service that fetch the robots.txt data using Sitecore's GraphQL API.
21
+ * @public
22
+ */
23
+ class RobotsService {
24
+ /**
25
+ * Creates an instance of graphQL robots.txt service with the provided options
26
+ * @param {RobotsServiceConfig} options instance
27
+ */
28
+ constructor(options) {
29
+ this.options = options;
30
+ this.graphQLClient = this.getGraphQLClient();
31
+ }
32
+ get query() {
33
+ return defaultQuery;
34
+ }
35
+ /**
36
+ * Fetch a data of robots.txt from API
37
+ * @param {FetchOptions} fetchOptions - The fetch options to be used for the request.
38
+ * @returns text of robots.txt
39
+ * @throws {Error} if the siteName is empty.
40
+ */
41
+ async fetchRobots(fetchOptions) {
42
+ const siteName = this.options.siteName;
43
+ if (!siteName) {
44
+ throw new Error(constants_1.siteNameError);
45
+ }
46
+ const robotsResult = this.graphQLClient.request(this.query, {
47
+ siteName,
48
+ }, fetchOptions);
49
+ try {
50
+ return robotsResult.then((result) => {
51
+ var _a, _b;
52
+ 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;
53
+ });
54
+ }
55
+ catch (e) {
56
+ return Promise.reject(e);
57
+ }
58
+ }
59
+ /**
60
+ * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
61
+ * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
62
+ * want to use something else.
63
+ * @returns {GraphQLClient} implementation
64
+ */
65
+ getGraphQLClient() {
66
+ if (!this.options.clientFactory) {
67
+ throw new Error('clientFactory needs to be provided when initializing GraphQL client.');
68
+ }
69
+ return this.options.clientFactory({
70
+ debugger: debug_1.default.robots,
71
+ });
72
+ }
73
+ }
74
+ exports.RobotsService = RobotsService;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SiteResolver = void 0;
4
+ // Delimiters for multi-value hostnames
5
+ const DELIMITERS = /\||,|;/g;
6
+ /**
7
+ * Resolves site based on the provided host or site name
8
+ * @public
9
+ */
10
+ class SiteResolver {
11
+ /**
12
+ * @param {SiteInfo[]} sites Array of sites to be used in resolution
13
+ */
14
+ constructor(sites) {
15
+ this.sites = sites;
16
+ /**
17
+ * Resolve site by host name
18
+ * @param {string} hostName the host name
19
+ * @returns {SiteInfo} the resolved site
20
+ * @throws {Error} if a matching site is not found
21
+ */
22
+ this.getByHost = (hostName) => {
23
+ for (const [hostname, site] of this.getHostMap()) {
24
+ if (this.matchesPattern(hostName, hostname)) {
25
+ return site;
26
+ }
27
+ }
28
+ throw new Error(`Could not resolve site for host ${hostName}`);
29
+ };
30
+ /**
31
+ * Resolve site by site name
32
+ * @param {string} siteName the site name
33
+ * @returns {SiteInfo} the resolved site or undefined if not found
34
+ */
35
+ this.getByName = (siteName) => {
36
+ const siteInfo = this.sites.find((info) => info.name.toLocaleLowerCase() === siteName.toLocaleLowerCase());
37
+ return siteInfo;
38
+ };
39
+ this.getHostMap = () => {
40
+ const map = new Map();
41
+ // First collect unique hostnames.
42
+ // For sites with same hostname defined, priority is given to the first encountered.
43
+ this.sites.forEach((site) => {
44
+ const hostnames = site.hostName.replace(/\s/g, '').toLocaleLowerCase().split(DELIMITERS);
45
+ hostnames.forEach((hostname) => {
46
+ if (!map.has(hostname)) {
47
+ map.set(hostname, site);
48
+ }
49
+ });
50
+ });
51
+ // Now order by specificity.
52
+ // This equivalates to sorting from longest to shortest with the assumption
53
+ // that your match is less specific as wildcards are introduced.
54
+ // E.g. order.eu.site.com → *.eu.site.com → *.site.com → *
55
+ // In case of a tie (e.g. *.site.com vs i.site.com), prefer one with less wildcards.
56
+ return new Map(Array.from(map).sort((a, b) => {
57
+ if (a[0].length === b[0].length) {
58
+ return (a[0].match(/\*/g) || []).length - (b[0].match(/\*/g) || []).length;
59
+ }
60
+ return b[0].length - a[0].length;
61
+ }));
62
+ };
63
+ }
64
+ // b[0].match(/\*/g) || []).length
65
+ matchesPattern(hostname, pattern) {
66
+ // dots should be treated as chars
67
+ // stars should be treated as wildcards
68
+ const regExpPattern = pattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
69
+ const regExp = new RegExp(`^${regExpPattern}$`, 'gi');
70
+ return !!hostname.match(regExp);
71
+ }
72
+ }
73
+ exports.SiteResolver = SiteResolver;