@sitecore-content-sdk/core 1.2.0-canary.13 → 1.2.0-canary.14

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.
@@ -150,7 +150,10 @@ class SitecoreClient {
150
150
  return null;
151
151
  }
152
152
  // If we're in Pages preview (editing) mode, prefetch the editing data
153
- const { site, itemId, language, version, variantIds, layoutKind, mode } = previewData;
153
+ const { site, itemId, language, version, layoutKind, mode } = previewData;
154
+ const variantIds = Array.isArray(previewData.variantIds)
155
+ ? previewData.variantIds
156
+ : previewData.variantIds.split(',');
154
157
  const data = await this.editingService.fetchEditingData({
155
158
  itemId,
156
159
  language,
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.tryParseEnvValue = exports.mergeURLSearchParams = exports.escapeNonSpecialQuestionMarks = exports.areURLSearchParamsEqual = exports.isRegexOrUrl = exports.getAllowedOriginsFromEnv = exports.enforceCors = exports.isTimeoutError = exports.isAbsoluteUrl = exports.resolveUrl = exports.isServer = void 0;
6
+ exports.tryParseEnvValue = exports.mergeURLSearchParams = exports.escapeNonSpecialQuestionMarks = exports.areURLSearchParamsEqual = exports.isRegexOrUrl = exports.getAllowedOriginsFromEnv = exports.getEnforcedCorsHeaders = exports.enforceCors = exports.isTimeoutError = exports.isAbsoluteUrl = exports.resolveUrl = exports.isServer = void 0;
7
7
  var is_server_1 = require("./is-server");
8
8
  Object.defineProperty(exports, "isServer", { enumerable: true, get: function () { return __importDefault(is_server_1).default; } });
9
9
  var utils_1 = require("./utils");
@@ -11,6 +11,7 @@ Object.defineProperty(exports, "resolveUrl", { enumerable: true, get: function (
11
11
  Object.defineProperty(exports, "isAbsoluteUrl", { enumerable: true, get: function () { return utils_1.isAbsoluteUrl; } });
12
12
  Object.defineProperty(exports, "isTimeoutError", { enumerable: true, get: function () { return utils_1.isTimeoutError; } });
13
13
  Object.defineProperty(exports, "enforceCors", { enumerable: true, get: function () { return utils_1.enforceCors; } });
14
+ Object.defineProperty(exports, "getEnforcedCorsHeaders", { enumerable: true, get: function () { return utils_1.getEnforcedCorsHeaders; } });
14
15
  Object.defineProperty(exports, "getAllowedOriginsFromEnv", { enumerable: true, get: function () { return utils_1.getAllowedOriginsFromEnv; } });
15
16
  Object.defineProperty(exports, "isRegexOrUrl", { enumerable: true, get: function () { return utils_1.isRegexOrUrl; } });
16
17
  Object.defineProperty(exports, "areURLSearchParamsEqual", { enumerable: true, get: function () { return utils_1.areURLSearchParamsEqual; } });
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.mergeURLSearchParams = exports.escapeNonSpecialQuestionMarks = exports.areURLSearchParamsEqual = exports.isRegexOrUrl = exports.enforceCors = exports.getAllowedOriginsFromEnv = exports.isTimeoutError = exports.isAbsoluteUrl = void 0;
6
+ exports.mergeURLSearchParams = exports.escapeNonSpecialQuestionMarks = exports.areURLSearchParamsEqual = exports.isRegexOrUrl = exports.getEnforcedCorsHeaders = exports.enforceCors = exports.getAllowedOriginsFromEnv = exports.isTimeoutError = exports.isAbsoluteUrl = void 0;
7
7
  exports.resolveUrl = resolveUrl;
8
8
  const is_server_1 = __importDefault(require("./is-server"));
9
9
  /**
@@ -94,6 +94,7 @@ exports.getAllowedOriginsFromEnv = getAllowedOriginsFromEnv;
94
94
  * @param {OutgoingMessage} res response to set CORS headers for
95
95
  * @param {string[]} [allowedOrigins] additional list of origins to test against
96
96
  * @returns true if incoming origin matches the allowed lists, false when it does not
97
+ * @deprecated use getEnforcedCorsHeaders instead
97
98
  */
98
99
  const enforceCors = (req, res, allowedOrigins) => {
99
100
  // origin in not present for non-CORS requests (e.g. server-side) - so we skip the checks
@@ -124,6 +125,38 @@ const enforceCors = (req, res, allowedOrigins) => {
124
125
  return false;
125
126
  };
126
127
  exports.enforceCors = enforceCors;
128
+ const getEnforcedCorsHeaders = ({ requestMethod, headers, presetCorsHeader, allowedOrigins = [], }) => {
129
+ // ugly but gotta satisfy both node.js and web fetch Headers interface somehow
130
+ const origin = headers.get
131
+ ? headers.get('origin')
132
+ : headers.origin;
133
+ if (!origin) {
134
+ return {};
135
+ }
136
+ // 3 sources of allowed origins are considered:
137
+ // the env value
138
+ const defaultAllowedOrigins = (0, exports.getAllowedOriginsFromEnv)();
139
+ // the allowedOrigins prop
140
+ allowedOrigins = defaultAllowedOrigins.concat(allowedOrigins || []);
141
+ // and the existing CORS header, if provided (i.e. from nextjs config)
142
+ if (presetCorsHeader) {
143
+ allowedOrigins.push(presetCorsHeader);
144
+ }
145
+ if (origin &&
146
+ allowedOrigins.some((allowedOrigin) => origin === allowedOrigin || new RegExp(convertToWildcardRegex(allowedOrigin)).test(origin))) {
147
+ const corsHeaders = {
148
+ 'Access-Control-Allow-Origin': origin,
149
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, DELETE, PUT, PATCH',
150
+ };
151
+ // set the allowed headers for preflight requests
152
+ if (requestMethod === 'OPTIONS') {
153
+ corsHeaders['Access-Control-Allow-Headers'] = 'Content-Type, Authorization';
154
+ }
155
+ return corsHeaders;
156
+ }
157
+ return null;
158
+ };
159
+ exports.getEnforcedCorsHeaders = getEnforcedCorsHeaders;
127
160
  /**
128
161
  * Determines whether the given input is a regular expression or resembles a URL.
129
162
  * @param {string} input - The input string to evaluate.
@@ -147,7 +147,10 @@ export class SitecoreClient {
147
147
  return null;
148
148
  }
149
149
  // If we're in Pages preview (editing) mode, prefetch the editing data
150
- const { site, itemId, language, version, variantIds, layoutKind, mode } = previewData;
150
+ const { site, itemId, language, version, layoutKind, mode } = previewData;
151
+ const variantIds = Array.isArray(previewData.variantIds)
152
+ ? previewData.variantIds
153
+ : previewData.variantIds.split(',');
151
154
  const data = await this.editingService.fetchEditingData({
152
155
  itemId,
153
156
  language,
@@ -1,3 +1,3 @@
1
1
  export { default as isServer } from './is-server';
2
- export { resolveUrl, isAbsoluteUrl, isTimeoutError, enforceCors, getAllowedOriginsFromEnv, isRegexOrUrl, areURLSearchParamsEqual, escapeNonSpecialQuestionMarks, mergeURLSearchParams, } from './utils';
2
+ export { resolveUrl, isAbsoluteUrl, isTimeoutError, enforceCors, getEnforcedCorsHeaders, getAllowedOriginsFromEnv, isRegexOrUrl, areURLSearchParamsEqual, escapeNonSpecialQuestionMarks, mergeURLSearchParams, } from './utils';
3
3
  export { tryParseEnvValue } from './env';
@@ -84,6 +84,7 @@ export const getAllowedOriginsFromEnv = () => process.env.JSS_ALLOWED_ORIGINS
84
84
  * @param {OutgoingMessage} res response to set CORS headers for
85
85
  * @param {string[]} [allowedOrigins] additional list of origins to test against
86
86
  * @returns true if incoming origin matches the allowed lists, false when it does not
87
+ * @deprecated use getEnforcedCorsHeaders instead
87
88
  */
88
89
  export const enforceCors = (req, res, allowedOrigins) => {
89
90
  // origin in not present for non-CORS requests (e.g. server-side) - so we skip the checks
@@ -113,6 +114,37 @@ export const enforceCors = (req, res, allowedOrigins) => {
113
114
  }
114
115
  return false;
115
116
  };
117
+ export const getEnforcedCorsHeaders = ({ requestMethod, headers, presetCorsHeader, allowedOrigins = [], }) => {
118
+ // ugly but gotta satisfy both node.js and web fetch Headers interface somehow
119
+ const origin = headers.get
120
+ ? headers.get('origin')
121
+ : headers.origin;
122
+ if (!origin) {
123
+ return {};
124
+ }
125
+ // 3 sources of allowed origins are considered:
126
+ // the env value
127
+ const defaultAllowedOrigins = getAllowedOriginsFromEnv();
128
+ // the allowedOrigins prop
129
+ allowedOrigins = defaultAllowedOrigins.concat(allowedOrigins || []);
130
+ // and the existing CORS header, if provided (i.e. from nextjs config)
131
+ if (presetCorsHeader) {
132
+ allowedOrigins.push(presetCorsHeader);
133
+ }
134
+ if (origin &&
135
+ allowedOrigins.some((allowedOrigin) => origin === allowedOrigin || new RegExp(convertToWildcardRegex(allowedOrigin)).test(origin))) {
136
+ const corsHeaders = {
137
+ 'Access-Control-Allow-Origin': origin,
138
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, DELETE, PUT, PATCH',
139
+ };
140
+ // set the allowed headers for preflight requests
141
+ if (requestMethod === 'OPTIONS') {
142
+ corsHeaders['Access-Control-Allow-Headers'] = 'Content-Type, Authorization';
143
+ }
144
+ return corsHeaders;
145
+ }
146
+ return null;
147
+ };
116
148
  /**
117
149
  * Determines whether the given input is a regular expression or resembles a URL.
118
150
  * @param {string} input - The input string to evaluate.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-content-sdk/core",
3
- "version": "1.2.0-canary.13",
3
+ "version": "1.2.0-canary.14",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -85,7 +85,7 @@
85
85
  },
86
86
  "description": "",
87
87
  "types": "types/index.d.ts",
88
- "gitHead": "2a13a10bb66e3d17858479c0889aa6a7fdfdd5bf",
88
+ "gitHead": "87baa0af30c41a696ab78363b2ff0fefd723b6f9",
89
89
  "files": [
90
90
  "dist",
91
91
  "types",
@@ -58,7 +58,7 @@ export type EditingPreviewData = {
58
58
  itemId: string;
59
59
  language: string;
60
60
  mode: Exclude<LayoutServicePageState, 'Normal'>;
61
- variantIds: string[];
61
+ variantIds: string[] | string;
62
62
  version?: string;
63
63
  layoutKind?: LayoutKind;
64
64
  };
@@ -1,3 +1,3 @@
1
1
  export { default as isServer } from './is-server';
2
- export { resolveUrl, isAbsoluteUrl, isTimeoutError, enforceCors, EnhancedOmit, getAllowedOriginsFromEnv, isRegexOrUrl, areURLSearchParamsEqual, escapeNonSpecialQuestionMarks, mergeURLSearchParams, } from './utils';
2
+ export { resolveUrl, isAbsoluteUrl, isTimeoutError, enforceCors, getEnforcedCorsHeaders, EnhancedOmit, getAllowedOriginsFromEnv, isRegexOrUrl, areURLSearchParamsEqual, escapeNonSpecialQuestionMarks, mergeURLSearchParams, } from './utils';
3
3
  export { tryParseEnvValue } from './env';
@@ -1,4 +1,4 @@
1
- import { IncomingMessage, OutgoingMessage } from 'http';
1
+ import { IncomingHttpHeaders, IncomingMessage, OutgoingMessage } from 'http';
2
2
  import { ParsedUrlQueryInput } from 'querystring';
3
3
  /**
4
4
  * Omit properties from T that are in K. This is a simplified version of TypeScript's built-in `Omit` utility type.
@@ -38,8 +38,17 @@ export declare const getAllowedOriginsFromEnv: () => string[];
38
38
  * @param {OutgoingMessage} res response to set CORS headers for
39
39
  * @param {string[]} [allowedOrigins] additional list of origins to test against
40
40
  * @returns true if incoming origin matches the allowed lists, false when it does not
41
+ * @deprecated use getEnforcedCorsHeaders instead
41
42
  */
42
43
  export declare const enforceCors: (req: IncomingMessage, res: OutgoingMessage, allowedOrigins?: string[]) => boolean;
44
+ export declare const getEnforcedCorsHeaders: ({ requestMethod, headers, presetCorsHeader, allowedOrigins, }: {
45
+ requestMethod: string | undefined;
46
+ headers: IncomingHttpHeaders | Headers;
47
+ presetCorsHeader?: string | string[];
48
+ allowedOrigins?: string[];
49
+ }) => {
50
+ [key: string]: string;
51
+ } | null;
43
52
  /**
44
53
  * Determines whether the given input is a regular expression or resembles a URL.
45
54
  * @param {string} input - The input string to evaluate.