@sitecore-content-sdk/core 0.1.0-beta.7 → 0.1.0-beta.9

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 (66) hide show
  1. package/config.d.ts +1 -0
  2. package/config.js +1 -0
  3. package/dist/cjs/config/define-config.js +106 -0
  4. package/dist/cjs/config/index.js +5 -0
  5. package/dist/cjs/config/models.js +2 -0
  6. package/dist/cjs/data-fetcher.js +3 -14
  7. package/dist/cjs/editing/graphql-editing-service.js +55 -68
  8. package/dist/cjs/graphql/app-root-query.js +21 -32
  9. package/dist/cjs/graphql/index.js +3 -2
  10. package/dist/cjs/graphql/search-service.js +18 -29
  11. package/dist/cjs/graphql-request-client.js +41 -90
  12. package/dist/cjs/i18n/graphql-dictionary-service.js +69 -55
  13. package/dist/cjs/i18n/index.js +1 -3
  14. package/dist/cjs/index.js +5 -2
  15. package/dist/cjs/layout/graphql-layout-service.js +8 -19
  16. package/dist/cjs/native-fetcher.js +55 -76
  17. package/dist/cjs/personalize/graphql-personalize-service.js +25 -36
  18. package/dist/cjs/retries.js +42 -0
  19. package/dist/cjs/site/graphql-error-pages-service.js +12 -23
  20. package/dist/cjs/site/graphql-redirects-service.js +14 -25
  21. package/dist/cjs/site/graphql-robots-service.js +16 -27
  22. package/dist/cjs/site/graphql-siteinfo-service.js +25 -38
  23. package/dist/cjs/site/graphql-sitemap-service.js +17 -30
  24. package/dist/cjs/site/models.js +2 -0
  25. package/dist/esm/config/define-config.js +101 -0
  26. package/dist/esm/config/index.js +1 -0
  27. package/dist/esm/config/models.js +1 -0
  28. package/dist/esm/data-fetcher.js +3 -14
  29. package/dist/esm/editing/graphql-editing-service.js +55 -68
  30. package/dist/esm/graphql/app-root-query.js +21 -32
  31. package/dist/esm/graphql/index.js +2 -1
  32. package/dist/esm/graphql/search-service.js +18 -29
  33. package/dist/esm/graphql-request-client.js +39 -87
  34. package/dist/esm/i18n/graphql-dictionary-service.js +69 -55
  35. package/dist/esm/i18n/index.js +0 -1
  36. package/dist/esm/index.js +3 -1
  37. package/dist/esm/layout/graphql-layout-service.js +8 -19
  38. package/dist/esm/native-fetcher.js +55 -76
  39. package/dist/esm/personalize/graphql-personalize-service.js +25 -36
  40. package/dist/esm/retries.js +38 -0
  41. package/dist/esm/site/graphql-error-pages-service.js +12 -23
  42. package/dist/esm/site/graphql-redirects-service.js +14 -25
  43. package/dist/esm/site/graphql-robots-service.js +16 -27
  44. package/dist/esm/site/graphql-siteinfo-service.js +25 -38
  45. package/dist/esm/site/graphql-sitemap-service.js +17 -30
  46. package/dist/esm/site/index.js +1 -1
  47. package/dist/esm/site/models.js +1 -0
  48. package/package.json +3 -3
  49. package/types/config/define-config.d.ts +12 -0
  50. package/types/config/index.d.ts +2 -0
  51. package/types/config/models.d.ts +120 -0
  52. package/types/graphql/index.d.ts +3 -1
  53. package/types/graphql-request-client.d.ts +2 -47
  54. package/types/i18n/graphql-dictionary-service.d.ts +40 -7
  55. package/types/i18n/index.d.ts +1 -2
  56. package/types/index.d.ts +4 -2
  57. package/types/layout/graphql-layout-service.d.ts +3 -12
  58. package/types/models.d.ts +33 -0
  59. package/types/retries.d.ts +24 -0
  60. package/types/site/graphql-siteinfo-service.d.ts +1 -18
  61. package/types/site/index.d.ts +2 -1
  62. package/types/site/models.d.ts +18 -0
  63. package/types/site/site-resolver.d.ts +1 -1
  64. package/dist/cjs/i18n/dictionary-service.js +0 -45
  65. package/dist/esm/i18n/dictionary-service.js +0 -41
  66. package/types/i18n/dictionary-service.d.ts +0 -56
