@sanity/client
Advanced tools
Comparing version 6.18.3 to 6.19.0
{ | ||
"name": "@sanity/client", | ||
"version": "6.18.3", | ||
"version": "6.19.0", | ||
"description": "Client for retrieving, creating and patching data from Sanity.io", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -9,5 +9,7 @@ import {from, type MonoTypeOperatorFunction, Observable} from 'rxjs' | ||
import type { | ||
Action, | ||
AllDocumentIdsMutationOptions, | ||
AllDocumentsMutationOptions, | ||
Any, | ||
BaseActionOptions, | ||
BaseMutationOptions, | ||
@@ -20,2 +22,3 @@ FirstDocumentIdMutationOptions, | ||
InitializedStegaConfig, | ||
MultipleActionResult, | ||
MultipleMutationResult, | ||
@@ -29,2 +32,3 @@ Mutation, | ||
SanityDocument, | ||
SingleActionResult, | ||
SingleMutationResult, | ||
@@ -247,2 +251,26 @@ } from '../types' | ||
*/ | ||
export function _action( | ||
client: SanityClient | ObservableSanityClient, | ||
httpRequest: HttpRequest, | ||
actions: Action | Action[], | ||
options?: BaseActionOptions, | ||
): Observable<SingleActionResult | MultipleActionResult> { | ||
const acts = Array.isArray(actions) ? actions : [actions] | ||
const transactionId = (options && options.transactionId) || undefined | ||
const skipCrossDatasetReferenceValidation = | ||
(options && options.skipCrossDatasetReferenceValidation) || undefined | ||
const dryRun = (options && options.dryRun) || undefined | ||
return _dataRequest( | ||
client, | ||
httpRequest, | ||
'actions', | ||
{actions: acts, transactionId, skipCrossDatasetReferenceValidation, dryRun}, | ||
options, | ||
) | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
export function _dataRequest( | ||
@@ -256,2 +284,3 @@ client: SanityClient | ObservableSanityClient, | ||
const isMutation = endpoint === 'mutate' | ||
const isAction = endpoint === 'actions' | ||
const isQuery = endpoint === 'query' | ||
@@ -261,4 +290,4 @@ | ||
// in which case we can use GET. Otherwise, use POST. | ||
const strQuery = isMutation ? '' : encodeQueryString(body) | ||
const useGet = !isMutation && strQuery.length < getQuerySizeLimit | ||
const strQuery = isMutation || isAction ? '' : encodeQueryString(body) | ||
const useGet = !isMutation && !isAction && strQuery.length < getQuerySizeLimit | ||
const stringQuery = useGet ? strQuery : '' | ||
@@ -265,0 +294,0 @@ const returnFirst = options.returnFirst |
@@ -1,2 +0,2 @@ | ||
import type {Any, ErrorProps, MutationError} from '../types' | ||
import type {ActionError, Any, ErrorProps, MutationError} from '../types' | ||
@@ -50,3 +50,3 @@ const MAX_ITEMS_IN_ERROR_MESSAGE = 5 | ||
// Mutation errors (specifically) | ||
if (isMutationError(body)) { | ||
if (isMutationError(body) || isActionError(body)) { | ||
const allItems = body.error.items || [] | ||
@@ -87,2 +87,11 @@ const items = allItems | ||
function isActionError(body: Any): body is ActionError { | ||
return ( | ||
isPlainObject(body) && | ||
isPlainObject(body.error) && | ||
body.error.type === 'actionError' && | ||
typeof body.error.description === 'string' | ||
) | ||
} | ||
function isPlainObject(obj: Any): obj is Record<string, unknown> { | ||
@@ -89,0 +98,0 @@ return typeof obj === 'object' && obj !== null && !Array.isArray(obj) |
@@ -13,5 +13,7 @@ import {lastValueFrom, Observable} from 'rxjs' | ||
import type { | ||
Action, | ||
AllDocumentIdsMutationOptions, | ||
AllDocumentsMutationOptions, | ||
Any, | ||
BaseActionOptions, | ||
BaseMutationOptions, | ||
@@ -25,2 +27,3 @@ ClientConfig, | ||
InitializedClientConfig, | ||
MultipleActionResult, | ||
MultipleMutationResult, | ||
@@ -39,2 +42,3 @@ Mutation, | ||
SanityDocumentStub, | ||
SingleActionResult, | ||
SingleMutationResult, | ||
@@ -672,2 +676,15 @@ UnfilteredResponseQueryOptions, | ||
/** | ||
* Perform action operations against the configured dataset | ||
* | ||
* @param operations - Action operation(s) to execute | ||
* @param options - Action options | ||
*/ | ||
action( | ||
operations: Action | Action[], | ||
options?: BaseActionOptions, | ||
): Observable<SingleActionResult | MultipleActionResult> { | ||
return dataMethods._action(this, this.#httpRequest, operations, options) | ||
} | ||
/** | ||
* Perform an HTTP request against the Sanity API | ||
@@ -1330,2 +1347,16 @@ * | ||
/** | ||
* Perform action operations against the configured dataset | ||
* Returns a promise that resolves to the transaction result | ||
* | ||
* @param operations - Action operation(s) to execute | ||
* @param options - Action options | ||
*/ | ||
action( | ||
operations: Action | Action[], | ||
options?: BaseActionOptions, | ||
): Promise<SingleActionResult | MultipleActionResult> { | ||
return lastValueFrom(dataMethods._action(this, this.#httpRequest, operations, options)) | ||
} | ||
/** | ||
* Perform a request against the Sanity API | ||
@@ -1332,0 +1363,0 @@ * NOTE: Only use this for Sanity API endpoints, not for your own APIs! |
223
src/types.ts
@@ -514,3 +514,190 @@ // deno-lint-ignore-file no-empty-interface | ||
/** @public */ | ||
export type Action = | ||
| CreateAction | ||
| ReplaceDraftAction | ||
| EditAction | ||
| DeleteAction | ||
| DiscardAction | ||
| PublishAction | ||
| UnpublishAction | ||
/** | ||
* Creates a new draft document. The published version of the document must not already exist. | ||
* If the draft version of the document already exists the action will fail by default, but | ||
* this can be adjusted to instead leave the existing document in place. | ||
* | ||
* @public | ||
*/ | ||
export type CreateAction = { | ||
actionType: 'sanity.action.document.create' | ||
/** | ||
* ID of the published document to create a draft for. | ||
*/ | ||
publishedId: string | ||
/** | ||
* Document to create. Requires a `_type` property. | ||
*/ | ||
attributes: IdentifiedSanityDocumentStub | ||
/** | ||
* ifExists controls what to do if the draft already exists | ||
*/ | ||
ifExists: 'fail' | 'ignore' | ||
} | ||
/** | ||
* Replaces an existing draft document. | ||
* At least one of the draft or published versions of the document must exist. | ||
* | ||
* @public | ||
*/ | ||
export type ReplaceDraftAction = { | ||
actionType: 'sanity.action.document.replaceDraft' | ||
/** | ||
* Draft document ID to replace, if it exists. | ||
*/ | ||
draftId: string | ||
/** | ||
* Published document ID to create draft from, if draft does not exist | ||
*/ | ||
publishedId: string | ||
/** | ||
* Document to create if it does not already exist. Requires `_id` and `_type` properties. | ||
*/ | ||
attributes: IdentifiedSanityDocumentStub | ||
} | ||
/** | ||
* Modifies an existing draft document. | ||
* It applies the given patch to the document referenced by draftId. | ||
* If there is no such document then one is created using the current state of the published version and then that is updated accordingly. | ||
* | ||
* @public | ||
*/ | ||
export type EditAction = { | ||
actionType: 'sanity.action.document.edit' | ||
/** | ||
* Draft document ID to edit | ||
*/ | ||
draftId: string | ||
/** | ||
* Published document ID to create draft from, if draft does not exist | ||
*/ | ||
publishedId: string | ||
/** | ||
* Patch operations to apply | ||
*/ | ||
patch: PatchOperations | ||
} | ||
/** | ||
* Deletes the published version of a document and optionally some (likely all known) draft versions. | ||
* If any draft version exists that is not specified for deletion this is an error. | ||
* If the purge flag is set then the document history is also deleted. | ||
* | ||
* @public | ||
*/ | ||
export type DeleteAction = { | ||
actionType: 'sanity.action.document.delete' | ||
/** | ||
* Published document ID to delete | ||
*/ | ||
publishedId: string | ||
/** | ||
* Draft document ID to delete | ||
*/ | ||
includeDrafts: string[] | ||
/** | ||
* Delete document history | ||
*/ | ||
purge: boolean | ||
} | ||
/** | ||
* Delete the draft version of a document. | ||
* It is an error if it does not exist. If the purge flag is set, the document history is also deleted. | ||
* | ||
* @public | ||
*/ | ||
export type DiscardAction = { | ||
actionType: 'sanity.action.document.discard' | ||
/** | ||
* Draft document ID to delete | ||
*/ | ||
draftId: string | ||
/** | ||
* Delete document history | ||
*/ | ||
purge: boolean | ||
} | ||
/** | ||
* Publishes a draft document. | ||
* If a published version of the document already exists this is replaced by the current draft document. | ||
* In either case the draft document is deleted. | ||
* The optional revision id parameters can be used for optimistic locking to ensure | ||
* that the draft and/or published versions of the document have not been changed by another client. | ||
* | ||
* @public | ||
*/ | ||
export type PublishAction = { | ||
actionType: 'sanity.action.document.publish' | ||
/** | ||
* Draft document ID to publish | ||
*/ | ||
draftId: string | ||
/** | ||
* Draft revision ID to match | ||
*/ | ||
ifDraftRevisionId: string | ||
/** | ||
* Published document ID to replace | ||
*/ | ||
publishedId: string | ||
/** | ||
* Published revision ID to match | ||
*/ | ||
ifPublishedRevisionId: string | ||
} | ||
/** | ||
* Retract a published document. | ||
* If there is no draft version then this is created from the published version. | ||
* In either case the published version is deleted. | ||
* | ||
* @public | ||
*/ | ||
export type UnpublishAction = { | ||
actionType: 'sanity.action.document.unpublish' | ||
/** | ||
* Draft document ID to replace the published document with | ||
*/ | ||
draftId: string | ||
/** | ||
* Published document ID to delete | ||
*/ | ||
publishedId: string | ||
} | ||
/** | ||
* A mutation was performed. Note that when updating multiple documents in a transaction, | ||
@@ -889,2 +1076,19 @@ * each document affected will get a separate mutation event. | ||
/** @internal */ | ||
export type BaseActionOptions = RequestOptions & { | ||
transactionId?: string | ||
skipCrossDatasetReferenceValidation?: boolean | ||
dryRun?: boolean | ||
} | ||
/** @internal */ | ||
export interface SingleActionResult { | ||
transactionId: string | ||
} | ||
/** @internal */ | ||
export interface MultipleActionResult { | ||
transactionId: string | ||
} | ||
/** @internal */ | ||
export interface RawRequestOptions { | ||
@@ -932,2 +1136,21 @@ url?: string | ||
/** @internal */ | ||
export interface ActionError { | ||
error: { | ||
type: 'actionError' | ||
description: string | ||
items?: ActionErrorItem[] | ||
} | ||
} | ||
/** @internal */ | ||
export interface ActionErrorItem { | ||
error: { | ||
type: string | ||
description: string | ||
value?: unknown | ||
} | ||
index: number | ||
} | ||
/** | ||
@@ -934,0 +1157,0 @@ * DocumentValueSource is a path to a value within a document |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2233263
25273