@sanity/export 3.42.1 → 3.43.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/package.json +1 -1
- package/src/AssetHandler.js +3 -1
- package/src/export.js +15 -2
- package/src/getDocumentCursorStream.js +6 -2
- package/src/getDocumentsStream.js +6 -1
- package/src/validateOptions.js +35 -2
package/package.json
CHANGED
package/src/AssetHandler.js
CHANGED
|
@@ -313,7 +313,9 @@ class AssetHandler {
|
|
|
313
313
|
findAndModify = (item, action) => {
|
|
314
314
|
if (Array.isArray(item)) {
|
|
315
315
|
const children = item.map((child) => this.findAndModify(child, action))
|
|
316
|
-
return children.filter(
|
|
316
|
+
return children.filter(function (child) {
|
|
317
|
+
return child !== null && child !== undefined
|
|
318
|
+
})
|
|
317
319
|
}
|
|
318
320
|
|
|
319
321
|
if (!item || typeof item !== 'object') {
|
package/src/export.js
CHANGED
|
@@ -47,7 +47,7 @@ async function exportDataset(opts) {
|
|
|
47
47
|
.replace(/[^a-z0-9]/gi, '-')
|
|
48
48
|
.toLowerCase()
|
|
49
49
|
|
|
50
|
-
const prefix = `${opts.dataset}-export-${slugDate}`
|
|
50
|
+
const prefix = `${opts.dataset ?? opts.mediaLibraryId}-export-${slugDate}`
|
|
51
51
|
const tmpDir = path.join(os.tmpdir(), prefix)
|
|
52
52
|
fs.mkdirSync(tmpDir, {recursive: true})
|
|
53
53
|
const dataPath = path.join(tmpDir, 'data.ndjson')
|
|
@@ -160,6 +160,15 @@ async function exportDataset(opts) {
|
|
|
160
160
|
assetStreamHandler,
|
|
161
161
|
filterDocumentTypes(options.types),
|
|
162
162
|
options.drafts ? miss.through.obj() : filterDrafts(),
|
|
163
|
+
miss.through.obj((doc, _enc, callback) => {
|
|
164
|
+
if (options.filterDocument(doc)) {
|
|
165
|
+
return callback(null, doc)
|
|
166
|
+
}
|
|
167
|
+
return callback()
|
|
168
|
+
}),
|
|
169
|
+
miss.through.obj((doc, _enc, callback) => {
|
|
170
|
+
callback(null, options.transformDocument(doc))
|
|
171
|
+
}),
|
|
163
172
|
miss.through.obj(reportDocumentCount),
|
|
164
173
|
stringifyStream(),
|
|
165
174
|
)
|
|
@@ -220,7 +229,11 @@ async function exportDataset(opts) {
|
|
|
220
229
|
|
|
221
230
|
const assetsStream = fs.createWriteStream(assetsPath)
|
|
222
231
|
await pipeAsync(new JsonStreamStringify(assetMap), assetsStream)
|
|
223
|
-
|
|
232
|
+
|
|
233
|
+
if (options.assetsMap) {
|
|
234
|
+
archive.file(assetsPath, {name: 'assets.json', prefix})
|
|
235
|
+
}
|
|
236
|
+
|
|
224
237
|
clearInterval(progressInterval)
|
|
225
238
|
} catch (assetErr) {
|
|
226
239
|
clearInterval(progressInterval)
|
|
@@ -66,9 +66,13 @@ module.exports = async (options) => {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
function startStream(options, nextCursor) {
|
|
69
|
-
const
|
|
70
|
-
|
|
69
|
+
const baseUrl = options.client.getUrl(
|
|
70
|
+
options.dataset
|
|
71
|
+
? `/data/export/${options.dataset}`
|
|
72
|
+
: `/media-libraries/${options.mediaLibraryId}/export`,
|
|
71
73
|
)
|
|
74
|
+
|
|
75
|
+
const url = `${baseUrl}?nextCursor=${encodeURIComponent(nextCursor)}`
|
|
72
76
|
const token = options.client.config().token
|
|
73
77
|
const headers = {
|
|
74
78
|
'User-Agent': `${pkg.name}@${pkg.version}`,
|
|
@@ -4,7 +4,12 @@ const requestStream = require('./requestStream')
|
|
|
4
4
|
module.exports = (options) => {
|
|
5
5
|
// Sanity client doesn't handle streams natively since we want to support node/browser
|
|
6
6
|
// with same API. We're just using it here to get hold of URLs and tokens.
|
|
7
|
-
const url = options.client.getUrl(
|
|
7
|
+
const url = options.client.getUrl(
|
|
8
|
+
options.dataset
|
|
9
|
+
? `/data/export/${options.dataset}`
|
|
10
|
+
: `/media-libraries/${options.mediaLibraryId}/export`,
|
|
11
|
+
)
|
|
12
|
+
|
|
8
13
|
const token = options.client.config().token
|
|
9
14
|
const headers = {
|
|
10
15
|
'User-Agent': `${pkg.name}@${pkg.version}`,
|
package/src/validateOptions.js
CHANGED
|
@@ -14,18 +14,33 @@ const exportDefaults = {
|
|
|
14
14
|
compress: true,
|
|
15
15
|
drafts: true,
|
|
16
16
|
assets: true,
|
|
17
|
+
assetsMap: true,
|
|
17
18
|
raw: false,
|
|
18
19
|
mode: MODE_STREAM,
|
|
19
20
|
maxRetries: DOCUMENT_STREAM_MAX_RETRIES,
|
|
20
21
|
maxAssetRetries: ASSET_DOWNLOAD_MAX_RETRIES,
|
|
21
22
|
readTimeout: REQUEST_READ_TIMEOUT,
|
|
23
|
+
filterDocument: () => true,
|
|
24
|
+
transformDocument: (doc) => doc,
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
function validateOptions(opts) {
|
|
25
28
|
const options = defaults({}, opts, exportDefaults)
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
const resources = [options.dataset, options.mediaLibraryId].filter(
|
|
31
|
+
(resource) => typeof resource === 'string' && resource.length !== 0,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if (resources.length === 0) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
'either `options.dataset` or `options.mediaLibraryId` must be specified, got neither',
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (resources.length === 2) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
'either `options.dataset` or `options.mediaLibraryId` must be specified, got both',
|
|
43
|
+
)
|
|
29
44
|
}
|
|
30
45
|
|
|
31
46
|
if (
|
|
@@ -77,6 +92,24 @@ function validateOptions(opts) {
|
|
|
77
92
|
throw new Error('`assetConcurrency` must be between 1 and 24')
|
|
78
93
|
}
|
|
79
94
|
|
|
95
|
+
if (
|
|
96
|
+
typeof options.filterDocument !== 'undefined' &&
|
|
97
|
+
typeof options.filterDocument !== 'function'
|
|
98
|
+
) {
|
|
99
|
+
throw new Error('`filterDocument` must be a function')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (
|
|
103
|
+
typeof options.transformDocument !== 'undefined' &&
|
|
104
|
+
typeof options.transformDocument !== 'function'
|
|
105
|
+
) {
|
|
106
|
+
throw new Error('`transformDocument` must be a function')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof assetsMap !== 'undefined' && typeof assetsMap !== 'boolean') {
|
|
110
|
+
throw new Error('`assetsMap` must be a boolean')
|
|
111
|
+
}
|
|
112
|
+
|
|
80
113
|
return options
|
|
81
114
|
}
|
|
82
115
|
|