package/config.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './types/config/index';
package/config.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/cjs/config/index');
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineConfig = exports.getFallbackConfig = void 0;
4
+ const constants_1 = require("../constants");
5
+ const retries_1 = require("../retries");
6
+ /**
7
+ * Provides default initial values for SitecoreConfig
8
+ * @returns default config
9
+ */
10
+ const getFallbackConfig = () => ({
11
+ api: {
12
+ edge: {
13
+ contextId: '',
14
+ clientContextId: '',
15
+ edgeUrl: constants_1.SITECORE_EDGE_URL_DEFAULT,
16
+ },
17
+ local: {
18
+ apiKey: '',
19
+ apiHost: '',
20
+ path: '/sitecore/api/graph/edge',
21
+ },
22
+ },
23
+ editingSecret: 'editing-secret-missing',
24
+ retries: {
25
+ count: 3,
26
+ retryStrategy: new retries_1.DefaultRetryStrategy({
27
+ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524],
28
+ }),
29
+ },
30
+ redirects: {
31
+ enabled: process.env.NODE_ENV !== 'development',
32
+ locales: ['en'],
33
+ },
34
+ multisite: {
35
+ enabled: true,
36
+ defaultHostname: '',
37
+ useCookieResolution: () => false,
38
+ },
39
+ personalize: {
40
+ enabled: process.env.NODE_ENV !== 'development',
41
+ edgeTimeout: 400,
42
+ cdpTimeout: 400,
43
+ scope: undefined,
44
+ channel: 'WEB',
45
+ currency: 'USD',
46
+ },
47
+ defaultSite: '',
48
+ defaultLanguage: 'en',
49
+ layout: {
50
+ formatLayoutQuery: null,
51
+ },
52
+ dictionary: {
53
+ caching: {
54
+ enabled: true,
55
+ timeout: 60,
56
+ },
57
+ },
58
+ });
59
+ exports.getFallbackConfig = getFallbackConfig;
60
+ /**
61
+ * Merges two SitecoreConfig objects
62
+ * @param {SitecoreConfig} base base sitecore config object
63
+ * @param {SitecoreConfig} override override sitecore config object
64
+ * @returns merged SitecoreConfig object
65
+ */
66
+ const deepMerge = (base, override) => {
67
+ var _a, _b, _c, _d, _e;
68
+ const result = Object.assign(Object.assign(Object.assign({}, base), override), { api: {
69
+ edge: Object.assign(Object.assign({}, (_a = base.api) === null || _a === void 0 ? void 0 : _a.edge), (_b = override.api) === null || _b === void 0 ? void 0 : _b.edge),
70
+ local: Object.assign(Object.assign({}, (_c = base.api) === null || _c === void 0 ? void 0 : _c.local), (_d = override.api) === null || _d === void 0 ? void 0 : _d.local),
71
+ }, retries: Object.assign(Object.assign({}, base.retries), override.retries), layout: Object.assign(Object.assign({}, base.layout), override.layout), multisite: Object.assign(Object.assign({}, base.multisite), override.multisite), personalize: Object.assign(Object.assign({}, base.personalize), override.personalize), redirects: Object.assign(Object.assign({}, base.redirects), override.redirects), dictionary: Object.assign(Object.assign({}, base.dictionary), override.dictionary) });
72
+ if (Number.isNaN(result.personalize.cdpTimeout)) {
73
+ result.personalize.cdpTimeout = base.personalize.cdpTimeout;
74
+ }
75
+ if (Number.isNaN(result.personalize.edgeTimeout)) {
76
+ result.personalize.edgeTimeout = base.personalize.edgeTimeout;
77
+ }
78
+ // fallback in case only one context provided
79
+ if (((_e = result.api.edge) === null || _e === void 0 ? void 0 : _e.clientContextId) && !result.api.edge.contextId) {
80
+ result.api.edge.contextId = result.api.edge.clientContextId;
81
+ }
82
+ return result;
83
+ };
84
+ const validateConfig = (config) => {
85
+ var _a, _b, _c, _d, _e, _f, _g, _h;
86
+ if (!((_b = (_a = config.api) === null || _a === void 0 ? void 0 : _a.edge) === null || _b === void 0 ? void 0 : _b.contextId) &&
87
+ (!((_d = (_c = config === null || config === void 0 ? void 0 : config.api) === null || _c === void 0 ? void 0 : _c.local) === null || _d === void 0 ? void 0 : _d.apiHost) || !((_f = (_e = config === null || config === void 0 ? void 0 : config.api) === null || _e === void 0 ? void 0 : _e.local) === null || _f === void 0 ? void 0 : _f.apiKey))) {
88
+ // consider client-side usecase
89
+ if (!((_h = (_g = config.api) === null || _g === void 0 ? void 0 : _g.edge) === null || _h === void 0 ? void 0 : _h.clientContextId)) {
90
+ throw new Error('Configuration error: either context ID or API key and host must be specified in sitecore.config');
91
+ }
92
+ }
93
+ if (!config.defaultSite) {
94
+ throw new Error('Configuration error: defaultSite value should be defined in sitecore.config');
95
+ }
96
+ };
97
+ /**
98
+ * Accepts a SitecoreConfigInput object and returns full sitecore configuration
99
+ * @param {SitecoreConfigInput} config override values to be written over default config settings
100
+ * @returns {SitecoreConfig} full sitecore configuration to use in application
101
+ */
102
+ const defineConfig = (config) => {
103
+ validateConfig(config);
104
+ return deepMerge((0, exports.getFallbackConfig)(), config);
105
+ };
106
+ exports.defineConfig = defineConfig;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineConfig = void 0;
4
+ var define_config_1 = require("./define-config");
5
+ Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return define_config_1.defineConfig; } });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.ResponseError = void 0;
13
4
  exports.fetchData = fetchData;
