@sanity/export 3.42.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/export",
3
- "version": "3.42.2",
3
+ "version": "3.43.0",
4
4
  "description": "Export Sanity documents and assets",
5
5
  "keywords": [
6
6
  "sanity",
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
- archive.file(assetsPath, {name: 'assets.json', prefix})
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 url = options.client.getUrl(
70
- `/data/export/${options.dataset}?nextCursor=${encodeURIComponent(nextCursor)}`,
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(`/data/export/${options.dataset}`)
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}`,
@@ -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
- if (typeof options.dataset !== 'string' || options.dataset.length < 1) {
28
- throw new Error(`options.dataset must be a valid dataset name`)
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