@sanity/export 3.42.2 → 3.44.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.44.0",
4
4
  "description": "Export Sanity documents and assets",
5
5
  "keywords": [
6
6
  "sanity",
package/src/export.js CHANGED
@@ -9,9 +9,7 @@ const JsonStreamStringify = require('json-stream-stringify')
9
9
  const AssetHandler = require('./AssetHandler')
10
10
  const debug = require('./debug')
11
11
  const pipeAsync = require('./util/pipeAsync')
12
- const filterDocumentTypes = require('./filterDocumentTypes')
13
- const filterDrafts = require('./filterDrafts')
14
- const filterSystemDocuments = require('./filterSystemDocuments')
12
+ const filterDocuments = require('./filterDocuments')
15
13
  const getDocumentsStream = require('./getDocumentsStream')
16
14
  const getDocumentCursorStream = require('./getDocumentCursorStream')
17
15
  const logFirstChunk = require('./logFirstChunk')
@@ -47,7 +45,7 @@ async function exportDataset(opts) {
47
45
  .replace(/[^a-z0-9]/gi, '-')
48
46
  .toLowerCase()
49
47
 
50
- const prefix = `${opts.dataset}-export-${slugDate}`
48
+ const prefix = `${opts.dataset ?? opts.mediaLibraryId}-export-${slugDate}`
51
49
  const tmpDir = path.join(os.tmpdir(), prefix)
52
50
  fs.mkdirSync(tmpDir, {recursive: true})
53
51
  const dataPath = path.join(tmpDir, 'data.ndjson')
@@ -156,10 +154,17 @@ async function exportDataset(opts) {
156
154
  logFirstChunk(),
157
155
  split(tryParseJson),
158
156
  rejectOnApiError(),
159
- filterSystemDocuments(),
157
+ filterDocuments(options.drafts),
160
158
  assetStreamHandler,
161
- filterDocumentTypes(options.types),
162
- options.drafts ? miss.through.obj() : filterDrafts(),
159
+ miss.through.obj((doc, _enc, callback) => {
160
+ if (options.filterDocument(doc)) {
161
+ return callback(null, doc)
162
+ }
163
+ return callback()
164
+ }),
165
+ miss.through.obj((doc, _enc, callback) => {
166
+ callback(null, options.transformDocument(doc))
167
+ }),
163
168
  miss.through.obj(reportDocumentCount),
164
169
  stringifyStream(),
165
170
  )
@@ -220,7 +225,11 @@ async function exportDataset(opts) {
220
225
 
221
226
  const assetsStream = fs.createWriteStream(assetsPath)
222
227
  await pipeAsync(new JsonStreamStringify(assetMap), assetsStream)
223
- archive.file(assetsPath, {name: 'assets.json', prefix})
228
+
229
+ if (options.assetsMap) {
230
+ archive.file(assetsPath, {name: 'assets.json', prefix})
231
+ }
232
+
224
233
  clearInterval(progressInterval)
225
234
  } catch (assetErr) {
226
235
  clearInterval(progressInterval)
@@ -1,19 +1,34 @@
1
1
  const miss = require('mississippi')
2
2
  const debug = require('./debug')
3
3
 
4
+ const isDraftOrVersion = (doc) => doc && doc._id && (
5
+ doc._id.indexOf('drafts.') === 0 ||
6
+ doc._id.indexOf('versions.') === 0
7
+ )
8
+
4
9
  const isSystemDocument = (doc) => doc && doc._id && doc._id.indexOf('_.') === 0
10
+ const isRelease = (doc) => doc && doc._id && doc._id.indexOf('_.releases.') === 0
5
11
  const isCursor = (doc) => doc && !doc._id && doc.nextCursor !== undefined
6
12
 
7
- module.exports = () =>
13
+ module.exports = (drafts) =>
8
14
  miss.through.obj((doc, enc, callback) => {
9
- if (isSystemDocument(doc)) {
10
- debug('%s is a system document, skipping', doc && doc._id)
11
- return callback()
12
- }
13
15
  if (isCursor(doc)) {
14
16
  debug('%o is a cursor, skipping', doc)
15
17
  return callback()
16
18
  }
17
19
 
20
+ if (!drafts && isDraftOrVersion(doc)) {
21
+ debug('%s is a draft or version, skipping', doc && doc._id)
22
+ return callback()
23
+ }
24
+
25
+ if (isSystemDocument(doc)) {
26
+ if (!drafts && isRelease(doc)) {
27
+ return callback(null, doc)
28
+ }
29
+ debug('%s is a system document, skipping', doc && doc._id)
30
+ return callback()
31
+ }
32
+
18
33
  return callback(null, doc)
19
- })
34
+ })
@@ -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
 
@@ -1,12 +0,0 @@
1
- const miss = require('mississippi')
2
-
3
- const isDraft = (doc) => doc && doc._id && doc._id.indexOf('drafts.') === 0
4
-
5
- module.exports = () =>
6
- miss.through.obj((doc, enc, callback) => {
7
- if (isDraft(doc)) {
8
- return callback()
9
- }
10
-
11
- return callback(null, doc)
12
- })