@bright.global/arboretum-sdk 0.0.7 → 0.0.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 (51) hide show
  1. package/dist/clients/contentful-client/impl/constants.js +8 -0
  2. package/dist/clients/contentful-client/impl/contentful-client-impl.js +21 -11
  3. package/dist/clients/contentful-client/impl/methods/contentful-fetch.js +116 -0
  4. package/dist/clients/contentful-client/impl/methods/get-content-types.js +27 -0
  5. package/dist/clients/contentful-client/impl/methods/get-entries.js +34 -0
  6. package/dist/clients/contentful-client/impl/methods/get-locales.js +20 -0
  7. package/dist/clients/contentful-client/impl/methods/get-tags.js +20 -0
  8. package/dist/es6/clients/contentful-client/impl/constants.js +5 -0
  9. package/dist/es6/clients/contentful-client/impl/contentful-client-impl.js +19 -10
  10. package/dist/es6/clients/contentful-client/impl/methods/contentful-fetch.js +112 -0
  11. package/dist/es6/clients/contentful-client/impl/methods/get-content-types.js +23 -0
  12. package/dist/es6/clients/contentful-client/impl/methods/get-entries.js +30 -0
  13. package/dist/es6/clients/contentful-client/impl/methods/get-locales.js +16 -0
  14. package/dist/es6/clients/contentful-client/impl/methods/get-tags.js +16 -0
  15. package/dist/es6/impl/arboretum-client.impl.js +3 -1
  16. package/dist/es6/impl/data/helpers/get-all-entries-recursively.js +5 -3
  17. package/dist/es6/impl/data/helpers/get-all-entries-recursively.test.js +3 -3
  18. package/dist/es6/impl/data/page-entries.js +1 -1
  19. package/dist/es6/impl/data/redirect-entries.js +1 -1
  20. package/dist/es6/impl/sitemap/adapters/page-entry-adapter.js +1 -0
  21. package/dist/es6/impl/sitemap/adapters/to-arboretum-page-adapter.test.js +6 -0
  22. package/dist/es6/impl/sitemap/adapters/to-arboretum-page-with-missing-data-adapter.js +2 -1
  23. package/dist/es6/impl/sitemap/helpers/__mocks__/mocked-localized-sitemap.js +59 -58
  24. package/dist/es6/impl/sitemap/helpers/build-localized-sitemap.test.js +34 -34
  25. package/dist/es6/utils/get-rendom-number.js +3 -0
  26. package/dist/impl/arboretum-client.impl.js +3 -1
  27. package/dist/impl/data/helpers/get-all-entries-recursively.js +5 -3
  28. package/dist/impl/data/helpers/get-all-entries-recursively.test.js +3 -3
  29. package/dist/impl/data/page-entries.js +1 -1
  30. package/dist/impl/data/redirect-entries.js +1 -1
  31. package/dist/impl/sitemap/adapters/page-entry-adapter.js +1 -0
  32. package/dist/impl/sitemap/adapters/to-arboretum-page-adapter.test.js +6 -0
  33. package/dist/impl/sitemap/adapters/to-arboretum-page-with-missing-data-adapter.js +2 -1
  34. package/dist/impl/sitemap/helpers/__mocks__/mocked-localized-sitemap.js +59 -58
  35. package/dist/impl/sitemap/helpers/build-localized-sitemap.test.js +30 -30
  36. package/dist/types/arboretum-client.d.ts +32 -4
  37. package/dist/types/clients/contentful-client/contentful-client.d.ts +37 -27
  38. package/dist/types/clients/contentful-client/impl/constants.d.ts +5 -0
  39. package/dist/types/clients/contentful-client/impl/contentful-client-impl.d.ts +2 -1
  40. package/dist/types/clients/contentful-client/impl/methods/contentful-fetch.d.ts +2 -0
  41. package/dist/types/clients/contentful-client/impl/methods/get-content-types.d.ts +3 -0
  42. package/dist/types/clients/contentful-client/impl/methods/get-entries.d.ts +3 -0
  43. package/dist/types/clients/contentful-client/impl/methods/get-locales.d.ts +3 -0
  44. package/dist/types/clients/contentful-client/impl/methods/get-tags.d.ts +3 -0
  45. package/dist/types/impl/arboretum-client.impl.d.ts +2 -1
  46. package/dist/types/impl/data/helpers/get-all-entries-recursively.d.ts +1 -1
  47. package/dist/types/impl/sitemap/adapters/to-arboretum-page-with-missing-data-adapter.d.ts +3 -3
  48. package/dist/types/impl/sitemap/helpers/__mocks__/mocked-localized-sitemap.d.ts +1 -1
  49. package/dist/types/utils/get-rendom-number.d.ts +1 -0
  50. package/dist/utils/get-rendom-number.js +7 -0
  51. package/package.json +2 -5
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_RETRY_LIMIT = exports.DEFAULT_RETRY_ON_ERROR = exports.DEFAULT_TIMEOUT = exports.DEFAULT_HOST = exports.DEFAULT_ENVIRONMENT = void 0;
4
+ exports.DEFAULT_ENVIRONMENT = "master";
5
+ exports.DEFAULT_HOST = "cdn.contentful.com";
6
+ exports.DEFAULT_TIMEOUT = 30000;
7
+ exports.DEFAULT_RETRY_ON_ERROR = true;
8
+ exports.DEFAULT_RETRY_LIMIT = 5;
@@ -11,26 +11,29 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.createContentfulClient = void 0;
15
- var contentful_1 = require("contentful");
14
+ exports.createCdaRestApiContentfulClient = exports.createContentfulClient = void 0;
15
+ var get_locales_1 = require("./methods/get-locales");
16
+ var get_content_types_1 = require("./methods/get-content-types");
17
+ var get_tags_1 = require("./methods/get-tags");
18
+ var get_entries_1 = require("./methods/get-entries");
16
19
  var createContentfulClient = function (config) {
17
20
  var configType = config.type;
18
- var clientFromCda = function (client) { return ({
19
- getEntries: client.getEntries,
20
- getLocales: client.getLocales,
21
- getContentTypes: client.getContentTypes,
22
- getTags: client.getTags,
23
- }); };
24
21
  switch (config.type) {
25
22
  case "cda-client": {
26
- return clientFromCda(config.contentful.client);
23
+ var c = config.contentful.client;
24
+ var client = c.withAllLocales ? c.withAllLocales : c;
25
+ return {
26
+ getEntries: client.getEntries,
27
+ getLocales: client.getLocales,
28
+ getContentTypes: client.getContentTypes,
29
+ getTags: client.getTags,
30
+ };
27
31
  }
28
32
  case "cda-client-params": {
29
33
  var previewHostOrUndefined = config.preview
30
34
  ? "preview.contentful.com"
31
35
  : undefined;
32
- var clientApi = (0, contentful_1.createClient)(__assign(__assign({}, config.contentful), { host: previewHostOrUndefined }));
33
- return clientFromCda(clientApi);
36
+ return (0, exports.createCdaRestApiContentfulClient)(__assign(__assign({}, config.contentful), { host: previewHostOrUndefined }));
34
37
  }
35
38
  case "cma-client": {
36
39
  var client_1 = config.contentful.client;
@@ -52,3 +55,10 @@ var createContentfulClient = function (config) {
52
55
  }
53
56
  };
54
57
  exports.createContentfulClient = createContentfulClient;
58
+ var createCdaRestApiContentfulClient = function (config) { return ({
59
+ getLocales: (0, get_locales_1.getLocales)(config),
60
+ getContentTypes: (0, get_content_types_1.getContentTypes)(config),
61
+ getTags: (0, get_tags_1.getTags)(config),
62
+ getEntries: (0, get_entries_1.getEntries)(config),
63
+ }); };
64
+ exports.createCdaRestApiContentfulClient = createCdaRestApiContentfulClient;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ Object.defineProperty(exports, "__esModule", { value: true });
55
+ exports.contentfulFetch = void 0;
56
+ var get_rendom_number_1 = require("../../../../utils/get-rendom-number");
57
+ var constants_1 = require("../constants");
58
+ var sleep = function (ms) { return new Promise(function (r) { return setTimeout(r, ms); }); };
59
+ var getRetryDelay = function (retry) {
60
+ return Math.ceil(Math.pow((0, get_rendom_number_1.getRandomNumber)(1.2, 1.4), retry) * 1000);
61
+ };
62
+ var contentfulFetchWithRetires = function (p, retryLimit, retry) {
63
+ if (retry === void 0) { retry = 0; }
64
+ return p().then(function (res) { return __awaiter(void 0, void 0, void 0, function () {
65
+ var delay, msg;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ if (!res.ok) return [3 /*break*/, 1];
70
+ return [2 /*return*/, res.json()];
71
+ case 1:
72
+ if (!(retry < retryLimit &&
73
+ (res.status === 429 || res.status === 500))) return [3 /*break*/, 3];
74
+ delay = getRetryDelay(retry);
75
+ msg = "Waiting for ".concat(delay, "ms before retrying... (retry: ").concat(delay, "/").concat(retryLimit, ")");
76
+ if (res.status === 500) {
77
+ console.log("[warning] Internal server error occurred. ".concat(msg));
78
+ }
79
+ else if (res.status === 429) {
80
+ console.log("[warning] Rate limit occurred. ".concat(msg));
81
+ }
82
+ return [4 /*yield*/, sleep(delay)];
83
+ case 2:
84
+ _a.sent();
85
+ return [2 /*return*/, contentfulFetchWithRetires(p, retryLimit, retry + 1)];
86
+ case 3: return [2 /*return*/, res
87
+ .json()
88
+ .then(function (res) {
89
+ throw res;
90
+ })
91
+ .catch(function (_) {
92
+ throw res;
93
+ })];
94
+ }
95
+ });
96
+ }); });
97
+ };
98
+ var contentfulFetch = function (config) {
99
+ return function (endpoint, query) {
100
+ var space = config.space, accessToken = config.accessToken;
101
+ var host = config.host || constants_1.DEFAULT_HOST;
102
+ var environment = config.environment || constants_1.DEFAULT_ENVIRONMENT;
103
+ var params = new URLSearchParams();
104
+ params.append("access_token", accessToken);
105
+ Object.entries(query).forEach(function (_a) {
106
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
107
+ params.append(key, value.toString());
108
+ });
109
+ var retryLimit = config.retryLimit || constants_1.DEFAULT_RETRY_LIMIT;
110
+ var retryOnError = config.retryOnError || constants_1.DEFAULT_RETRY_ON_ERROR;
111
+ return contentfulFetchWithRetires(function () {
112
+ return fetch("https://".concat(host, "/spaces/").concat(space, "/environments/").concat(environment).concat(endpoint, "?").concat(params.toString()), { method: "GET" });
113
+ }, retryOnError ? retryLimit : 0);
114
+ };
115
+ };
116
+ exports.contentfulFetch = contentfulFetch;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getContentTypes = void 0;
4
+ var contentful_fetch_1 = require("./contentful-fetch");
5
+ var getContentTypes = function (config) {
6
+ return function (query) {
7
+ return (0, contentful_fetch_1.contentfulFetch)(config)("/content_types", query || {}).then(function (res) { return ({
8
+ total: res.total,
9
+ skip: res.skip,
10
+ limit: res.limit,
11
+ items: res.items.map(function (i) { return ({
12
+ sys: { id: i.sys.id },
13
+ fields: i.fields.map(function (f) { return ({
14
+ id: f.id,
15
+ name: f.name,
16
+ localized: f.localized,
17
+ type: f.type,
18
+ linkType: f.linkType,
19
+ items: f.items
20
+ ? { type: f.items.type, linkType: f.items.linkType }
21
+ : undefined,
22
+ }); }),
23
+ }); }),
24
+ }); });
25
+ };
26
+ };
27
+ exports.getContentTypes = getContentTypes;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEntries = void 0;
4
+ var contentful_fetch_1 = require("./contentful-fetch");
5
+ var getEntries = function (config) {
6
+ return function (query) {
7
+ return (0, contentful_fetch_1.contentfulFetch)(config)("/entries", query || {}).then(function (res) {
8
+ var _a;
9
+ return ({
10
+ total: res.total,
11
+ skip: res.skip,
12
+ limit: res.limit,
13
+ items: (_a = res.items) === null || _a === void 0 ? void 0 : _a.map(function (i) {
14
+ var _a, _b;
15
+ return ({
16
+ metadata: i.metadata
17
+ ? { tags: i.metadata.tags.map(function (t) { return ({ sys: { id: t.sys.id } }); }) }
18
+ : undefined,
19
+ fields: i.fields,
20
+ sys: {
21
+ id: i.sys.id,
22
+ archivedVersion: i.sys.archivedVersion,
23
+ version: i.sys.version,
24
+ publishedVersion: i.sys.publishedVersion,
25
+ contentType: { sys: { id: (_b = (_a = i.sys.contentType) === null || _a === void 0 ? void 0 : _a.sys) === null || _b === void 0 ? void 0 : _b.id } },
26
+ cmaOnlyStatus: undefined,
27
+ },
28
+ });
29
+ }),
30
+ });
31
+ });
32
+ };
33
+ };
34
+ exports.getEntries = getEntries;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLocales = void 0;
4
+ var contentful_fetch_1 = require("./contentful-fetch");
5
+ var getLocales = function (config) {
6
+ return function () {
7
+ return (0, contentful_fetch_1.contentfulFetch)(config)("/locales", {}).then(function (res) { return ({
8
+ total: res.total,
9
+ skip: res.skip,
10
+ limit: res.limit,
11
+ items: res.items.map(function (i) { return ({
12
+ code: i.code,
13
+ name: i.name,
14
+ default: i.default,
15
+ fallbackCode: i.fallbackCode,
16
+ }); }),
17
+ }); });
18
+ };
19
+ };
20
+ exports.getLocales = getLocales;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTags = void 0;
4
+ var contentful_fetch_1 = require("./contentful-fetch");
5
+ var getTags = function (config) {
6
+ return function (query) {
7
+ return (0, contentful_fetch_1.contentfulFetch)(config)("/tags", query || {}).then(function (res) { return ({
8
+ total: res.total,
9
+ skip: res.skip,
10
+ limit: res.limit,
11
+ items: res.items.map(function (i) { return ({
12
+ name: i.name,
13
+ sys: {
14
+ id: i.sys.id,
15
+ },
16
+ }); }),
17
+ }); });
18
+ };
19
+ };
20
+ exports.getTags = getTags;
@@ -0,0 +1,5 @@
1
+ export var DEFAULT_ENVIRONMENT = "master";
2
+ export var DEFAULT_HOST = "cdn.contentful.com";
3
+ export var DEFAULT_TIMEOUT = 30000;
4
+ export var DEFAULT_RETRY_ON_ERROR = true;
5
+ export var DEFAULT_RETRY_LIMIT = 5;
@@ -9,25 +9,28 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
- import { createClient } from "contentful";
12
+ import { getLocales } from "./methods/get-locales";
13
+ import { getContentTypes } from "./methods/get-content-types";
14
+ import { getTags } from "./methods/get-tags";
15
+ import { getEntries } from "./methods/get-entries";
13
16
  export var createContentfulClient = function (config) {
14
17
  var configType = config.type;
15
- var clientFromCda = function (client) { return ({
16
- getEntries: client.getEntries,
17
- getLocales: client.getLocales,
18
- getContentTypes: client.getContentTypes,
19
- getTags: client.getTags,
20
- }); };
21
18
  switch (config.type) {
22
19
  case "cda-client": {
23
- return clientFromCda(config.contentful.client);
20
+ var c = config.contentful.client;
21
+ var client = c.withAllLocales ? c.withAllLocales : c;
22
+ return {
23
+ getEntries: client.getEntries,
24
+ getLocales: client.getLocales,
25
+ getContentTypes: client.getContentTypes,
26
+ getTags: client.getTags,
27
+ };
24
28
  }
25
29
  case "cda-client-params": {
26
30
  var previewHostOrUndefined = config.preview
27
31
  ? "preview.contentful.com"
28
32
  : undefined;
29
- var clientApi = createClient(__assign(__assign({}, config.contentful), { host: previewHostOrUndefined }));
30
- return clientFromCda(clientApi);
33
+ return createCdaRestApiContentfulClient(__assign(__assign({}, config.contentful), { host: previewHostOrUndefined }));
31
34
  }
32
35
  case "cma-client": {
33
36
  var client_1 = config.contentful.client;
@@ -48,3 +51,9 @@ export var createContentfulClient = function (config) {
48
51
  }
49
52
  }
50
53
  };
54
+ export var createCdaRestApiContentfulClient = function (config) { return ({
55
+ getLocales: getLocales(config),
56
+ getContentTypes: getContentTypes(config),
57
+ getTags: getTags(config),
58
+ getEntries: getEntries(config),
59
+ }); };
@@ -0,0 +1,112 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ var __read = (this && this.__read) || function (o, n) {
38
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
39
+ if (!m) return o;
40
+ var i = m.call(o), r, ar = [], e;
41
+ try {
42
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
43
+ }
44
+ catch (error) { e = { error: error }; }
45
+ finally {
46
+ try {
47
+ if (r && !r.done && (m = i["return"])) m.call(i);
48
+ }
49
+ finally { if (e) throw e.error; }
50
+ }
51
+ return ar;
52
+ };
53
+ import { getRandomNumber } from "../../../../utils/get-rendom-number";
54
+ import { DEFAULT_ENVIRONMENT, DEFAULT_HOST, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_ON_ERROR, } from "../constants";
55
+ var sleep = function (ms) { return new Promise(function (r) { return setTimeout(r, ms); }); };
56
+ var getRetryDelay = function (retry) {
57
+ return Math.ceil(Math.pow(getRandomNumber(1.2, 1.4), retry) * 1000);
58
+ };
59
+ var contentfulFetchWithRetires = function (p, retryLimit, retry) {
60
+ if (retry === void 0) { retry = 0; }
61
+ return p().then(function (res) { return __awaiter(void 0, void 0, void 0, function () {
62
+ var delay, msg;
63
+ return __generator(this, function (_a) {
64
+ switch (_a.label) {
65
+ case 0:
66
+ if (!res.ok) return [3 /*break*/, 1];
67
+ return [2 /*return*/, res.json()];
68
+ case 1:
69
+ if (!(retry < retryLimit &&
70
+ (res.status === 429 || res.status === 500))) return [3 /*break*/, 3];
71
+ delay = getRetryDelay(retry);
72
+ msg = "Waiting for ".concat(delay, "ms before retrying... (retry: ").concat(delay, "/").concat(retryLimit, ")");
73
+ if (res.status === 500) {
74
+ console.log("[warning] Internal server error occurred. ".concat(msg));
75
+ }
76
+ else if (res.status === 429) {
77
+ console.log("[warning] Rate limit occurred. ".concat(msg));
78
+ }
79
+ return [4 /*yield*/, sleep(delay)];
80
+ case 2:
81
+ _a.sent();
82
+ return [2 /*return*/, contentfulFetchWithRetires(p, retryLimit, retry + 1)];
83
+ case 3: return [2 /*return*/, res
84
+ .json()
85
+ .then(function (res) {
86
+ throw res;
87
+ })
88
+ .catch(function (_) {
89
+ throw res;
90
+ })];
91
+ }
92
+ });
93
+ }); });
94
+ };
95
+ export var contentfulFetch = function (config) {
96
+ return function (endpoint, query) {
97
+ var space = config.space, accessToken = config.accessToken;
98
+ var host = config.host || DEFAULT_HOST;
99
+ var environment = config.environment || DEFAULT_ENVIRONMENT;
100
+ var params = new URLSearchParams();
101
+ params.append("access_token", accessToken);
102
+ Object.entries(query).forEach(function (_a) {
103
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
104
+ params.append(key, value.toString());
105
+ });
106
+ var retryLimit = config.retryLimit || DEFAULT_RETRY_LIMIT;
107
+ var retryOnError = config.retryOnError || DEFAULT_RETRY_ON_ERROR;
108
+ return contentfulFetchWithRetires(function () {
109
+ return fetch("https://".concat(host, "/spaces/").concat(space, "/environments/").concat(environment).concat(endpoint, "?").concat(params.toString()), { method: "GET" });
110
+ }, retryOnError ? retryLimit : 0);
111
+ };
112
+ };
@@ -0,0 +1,23 @@
1
+ import { contentfulFetch } from "./contentful-fetch";
2
+ export var getContentTypes = function (config) {
3
+ return function (query) {
4
+ return contentfulFetch(config)("/content_types", query || {}).then(function (res) { return ({
5
+ total: res.total,
6
+ skip: res.skip,
7
+ limit: res.limit,
8
+ items: res.items.map(function (i) { return ({
9
+ sys: { id: i.sys.id },
10
+ fields: i.fields.map(function (f) { return ({
11
+ id: f.id,
12
+ name: f.name,
13
+ localized: f.localized,
14
+ type: f.type,
15
+ linkType: f.linkType,
16
+ items: f.items
17
+ ? { type: f.items.type, linkType: f.items.linkType }
18
+ : undefined,
19
+ }); }),
20
+ }); }),
21
+ }); });
22
+ };
23
+ };
@@ -0,0 +1,30 @@
1
+ import { contentfulFetch } from "./contentful-fetch";
2
+ export var getEntries = function (config) {
3
+ return function (query) {
4
+ return contentfulFetch(config)("/entries", query || {}).then(function (res) {
5
+ var _a;
6
+ return ({
7
+ total: res.total,
8
+ skip: res.skip,
9
+ limit: res.limit,
10
+ items: (_a = res.items) === null || _a === void 0 ? void 0 : _a.map(function (i) {
11
+ var _a, _b;
12
+ return ({
13
+ metadata: i.metadata
14
+ ? { tags: i.metadata.tags.map(function (t) { return ({ sys: { id: t.sys.id } }); }) }
15
+ : undefined,
16
+ fields: i.fields,
17
+ sys: {
18
+ id: i.sys.id,
19
+ archivedVersion: i.sys.archivedVersion,
20
+ version: i.sys.version,
21
+ publishedVersion: i.sys.publishedVersion,
22
+ contentType: { sys: { id: (_b = (_a = i.sys.contentType) === null || _a === void 0 ? void 0 : _a.sys) === null || _b === void 0 ? void 0 : _b.id } },
23
+ cmaOnlyStatus: undefined,
24
+ },
25
+ });
26
+ }),
27
+ });
28
+ });
29
+ };
30
+ };
@@ -0,0 +1,16 @@
1
+ import { contentfulFetch } from "./contentful-fetch";
2
+ export var getLocales = function (config) {
3
+ return function () {
4
+ return contentfulFetch(config)("/locales", {}).then(function (res) { return ({
5
+ total: res.total,
6
+ skip: res.skip,
7
+ limit: res.limit,
8
+ items: res.items.map(function (i) { return ({
9
+ code: i.code,
10
+ name: i.name,
11
+ default: i.default,
12
+ fallbackCode: i.fallbackCode,
13
+ }); }),
14
+ }); });
15
+ };
16
+ };
@@ -0,0 +1,16 @@
1
+ import { contentfulFetch } from "./contentful-fetch";
2
+ export var getTags = function (config) {
3
+ return function (query) {
4
+ return contentfulFetch(config)("/tags", query || {}).then(function (res) { return ({
5
+ total: res.total,
6
+ skip: res.skip,
7
+ limit: res.limit,
8
+ items: res.items.map(function (i) { return ({
9
+ name: i.name,
10
+ sys: {
11
+ id: i.sys.id,
12
+ },
13
+ }); }),
14
+ }); });
15
+ };
16
+ };
@@ -126,7 +126,9 @@ export var createArboretumClient = function (config) { return __awaiter(void 0,
126
126
  options: __assign(__assign({}, options), { includeEntryStatus: includeEntryStatus }),
127
127
  localeTagIdPrefix: localeTagIdPrefix,
128
128
  pageHomeTagId: options.homePageTagId || pageHomeTagId,
129
- contentfulClientType: config.type,
129
+ contentfulClientType: config.type === "cda-client" && config.contentful.client.withAllLocales
130
+ ? "cda-client-with-all-locales"
131
+ : config.type,
130
132
  pageTagIdPrefix: pageTagIdPrefix,
131
133
  cmaPreviewClientApi: cmaPreviewClientApi,
132
134
  };
@@ -80,7 +80,7 @@ var filterOutNestedEntriesFieldsOtherThanSysEff = function (entry) {
80
80
  return entry;
81
81
  };
82
82
  };
83
- export var getAllEntriesRecursively = function (_a, contentType, skip, acc, select, refFieldsToFilterOut, limit) {
83
+ export var getAllEntriesRecursively = function (_a, contentfulClientType, contentType, skip, acc, select, refFieldsToFilterOut, limit) {
84
84
  var getEntries = _a.getEntries;
85
85
  if (refFieldsToFilterOut === void 0) { refFieldsToFilterOut = []; }
86
86
  if (limit === void 0) { limit = 1000; }
@@ -94,7 +94,9 @@ export var getAllEntriesRecursively = function (_a, contentType, skip, acc, sele
94
94
  content_type: contentType,
95
95
  include: 0,
96
96
  select: select,
97
- locale: "*",
97
+ locale: contentfulClientType === "cda-client-with-all-locales"
98
+ ? undefined
99
+ : "*",
98
100
  })];
99
101
  case 1:
100
102
  items = (_b.sent()).items;
@@ -105,7 +107,7 @@ export var getAllEntriesRecursively = function (_a, contentType, skip, acc, sele
105
107
  });
