contentful-export 8.2.2 → 8.3.0
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.
- package/dist/index.js +1 -4
- package/dist/tasks/get-space-data.js +156 -102
- package/dist/tasks/init-client.js +5 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -18,9 +18,8 @@ var _formatDistance = require("date-fns/formatDistance");
|
|
|
18
18
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
|
19
19
|
var _downloadAssets = _interopRequireDefault(require("./tasks/download-assets"));
|
|
20
20
|
var _getSpaceData = _interopRequireDefault(require("./tasks/get-space-data"));
|
|
21
|
-
var _initClient =
|
|
21
|
+
var _initClient = _interopRequireDefault(require("./tasks/init-client"));
|
|
22
22
|
var _parseOptions = _interopRequireDefault(require("./parseOptions"));
|
|
23
|
-
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
24
23
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
25
24
|
const accessP = _bluebird.default.promisify(_fs.access);
|
|
26
25
|
const tableOptions = {
|
|
@@ -54,7 +53,6 @@ function runContentfulExport(params) {
|
|
|
54
53
|
try {
|
|
55
54
|
// CMA client
|
|
56
55
|
ctx.client = (0, _initClient.default)(options);
|
|
57
|
-
ctx.plainClient = (0, _initClient.initPlainClient)(options);
|
|
58
56
|
if (options.deliveryToken && !options.includeDrafts) {
|
|
59
57
|
// CDA client for fetching only public entries
|
|
60
58
|
ctx.cdaClient = (0, _initClient.default)(options, true);
|
|
@@ -69,7 +67,6 @@ function runContentfulExport(params) {
|
|
|
69
67
|
task: ctx => {
|
|
70
68
|
return (0, _getSpaceData.default)({
|
|
71
69
|
client: ctx.client,
|
|
72
|
-
plainClient: ctx.plainClient,
|
|
73
70
|
cdaClient: ctx.cdaClient,
|
|
74
71
|
spaceId: options.spaceId,
|
|
75
72
|
environmentId: options.environmentId,
|
|
@@ -9,6 +9,11 @@ var _contentfulBatchLibs = require("contentful-batch-libs");
|
|
|
9
9
|
var _listr = _interopRequireDefault(require("listr"));
|
|
10
10
|
var _listrVerboseRenderer = _interopRequireDefault(require("listr-verbose-renderer"));
|
|
11
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
13
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
14
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
12
17
|
const MAX_ALLOWED_LIMIT = 1000;
|
|
13
18
|
let pageLimit = MAX_ALLOWED_LIMIT;
|
|
14
19
|
|
|
@@ -18,7 +23,6 @@ let pageLimit = MAX_ALLOWED_LIMIT;
|
|
|
18
23
|
*/
|
|
19
24
|
function getFullSourceSpace({
|
|
20
25
|
client,
|
|
21
|
-
plainClient,
|
|
22
26
|
cdaClient,
|
|
23
27
|
spaceId,
|
|
24
28
|
environmentId = 'master',
|
|
@@ -43,22 +47,32 @@ function getFullSourceSpace({
|
|
|
43
47
|
renderer: _listrVerboseRenderer.default
|
|
44
48
|
};
|
|
45
49
|
return new _listr.default([{
|
|
46
|
-
title: 'Connecting to space',
|
|
47
|
-
task: (0, _contentfulBatchLibs.wrapTask)(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
title: 'Connecting to space/environment',
|
|
51
|
+
task: (0, _contentfulBatchLibs.wrapTask)(async () => {
|
|
52
|
+
try {
|
|
53
|
+
await client.space.get({
|
|
54
|
+
spaceId
|
|
55
|
+
});
|
|
56
|
+
} catch (err) {
|
|
57
|
+
throw new Error(`Unable to retrieve space ${spaceId}, please ensure the space exists and the token is valid. (${err.message})`);
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await client.environment.get({
|
|
61
|
+
spaceId,
|
|
62
|
+
environmentId
|
|
63
|
+
});
|
|
64
|
+
} catch (err) {
|
|
65
|
+
throw new Error(`Unable to retrieve environment ${environmentId}, please ensure the environment exists and the token is valid. (${err.message})`);
|
|
66
|
+
}
|
|
54
67
|
})
|
|
55
68
|
}, {
|
|
56
69
|
title: 'Fetching content types data',
|
|
57
70
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
58
|
-
return pagedGet({
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
return pagedGet(query => client.contentType.getMany({
|
|
72
|
+
spaceId,
|
|
73
|
+
environmentId,
|
|
74
|
+
query
|
|
75
|
+
})).then(extractItems).then(items => {
|
|
62
76
|
ctx.data.contentTypes = items;
|
|
63
77
|
});
|
|
64
78
|
}),
|
|
@@ -66,10 +80,11 @@ function getFullSourceSpace({
|
|
|
66
80
|
}, {
|
|
67
81
|
title: 'Fetching tags data',
|
|
68
82
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
69
|
-
return pagedGet({
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
83
|
+
return pagedGet(query => client.tag.getMany({
|
|
84
|
+
spaceId,
|
|
85
|
+
environmentId,
|
|
86
|
+
query
|
|
87
|
+
})).then(extractItems).then(items => {
|
|
73
88
|
ctx.data.tags = items;
|
|
74
89
|
}).catch(err => {
|
|
75
90
|
_contentfulBatchLibs.logEmitter.emit('error', new Error(`Fetching tags failed: ${err.message}`, {
|
|
@@ -82,7 +97,7 @@ function getFullSourceSpace({
|
|
|
82
97
|
}, {
|
|
83
98
|
title: 'Fetching editor interfaces data',
|
|
84
99
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
85
|
-
return getEditorInterfaces(ctx.data.contentTypes).then(editorInterfaces => {
|
|
100
|
+
return getEditorInterfaces(client, spaceId, environmentId, ctx.data.contentTypes).then(editorInterfaces => {
|
|
86
101
|
ctx.data.editorInterfaces = editorInterfaces.filter(editorInterface => {
|
|
87
102
|
return editorInterface !== null;
|
|
88
103
|
});
|
|
@@ -92,17 +107,17 @@ function getFullSourceSpace({
|
|
|
92
107
|
}, {
|
|
93
108
|
title: 'Fetching content entries data',
|
|
94
109
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
95
|
-
const source = (cdaClient === null || cdaClient === void 0 ? void 0 : cdaClient.withAllLocales) || ctx.environment;
|
|
96
110
|
if (cdaClient) {
|
|
97
111
|
// let's not fetch children when using Content Delivery API
|
|
98
112
|
queryEntries = queryEntries || {};
|
|
99
113
|
queryEntries.include = 0;
|
|
100
114
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
query
|
|
105
|
-
})
|
|
115
|
+
const fetchFn = cdaClient ? query => cdaClient.withAllLocales.getEntries(query) : query => client.entry.getMany({
|
|
116
|
+
spaceId,
|
|
117
|
+
environmentId,
|
|
118
|
+
query
|
|
119
|
+
});
|
|
120
|
+
return pagedGet(fetchFn, queryEntries).then(extractItems).then(items => filterDrafts(items, includeDrafts, cdaClient)).then(items => filterArchived(items, includeArchived)).then(items => removeTags(items, stripTags)).then(items => {
|
|
106
121
|
ctx.data.entries = items;
|
|
107
122
|
});
|
|
108
123
|
}),
|
|
@@ -110,13 +125,13 @@ function getFullSourceSpace({
|
|
|
110
125
|
}, {
|
|
111
126
|
title: 'Fetching assets data',
|
|
112
127
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
113
|
-
const source = (cdaClient === null || cdaClient === void 0 ? void 0 : cdaClient.withAllLocales) || ctx.environment;
|
|
114
128
|
queryAssets = queryAssets || {};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
query
|
|
119
|
-
})
|
|
129
|
+
const fetchFn = cdaClient ? query => cdaClient.withAllLocales.getAssets(query) : query => client.asset.getMany({
|
|
130
|
+
spaceId,
|
|
131
|
+
environmentId,
|
|
132
|
+
query
|
|
133
|
+
});
|
|
134
|
+
return pagedGet(fetchFn, queryAssets).then(extractItems).then(items => filterDrafts(items, includeDrafts, cdaClient)).then(items => filterArchived(items, includeArchived)).then(items => removeTags(items, stripTags)).then(items => {
|
|
120
135
|
ctx.data.assets = items;
|
|
121
136
|
});
|
|
122
137
|
}),
|
|
@@ -124,33 +139,34 @@ function getFullSourceSpace({
|
|
|
124
139
|
}, {
|
|
125
140
|
title: 'Fetching locales data',
|
|
126
141
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
127
|
-
return pagedGet({
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
return pagedGet(query => client.locale.getMany({
|
|
143
|
+
spaceId,
|
|
144
|
+
environmentId,
|
|
145
|
+
query
|
|
146
|
+
})).then(extractItems).then(items => {
|
|
131
147
|
ctx.data.locales = items;
|
|
132
148
|
});
|
|
133
149
|
}),
|
|
134
150
|
skip: () => skipContentModel
|
|
135
151
|
}, {
|
|
136
152
|
title: 'Fetching webhooks data',
|
|
137
|
-
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
153
|
+
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
154
|
+
const webhooksResponse = await client.webhook.getMany({
|
|
155
|
+
query: {
|
|
156
|
+
// webhooks are capped to 100 per space
|
|
157
|
+
limit: 100
|
|
158
|
+
},
|
|
159
|
+
spaceId
|
|
143
160
|
});
|
|
161
|
+
ctx.data.webhooks = webhooksResponse.items;
|
|
144
162
|
}),
|
|
145
163
|
skip: () => skipWebhooks || environmentId !== 'master' && 'Webhooks can only be exported from master environment'
|
|
146
164
|
}, {
|
|
147
165
|
title: 'Fetching roles data',
|
|
148
|
-
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}).then(extractItems).then(items => {
|
|
153
|
-
ctx.data.roles = items;
|
|
166
|
+
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
167
|
+
ctx.data.roles = await pagedRolesGet({
|
|
168
|
+
client,
|
|
169
|
+
spaceId
|
|
154
170
|
});
|
|
155
171
|
}),
|
|
156
172
|
skip: () => skipRoles || environmentId !== 'master' && 'Roles can only be exported from master environment'
|
|
@@ -158,12 +174,11 @@ function getFullSourceSpace({
|
|
|
158
174
|
title: 'Fetching Design Tokens data',
|
|
159
175
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
160
176
|
try {
|
|
161
|
-
ctx.data.designTokens = await cursorPagedGet({
|
|
162
|
-
client: plainClient,
|
|
177
|
+
ctx.data.designTokens = await cursorPagedGet(query => client.designToken.getMany({
|
|
163
178
|
spaceId,
|
|
164
179
|
environmentId,
|
|
165
|
-
|
|
166
|
-
});
|
|
180
|
+
query
|
|
181
|
+
}), 'designToken');
|
|
167
182
|
} catch (err) {
|
|
168
183
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Design Tokens export: ${err.message}`);
|
|
169
184
|
ctx.data.designTokens = [];
|
|
@@ -174,12 +189,11 @@ function getFullSourceSpace({
|
|
|
174
189
|
title: 'Fetching Components data',
|
|
175
190
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
176
191
|
try {
|
|
177
|
-
ctx.data.components = await cursorPagedGet({
|
|
178
|
-
client: plainClient,
|
|
192
|
+
ctx.data.components = await cursorPagedGet(query => client.component.getMany({
|
|
179
193
|
spaceId,
|
|
180
194
|
environmentId,
|
|
181
|
-
|
|
182
|
-
});
|
|
195
|
+
query
|
|
196
|
+
}), 'component');
|
|
183
197
|
} catch (err) {
|
|
184
198
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Components export: ${err.message}`);
|
|
185
199
|
ctx.data.components = [];
|
|
@@ -190,12 +204,11 @@ function getFullSourceSpace({
|
|
|
190
204
|
title: 'Fetching Experience Templates data',
|
|
191
205
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
192
206
|
try {
|
|
193
|
-
ctx.data.experienceTemplates = await cursorPagedGet({
|
|
194
|
-
client: plainClient,
|
|
207
|
+
ctx.data.experienceTemplates = await cursorPagedGet(query => client.experienceTemplate.getMany({
|
|
195
208
|
spaceId,
|
|
196
209
|
environmentId,
|
|
197
|
-
|
|
198
|
-
});
|
|
210
|
+
query
|
|
211
|
+
}), 'experienceTemplate');
|
|
199
212
|
} catch (err) {
|
|
200
213
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Experience Templates export: ${err.message}`);
|
|
201
214
|
ctx.data.experienceTemplates = [];
|
|
@@ -206,12 +219,11 @@ function getFullSourceSpace({
|
|
|
206
219
|
title: 'Fetching Data Assemblies data',
|
|
207
220
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
208
221
|
try {
|
|
209
|
-
ctx.data.dataAssemblies = await cursorPagedGet({
|
|
210
|
-
client: plainClient,
|
|
222
|
+
ctx.data.dataAssemblies = await cursorPagedGet(query => client.dataAssembly.getMany({
|
|
211
223
|
spaceId,
|
|
212
224
|
environmentId,
|
|
213
|
-
|
|
214
|
-
});
|
|
225
|
+
query
|
|
226
|
+
}), 'dataAssembly');
|
|
215
227
|
} catch (err) {
|
|
216
228
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Data Assemblies export: ${err.message}`);
|
|
217
229
|
ctx.data.dataAssemblies = [];
|
|
@@ -222,12 +234,11 @@ function getFullSourceSpace({
|
|
|
222
234
|
title: 'Fetching Experience Fragments data',
|
|
223
235
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
224
236
|
try {
|
|
225
|
-
ctx.data.experienceFragments = await cursorPagedGet({
|
|
226
|
-
client: plainClient,
|
|
237
|
+
ctx.data.experienceFragments = await cursorPagedGet(query => client.experienceFragment.getMany({
|
|
227
238
|
spaceId,
|
|
228
239
|
environmentId,
|
|
229
|
-
|
|
230
|
-
});
|
|
240
|
+
query
|
|
241
|
+
}), 'experienceFragment');
|
|
231
242
|
} catch (err) {
|
|
232
243
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Experience Fragments export: ${err.message}`);
|
|
233
244
|
ctx.data.experienceFragments = [];
|
|
@@ -238,12 +249,11 @@ function getFullSourceSpace({
|
|
|
238
249
|
title: 'Fetching Experiences data',
|
|
239
250
|
task: (0, _contentfulBatchLibs.wrapTask)(async ctx => {
|
|
240
251
|
try {
|
|
241
|
-
ctx.data.experiences = await cursorPagedGet({
|
|
242
|
-
client: plainClient,
|
|
252
|
+
ctx.data.experiences = await cursorPagedGet(query => client.experience.getMany({
|
|
243
253
|
spaceId,
|
|
244
254
|
environmentId,
|
|
245
|
-
|
|
246
|
-
});
|
|
255
|
+
query
|
|
256
|
+
}), 'experience');
|
|
247
257
|
} catch (err) {
|
|
248
258
|
_contentfulBatchLibs.logEmitter.emit('warning', `Skipping Experiences export: ${err.message}`);
|
|
249
259
|
ctx.data.experiences = [];
|
|
@@ -252,9 +262,13 @@ function getFullSourceSpace({
|
|
|
252
262
|
skip: () => !includeExperienceOrchestration
|
|
253
263
|
}], listrOptions);
|
|
254
264
|
}
|
|
255
|
-
function getEditorInterfaces(contentTypes) {
|
|
265
|
+
function getEditorInterfaces(client, spaceId, environmentId, contentTypes) {
|
|
256
266
|
return _bluebird.default.map(contentTypes, contentType => {
|
|
257
|
-
return
|
|
267
|
+
return client.editorInterface.get({
|
|
268
|
+
spaceId,
|
|
269
|
+
environmentId,
|
|
270
|
+
contentTypeId: contentType.sys.id
|
|
271
|
+
}).then(editorInterface => {
|
|
258
272
|
_contentfulBatchLibs.logEmitter.emit('info', `Fetched editor interface for ${contentType.name}`);
|
|
259
273
|
return editorInterface;
|
|
260
274
|
}).catch(() => {
|
|
@@ -272,55 +286,101 @@ function getEditorInterfaces(contentTypes) {
|
|
|
272
286
|
* Gets all ExO entities using cursor-based pagination (pageNext/pagePrev tokens).
|
|
273
287
|
* ExO list endpoints do not support skip-based pagination or the order param.
|
|
274
288
|
*/
|
|
275
|
-
async function cursorPagedGet({
|
|
276
|
-
client,
|
|
277
|
-
spaceId,
|
|
278
|
-
environmentId,
|
|
279
|
-
method
|
|
280
|
-
}) {
|
|
281
|
-
const [entity, operation] = method.split('.');
|
|
289
|
+
async function cursorPagedGet(fetchFn, entityLabel) {
|
|
282
290
|
const allItems = [];
|
|
283
291
|
let pageNext = null;
|
|
284
292
|
do {
|
|
285
293
|
var _response$pages$next, _response$pages;
|
|
286
294
|
const query = {
|
|
287
|
-
spaceId,
|
|
288
|
-
environmentId,
|
|
289
295
|
limit: pageLimit
|
|
290
296
|
};
|
|
291
297
|
if (pageNext) {
|
|
292
298
|
query.pageNext = pageNext;
|
|
293
299
|
}
|
|
294
|
-
const response = await
|
|
300
|
+
const response = await fetchFn(query);
|
|
295
301
|
allItems.push(...response.items);
|
|
296
|
-
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${allItems.length} ${
|
|
302
|
+
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${allItems.length} ${entityLabel} items`);
|
|
297
303
|
pageNext = (_response$pages$next = (_response$pages = response.pages) === null || _response$pages === void 0 ? void 0 : _response$pages.next) !== null && _response$pages$next !== void 0 ? _response$pages$next : null;
|
|
298
304
|
} while (pageNext);
|
|
299
305
|
return allItems;
|
|
300
306
|
}
|
|
301
307
|
|
|
302
308
|
/**
|
|
309
|
+
* Gets all roles. Roles are scheduled to switch from skip/limit to
|
|
310
|
+
* cursor-based (pages.next/pagePrev) pagination on Feb 15, 2027:
|
|
311
|
+
* https://www.contentful.com/developers/api-changes/space-roles-collection-endpoints-update/
|
|
312
|
+
* Handles both shapes so this keeps working across that migration.
|
|
313
|
+
*/
|
|
314
|
+
async function pagedRolesGet({
|
|
315
|
+
client,
|
|
316
|
+
spaceId
|
|
317
|
+
}) {
|
|
318
|
+
const allItems = [];
|
|
319
|
+
const order = 'sys.createdAt,sys.id';
|
|
320
|
+
let response = await client.role.getMany({
|
|
321
|
+
spaceId,
|
|
322
|
+
query: {
|
|
323
|
+
limit: pageLimit,
|
|
324
|
+
order
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
allItems.push(...response.items);
|
|
328
|
+
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${allItems.length} roles`);
|
|
329
|
+
if (response.pages !== undefined) {
|
|
330
|
+
var _response$pages$next2, _response$pages2;
|
|
331
|
+
let pageNext = (_response$pages$next2 = (_response$pages2 = response.pages) === null || _response$pages2 === void 0 ? void 0 : _response$pages2.next) !== null && _response$pages$next2 !== void 0 ? _response$pages$next2 : null;
|
|
332
|
+
while (pageNext) {
|
|
333
|
+
var _response$pages$next3, _response$pages3;
|
|
334
|
+
response = await client.role.getMany({
|
|
335
|
+
spaceId,
|
|
336
|
+
query: {
|
|
337
|
+
limit: pageLimit,
|
|
338
|
+
order,
|
|
339
|
+
pageNext
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
allItems.push(...response.items);
|
|
343
|
+
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${allItems.length} roles`);
|
|
344
|
+
pageNext = (_response$pages$next3 = (_response$pages3 = response.pages) === null || _response$pages3 === void 0 ? void 0 : _response$pages3.next) !== null && _response$pages$next3 !== void 0 ? _response$pages$next3 : null;
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
let skip = allItems.length;
|
|
348
|
+
while (allItems.length < response.total && response.items.length > 0) {
|
|
349
|
+
response = await client.role.getMany({
|
|
350
|
+
spaceId,
|
|
351
|
+
query: {
|
|
352
|
+
limit: pageLimit,
|
|
353
|
+
order,
|
|
354
|
+
skip
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
allItems.push(...response.items);
|
|
358
|
+
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${allItems.length} roles`);
|
|
359
|
+
skip += response.items.length;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return allItems;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Recursively fetches all pages of a offset-based paginated CMA collection and returns
|
|
367
|
+
* them merged into a single response shaped like `{ items: [...], total, ... }`
|
|
368
|
+
*
|
|
303
369
|
* Gets all the existing entities based on pagination parameters.
|
|
304
370
|
* The first call will have no aggregated response. Subsequent calls will
|
|
305
371
|
* concatenate the new responses to the original one.
|
|
306
372
|
*/
|
|
307
|
-
function pagedGet({
|
|
308
|
-
|
|
309
|
-
method,
|
|
310
|
-
skip = 0,
|
|
311
|
-
aggregatedResponse = null,
|
|
312
|
-
query = null
|
|
313
|
-
}) {
|
|
314
|
-
const userQueryLimit = query && query.limit;
|
|
373
|
+
function pagedGet(fetchFn, userQuery = null, skip = 0, aggregatedResponse = null) {
|
|
374
|
+
const userQueryLimit = userQuery && userQuery.limit;
|
|
315
375
|
const fetchedTotal = aggregatedResponse && aggregatedResponse.items.length;
|
|
316
376
|
const limit = userQueryLimit ? Math.min(pageLimit, userQueryLimit - fetchedTotal) : pageLimit;
|
|
317
|
-
const
|
|
377
|
+
const query = _objectSpread(_objectSpread({
|
|
318
378
|
skip,
|
|
319
379
|
order: 'sys.createdAt,sys.id'
|
|
320
|
-
},
|
|
380
|
+
}, userQuery), {}, {
|
|
321
381
|
limit
|
|
322
382
|
});
|
|
323
|
-
return
|
|
383
|
+
return fetchFn(query).then(response => {
|
|
324
384
|
if (!aggregatedResponse) {
|
|
325
385
|
aggregatedResponse = response;
|
|
326
386
|
} else {
|
|
@@ -328,20 +388,14 @@ function pagedGet({
|
|
|
328
388
|
}
|
|
329
389
|
const totalItemsLength = aggregatedResponse.items.length;
|
|
330
390
|
const total = response.total;
|
|
331
|
-
logPagingStatus(response,
|
|
391
|
+
logPagingStatus(response, query, userQueryLimit);
|
|
332
392
|
const gotAllQueryLimitedItems = userQueryLimit && totalItemsLength >= userQueryLimit;
|
|
333
393
|
const gotAllItems = totalItemsLength >= total;
|
|
334
394
|
const gotNoItems = totalItemsLength <= 0;
|
|
335
395
|
if (gotAllQueryLimitedItems || gotAllItems || gotNoItems) {
|
|
336
396
|
return aggregatedResponse;
|
|
337
397
|
}
|
|
338
|
-
return pagedGet(
|
|
339
|
-
source,
|
|
340
|
-
method,
|
|
341
|
-
skip: skip + response.items.length,
|
|
342
|
-
aggregatedResponse,
|
|
343
|
-
query
|
|
344
|
-
});
|
|
398
|
+
return pagedGet(fetchFn, userQuery, skip + response.items.length, aggregatedResponse);
|
|
345
399
|
});
|
|
346
400
|
}
|
|
347
401
|
function logPagingStatus(response, requestQuery, userLimit) {
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = initClient;
|
|
7
|
-
exports.initPlainClient = initPlainClient;
|
|
8
7
|
var _contentful = require("contentful");
|
|
9
8
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
|
10
9
|
var _contentfulManagement = require("contentful-management");
|
|
@@ -32,13 +31,10 @@ function initClient(opts, useCda = false) {
|
|
|
32
31
|
return (0, _contentful.createClient)(cdaConfig).withoutLinkResolution;
|
|
33
32
|
}
|
|
34
33
|
return (0, _contentfulManagement.createClient)(config, {
|
|
35
|
-
|
|
34
|
+
defaults: {
|
|
35
|
+
spaceId: config.spaceId,
|
|
36
|
+
environmentId: config.environmentId
|
|
37
|
+
}
|
|
36
38
|
});
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
return (0, _contentfulManagement.createClient)({
|
|
40
|
-
accessToken: opts.managementToken,
|
|
41
|
-
host: opts.host,
|
|
42
|
-
logHandler
|
|
43
|
-
});
|
|
44
|
-
}
|
|
40
|
+
module.exports = exports.default;
|