@@ -25,9 +16,7 @@ exports.ResponseError = ResponseError;
25
16
  * @param {HttpDataFetcher<T> | NativeDataFetcherFunction<T>} fetcher the fetcher to use to perform the request
26
17
  * @param {ParsedUrlQueryInput} params the query string parameters to send with the request
27
18
  */
28
- function fetchData(url_1, fetcher_1) {
29
- return __awaiter(this, arguments, void 0, function* (url, fetcher, params = {}) {
30
- const response = yield fetcher((0, utils_1.resolveUrl)(url, params));
31
- return response.data;
32
- });
19
+ async function fetchData(url, fetcher, params = {}) {
20
+ const response = await fetcher((0, utils_1.resolveUrl)(url, params));
21
+ return response.data;
33
22
  }
@@ -1,13 +1,4 @@
1
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
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -100,72 +91,68 @@ class GraphQLEditingService {
100
91
  * @param {LayoutKind} [variables.layoutKind] - The final or shared layout variant.
101
92
  * @returns {Promise} The layout data and dictionary phrases.
102
93
  */
103
- fetchEditingData(_a) {
104
- return __awaiter(this, arguments, void 0, function* ({ siteName, itemId, language, version, layoutKind = models_1.LayoutKind.Final, }) {
105
- var _b, _c, _d;
106
- debug_1.default.editing('fetching editing data for %s %s %s %s', siteName, itemId, language, version, layoutKind);
107
- if (!siteName) {
108
- throw new RangeError('The site name must be a non-empty string');
109
- }
110
- if (!language) {
111
- throw new RangeError('The language must be a non-empty string');
112
- }
113
- let dictionaryResults = [];
114
- let hasNext = true;
115
- let after = '';
116
- const editingData = yield this.graphQLClient.request(exports.query, {
94
+ async fetchEditingData({ siteName, itemId, language, version, layoutKind = models_1.LayoutKind.Final, }) {
95
+ var _a, _b, _c;
96
+ debug_1.default.editing('fetching editing data for %s %s %s %s', siteName, itemId, language, version, layoutKind);
97
+ if (!siteName) {
98
+ throw new RangeError('The site name must be a non-empty string');
99
+ }
100
+ if (!language) {
101
+ throw new RangeError('The language must be a non-empty string');
102
+ }
103
+ let dictionaryResults = [];
104
+ let hasNext = true;
105
+ let after = '';
106
+ const editingData = await this.graphQLClient.request(exports.query, {
107
+ siteName,
108
+ itemId,
109
+ version,
110
+ language,
111
+ }, {
112
+ headers: {
113
+ sc_layoutKind: layoutKind,
114
+ },
115
+ });
116
+ if ((_b = (_a = editingData === null || editingData === void 0 ? void 0 : editingData.site) === null || _a === void 0 ? void 0 : _a.siteInfo) === null || _b === void 0 ? void 0 : _b.dictionary) {
117
+ dictionaryResults = editingData.site.siteInfo.dictionary.results;
118
+ hasNext = editingData.site.siteInfo.dictionary.pageInfo.hasNext;
119
+ after = editingData.site.siteInfo.dictionary.pageInfo.endCursor;
120
+ }
121
+ else {
122
+ hasNext = false;
123
+ }
124
+ const dictionary = await this.fetchDictionaryData({ siteName, language }, dictionaryResults, hasNext, after);
125
+ return {
126
+ layoutData: ((_c = editingData === null || editingData === void 0 ? void 0 : editingData.item) === null || _c === void 0 ? void 0 : _c.rendered) || {
127
+ sitecore: {
128
+ context: { pageEditing: true, language },
129
+ route: null,
130
+ },
131
+ },
132
+ dictionary,
133
+ };
134
+ }
135
+ async fetchDictionaryData({ siteName, language, }, initDictionary = [], hasNext = true, after) {
136
+ var _a, _b;
137
+ let dictionaryResults = initDictionary;
138
+ const dictionary = {};
139
+ while (hasNext) {
140
+ const data = await this.graphQLClient.request(exports.dictionaryQuery, {
117
141
  siteName,
118
- itemId,
119
- version,
120
142
  language,
121
- }, {
122
- headers: {
123
- sc_layoutKind: layoutKind,
124
- },
143
+ after,
125
144
  });
126
- if ((_c = (_b = editingData === null || editingData === void 0 ? void 0 : editingData.site) === null || _b === void 0 ? void 0 : _b.siteInfo) === null || _c === void 0 ? void 0 : _c.dictionary) {
127
- dictionaryResults = editingData.site.siteInfo.dictionary.results;
128
- hasNext = editingData.site.siteInfo.dictionary.pageInfo.hasNext;
129
- after = editingData.site.siteInfo.dictionary.pageInfo.endCursor;
145
+ if ((_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.dictionary) {
146
+ dictionaryResults = dictionaryResults.concat(data.site.siteInfo.dictionary.results);
147
+ hasNext = data.site.siteInfo.dictionary.pageInfo.hasNext;
148
+ after = data.site.siteInfo.dictionary.pageInfo.endCursor;
130
149
  }
131
150
  else {
132
151
  hasNext = false;
133
152
  }
134
- const dictionary = yield this.fetchDictionaryData({ siteName, language }, dictionaryResults, hasNext, after);
135
- return {
136
- layoutData: ((_d = editingData === null || editingData === void 0 ? void 0 : editingData.item) === null || _d === void 0 ? void 0 : _d.rendered) || {
137
- sitecore: {
138
- context: { pageEditing: true, language },
139
- route: null,
140
- },
141
- },
142
- dictionary,
143
- };
144
- });
145
- }
146
- fetchDictionaryData(_a) {
147
- return __awaiter(this, arguments, void 0, function* ({ siteName, language, }, initDictionary = [], hasNext = true, after) {
148
- var _b, _c;
149
- let dictionaryResults = initDictionary;
150
- const dictionary = {};
151
- while (hasNext) {
152
- const data = yield this.graphQLClient.request(exports.dictionaryQuery, {
153
- siteName,
154
- language,
155
- after,
156
- });
157
- if ((_c = (_b = data === null || data === void 0 ? void 0 : data.site) === null || _b === void 0 ? void 0 : _b.siteInfo) === null || _c === void 0 ? void 0 : _c.dictionary) {
158
- dictionaryResults = dictionaryResults.concat(data.site.siteInfo.dictionary.results);
159
- hasNext = data.site.siteInfo.dictionary.pageInfo.hasNext;
160
- after = data.site.siteInfo.dictionary.pageInfo.endCursor;
161
- }
162
- else {
163
- hasNext = false;
164
- }
165
- }
166
- dictionaryResults.forEach((item) => (dictionary[item.key] = item.value));
167
- return dictionary;
168
- });
153
+ }
154
+ dictionaryResults.forEach((item) => (dictionary[item.key] = item.value));
155
+ return dictionary;
169
156
  }
170
157
  /**
171
158
  * Gets a GraphQL client that can make requests to the API.
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.languageError = exports.siteNameError = void 0;
13
4
  exports.getAppRootId = getAppRootId;
@@ -44,30 +35,28 @@ const appRootQuery = /* GraphQL */ `
44
35
  * @summary This function intentionally avoids throwing an error if a root item is not found,
45
36
  * leaving that decision up to implementations.
46
37
  */
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, {
38
+ async function getAppRootId(client, siteName, language, jssAppTemplateId) {
39
+ var _a, _b, _c, _d, _e, _f;
40
+ if (!siteName) {
41
+ throw new RangeError(exports.siteNameError);
42
+ }
43
+ if (!language) {
44
+ throw new RangeError(exports.languageError);
45
+ }
46
+ let fetchResponse = await client.request(appRootQuery, {
47
+ jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
48
+ siteName,
49
+ language,
50
+ });
51
+ 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') {
52
+ fetchResponse = await client.request(appRootQuery, {
57
53
  jssAppTemplateId: jssAppTemplateId || constants_1.SitecoreTemplateId.JssApp,
58
54
  siteName,
59
- language,
55
+ language: 'en',
60
56
  });
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
- });
57
+ }
58
+ 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)) {
59
+ return null;
60
+ }
61
+ return fetchResponse.layout.homePage.rootItem[0].id;
73
62
  }
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEdgeProxyFormsUrl = exports.getEdgeProxyContentUrl = exports.SearchQueryService = exports.GraphQLRequestClient = exports.DefaultRetryStrategy = exports.getAppRootId = void 0;
3
+ exports.getEdgeProxyFormsUrl = exports.getEdgeProxyContentUrl = exports.DefaultRetryStrategy = exports.SearchQueryService = exports.GraphQLRequestClient = exports.getAppRootId = void 0;
4
4
  var app_root_query_1 = require("./app-root-query");
5
5
  Object.defineProperty(exports, "getAppRootId", { enumerable: true, get: function () { return app_root_query_1.getAppRootId; } });
6
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
7
  Object.defineProperty(exports, "GraphQLRequestClient", { enumerable: true, get: function () { return graphql_request_client_1.GraphQLRequestClient; } });
9
8
  var search_service_1 = require("./search-service");
10
9
  Object.defineProperty(exports, "SearchQueryService", { enumerable: true, get: function () { return search_service_1.SearchQueryService; } });
10
+ var retries_1 = require("../retries");
11
+ Object.defineProperty(exports, "DefaultRetryStrategy", { enumerable: true, get: function () { return retries_1.DefaultRetryStrategy; } });
11
12
  var graphql_edge_proxy_1 = require("./graphql-edge-proxy");
12
13
  Object.defineProperty(exports, "getEdgeProxyContentUrl", { enumerable: true, get: function () { return graphql_edge_proxy_1.getEdgeProxyContentUrl; } });
13
14
  Object.defineProperty(exports, "getEdgeProxyFormsUrl", { enumerable: true, get: function () { return graphql_edge_proxy_1.getEdgeProxyFormsUrl; } });
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.SearchQueryService = void 0;
13
4
  /**
@@ -36,26 +27,24 @@ class SearchQueryService {
36
27
  * @throws {RangeError} if a valid root item ID is not provided.
37
28
  * @throws {RangeError} if the provided language(s) is(are) not valid.
38
29
  */
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
- });
30
+ async fetch(query, args) {
31
+ var _a;
32
+ if (!args.rootItemId) {
33
+ throw new RangeError('"rootItemId" and "language" must be non-empty strings');
34
+ }
35
+ if (!args.language) {
36
+ throw new RangeError('"rootItemId" and "language" must be non-empty strings');
37
+ }
38
+ let results = [];
39
+ let hasNext = true;
40
+ let after = '';
41
+ while (hasNext) {
42
+ const fetchResponse = await this.client.request(query, Object.assign(Object.assign({}, args), { after }));
43
+ results = results.concat((_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.search) === null || _a === void 0 ? void 0 : _a.results);
44
+ hasNext = fetchResponse.search.pageInfo.hasNext;
45
+ after = fetchResponse.search.pageInfo.endCursor;
46
+ }
47
+ return results;
59
48
  }
60
49
  }
61
50
  exports.SearchQueryService = SearchQueryService;
@@ -1,61 +1,14 @@
1
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
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
14
5
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.GraphQLRequestClient = exports.DefaultRetryStrategy = void 0;
6
+ exports.GraphQLRequestClient = void 0;
16
7
  const graphql_request_1 = require("graphql-request");
17
8
  const url_parse_1 = __importDefault(require("url-parse"));
18
9
  const debug_1 = __importDefault(require("./debug"));
19
10
  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;
11
+ const retries_1 = require("./retries");
59
12
  /**
60
13
  * A GraphQL client for Sitecore APIs that uses the 'graphql-request' library.
61
14
  * https://github.com/prisma-labs/graphql-request
@@ -83,7 +36,7 @@ class GraphQLRequestClient {
83
36
  this.retries = (_a = clientConfig.retries) !== null && _a !== void 0 ? _a : 3;
84
37
  this.retryStrategy =
85
38
  clientConfig.retryStrategy ||
86
- new DefaultRetryStrategy({ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524] });
39
+ new retries_1.DefaultRetryStrategy({ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524] });
87
40
  this.client = new graphql_request_1.GraphQLClient(endpoint, {
88
41
  headers: this.headers,
89
42
  fetch: clientConfig.fetch,
@@ -105,48 +58,46 @@ class GraphQLRequestClient {
105
58
  * @param {object} [variables] graphql variables
106
59
  * @param {RequestOptions} [options] Options for configuring a GraphQL request.
107
60
  */
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);
61
+ async request(query, variables, options) {
62
+ let attempt = 1;
63
+ const retryer = async () => {
64
+ // Note we don't have access to raw request/response with graphql-request
65
+ // but we should log whatever we have.
66
+ this.debug('request: %o', {
67
+ url: this.endpoint,
68
+ headers: Object.assign(Object.assign({}, this.headers), options === null || options === void 0 ? void 0 : options.headers),
69
+ query,
70
+ variables,
71
+ });
72
+ const startTimestamp = Date.now();
73
+ const fetchWithOptionalTimeout = [this.client.request(query, variables, options === null || options === void 0 ? void 0 : options.headers)];
74
+ if (this.timeout) {
75
+ this.abortTimeout = new timeout_promise_1.default(this.timeout);
76
+ fetchWithOptionalTimeout.push(this.abortTimeout.start);
77
+ }
78
+ return Promise.race(fetchWithOptionalTimeout).then((data) => {
79
+ var _a;
80
+ (_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
81
+ this.debug('response in %dms: %o', Date.now() - startTimestamp, data);
82
+ return Promise.resolve(data);
83
+ }, async (error) => {
84
+ var _a, _b;
85
+ (_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
86
+ this.debug('response error: %o', error.response || error.message || error);
87
+ const status = ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) || error.code;
88
+ const shouldRetry = this.retryStrategy.shouldRetry(error, attempt, this.retries);
89
+ if (shouldRetry) {
90
+ const delayMs = this.retryStrategy.getDelay(error, attempt);
91
+ this.debug('Error: %s. Retrying in %dms (attempt %d).', status, delayMs, attempt);
92
+ attempt++;
93
+ return new Promise((resolve) => setTimeout(resolve, delayMs)).then(retryer);
94
+ }
95
+ else {
96
+ return Promise.reject(error);
125
97
  }
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
98
  });
148
- return retryer();
149
- });
99
+ };
100
+ return retryer();
150
101
  }
151
102
  }
152
103
  exports.GraphQLRequestClient = GraphQLRequestClient;