@sanity/export 3.44.0 → 3.45.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/README.md CHANGED
@@ -18,6 +18,7 @@ exportDataset({
18
18
  client: someInstantiatedSanityClientInstance,
19
19
 
20
20
  // Name of dataset to export
21
+ // Cannot be combined with `mediaLibraryId`.
21
22
  dataset: 'myDataset',
22
23
 
23
24
  // Path to write tar.gz-archive file to, or `-` for stdout
@@ -47,6 +48,28 @@ exportDataset({
47
48
  // Cursor mode might help when dealing with large datasets, but might yield inconsistent results if the dataset is mutated during export.
48
49
  // Default: 'stream'
49
50
  mode: 'stream',
51
+
52
+ // Export data from a media library, instead of a dataset.
53
+ // Cannot be combined with `dataset`.
54
+ mediaLibraryId: 'myMediaLibrary',
55
+
56
+ // Whether to include the `assets.json` assets map. This file is not necessary when creating a
57
+ // media library archive.
58
+ // Caution: customising this option may result in an archive being produced that is impossible to import.
59
+ // Optional, default: `true`
60
+ assetsMap: true,
61
+
62
+ // A custom filter function for controlling which documents are exported.
63
+ // Optional, default: `() => true`
64
+ filterDocument: document => (document.title ?? '').includes('capybara'),
65
+
66
+ // A custom transformation function for controlling how each document is exported.
67
+ // Caution: customising this option may result in an archive being produced that is impossible to import.
68
+ // Optional, default: `document => document`
69
+ transformDocument: document => ({
70
+ ...document,
71
+ title: document.title ?? 'capybara',
72
+ }),
50
73
  })
51
74
  ```
52
75
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/export",
3
- "version": "3.44.0",
3
+ "version": "3.45.0",
4
4
  "description": "Export Sanity documents and assets",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,7 +1,7 @@
1
1
  const miss = require('mississippi')
2
2
 
3
3
  module.exports = (allowedTypes) =>
4
- allowedTypes
4
+ allowedTypes && allowedTypes.length > 0
5
5
  ? miss.through.obj((doc, enc, callback) => {
6
6
  const type = doc && doc._type
7
7
  if (allowedTypes.includes(type)) {
@@ -72,7 +72,13 @@ function startStream(options, nextCursor) {
72
72
  : `/media-libraries/${options.mediaLibraryId}/export`,
73
73
  )
74
74
 
75
- const url = `${baseUrl}?nextCursor=${encodeURIComponent(nextCursor)}`
75
+ const url = new URL(baseUrl)
76
+ url.searchParams.set('nextCursor', nextCursor)
77
+
78
+ // Type filtering is only supported for datasets.
79
+ if (options.types && options.types.length > 0 && options.dataset) {
80
+ url.searchParams.set('types', options.types.join())
81
+ }
76
82
  const token = options.client.config().token
77
83
  const headers = {
78
84
  'User-Agent': `${pkg.name}@${pkg.version}`,
@@ -81,7 +87,7 @@ function startStream(options, nextCursor) {
81
87
 
82
88
  debug('Starting stream with cursor "%s"', nextCursor)
83
89
 
84
- return requestStream({url, headers, maxRetries: options.maxRetries}).then((res) => {
90
+ return requestStream({url: url.toString(), headers, maxRetries: options.maxRetries}).then((res) => {
85
91
  debug('Got stream with HTTP %d', res.statusCode)
86
92
 
87
93
  return res
@@ -4,11 +4,17 @@ 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 baseUrl = options.client.getUrl(
8
8
  options.dataset
9
9
  ? `/data/export/${options.dataset}`
10
10
  : `/media-libraries/${options.mediaLibraryId}/export`,
11
11
  )
12
+
13
+ // Type filtering is only supported for datasets.
14
+ const url = new URL(baseUrl)
15
+ if (options.types && options.types.length > 0 && options.dataset) {
16
+ url.searchParams.set('types', options.types.join())
17
+ }
12
18
 
13
19
  const token = options.client.config().token
14
20
  const headers = {
@@ -17,7 +23,7 @@ module.exports = (options) => {
17
23
  }
18
24
 
19
25
  return requestStream({
20
- url,
26
+ url: url.toString(),
21
27
  headers,
22
28
  maxRetries: options.maxRetries,
23
29
  readTimeout: options.readTimeout,
@@ -43,6 +43,10 @@ function validateOptions(opts) {
43
43
  )
44
44
  }
45
45
 
46
+ if (options.types && !options.dataset) {
47
+ throw new Error('`options.types` is only supported when exporting from a dataset')
48
+ }
49
+
46
50
  if (
47
51
  typeof options.mode !== 'string' ||
48
52
  (options.mode !== MODE_STREAM && options.mode !== MODE_CURSOR)