106
108
  acc.push.apply(acc, __spreadArray([], __read(items), false));
107
109
  return [2 /*return*/, items.length >= limit
108
- ? getAllEntriesRecursively({ getEntries: getEntries }, contentType, skip + limit, acc, select)
110
+ ? getAllEntriesRecursively({ getEntries: getEntries }, contentfulClientType, contentType, skip + limit, acc, select)
109
111
  : acc];
110
112
  }
111
113
  });
@@ -67,7 +67,7 @@ describe(getAllEntriesRecursively, function () {
67
67
  var entries;
68
68
  return __generator(this, function (_a) {
69
69
  switch (_a.label) {
70
- case 0: return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, 0), testContentTypeId, 0, [], undefined, [], 10)];
70
+ case 0: return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, 0), "cda-client", testContentTypeId, 0, [], undefined, [], 10)];
71
71
  case 1:
72
72
  entries = _a.sent();
73
73
  expect(entries.length).toBe(0);
@@ -81,7 +81,7 @@ describe(getAllEntriesRecursively, function () {
81
81
  switch (_a.label) {
82
82
  case 0:
83
83
  total = 10;
84
- return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, total), testContentTypeId, 0, [], undefined, [], 10)];
84
+ return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, total), "cda-client", testContentTypeId, 0, [], undefined, [], 10)];
85
85
  case 1:
