@sanity/client 7.6.0 → 7.8.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/dist/_chunks-cjs/config.cjs +3 -0
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +3 -0
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +63 -10
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +115 -24
- package/dist/index.browser.d.ts +115 -24
- package/dist/index.browser.js +63 -10
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +62 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +115 -24
- package/dist/index.d.ts +115 -24
- package/dist/index.js +63 -11
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +115 -24
- package/dist/stega.browser.d.ts +115 -24
- package/dist/stega.d.cts +115 -24
- package/dist/stega.d.ts +115 -24
- package/package.json +1 -1
- package/src/SanityClient.ts +61 -21
- package/src/data/dataMethods.ts +39 -2
- package/src/defineCreateClient.ts +5 -1
- package/src/http/errors.ts +53 -0
- package/src/http/request.ts +31 -3
- package/src/types.ts +62 -5
- package/src/warnings.ts +4 -0
- package/umd/sanityClient.js +63 -10
- package/umd/sanityClient.min.js +2 -2
package/dist/stega.d.ts
CHANGED
|
@@ -692,6 +692,30 @@ export declare interface ClientConfig {
|
|
|
692
692
|
*/
|
|
693
693
|
headers?: Record<string, string>
|
|
694
694
|
ignoreBrowserTokenWarning?: boolean
|
|
695
|
+
/**
|
|
696
|
+
* Ignore specific warning messages from the client.
|
|
697
|
+
*
|
|
698
|
+
* @remarks
|
|
699
|
+
* - String values perform substring matching (not exact matching) against warning messages
|
|
700
|
+
* - RegExp values are tested against the full warning message
|
|
701
|
+
* - Array values allow multiple patterns to be specified
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```typescript
|
|
705
|
+
* // Ignore warnings containing "experimental"
|
|
706
|
+
* ignoreWarnings: 'experimental'
|
|
707
|
+
*
|
|
708
|
+
* // Ignore multiple warning types
|
|
709
|
+
* ignoreWarnings: ['experimental', 'deprecated']
|
|
710
|
+
*
|
|
711
|
+
* // Use regex for exact matching
|
|
712
|
+
* ignoreWarnings: /^This is an experimental API version$/
|
|
713
|
+
*
|
|
714
|
+
* // Mix strings and regex patterns
|
|
715
|
+
* ignoreWarnings: ['rate limit', /^deprecated/i]
|
|
716
|
+
* ```
|
|
717
|
+
*/
|
|
718
|
+
ignoreWarnings?: string | RegExp | Array<string | RegExp>
|
|
695
719
|
withCredentials?: boolean
|
|
696
720
|
allowReconfigure?: boolean
|
|
697
721
|
timeout?: number
|
|
@@ -1080,16 +1104,29 @@ export declare interface CreateReleaseAction {
|
|
|
1080
1104
|
}
|
|
1081
1105
|
|
|
1082
1106
|
/**
|
|
1083
|
-
* Creates a new version of an existing document
|
|
1084
|
-
*
|
|
1107
|
+
* Creates a new version of an existing document.
|
|
1108
|
+
*
|
|
1109
|
+
* If the `document` is provided, the version is created from the document
|
|
1110
|
+
* attached to the release as given by `document._id`
|
|
1111
|
+
*
|
|
1112
|
+
* If the `baseId` and `versionId` are provided, the version is created from the base document
|
|
1113
|
+
* and the version is attached to the release as given by `publishedId` and `versionId`
|
|
1085
1114
|
*
|
|
1086
1115
|
* @public
|
|
1087
1116
|
*/
|
|
1088
|
-
export declare
|
|
1117
|
+
export declare type CreateVersionAction = {
|
|
1089
1118
|
actionType: 'sanity.action.document.version.create'
|
|
1090
1119
|
publishedId: string
|
|
1091
|
-
|
|
1092
|
-
|
|
1120
|
+
} & (
|
|
1121
|
+
| {
|
|
1122
|
+
document: IdentifiedSanityDocumentStub
|
|
1123
|
+
}
|
|
1124
|
+
| {
|
|
1125
|
+
baseId: string
|
|
1126
|
+
versionId: string
|
|
1127
|
+
ifBaseRevisionId?: string
|
|
1128
|
+
}
|
|
1129
|
+
)
|
|
1093
1130
|
|
|
1094
1131
|
/** @public */
|
|
1095
1132
|
export declare interface CurrentSanityUser {
|
|
@@ -1385,6 +1422,26 @@ export declare type EventSourceEvent<Name extends string> = ServerSentEvent<Name
|
|
|
1385
1422
|
*/
|
|
1386
1423
|
export declare type EventSourceInstance = InstanceType<typeof globalThis.EventSource>
|
|
1387
1424
|
|
|
1425
|
+
/**
|
|
1426
|
+
* A string constant containing the experimental API version warning message.
|
|
1427
|
+
* Use this with the `ignoreWarnings` option to suppress warnings when using experimental API versions.
|
|
1428
|
+
*
|
|
1429
|
+
* @example
|
|
1430
|
+
* ```typescript
|
|
1431
|
+
* import { createClient, EXPERIMENTAL_API_WARNING } from '@sanity/client'
|
|
1432
|
+
*
|
|
1433
|
+
* const client = createClient({
|
|
1434
|
+
* projectId: 'your-project-id',
|
|
1435
|
+
* dataset: 'production',
|
|
1436
|
+
* apiVersion: 'vX', // experimental version
|
|
1437
|
+
* ignoreWarnings: EXPERIMENTAL_API_WARNING
|
|
1438
|
+
* })
|
|
1439
|
+
* ```
|
|
1440
|
+
*
|
|
1441
|
+
* @public
|
|
1442
|
+
*/
|
|
1443
|
+
export declare const EXPERIMENTAL_API_WARNING = 'This is an experimental API version'
|
|
1444
|
+
|
|
1388
1445
|
/**
|
|
1389
1446
|
*
|
|
1390
1447
|
*
|
|
@@ -1849,6 +1906,25 @@ export declare interface GroqAgentActionParam {
|
|
|
1849
1906
|
params?: Record<string, string>
|
|
1850
1907
|
}
|
|
1851
1908
|
|
|
1909
|
+
/**
|
|
1910
|
+
* Shared properties for HTTP errors (eg both ClientError and ServerError)
|
|
1911
|
+
* Use `isHttpError` for type narrowing and accessing response properties.
|
|
1912
|
+
*
|
|
1913
|
+
* @public
|
|
1914
|
+
*/
|
|
1915
|
+
export declare interface HttpError {
|
|
1916
|
+
statusCode: number
|
|
1917
|
+
message: string
|
|
1918
|
+
response: {
|
|
1919
|
+
body: unknown
|
|
1920
|
+
url: string
|
|
1921
|
+
method: string
|
|
1922
|
+
headers: Record<string, string>
|
|
1923
|
+
statusCode: number
|
|
1924
|
+
statusMessage: string | null
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1852
1928
|
/** @public */
|
|
1853
1929
|
export declare type HttpRequest = {
|
|
1854
1930
|
(options: RequestOptions, requester: Requester): ReturnType<Requester>
|
|
@@ -1968,6 +2044,15 @@ export declare type InsertPatch =
|
|
|
1968
2044
|
items: Any[]
|
|
1969
2045
|
}
|
|
1970
2046
|
|
|
2047
|
+
/**
|
|
2048
|
+
* Checks if the provided error is an HTTP error.
|
|
2049
|
+
*
|
|
2050
|
+
* @param error - The error to check.
|
|
2051
|
+
* @returns `true` if the error is an HTTP error, `false` otherwise.
|
|
2052
|
+
* @public
|
|
2053
|
+
*/
|
|
2054
|
+
export declare function isHttpError(error: unknown): error is HttpError
|
|
2055
|
+
|
|
1971
2056
|
/** @internal */
|
|
1972
2057
|
export declare function isQueryParseError(error: object): error is QueryParseError
|
|
1973
2058
|
|
|
@@ -3224,6 +3309,15 @@ export declare class ObservableSanityClient {
|
|
|
3224
3309
|
},
|
|
3225
3310
|
options?: BaseActionOptions,
|
|
3226
3311
|
): Observable<SingleActionResult | MultipleActionResult>
|
|
3312
|
+
createVersion(
|
|
3313
|
+
args: {
|
|
3314
|
+
baseId: string
|
|
3315
|
+
releaseId: string
|
|
3316
|
+
publishedId: string
|
|
3317
|
+
ifBaseRevisionId?: string
|
|
3318
|
+
},
|
|
3319
|
+
options?: BaseActionOptions,
|
|
3320
|
+
): Observable<SingleActionResult | MultipleActionResult>
|
|
3227
3321
|
/**
|
|
3228
3322
|
* Deletes a document with the given document ID.
|
|
3229
3323
|
* Returns an observable that resolves to the deleted document.
|
|
@@ -4951,7 +5045,8 @@ export declare class SanityClient {
|
|
|
4951
5045
|
* Creates a new version of a published document.
|
|
4952
5046
|
*
|
|
4953
5047
|
* @remarks
|
|
4954
|
-
* *
|
|
5048
|
+
* * The preferred approach is to use `baseId` to refer to the existing published document, but it is also possible to provide a complete `document` instead.
|
|
5049
|
+
* * If `document` is provided, it must have a `_type` property.
|
|
4955
5050
|
* * Creating a version with no `releaseId` will create a new draft version of the published document.
|
|
4956
5051
|
* * If the `document._id` is defined, it should be a draft or release version ID that matches the version ID generated from `publishedId` and `releaseId`.
|
|
4957
5052
|
* * If the `document._id` is not defined, it will be generated from `publishedId` and `releaseId`.
|
|
@@ -4960,17 +5055,18 @@ export declare class SanityClient {
|
|
|
4960
5055
|
* @category Versions
|
|
4961
5056
|
*
|
|
4962
5057
|
* @param params - Version action parameters:
|
|
5058
|
+
* - `baseId` - The ID of the published document from which to create a new version from.
|
|
5059
|
+
* - `ifBaseRevisionId` - If `baseId` is provided, this ensures the `baseId`'s revision Id is as expected before creating the new version from it.
|
|
4963
5060
|
* - `document` - The document to create as a new version (must include `_type`).
|
|
4964
5061
|
* - `publishedId` - The ID of the published document being versioned.
|
|
4965
5062
|
* - `releaseId` - The ID of the release to create the version for.
|
|
4966
5063
|
* @param options - Additional action options.
|
|
4967
5064
|
* @returns A promise that resolves to the `transactionId`.
|
|
4968
5065
|
*
|
|
4969
|
-
* @example Creating a new version of a published document
|
|
5066
|
+
* @example Creating a new version of a published document
|
|
4970
5067
|
* ```ts
|
|
4971
5068
|
* const transactionId = await client.createVersion({
|
|
4972
|
-
*
|
|
4973
|
-
* document: {_type: 'myDocument', title: 'My Document'},
|
|
5069
|
+
* baseId: 'myDocument',
|
|
4974
5070
|
* publishedId: 'myDocument',
|
|
4975
5071
|
* releaseId: 'myRelease',
|
|
4976
5072
|
* })
|
|
@@ -4983,25 +5079,11 @@ export declare class SanityClient {
|
|
|
4983
5079
|
* // }
|
|
4984
5080
|
* ```
|
|
4985
5081
|
*
|
|
4986
|
-
* @example Creating a new version of a published document with a specified version ID
|
|
4987
|
-
* ```ts
|
|
4988
|
-
* const transactionId = await client.createVersion({
|
|
4989
|
-
* document: {_type: 'myDocument', _id: 'versions.myRelease.myDocument', title: 'My Document'},
|
|
4990
|
-
* // `publishedId` and `releaseId` are not required since `document._id` has been specified
|
|
4991
|
-
* })
|
|
4992
|
-
*
|
|
4993
|
-
* // The following document will be created:
|
|
4994
|
-
* // {
|
|
4995
|
-
* // _id: 'versions.myRelease.myDocument',
|
|
4996
|
-
* // _type: 'myDocument',
|
|
4997
|
-
* // title: 'My Document',
|
|
4998
|
-
* // }
|
|
4999
|
-
* ```
|
|
5000
5082
|
*
|
|
5001
5083
|
* @example Creating a new draft version of a published document
|
|
5002
5084
|
* ```ts
|
|
5003
5085
|
* const transactionId = await client.createVersion({
|
|
5004
|
-
*
|
|
5086
|
+
* baseId: 'myDocument',
|
|
5005
5087
|
* publishedId: 'myDocument',
|
|
5006
5088
|
* })
|
|
5007
5089
|
*
|
|
@@ -5029,6 +5111,15 @@ export declare class SanityClient {
|
|
|
5029
5111
|
},
|
|
5030
5112
|
options?: BaseActionOptions,
|
|
5031
5113
|
): Promise<SingleActionResult | MultipleActionResult>
|
|
5114
|
+
createVersion(
|
|
5115
|
+
args: {
|
|
5116
|
+
publishedId: string
|
|
5117
|
+
baseId: string
|
|
5118
|
+
releaseId: string
|
|
5119
|
+
ifBaseRevisionId?: string
|
|
5120
|
+
},
|
|
5121
|
+
options?: BaseActionOptions,
|
|
5122
|
+
): Promise<SingleActionResult | MultipleActionResult>
|
|
5032
5123
|
/**
|
|
5033
5124
|
* Deletes a document with the given document ID.
|
|
5034
5125
|
* Returns a promise that resolves to the deleted document.
|
package/package.json
CHANGED
package/src/SanityClient.ts
CHANGED
|
@@ -543,18 +543,43 @@ export class ObservableSanityClient {
|
|
|
543
543
|
},
|
|
544
544
|
options?: BaseActionOptions,
|
|
545
545
|
): Observable<SingleActionResult | MultipleActionResult>
|
|
546
|
+
createVersion(
|
|
547
|
+
args: {
|
|
548
|
+
baseId: string
|
|
549
|
+
releaseId: string
|
|
550
|
+
publishedId: string
|
|
551
|
+
ifBaseRevisionId?: string
|
|
552
|
+
},
|
|
553
|
+
options?: BaseActionOptions,
|
|
554
|
+
): Observable<SingleActionResult | MultipleActionResult>
|
|
546
555
|
createVersion<R extends Record<string, Any>>(
|
|
547
556
|
{
|
|
548
557
|
document,
|
|
549
558
|
publishedId,
|
|
550
559
|
releaseId,
|
|
560
|
+
baseId,
|
|
561
|
+
ifBaseRevisionId,
|
|
551
562
|
}: {
|
|
552
|
-
document
|
|
563
|
+
document?: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
|
|
553
564
|
publishedId?: string
|
|
554
565
|
releaseId?: string
|
|
566
|
+
baseId?: string
|
|
567
|
+
ifBaseRevisionId?: string
|
|
555
568
|
},
|
|
556
569
|
options?: BaseActionOptions,
|
|
557
570
|
): Observable<SingleActionResult | MultipleActionResult> {
|
|
571
|
+
if (!document) {
|
|
572
|
+
return dataMethods._createVersionFromBase(
|
|
573
|
+
this,
|
|
574
|
+
this.#httpRequest,
|
|
575
|
+
publishedId,
|
|
576
|
+
baseId,
|
|
577
|
+
releaseId,
|
|
578
|
+
ifBaseRevisionId,
|
|
579
|
+
options,
|
|
580
|
+
)
|
|
581
|
+
}
|
|
582
|
+
|
|
558
583
|
const documentVersionId = deriveDocumentVersionId('createVersion', {
|
|
559
584
|
document,
|
|
560
585
|
publishedId,
|
|
@@ -1446,7 +1471,8 @@ export class SanityClient {
|
|
|
1446
1471
|
* Creates a new version of a published document.
|
|
1447
1472
|
*
|
|
1448
1473
|
* @remarks
|
|
1449
|
-
* *
|
|
1474
|
+
* * The preferred approach is to use `baseId` to refer to the existing published document, but it is also possible to provide a complete `document` instead.
|
|
1475
|
+
* * If `document` is provided, it must have a `_type` property.
|
|
1450
1476
|
* * Creating a version with no `releaseId` will create a new draft version of the published document.
|
|
1451
1477
|
* * If the `document._id` is defined, it should be a draft or release version ID that matches the version ID generated from `publishedId` and `releaseId`.
|
|
1452
1478
|
* * If the `document._id` is not defined, it will be generated from `publishedId` and `releaseId`.
|
|
@@ -1455,17 +1481,18 @@ export class SanityClient {
|
|
|
1455
1481
|
* @category Versions
|
|
1456
1482
|
*
|
|
1457
1483
|
* @param params - Version action parameters:
|
|
1484
|
+
* - `baseId` - The ID of the published document from which to create a new version from.
|
|
1485
|
+
* - `ifBaseRevisionId` - If `baseId` is provided, this ensures the `baseId`'s revision Id is as expected before creating the new version from it.
|
|
1458
1486
|
* - `document` - The document to create as a new version (must include `_type`).
|
|
1459
1487
|
* - `publishedId` - The ID of the published document being versioned.
|
|
1460
1488
|
* - `releaseId` - The ID of the release to create the version for.
|
|
1461
1489
|
* @param options - Additional action options.
|
|
1462
1490
|
* @returns A promise that resolves to the `transactionId`.
|
|
1463
1491
|
*
|
|
1464
|
-
* @example Creating a new version of a published document
|
|
1492
|
+
* @example Creating a new version of a published document
|
|
1465
1493
|
* ```ts
|
|
1466
1494
|
* const transactionId = await client.createVersion({
|
|
1467
|
-
*
|
|
1468
|
-
* document: {_type: 'myDocument', title: 'My Document'},
|
|
1495
|
+
* baseId: 'myDocument',
|
|
1469
1496
|
* publishedId: 'myDocument',
|
|
1470
1497
|
* releaseId: 'myRelease',
|
|
1471
1498
|
* })
|
|
@@ -1478,25 +1505,11 @@ export class SanityClient {
|
|
|
1478
1505
|
* // }
|
|
1479
1506
|
* ```
|
|
1480
1507
|
*
|
|
1481
|
-
* @example Creating a new version of a published document with a specified version ID
|
|
1482
|
-
* ```ts
|
|
1483
|
-
* const transactionId = await client.createVersion({
|
|
1484
|
-
* document: {_type: 'myDocument', _id: 'versions.myRelease.myDocument', title: 'My Document'},
|
|
1485
|
-
* // `publishedId` and `releaseId` are not required since `document._id` has been specified
|
|
1486
|
-
* })
|
|
1487
|
-
*
|
|
1488
|
-
* // The following document will be created:
|
|
1489
|
-
* // {
|
|
1490
|
-
* // _id: 'versions.myRelease.myDocument',
|
|
1491
|
-
* // _type: 'myDocument',
|
|
1492
|
-
* // title: 'My Document',
|
|
1493
|
-
* // }
|
|
1494
|
-
* ```
|
|
1495
1508
|
*
|
|
1496
1509
|
* @example Creating a new draft version of a published document
|
|
1497
1510
|
* ```ts
|
|
1498
1511
|
* const transactionId = await client.createVersion({
|
|
1499
|
-
*
|
|
1512
|
+
* baseId: 'myDocument',
|
|
1500
1513
|
* publishedId: 'myDocument',
|
|
1501
1514
|
* })
|
|
1502
1515
|
*
|
|
@@ -1524,18 +1537,45 @@ export class SanityClient {
|
|
|
1524
1537
|
},
|
|
1525
1538
|
options?: BaseActionOptions,
|
|
1526
1539
|
): Promise<SingleActionResult | MultipleActionResult>
|
|
1540
|
+
createVersion(
|
|
1541
|
+
args: {
|
|
1542
|
+
publishedId: string
|
|
1543
|
+
baseId: string
|
|
1544
|
+
releaseId: string
|
|
1545
|
+
ifBaseRevisionId?: string
|
|
1546
|
+
},
|
|
1547
|
+
options?: BaseActionOptions,
|
|
1548
|
+
): Promise<SingleActionResult | MultipleActionResult>
|
|
1527
1549
|
createVersion<R extends Record<string, Any>>(
|
|
1528
1550
|
{
|
|
1529
1551
|
document,
|
|
1530
1552
|
publishedId,
|
|
1531
1553
|
releaseId,
|
|
1554
|
+
baseId,
|
|
1555
|
+
ifBaseRevisionId,
|
|
1532
1556
|
}: {
|
|
1533
|
-
document
|
|
1557
|
+
document?: SanityDocumentStub<R> | IdentifiedSanityDocumentStub<R>
|
|
1534
1558
|
publishedId?: string
|
|
1535
1559
|
releaseId?: string
|
|
1560
|
+
baseId?: string
|
|
1561
|
+
ifBaseRevisionId?: string
|
|
1536
1562
|
},
|
|
1537
1563
|
options?: BaseActionOptions,
|
|
1538
1564
|
): Promise<SingleActionResult | MultipleActionResult> {
|
|
1565
|
+
if (!document) {
|
|
1566
|
+
return firstValueFrom(
|
|
1567
|
+
dataMethods._createVersionFromBase(
|
|
1568
|
+
this,
|
|
1569
|
+
this.#httpRequest,
|
|
1570
|
+
publishedId,
|
|
1571
|
+
baseId,
|
|
1572
|
+
releaseId,
|
|
1573
|
+
ifBaseRevisionId,
|
|
1574
|
+
options,
|
|
1575
|
+
),
|
|
1576
|
+
)
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1539
1579
|
const documentVersionId = deriveDocumentVersionId('createVersion', {
|
|
1540
1580
|
document,
|
|
1541
1581
|
publishedId,
|
package/src/data/dataMethods.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {getVersionFromId, getVersionId, isDraftId} from '@sanity/client/csm'
|
|
1
|
+
import {getDraftId, getVersionFromId, getVersionId, isDraftId} from '@sanity/client/csm'
|
|
2
2
|
import {from, type MonoTypeOperatorFunction, Observable} from 'rxjs'
|
|
3
3
|
import {combineLatestWith, filter, map} from 'rxjs/operators'
|
|
4
4
|
|
|
@@ -39,7 +39,11 @@ import type {
|
|
|
39
39
|
import {getSelection} from '../util/getSelection'
|
|
40
40
|
import * as validate from '../validators'
|
|
41
41
|
import * as validators from '../validators'
|
|
42
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
printCdnPreviewDraftsWarning,
|
|
44
|
+
printCreateVersionWithBaseIdWarning,
|
|
45
|
+
printPreviewDraftsDeprecationWarning,
|
|
46
|
+
} from '../warnings'
|
|
43
47
|
import {encodeQueryString} from './encodeQueryString'
|
|
44
48
|
import {ObservablePatch, Patch} from './patch'
|
|
45
49
|
import {ObservableTransaction, Transaction} from './transaction'
|
|
@@ -266,6 +270,7 @@ export function _createVersion<R extends Record<string, Any>>(
|
|
|
266
270
|
): Observable<SingleActionResult> {
|
|
267
271
|
validators.requireDocumentId('createVersion', doc)
|
|
268
272
|
validators.requireDocumentType('createVersion', doc)
|
|
273
|
+
printCreateVersionWithBaseIdWarning()
|
|
269
274
|
|
|
270
275
|
const createVersionAction: CreateVersionAction = {
|
|
271
276
|
actionType: 'sanity.action.document.version.create',
|
|
@@ -276,6 +281,38 @@ export function _createVersion<R extends Record<string, Any>>(
|
|
|
276
281
|
return _action(client, httpRequest, createVersionAction, options)
|
|
277
282
|
}
|
|
278
283
|
|
|
284
|
+
/** @internal */
|
|
285
|
+
export function _createVersionFromBase(
|
|
286
|
+
client: ObservableSanityClient | SanityClient,
|
|
287
|
+
httpRequest: HttpRequest,
|
|
288
|
+
publishedId?: string,
|
|
289
|
+
baseId?: string,
|
|
290
|
+
releaseId?: string,
|
|
291
|
+
ifBaseRevisionId?: string,
|
|
292
|
+
options?: BaseActionOptions,
|
|
293
|
+
): Observable<SingleActionResult> {
|
|
294
|
+
if (!baseId) {
|
|
295
|
+
throw new Error('`createVersion()` requires `baseId` when no `document` is provided')
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (!publishedId) {
|
|
299
|
+
throw new Error('`createVersion()` requires `publishedId` when `baseId` is provided')
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
validators.validateDocumentId('createVersion', baseId)
|
|
303
|
+
validators.validateDocumentId('createVersion', publishedId)
|
|
304
|
+
|
|
305
|
+
const createVersionAction: CreateVersionAction = {
|
|
306
|
+
actionType: 'sanity.action.document.version.create',
|
|
307
|
+
publishedId,
|
|
308
|
+
baseId,
|
|
309
|
+
versionId: releaseId ? getVersionId(publishedId, releaseId) : getDraftId(publishedId),
|
|
310
|
+
ifBaseRevisionId,
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return _action(client, httpRequest, createVersionAction, options)
|
|
314
|
+
}
|
|
315
|
+
|
|
279
316
|
/** @internal */
|
|
280
317
|
export function _delete<R extends Record<string, Any>>(
|
|
281
318
|
client: Client,
|
|
@@ -21,6 +21,8 @@ export {
|
|
|
21
21
|
ClientError,
|
|
22
22
|
CorsOriginError,
|
|
23
23
|
formatQueryParseError,
|
|
24
|
+
type HttpError,
|
|
25
|
+
isHttpError,
|
|
24
26
|
isQueryParseError,
|
|
25
27
|
ServerError,
|
|
26
28
|
} from './http/errors'
|
|
@@ -45,7 +47,9 @@ export default function defineCreateClientExports<
|
|
|
45
47
|
const defaultRequester = defineHttpRequest(envMiddleware)
|
|
46
48
|
|
|
47
49
|
const createClient = (config: ClientConfigType) => {
|
|
48
|
-
const clientRequester = defineHttpRequest(envMiddleware
|
|
50
|
+
const clientRequester = defineHttpRequest(envMiddleware, {
|
|
51
|
+
ignoreWarnings: config.ignoreWarnings,
|
|
52
|
+
})
|
|
49
53
|
return new ClassConstructor(
|
|
50
54
|
(options, requester) =>
|
|
51
55
|
(requester || clientRequester)({
|
package/src/http/errors.ts
CHANGED
|
@@ -6,6 +6,59 @@ import {isRecord} from '../util/isRecord'
|
|
|
6
6
|
|
|
7
7
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Shared properties for HTTP errors (eg both ClientError and ServerError)
|
|
11
|
+
* Use `isHttpError` for type narrowing and accessing response properties.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface HttpError {
|
|
16
|
+
statusCode: number
|
|
17
|
+
message: string
|
|
18
|
+
response: {
|
|
19
|
+
body: unknown
|
|
20
|
+
url: string
|
|
21
|
+
method: string
|
|
22
|
+
headers: Record<string, string>
|
|
23
|
+
statusCode: number
|
|
24
|
+
statusMessage: string | null
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the provided error is an HTTP error.
|
|
30
|
+
*
|
|
31
|
+
* @param error - The error to check.
|
|
32
|
+
* @returns `true` if the error is an HTTP error, `false` otherwise.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export function isHttpError(error: unknown): error is HttpError {
|
|
36
|
+
if (!isRecord(error)) {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const response = error.response
|
|
41
|
+
if (
|
|
42
|
+
typeof error.statusCode !== 'number' ||
|
|
43
|
+
typeof error.message !== 'string' ||
|
|
44
|
+
!isRecord(response)
|
|
45
|
+
) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
typeof response.body === 'undefined' ||
|
|
51
|
+
typeof response.url !== 'string' ||
|
|
52
|
+
typeof response.method !== 'string' ||
|
|
53
|
+
typeof response.headers !== 'object' ||
|
|
54
|
+
typeof response.statusCode !== 'number'
|
|
55
|
+
) {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
9
62
|
/** @public */
|
|
10
63
|
export class ClientError extends Error {
|
|
11
64
|
response: ErrorProps['response']
|
package/src/http/request.ts
CHANGED
|
@@ -17,14 +17,39 @@ const httpError = {
|
|
|
17
17
|
},
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function printWarnings() {
|
|
20
|
+
function printWarnings(config: {ignoreWarnings?: string | RegExp | Array<string | RegExp>} = {}) {
|
|
21
21
|
const seen: Record<string, boolean> = {}
|
|
22
|
+
|
|
23
|
+
// Helper function to check if a warning should be ignored
|
|
24
|
+
const shouldIgnoreWarning = (message: string): boolean => {
|
|
25
|
+
if (config.ignoreWarnings === undefined) return false
|
|
26
|
+
|
|
27
|
+
const patterns = Array.isArray(config.ignoreWarnings)
|
|
28
|
+
? config.ignoreWarnings
|
|
29
|
+
: [config.ignoreWarnings]
|
|
30
|
+
|
|
31
|
+
return patterns.some((pattern) => {
|
|
32
|
+
if (typeof pattern === 'string') {
|
|
33
|
+
return message.includes(pattern)
|
|
34
|
+
} else if (pattern instanceof RegExp) {
|
|
35
|
+
return pattern.test(message)
|
|
36
|
+
}
|
|
37
|
+
return false
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
22
41
|
return {
|
|
23
42
|
onResponse: (res: Any) => {
|
|
24
43
|
const warn = res.headers['x-sanity-warning']
|
|
25
44
|
const warnings = Array.isArray(warn) ? warn : [warn]
|
|
26
45
|
for (const msg of warnings) {
|
|
27
46
|
if (!msg || seen[msg]) continue
|
|
47
|
+
|
|
48
|
+
// Skip warnings that match ignore patterns
|
|
49
|
+
if (shouldIgnoreWarning(msg)) {
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
|
|
28
53
|
seen[msg] = true
|
|
29
54
|
console.warn(msg) // eslint-disable-line no-console
|
|
30
55
|
}
|
|
@@ -34,11 +59,14 @@ function printWarnings() {
|
|
|
34
59
|
}
|
|
35
60
|
|
|
36
61
|
/** @internal */
|
|
37
|
-
export function defineHttpRequest(
|
|
62
|
+
export function defineHttpRequest(
|
|
63
|
+
envMiddleware: Middlewares,
|
|
64
|
+
config: {ignoreWarnings?: string | RegExp | Array<string | RegExp>} = {},
|
|
65
|
+
): Requester {
|
|
38
66
|
return getIt([
|
|
39
67
|
retry({shouldRetry}),
|
|
40
68
|
...envMiddleware,
|
|
41
|
-
printWarnings(),
|
|
69
|
+
printWarnings(config),
|
|
42
70
|
jsonRequest(),
|
|
43
71
|
jsonResponse(),
|
|
44
72
|
progress(),
|
package/src/types.ts
CHANGED
|
@@ -111,6 +111,30 @@ export interface ClientConfig {
|
|
|
111
111
|
headers?: Record<string, string>
|
|
112
112
|
|
|
113
113
|
ignoreBrowserTokenWarning?: boolean
|
|
114
|
+
/**
|
|
115
|
+
* Ignore specific warning messages from the client.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* - String values perform substring matching (not exact matching) against warning messages
|
|
119
|
+
* - RegExp values are tested against the full warning message
|
|
120
|
+
* - Array values allow multiple patterns to be specified
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Ignore warnings containing "experimental"
|
|
125
|
+
* ignoreWarnings: 'experimental'
|
|
126
|
+
*
|
|
127
|
+
* // Ignore multiple warning types
|
|
128
|
+
* ignoreWarnings: ['experimental', 'deprecated']
|
|
129
|
+
*
|
|
130
|
+
* // Use regex for exact matching
|
|
131
|
+
* ignoreWarnings: /^This is an experimental API version$/
|
|
132
|
+
*
|
|
133
|
+
* // Mix strings and regex patterns
|
|
134
|
+
* ignoreWarnings: ['rate limit', /^deprecated/i]
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
ignoreWarnings?: string | RegExp | Array<string | RegExp>
|
|
114
138
|
withCredentials?: boolean
|
|
115
139
|
allowReconfigure?: boolean
|
|
116
140
|
timeout?: number
|
|
@@ -701,16 +725,29 @@ export interface DeleteReleaseAction {
|
|
|
701
725
|
}
|
|
702
726
|
|
|
703
727
|
/**
|
|
704
|
-
* Creates a new version of an existing document
|
|
705
|
-
*
|
|
728
|
+
* Creates a new version of an existing document.
|
|
729
|
+
*
|
|
730
|
+
* If the `document` is provided, the version is created from the document
|
|
731
|
+
* attached to the release as given by `document._id`
|
|
732
|
+
*
|
|
733
|
+
* If the `baseId` and `versionId` are provided, the version is created from the base document
|
|
734
|
+
* and the version is attached to the release as given by `publishedId` and `versionId`
|
|
706
735
|
*
|
|
707
736
|
* @public
|
|
708
737
|
*/
|
|
709
|
-
export
|
|
738
|
+
export type CreateVersionAction = {
|
|
710
739
|
actionType: 'sanity.action.document.version.create'
|
|
711
740
|
publishedId: string
|
|
712
|
-
|
|
713
|
-
|
|
741
|
+
} & (
|
|
742
|
+
| {
|
|
743
|
+
document: IdentifiedSanityDocumentStub
|
|
744
|
+
}
|
|
745
|
+
| {
|
|
746
|
+
baseId: string
|
|
747
|
+
versionId: string
|
|
748
|
+
ifBaseRevisionId?: string
|
|
749
|
+
}
|
|
750
|
+
)
|
|
714
751
|
|
|
715
752
|
/**
|
|
716
753
|
* Delete a version of a document.
|
|
@@ -1660,3 +1697,23 @@ export type {
|
|
|
1660
1697
|
StudioBaseUrl,
|
|
1661
1698
|
StudioUrl,
|
|
1662
1699
|
} from './stega/types'
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* A string constant containing the experimental API version warning message.
|
|
1703
|
+
* Use this with the `ignoreWarnings` option to suppress warnings when using experimental API versions.
|
|
1704
|
+
*
|
|
1705
|
+
* @example
|
|
1706
|
+
* ```typescript
|
|
1707
|
+
* import { createClient, EXPERIMENTAL_API_WARNING } from '@sanity/client'
|
|
1708
|
+
*
|
|
1709
|
+
* const client = createClient({
|
|
1710
|
+
* projectId: 'your-project-id',
|
|
1711
|
+
* dataset: 'production',
|
|
1712
|
+
* apiVersion: 'vX', // experimental version
|
|
1713
|
+
* ignoreWarnings: EXPERIMENTAL_API_WARNING
|
|
1714
|
+
* })
|
|
1715
|
+
* ```
|
|
1716
|
+
*
|
|
1717
|
+
* @public
|
|
1718
|
+
*/
|
|
1719
|
+
export const EXPERIMENTAL_API_WARNING = 'This is an experimental API version'
|
package/src/warnings.ts
CHANGED
|
@@ -46,3 +46,7 @@ export const printNoApiVersionSpecifiedWarning = createWarningPrinter([
|
|
|
46
46
|
export const printNoDefaultExport = createWarningPrinter([
|
|
47
47
|
'The default export of @sanity/client has been deprecated. Use the named export `createClient` instead.',
|
|
48
48
|
])
|
|
49
|
+
|
|
50
|
+
export const printCreateVersionWithBaseIdWarning = createWarningPrinter([
|
|
51
|
+
'You have called `createVersion()` with a defined `document`. The recommended approach is to provide a `baseId` and `releaseId` instead.',
|
|
52
|
+
])
|