@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,189 @@
1
+ import { constants, DefaultRetryStrategy } from '@sitecore-content-sdk/core';
2
+ import { SITECORE_CLI_MODE_ENV_VAR } from '../config-cli';
3
+ const { SITECORE_EDGE_URL_DEFAULT } = constants;
4
+ /**
5
+ * Provides default initial values for SitecoreConfig
6
+ * @returns default config
7
+ */
8
+ export const getFallbackConfig = () => ({
9
+ api: {
10
+ edge: {
11
+ contextId: process.env.SITECORE_EDGE_CONTEXT_ID || '',
12
+ clientContextId: '',
13
+ edgeUrl: process.env.SITECORE_EDGE_URL || SITECORE_EDGE_URL_DEFAULT,
14
+ },
15
+ local: {
16
+ apiKey: process.env.SITECORE_API_KEY || process.env.NEXT_PUBLIC_SITECORE_API_KEY || '',
17
+ apiHost: process.env.SITECORE_API_HOST || process.env.NEXT_PUBLIC_SITECORE_API_HOST || '',
18
+ path: '/sitecore/api/graph/edge',
19
+ },
20
+ },
21
+ editingSecret: process.env.SITECORE_EDITING_SECRET || 'editing-secret-missing',
22
+ retries: {
23
+ count: 3,
24
+ retryStrategy: new DefaultRetryStrategy({
25
+ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524],
26
+ }),
27
+ },
28
+ redirects: {
29
+ enabled: process.env.NODE_ENV !== 'development',
30
+ locales: ['en'],
31
+ },
32
+ multisite: {
33
+ enabled: true,
34
+ useCookieResolution: () => false,
35
+ },
36
+ personalize: {
37
+ enabled: process.env.NODE_ENV !== 'development',
38
+ edgeTimeout: parseInt(process.env.PERSONALIZE_MIDDLEWARE_EDGE_TIMEOUT, 10) || 400,
39
+ cdpTimeout: parseInt(process.env.PERSONALIZE_MIDDLEWARE_CDP_TIMEOUT, 10) || 400,
40
+ scope: '',
41
+ channel: 'WEB',
42
+ currency: 'USD',
43
+ },
44
+ defaultSite: '',
45
+ defaultLanguage: 'en',
46
+ layout: {
47
+ formatLayoutQuery: null,
48
+ },
49
+ dictionary: {
50
+ caching: {
51
+ enabled: true,
52
+ timeout: 60,
53
+ },
54
+ },
55
+ disableCodeGeneration: false,
56
+ });
57
+ /**
58
+ * Deep merge utility that skips undefined or empty string values in the override.
59
+ * @param {T} base base value
60
+ * @param {DeepPartial<T>} [override] override value
61
+ */
62
+ export function deepMerge(base, override) {
63
+ if (!override)
64
+ return base;
65
+ const result = Object.assign({}, base);
66
+ for (const key in override) {
67
+ if (!Object.prototype.hasOwnProperty.call(override, key))
68
+ continue;
69
+ const typedKey = key;
70
+ const baseValue = base[typedKey];
71
+ const overrideValue = override[typedKey];
72
+ // Skip undefined and empty string overrides
73
+ if (overrideValue === undefined || overrideValue === '') {
74
+ continue;
75
+ }
76
+ if (typeof overrideValue === 'object' &&
77
+ overrideValue !== null &&
78
+ !Array.isArray(overrideValue) &&
79
+ Object.getPrototypeOf(overrideValue) === Object.prototype) {
80
+ result[typedKey] = deepMerge(baseValue, overrideValue);
81
+ }
82
+ else {
83
+ result[typedKey] = overrideValue;
84
+ }
85
+ }
86
+ return result;
87
+ }
88
+ /**
89
+ * Resolves sitecore config based on base config and overrides
90
+ * @param {SitecoreConfig} base base sitecore config object
91
+ * @param {SitecoreConfig} override override sitecore config object
92
+ * @returns resolved SitecoreConfig object
93
+ */
94
+ const resolveConfig = (base, override) => {
95
+ const result = deepMerge(base, override);
96
+ if (Number.isNaN(result.personalize.cdpTimeout) || !result.personalize.cdpTimeout) {
97
+ result.personalize.cdpTimeout = base.personalize.cdpTimeout;
98
+ }
99
+ if (Number.isNaN(result.personalize.edgeTimeout) || !result.personalize.edgeTimeout) {
100
+ result.personalize.edgeTimeout = base.personalize.edgeTimeout;
101
+ }
102
+ return result;
103
+ };
104
+ const validateApiConfiguration = (config) => {
105
+ var _a, _b, _c, _d, _e, _f, _g, _h;
106
+ const isBrowser = typeof window !== 'undefined';
107
+ const hasEdgeContextId = !!((_b = (_a = config.api) === null || _a === void 0 ? void 0 : _a.edge) === null || _b === void 0 ? void 0 : _b.contextId);
108
+ const hasClientContextId = !!((_d = (_c = config.api) === null || _c === void 0 ? void 0 : _c.edge) === null || _d === void 0 ? void 0 : _d.clientContextId);
109
+ const hasLocalCreds = !!((_f = (_e = config.api) === null || _e === void 0 ? void 0 : _e.local) === null || _f === void 0 ? void 0 : _f.apiHost) && !!((_h = (_g = config.api) === null || _g === void 0 ? void 0 : _g.local) === null || _h === void 0 ? void 0 : _h.apiKey);
110
+ // Server-side: allow Edge OR Local; clientContextId alone is NOT sufficient
111
+ if (!isBrowser) {
112
+ if (!hasEdgeContextId && !hasLocalCreds) {
113
+ throw new Error('Configuration error: provide either Edge contextId (api.edge.contextId) or local credentials (api.local.apiHost + api.local.apiKey).');
114
+ }
115
+ if (hasEdgeContextId && !hasClientContextId) {
116
+ // eslint-disable-next-line no-console
117
+ if (process.env.NODE_ENV === 'development') {
118
+ console.warn('Warning: only a server-side edge contextId is provided. Client-side requests will require api.edge.clientContextId or a proxy.');
119
+ }
120
+ }
121
+ return; // validation complete on the server
122
+ }
123
+ // Browser-side warning (runs only if contextId exists but clientContextId is missing)
124
+ if (isBrowser && !hasClientContextId) {
125
+ // eslint-disable-next-line no-console
126
+ if (process.env.NODE_ENV === 'development') {
127
+ console.warn(`Warning: clientContextId is missing. The browser will use contextId instead.
128
+ Client Side functionalities (like Tracking and Personalization) may be limited.`);
129
+ }
130
+ }
131
+ };
132
+ /**
133
+ * The validators for the config object during build time.
134
+ * Validators are called when the literal path of the config object is accessed.
135
+ */
136
+ const PROPERTY_VALIDATORS = {
137
+ 'api.edge.contextId': validateApiConfiguration,
138
+ 'api.local.apiKey': validateApiConfiguration,
139
+ };
140
+ /**
141
+ * Creates a proxy for the config object to validate the config object during build time.
142
+ * @param {SitecoreConfig} config - The config object to create a proxy for.
143
+ * @returns {SitecoreConfig} The proxy for the config object.
144
+ */
145
+ const createConfigProxy = (config) => {
146
+ const validated = new Set();
147
+ const createProxy = (target, propPath = '') => {
148
+ return new Proxy(target, {
149
+ get(obj, prop, receiver) {
150
+ // Skip symbol properties, do not attempt to stringify them
151
+ // Type safety check
152
+ if (typeof prop === 'symbol') {
153
+ return Reflect.get(obj, prop, receiver);
154
+ }
155
+ const fullPath = propPath ? `${propPath}.${prop}` : prop;
156
+ const value = Reflect.get(obj, prop, receiver);
157
+ if (fullPath in PROPERTY_VALIDATORS &&
158
+ !validated.has(fullPath)) {
159
+ const validator = PROPERTY_VALIDATORS[fullPath];
160
+ validator(config);
161
+ validated.add(fullPath);
162
+ }
163
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
164
+ return createProxy(value, fullPath);
165
+ }
166
+ return value;
167
+ },
168
+ });
169
+ };
170
+ return createProxy(config);
171
+ };
172
+ /**
173
+ * Accepts a SitecoreConfigInput object and returns full sitecore configuration
174
+ * @param {SitecoreConfigInput} config override values to be written over default config settings
175
+ * @returns {SitecoreConfig} full sitecore configuration to use in application
176
+ * @public
177
+ */
178
+ export const defineConfig = (config) => {
179
+ const resolvedConfig = resolveConfig(getFallbackConfig(), config);
180
+ const isCLI = process.env[SITECORE_CLI_MODE_ENV_VAR] === 'true';
181
+ // At `build time`, we create a proxy for the config object to validate the config by
182
+ // accessing the literal paths instead of validating the whole object at once.
183
+ // At `runtime` all the config should be validated to fail fast in case of invalid configuration.
184
+ if (isCLI) {
185
+ return createConfigProxy(resolvedConfig);
186
+ }
187
+ validateApiConfiguration(resolvedConfig);
188
+ return resolvedConfig;
189
+ };
@@ -0,0 +1,2 @@
1
+ export { ComponentTemplateType, } from './models';
2
+ export { defineConfig } from './define-config';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Enumeration of default component templates
3
+ * @internal
4
+ */
5
+ export var ComponentTemplateType;
6
+ (function (ComponentTemplateType) {
7
+ ComponentTemplateType["BYOC"] = "byoc";
8
+ ComponentTemplateType["DEFAULT"] = "default";
9
+ })(ComponentTemplateType || (ComponentTemplateType = {}));
@@ -0,0 +1,19 @@
1
+ const validateConfig = (cliConfig) => {
2
+ var _a, _b, _c, _d;
3
+ if (!((_b = (_a = cliConfig.build) === null || _a === void 0 ? void 0 : _a.commands) === null || _b === void 0 ? void 0 : _b.length)) {
4
+ throw new Error('Configuration error: build commands should be defined in sitecore.cli.config');
5
+ }
6
+ if (!((_d = (_c = cliConfig.scaffold) === null || _c === void 0 ? void 0 : _c.templates) === null || _d === void 0 ? void 0 : _d.length)) {
7
+ throw new Error('Configuration error: scaffold templates should be defined in sitecore.cli.config');
8
+ }
9
+ };
10
+ /**
11
+ * Accepts a `SitecoreCliConfigInput` object and returns Sitecore Content SDK CLI configuration, updated with required default values
12
+ * @param {SitecoreCliConfigInput} cliConfig the cli configuration provided by the application
13
+ * @returns {SitecoreCliConfig} full sitecore cli configuration to use with cli
14
+ * @public
15
+ */
16
+ export const defineCliConfig = (cliConfig) => {
17
+ validateConfig(cliConfig);
18
+ return cliConfig;
19
+ };
@@ -0,0 +1,2 @@
1
+ export { defineCliConfig } from './define-cli-config';
2
+ export { SITECORE_CLI_MODE_ENV_VAR } from './models';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Environment variable to indicate that the application is running in CLI mode
3
+ * @internal
4
+ */
5
+ export const SITECORE_CLI_MODE_ENV_VAR = 'SITECORE_CLI_MODE';
@@ -0,0 +1,9 @@
1
+ /** @private */
2
+ export const siteNameError = 'The site name must be a non-empty string';
3
+ /** @private */
4
+ export const languageError = 'The language must be a non-empty string';
5
+ /**
6
+ * Hidden rendering name
7
+ * @internal
8
+ */
9
+ export const HIDDEN_RENDERING_NAME = 'Hidden Rendering';
@@ -0,0 +1,19 @@
1
+ import { debugNamespace, debugModule } from '@sitecore-content-sdk/core';
2
+ /**
3
+ * Debug module for content package
4
+ * @public
5
+ */
6
+ const debug = {
7
+ form: debugModule(`${debugNamespace}:form`),
8
+ layout: debugModule(`${debugNamespace}:layout`),
9
+ dictionary: debugModule(`${debugNamespace}:dictionary`),
10
+ editing: debugModule(`${debugNamespace}:editing`),
11
+ sitemap: debugModule(`${debugNamespace}:sitemap`),
12
+ multisite: debugModule(`${debugNamespace}:multisite`),
13
+ robots: debugModule(`${debugNamespace}:robots`),
14
+ redirects: debugModule(`${debugNamespace}:redirects`),
15
+ personalize: debugModule(`${debugNamespace}:personalize`),
16
+ locale: debugModule(`${debugNamespace}:locale`),
17
+ errorpages: debugModule(`${debugNamespace}:errorpages`),
18
+ };
19
+ export default debug;
@@ -0,0 +1 @@
1
+ export { addComponentPreviewHandler, addServerComponentPreviewHandler, getDesignLibraryComponentPreviewErrorEvent, getDesignLibraryComponentPropsEvent, getDesignLibraryImportMapEvent, getImportMapInfo, addStyleElement, createComponentInstance, sendErrorEvent, DesignLibraryPreviewError, } from './preview';
@@ -0,0 +1,263 @@
1
+ import { validateEvent } from '../design-library';
2
+ /**
3
+ * Event to send import map to design library
4
+ */
5
+ const DESIGN_LIBRARY_IMPORT_MAP_EVENT_NAME = 'component:generation:import-map';
6
+ /**
7
+ * Event to send component props to design library
8
+ */
9
+ const DESIGN_LIBRARY_COMPONENT_PROPS_EVENT_NAME = 'component:generation:component-props';
10
+ /**
11
+ * Event to receive component data from design library
12
+ */
13
+ const DESIGN_LIBRARY_COMPONENT_PREVIEW_EVENT_NAME = 'component:generation:component-preview';
14
+ /**
15
+ * Event to send component error to design library
16
+ */
17
+ const DESIGN_LIBRARY_COMPONENT_PREVIEW_ERROR_EVENT_NAME = 'component:generation:component-preview-error';
18
+ /**
19
+ * Enumeration of error types for the design library preview.
20
+ * @internal
21
+ */
22
+ export var DesignLibraryPreviewError;
23
+ (function (DesignLibraryPreviewError) {
24
+ /**
25
+ * Error occurred during component rendering.
26
+ */
27
+ DesignLibraryPreviewError["Render"] = "render";
28
+ /**
29
+ * Error occurred during component and event handlers initialization.
30
+ */
31
+ DesignLibraryPreviewError["RenderInit"] = "render-init";
32
+ })(DesignLibraryPreviewError || (DesignLibraryPreviewError = {}));
33
+ /**
34
+ * Builds the component dependencies from the component imports and the import map.
35
+ * @param {ComponentImport[]} componentImports - The component imports.
36
+ * @param {ImportEntry[]} importMap - The import map to be used for the component.
37
+ * @returns {Array<{ name: string; value: unknown }>} The component dependencies.
38
+ */
39
+ export function buildComponentDependencies(componentImports, importMap) {
40
+ const successful = [];
41
+ const missing = {
42
+ modules: [],
43
+ exports: [],
44
+ };
45
+ componentImports.forEach((componentImport) => {
46
+ const moduleEntry = importMap.find((entry) => entry.module === componentImport.module);
47
+ if (!moduleEntry) {
48
+ missing.modules.push({
49
+ module: componentImport.module,
50
+ alias: componentImport.alias,
51
+ });
52
+ return;
53
+ }
54
+ const exportEntry = moduleEntry.exports.find((exp) => exp.name === componentImport.export);
55
+ if (!exportEntry) {
56
+ missing.exports.push({
57
+ alias: componentImport.alias,
58
+ export: componentImport.export,
59
+ module: componentImport.module,
60
+ });
61
+ return;
62
+ }
63
+ return successful.push({
64
+ name: componentImport.alias,
65
+ value: exportEntry.value,
66
+ });
67
+ });
68
+ return {
69
+ successful,
70
+ missing,
71
+ };
72
+ }
73
+ /**
74
+ * Adds the browser-side event handler for 'component:generation:component-preview' message used in Design Library
75
+ * The event should contain the component code, styles and imports.
76
+ * @param {ImportEntry[]} importMap - The import map to be used for the component.
77
+ * @param {Function} callback callback to be called after component is received
78
+ * @internal
79
+ */
80
+ export const addComponentPreviewHandler = (importMap, callback) => {
81
+ if (!window)
82
+ return;
83
+ const handler = (e) => {
84
+ const eventArgs = e.data;
85
+ try {
86
+ if (!validateEvent(e, DESIGN_LIBRARY_COMPONENT_PREVIEW_EVENT_NAME)) {
87
+ return;
88
+ }
89
+ console.debug('Component Library: message received', eventArgs);
90
+ const Component = createComponentInstance(importMap, eventArgs);
91
+ addStyleElement(eventArgs.message.styles.content);
92
+ callback(null, Component);
93
+ }
94
+ catch (error) {
95
+ sendErrorEvent(eventArgs.message.uid, error, DesignLibraryPreviewError.RenderInit);
96
+ callback(error, null);
97
+ }
98
+ };
99
+ window.addEventListener('message', handler);
100
+ const unsubscribe = () => {
101
+ window.removeEventListener('message', handler);
102
+ };
103
+ return unsubscribe;
104
+ };
105
+ /**
106
+ * Adds the browser-side event handler for 'component:generation:component-preview' message used in Design Library for server components
107
+ * The event should contain the component code, styles and imports.
108
+ * @param {Function} callback callback to be called after component is received
109
+ * @internal
110
+ */
111
+ export const addServerComponentPreviewHandler = (callback) => {
112
+ const handler = (e) => {
113
+ if (!validateEvent(e, DESIGN_LIBRARY_COMPONENT_PREVIEW_EVENT_NAME)) {
114
+ return;
115
+ }
116
+ console.debug('Component Library: message received', e.data);
117
+ callback(e.data);
118
+ };
119
+ window.addEventListener('message', handler);
120
+ const unsubscribe = () => {
121
+ window.removeEventListener('message', handler);
122
+ };
123
+ return unsubscribe;
124
+ };
125
+ /**
126
+ * Adds <style> element in the document head with the provided CSS.
127
+ * If an existing style element with the id "content-sdk-style-preview" is found, it is removed
128
+ * to prevent duplicates
129
+ * @param {string} stylesContent - The raw CSS text to inject into the style element.
130
+ * @internal
131
+ */
132
+ export function addStyleElement(stylesContent) {
133
+ const styleId = 'content-sdk-style-preview';
134
+ const styleElement = document.getElementById(styleId);
135
+ // remove existing style element if it exists to avoid duplicates
136
+ if (styleElement) {
137
+ styleElement.remove();
138
+ }
139
+ // create new style element and attach it to DOM
140
+ const style = document.createElement('style');
141
+ style.setAttribute('id', styleId);
142
+ style.innerHTML = stylesContent;
143
+ document.head.appendChild(style);
144
+ }
145
+ /**
146
+ * Dynamically creates a React component instance from provided importMap and from code, styles, and dependencies provided in the preview event.
147
+ * @param {ImportEntry[]} importMap - The import map containing module and export references that might be injected as dependencies in the provided code.
148
+ * @param {ComponentPreviewEventArgs} previewEventArgs - The event arguments containing the component code, styles, and import definitions.
149
+ * @returns The dynamically created React component instance.
150
+ * @throws If any required modules or exports are missing from the import map, an error is thrown describing the missing dependencies.
151
+ * @internal
152
+ */
153
+ export const createComponentInstance = (importMap, previewEventArgs) => {
154
+ const { message } = previewEventArgs;
155
+ const dependencies = buildComponentDependencies(message.imports, importMap);
156
+ if (dependencies.missing.modules.length > 0 || dependencies.missing.exports.length > 0) {
157
+ let errorMessage = '';
158
+ dependencies.missing.modules.forEach((mod) => {
159
+ errorMessage += `Missing module: '${mod.module}' with alias: '${mod.alias}'\n`;
160
+ });
161
+ dependencies.missing.exports.forEach((exp) => {
162
+ const alias = exp.export !== exp.alias ? ` with alias: '${exp.alias}'` : '';
163
+ errorMessage += `Missing export: '${exp.export}' from module: '${exp.module}'${alias}\n`;
164
+ });
165
+ throw errorMessage;
166
+ }
167
+ const importNames = dependencies.successful.map((entry) => entry.name);
168
+ const importInstances = dependencies.successful.map((entry) => entry.value);
169
+ const exports = { Component: null };
170
+ const componentFn = new Function('exports', message.styles.styleImport.name, ...importNames, message.code.content);
171
+ // Function will set exports.Component
172
+ componentFn(exports, message.styles.styleImport.content, ...importInstances);
173
+ return exports.Component;
174
+ };
175
+ /**
176
+ * Generates a DesignLibraryComponentPreviewErrorEvent with the given uid and error.
177
+ * @param {string} uid - The unique identifier for the event.
178
+ * @param {unknown} error - The error to be sent.
179
+ * @param {DesignLibraryPreviewError} type - The type of error.
180
+ * @returns An object representing the DesignLibraryComponentPreviewErrorEvent.
181
+ * @internal
182
+ */
183
+ export function getDesignLibraryComponentPreviewErrorEvent(uid, error, type) {
184
+ return {
185
+ name: DESIGN_LIBRARY_COMPONENT_PREVIEW_ERROR_EVENT_NAME,
186
+ message: { uid, error, type },
187
+ };
188
+ }
189
+ /**
190
+ * Generates a DesignLibraryComponentPropsEvent with the given uid, fields and parameters.
191
+ * @param {string} uid - The unique identifier for the event.
192
+ * @param {ComponentFields} fields - The fields of the component.
193
+ * @param {ComponentParams} parameters - The parameters of the component.
194
+ * @returns An object representing the DesignLibraryComponentPropsEvent.
195
+ * @internal
196
+ */
197
+ export function getDesignLibraryComponentPropsEvent(uid, fields = {}, parameters = {}) {
198
+ return {
199
+ name: DESIGN_LIBRARY_COMPONENT_PROPS_EVENT_NAME,
200
+ message: {
201
+ uid,
202
+ fields,
203
+ parameters,
204
+ },
205
+ };
206
+ }
207
+ /**
208
+ * Generates a DesignLibraryImportMapEvent with the given uid and importMap.
209
+ * @param {string} uid - The unique identifier for the event.
210
+ * @param {ImportEntry[]} importMap - The imports map to be sent.
211
+ * @returns An object representing the DesignLibraryImportMapEvent.
212
+ * @internal
213
+ */
214
+ export function getDesignLibraryImportMapEvent(uid, importMap) {
215
+ const importMapPayload = isImportEntryInfoArray(importMap)
216
+ ? importMap
217
+ : getImportMapInfo(importMap);
218
+ return {
219
+ name: DESIGN_LIBRARY_IMPORT_MAP_EVENT_NAME,
220
+ message: {
221
+ uid,
222
+ importMap: importMapPayload,
223
+ },
224
+ };
225
+ }
226
+ /**
227
+ * Generates the payload for the import map to be sent to design library.
228
+ * @param {ImportEntry[]} importMap - The imports map to be sent.
229
+ * @internal
230
+ */
231
+ export function getImportMapInfo(importMap) {
232
+ return importMap.map((entry) => ({
233
+ module: entry.module,
234
+ exports: entry.exports.map((exp) => exp.name),
235
+ }));
236
+ }
237
+ /**
238
+ * Type guard for ImportEntryInfo[]
239
+ * @param {unknown} data import entry data to check
240
+ * @returns true if the data is ImportEntryInfo array
241
+ */
242
+ export function isImportEntryInfoArray(data) {
243
+ return (Array.isArray(data) &&
244
+ data.length > 0 &&
245
+ typeof data[0].module === 'string' &&
246
+ Array.isArray(data[0].exports) &&
247
+ typeof data[0].exports[0] === 'string');
248
+ }
249
+ /**
250
+ * Sends a component preview error event to the design library
251
+ * @param {string} uid - The unique identifier of the component that's being edited.
252
+ * @param {unknown} error - The error object or message to be sent.
253
+ * @param {DesignLibraryPreviewError} type - The type of error, as defined in DesignLibraryPreviewError.
254
+ * @internal
255
+ */
256
+ export const sendErrorEvent = (uid, error, type) => {
257
+ const errorEvent = getDesignLibraryComponentPreviewErrorEvent(uid, error, type);
258
+ console.error('Component Library: sending error event', errorEvent);
259
+ if (typeof window !== 'undefined') {
260
+ const target = window.parent && window.parent !== window ? window.parent : window;
261
+ target.postMessage(errorEvent, '*');
262
+ }
263
+ };
@@ -0,0 +1,55 @@
1
+ import { NativeDataFetcher, constants } from '@sitecore-content-sdk/core';
2
+ import { resolveUrl } from '@sitecore-content-sdk/core/tools';
3
+ import debug from '../debug';
4
+ import { DesignLibraryMode } from './models';
5
+ const { SITECORE_EDGE_URL_DEFAULT } = constants;
6
+ /**
7
+ * REST service that enables Design Library functionality.
8
+ * Returns layout data for a single rendered component.
9
+ * @public
10
+ */
11
+ export class ComponentLayoutService {
12
+ constructor(config) {
13
+ this.config = config;
14
+ }
15
+ fetchComponentData(params, fetchOptions) {
16
+ // Choose the correct Edge ID per environment
17
+ const sitecoreContextId = this.config.contextId || this.config.clientContextId;
18
+ if (!sitecoreContextId) {
19
+ throw new Error(`ComponentLayoutService misconfigured: contextId is missing.
20
+ Provide contextId on the server, and clientContextId in the browser if you need to full client-side functionality.`);
21
+ }
22
+ const fetcher = new NativeDataFetcher({ debugger: debug.layout });
23
+ debug.layout('fetching component with uid %s for %s %s %s %s', params.componentUid, params.itemId, params.language, params.siteName, params.dataSourceId);
24
+ return fetcher
25
+ .get(this.getFetchUrl(params), Object.assign(Object.assign({}, fetchOptions), { headers: Object.assign(Object.assign({}, fetchOptions === null || fetchOptions === void 0 ? void 0 : fetchOptions.headers), { 'x-sitecore-contextid': sitecoreContextId, sc_editMode: `${params.mode === DesignLibraryMode.Metadata}` }) }))
26
+ .then((response) => response.data)
27
+ .catch((error) => {
28
+ var _a;
29
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
30
+ return error.response.data;
31
+ }
32
+ throw error;
33
+ });
34
+ }
35
+ getComponentFetchParams(params) {
36
+ // strip undefined fields
37
+ return JSON.parse(JSON.stringify({
38
+ item: params.itemId,
39
+ uid: params.componentUid,
40
+ dataSourceId: params.dataSourceId,
41
+ renderingItemId: params.renderingId,
42
+ version: params.version,
43
+ sc_site: params.siteName,
44
+ sc_lang: params.language || 'en',
45
+ }));
46
+ }
47
+ /**
48
+ * Get the fetch URL for the partial layout data endpoint
49
+ * @param {ComponentLayoutRequestParams} params - The parameters for the request
50
+ * @returns {string} The fetch URL for the component data
51
+ */
52
+ getFetchUrl(params) {
53
+ return resolveUrl(`${this.config.edgeUrl || SITECORE_EDGE_URL_DEFAULT}/layout/component`, this.getComponentFetchParams(params));
54
+ }
55
+ }