@sanity/client 6.20.0-canary.0 → 6.20.1

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
@@ -101,6 +101,15 @@ export async function updateDocumentTitle(_id, title) {
101
101
  - [2. Cancel a request by unsubscribing from the Observable](#2-cancel-a-request-by-unsubscribing-from-the-observable)
102
102
  - [Get client configuration](#get-client-configuration)
103
103
  - [Set client configuration](#set-client-configuration)
104
+ - [Server Side Actions](#server-side-actions)
105
+ - [Action options](#action-options)
106
+ - [Create Action](#create-action)
107
+ - [Delete Action](#delete-action)
108
+ - [Discard Action](#discard-action)
109
+ - [Edit Action](#edit-action)
110
+ - [Publish Action](#publish-action)
111
+ - [ReplaceDraft Action](#replacedraft-action)
112
+ - [Unpublish Action](#unpublish-action)
104
113
  - [License](#license)
105
114
  - [From `v5`](#from-v5)
106
115
  - [The default `useCdn` is changed to `true`](#the-default-usecdn-is-changed-to-true)
@@ -1148,6 +1157,176 @@ client.mutate(transaction)
1148
1157
 
1149
1158
  An important note on this approach is that you cannot call `commit()` on transactions or patches instantiated this way, instead you have to pass them to `client.mutate()`
1150
1159
 
1160
+ ### Server Side Actions
1161
+
1162
+ The Server Side Actions API provides a new interface for creating, updating and publishing documents. It is a wrapper around the [Server Side Actions API](https://www.sanity.io/docs/http-actions).
1163
+
1164
+ This API is only available from API version `v2024-05-23`.
1165
+
1166
+ #### Action options
1167
+
1168
+ The following options are available for actions, and can be applied as the second argument to `action()`.
1169
+
1170
+ - `transactionId`: If set, this ID is as transaction ID for the action instead of using an autogenerated one.
1171
+ - `dryRun` (`true|false`) - default `false`. If true, the mutation will be a dry run - the response will be identical to the one returned had this property been omitted or false (including error responses) but no documents will be affected.
1172
+ - `skipCrossDatasetReferenceValidation` (`true|false`) - default `false`. If true, the mutation will be skipped validation of cross dataset references. This is useful when you are creating a document that references a document in a different dataset, and you want to skip the validation to avoid an error.
1173
+
1174
+ #### Create Action
1175
+
1176
+ A document draft can be created by specifying a create action type:
1177
+
1178
+ ```js
1179
+ client
1180
+ .action(
1181
+ {
1182
+ actionType: 'sanity.action.document.create',
1183
+ publishedId: 'bike-123',
1184
+ attributes: {name: 'Sanity Tandem Extraordinaire', _type: 'bike', seats: 1},
1185
+ ifExists: 'fail',
1186
+ },
1187
+ actionOptions,
1188
+ )
1189
+ .then(() => {
1190
+ console.log('Bike draft created')
1191
+ })
1192
+ .catch((err) => {
1193
+ console.error('Create draft failed: ', err.message)
1194
+ })
1195
+ ```
1196
+
1197
+ #### Delete Action
1198
+
1199
+ A published document can be deleted by specifying a delete action type, optionally including some drafts:
1200
+
1201
+ ```js
1202
+ client
1203
+ .action(
1204
+ {
1205
+ actionType: 'sanity.action.document.delete',
1206
+ publishedId: 'bike-123',
1207
+ includeDrafts: ['draft.bike-123'],
1208
+ },
1209
+ actionOptions,
1210
+ )
1211
+ .then(() => {
1212
+ console.log('Bike deleted')
1213
+ })
1214
+ .catch((err) => {
1215
+ console.error('Delete failed: ', err.message)
1216
+ })
1217
+ ```
1218
+
1219
+ #### Discard Action
1220
+
1221
+ A draft document can be deleted by specifying a discard action type:
1222
+
1223
+ ```js
1224
+ client
1225
+ .action(
1226
+ {
1227
+ actionType: 'sanity.action.document.discard',
1228
+ draftId: 'draft.bike-123',
1229
+ },
1230
+ actionOptions,
1231
+ )
1232
+ .then(() => {
1233
+ console.log('Bike draft deleted')
1234
+ })
1235
+ .catch((err) => {
1236
+ console.error('Discard failed: ', err.message)
1237
+ })
1238
+ ```
1239
+
1240
+ #### Edit Action
1241
+
1242
+ A patch can be applied to an existing document draft or create a new one by specifying an edit action type:
1243
+
1244
+ ```js
1245
+ client
1246
+ .action(
1247
+ {
1248
+ actionType: 'sanity.action.document.edit',
1249
+ publishedId: 'bike-123',
1250
+ attributes: {name: 'Sanity Tandem Extraordinaire', _type: 'bike', seats: 2},
1251
+ },
1252
+ actionOptions,
1253
+ )
1254
+ .then(() => {
1255
+ console.log('Bike draft edited')
1256
+ })
1257
+ .catch((err) => {
1258
+ console.error('Edit draft failed: ', err.message)
1259
+ })
1260
+ ```
1261
+
1262
+ #### Publish Action
1263
+
1264
+ A draft document can be published by specifying a publish action type, optionally with revision ID checks:
1265
+
1266
+ ```js
1267
+ client
1268
+ .action(
1269
+ {
1270
+ actionType: 'sanity.action.document.publish',
1271
+ draftId: 'draft.bike-123',
1272
+ ifDraftRevisionId: '<previously-known-revision>',
1273
+ publishedId: 'bike-123',
1274
+ ifPublishedRevisionId: '<previously-known-revision>',
1275
+ },
1276
+ actionOptions,
1277
+ )
1278
+ .then(() => {
1279
+ console.log('Bike draft published')
1280
+ })
1281
+ .catch((err) => {
1282
+ console.error('Publish draft failed: ', err.message)
1283
+ })
1284
+ ```
1285
+
1286
+ #### ReplaceDraft Action
1287
+
1288
+ An existing document draft can be deleted and replaced by a new one by specifying a replaceDraft action type:
1289
+
1290
+ ```js
1291
+ client
1292
+ .action(
1293
+ {
1294
+ actionType: 'sanity.action.document.replaceDraft',
1295
+ publishedId: 'bike-123',
1296
+ attributes: {name: 'Sanity Tandem Extraordinaire', _type: 'bike', seats: 1},
1297
+ },
1298
+ actionOptions,
1299
+ )
1300
+ .then(() => {
1301
+ console.log('Bike draft replaced')
1302
+ })
1303
+ .catch((err) => {
1304
+ console.error('Replace draft failed: ', err.message)
1305
+ })
1306
+ ```
1307
+
1308
+ #### Unpublish Action
1309
+
1310
+ A published document can be retracted by specifying an unpublish action type:
1311
+
1312
+ ```js
1313
+ client
1314
+ .action(
1315
+ {
1316
+ actionType: 'sanity.action.document.unpublish',
1317
+ draftId: 'draft.bike-123',
1318
+ publishedId: 'bike-123',
1319
+ },
1320
+ actionOptions,
1321
+ )
1322
+ .then(() => {
1323
+ console.log('Bike draft unpublished')
1324
+ })
1325
+ .catch((err) => {
1326
+ console.error('Unpublish draft failed: ', err.message)
1327
+ })
1328
+ ```
1329
+
1151
1330
  ### Uploading assets
1152
1331
 
1153
1332
  Assets can be uploaded using the `client.assets.upload(...)` method.
@@ -506,7 +506,10 @@ function once(fn) {
506
506
  const createWarningPrinter = (message) => (
507
507
  // eslint-disable-next-line no-console
508
508
  once((...args) => console.warn(message.join(" "), ...args))
509
- ), printCdnWarning = createWarningPrinter([
509
+ ), printCdnAndWithCredentialsWarning = createWarningPrinter([
510
+ "Because you set `withCredentials` to true, we will override your `useCdn`",
511
+ "setting to be false since (cookie-based) credentials are never set on the CDN"
512
+ ]), printCdnWarning = createWarningPrinter([
510
513
  "Since you haven't set a value for `useCdn`, we will deliver content using our",
511
514
  "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
512
515
  "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
@@ -584,7 +587,7 @@ const validateApiPerspective = function(perspective) {
584
587
  `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
585
588
  );
586
589
  const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
587
- isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== !0 ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn = newConfig.useCdn !== !1 && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
590
+ isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== !0 ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === !0 && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== !1 && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
588
591
  const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
589
592
  return newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
590
593
  }, projectHeader = "X-Sanity-Project-ID";