86
86
  entries = _a.sent();
87
87
  entriesIds = entries.map(function (e) { return e.sys.id; });
@@ -99,7 +99,7 @@ describe(getAllEntriesRecursively, function () {
99
99
  switch (_a.label) {
100
100
  case 0:
101
101
  total = 101;
102
- return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, total), testContentTypeId, 0, [], undefined, [], 10)];
102
+ return [4 /*yield*/, getAllEntriesRecursively(mockedContentfulEntriesClientApi(testContentTypeId, total), "cda-client", testContentTypeId, 0, [], undefined, [], 10)];
103
103
  case 1:
104
104
  entries = _a.sent();
105
105
  entriesIds = entries.map(function (e) { return e.sys.id; });
@@ -83,7 +83,7 @@ var getAllPageEntriesRecursively = function (_a, contentfulClientType, pageConte
83
83
  pageContentTypeOpt.childPagesFieldId,
84
84
  pageContentTypeOpt.titleFieldId,
85
85
  ].flatMap(function (id) { return (id ? ["fields.".concat(id)] : []); });
86
- return [2 /*return*/, getAllEntriesRecursively({ getEntries: getEntries }, pageContentTypeOpt.id, skip, acc,
86
+ return [2 /*return*/, getAllEntriesRecursively({ getEntries: getEntries }, contentfulClientType, pageContentTypeOpt.id, skip, acc,
87
87
  /* For some reason select param causes errors in CMA. I'm getting the following response:
88
88
  {
89
89
  "status": 400,
@@ -82,7 +82,7 @@ var getAllRedirectEntriesRecursively = function (ctx, redirectContentType, skip,
82
82
  redirectContentType.typeFieldId,
83
83
  redirectContentType.pathFieldId,
84
84
  ].flatMap(function (id) { return (id ? ["fields.".concat(id)] : []); });
85
- return [2 /*return*/, getAllEntriesRecursively(ctx.clientApi, redirectContentType.id, skip, acc,
85
+ return [2 /*return*/, getAllEntriesRecursively(ctx.clientApi, ctx.contentfulClientType, redirectContentType.id, skip, acc,
86
86
  /* For some reason select param causes errors in CMA. I'm getting the following response:
87
87
  {
88
88
  "status": 400,
@@ -19,6 +19,7 @@ export var pageEntryAdapter = function (data, slugField, titleField, childPagesF
19
19
  sys: {
20
20
  id: entry.sys.id,
21
21
  cmaOnlyStatus: entry.sys.cmaOnlyStatus,
22
+ contentTypeId: entry.sys.contentType.sys.id
22
23
  },
23
24
  metadata: entry.metadata,
24
25
  childPages: (childPages === null || childPages === void 0 ? void 0 : childPages.flatMap(function (childPage) { var _a; return ((_a = childPage === null || childPage === void 0 ? void 0 : childPage.sys) === null || _a === void 0 ? void 0 : _a.id) ? [{ sys: { id: childPage === null || childPage === void 0 ? void 0 : childPage.sys.id } }] : []; })) || [],