@sanity/client 7.1.0-views.0 → 7.1.0-views.2
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 +810 -40
- package/dist/_chunks-cjs/config.cjs +14 -0
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-cjs/dataMethods.cjs +197 -32
- package/dist/_chunks-cjs/dataMethods.cjs.map +1 -1
- package/dist/_chunks-cjs/isRecord.cjs +6 -0
- package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveEditInfo.cjs +3 -5
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaClean.cjs +4 -0
- package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +2 -5
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/config.js +15 -1
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/_chunks-es/dataMethods.js +200 -33
- package/dist/_chunks-es/dataMethods.js.map +1 -1
- package/dist/_chunks-es/isRecord.js +7 -0
- package/dist/_chunks-es/isRecord.js.map +1 -0
- package/dist/_chunks-es/resolveEditInfo.js +1 -3
- package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
- package/dist/_chunks-es/stegaClean.js +4 -0
- package/dist/_chunks-es/stegaClean.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -4
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/index.browser.cjs +1019 -59
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +1950 -149
- package/dist/index.browser.d.ts +1950 -149
- package/dist/index.browser.js +1021 -60
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +825 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1950 -149
- package/dist/index.d.ts +1950 -149
- package/dist/index.js +826 -31
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +1950 -149
- package/dist/stega.browser.d.ts +1950 -149
- package/dist/stega.d.cts +1950 -149
- package/dist/stega.d.ts +1950 -149
- package/dist/views.cjs +21 -5
- package/dist/views.cjs.map +1 -1
- package/dist/views.d.cts +54 -36
- package/dist/views.d.ts +54 -36
- package/dist/views.js +22 -5
- package/dist/views.js.map +1 -1
- package/package.json +2 -1
- package/src/SanityClient.ts +652 -12
- package/src/agent/actions/AgentActionsClient.ts +29 -2
- package/src/agent/actions/commonTypes.ts +57 -17
- package/src/agent/actions/generate.ts +36 -2
- package/src/agent/actions/patch.ts +136 -0
- package/src/agent/actions/prompt.ts +145 -0
- package/src/agent/actions/transform.ts +105 -7
- package/src/agent/actions/translate.ts +5 -2
- package/src/config.ts +3 -1
- package/src/csm/walkMap.ts +1 -1
- package/src/data/dataMethods.ts +170 -12
- package/src/data/encodeQueryString.ts +1 -1
- package/src/data/eventsource.ts +16 -7
- package/src/data/listen.ts +10 -4
- package/src/data/live.ts +13 -5
- package/src/datasets/DatasetsClient.ts +4 -1
- package/src/defineCreateClient.ts +7 -1
- package/src/http/errors.ts +92 -27
- package/src/http/request.ts +3 -3
- package/src/http/requestOptions.ts +4 -0
- package/src/projects/ProjectsClient.ts +6 -2
- package/src/releases/ReleasesClient.ts +693 -0
- package/src/releases/createRelease.ts +53 -0
- package/src/types.ts +293 -10
- package/src/users/UsersClient.ts +7 -3
- package/src/util/codeFrame.ts +174 -0
- package/src/util/createVersionId.ts +79 -0
- package/src/{csm → util}/isRecord.ts +1 -1
- package/src/validators.ts +23 -1
- package/src/views/index.ts +65 -15
- package/umd/sanityClient.js +1067 -61
- package/umd/sanityClient.min.js +2 -2
package/src/http/errors.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {HttpContext} from 'get-it'
|
|
2
|
+
|
|
3
|
+
import type {ActionError, Any, ErrorProps, MutationError, QueryParseError} from '../types'
|
|
4
|
+
import {codeFrame} from '../util/codeFrame'
|
|
5
|
+
import {isRecord} from '../util/isRecord'
|
|
2
6
|
|
|
3
7
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5
|
|
4
8
|
|
|
@@ -9,8 +13,8 @@ export class ClientError extends Error {
|
|
|
9
13
|
responseBody: ErrorProps['responseBody']
|
|
10
14
|
details: ErrorProps['details']
|
|
11
15
|
|
|
12
|
-
constructor(res: Any) {
|
|
13
|
-
const props = extractErrorProps(res)
|
|
16
|
+
constructor(res: Any, context?: HttpContext) {
|
|
17
|
+
const props = extractErrorProps(res, context)
|
|
14
18
|
super(props.message)
|
|
15
19
|
Object.assign(this, props)
|
|
16
20
|
}
|
|
@@ -30,7 +34,7 @@ export class ServerError extends Error {
|
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
function extractErrorProps(res: Any): ErrorProps {
|
|
37
|
+
function extractErrorProps(res: Any, context?: HttpContext): ErrorProps {
|
|
34
38
|
const body = res.body
|
|
35
39
|
const props = {
|
|
36
40
|
response: res,
|
|
@@ -40,15 +44,35 @@ function extractErrorProps(res: Any): ErrorProps {
|
|
|
40
44
|
details: undefined as Any,
|
|
41
45
|
}
|
|
42
46
|
|
|
47
|
+
// Fall back early if we didn't get a JSON object returned as expected
|
|
48
|
+
if (!isRecord(body)) {
|
|
49
|
+
props.message = httpErrorMessage(res, body)
|
|
50
|
+
return props
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const error = body.error
|
|
54
|
+
|
|
43
55
|
// API/Boom style errors ({statusCode, error, message})
|
|
44
|
-
if (
|
|
45
|
-
props.message = `${
|
|
56
|
+
if (typeof error === 'string' && typeof body.message === 'string') {
|
|
57
|
+
props.message = `${error} - ${body.message}`
|
|
58
|
+
return props
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Content Lake errors with a `error` prop being an object
|
|
62
|
+
if (typeof error !== 'object' || error === null) {
|
|
63
|
+
if (typeof error === 'string') {
|
|
64
|
+
props.message = error
|
|
65
|
+
} else if (typeof body.message === 'string') {
|
|
66
|
+
props.message = body.message
|
|
67
|
+
} else {
|
|
68
|
+
props.message = httpErrorMessage(res, body)
|
|
69
|
+
}
|
|
46
70
|
return props
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
// Mutation errors (specifically)
|
|
50
|
-
if (isMutationError(
|
|
51
|
-
const allItems =
|
|
74
|
+
if (isMutationError(error) || isActionError(error)) {
|
|
75
|
+
const allItems = error.items || []
|
|
52
76
|
const items = allItems
|
|
53
77
|
.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE)
|
|
54
78
|
.map((item) => item.error?.description)
|
|
@@ -57,48 +81,85 @@ function extractErrorProps(res: Any): ErrorProps {
|
|
|
57
81
|
if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
|
|
58
82
|
itemsStr += `\n...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`
|
|
59
83
|
}
|
|
60
|
-
props.message = `${
|
|
84
|
+
props.message = `${error.description}${itemsStr}`
|
|
61
85
|
props.details = body.error
|
|
62
86
|
return props
|
|
63
87
|
}
|
|
64
88
|
|
|
65
|
-
// Query
|
|
66
|
-
if (
|
|
67
|
-
|
|
89
|
+
// Query parse errors
|
|
90
|
+
if (isQueryParseError(error)) {
|
|
91
|
+
const tag = context?.options?.query?.tag
|
|
92
|
+
props.message = formatQueryParseError(error, tag)
|
|
68
93
|
props.details = body.error
|
|
69
94
|
return props
|
|
70
95
|
}
|
|
71
96
|
|
|
97
|
+
if ('description' in error && typeof error.description === 'string') {
|
|
98
|
+
// Query/database errors ({error: {description, other, arb, props}})
|
|
99
|
+
props.message = error.description
|
|
100
|
+
props.details = error
|
|
101
|
+
return props
|
|
102
|
+
}
|
|
103
|
+
|
|
72
104
|
// Other, more arbitrary errors
|
|
73
|
-
props.message =
|
|
105
|
+
props.message = httpErrorMessage(res, body)
|
|
74
106
|
return props
|
|
75
107
|
}
|
|
76
108
|
|
|
77
|
-
function isMutationError(
|
|
109
|
+
function isMutationError(error: object): error is MutationError {
|
|
110
|
+
return (
|
|
111
|
+
'type' in error &&
|
|
112
|
+
error.type === 'mutationError' &&
|
|
113
|
+
'description' in error &&
|
|
114
|
+
typeof error.description === 'string'
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function isActionError(error: object): error is ActionError {
|
|
78
119
|
return (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
typeof
|
|
120
|
+
'type' in error &&
|
|
121
|
+
error.type === 'actionError' &&
|
|
122
|
+
'description' in error &&
|
|
123
|
+
typeof error.description === 'string'
|
|
83
124
|
)
|
|
84
125
|
}
|
|
85
126
|
|
|
86
|
-
|
|
127
|
+
/** @internal */
|
|
128
|
+
export function isQueryParseError(error: object): error is QueryParseError {
|
|
87
129
|
return (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
typeof
|
|
130
|
+
isRecord(error) &&
|
|
131
|
+
error.type === 'queryParseError' &&
|
|
132
|
+
typeof error.query === 'string' &&
|
|
133
|
+
typeof error.start === 'number' &&
|
|
134
|
+
typeof error.end === 'number'
|
|
92
135
|
)
|
|
93
136
|
}
|
|
94
137
|
|
|
95
|
-
|
|
96
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Formats a GROQ query parse error into a human-readable string.
|
|
140
|
+
*
|
|
141
|
+
* @param error - The error object containing details about the parse error.
|
|
142
|
+
* @param tag - An optional tag to include in the error message.
|
|
143
|
+
* @returns A formatted error message string.
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export function formatQueryParseError(error: QueryParseError, tag?: string | null) {
|
|
147
|
+
const {query, start, end, description} = error
|
|
148
|
+
|
|
149
|
+
if (!query || typeof start === 'undefined') {
|
|
150
|
+
return `GROQ query parse error: ${description}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const withTag = tag ? `\n\nTag: ${tag}` : ''
|
|
154
|
+
const framed = codeFrame(query, {start, end}, description)
|
|
155
|
+
|
|
156
|
+
return `GROQ query parse error:\n${framed}${withTag}`
|
|
97
157
|
}
|
|
98
158
|
|
|
99
|
-
function httpErrorMessage(res: Any) {
|
|
159
|
+
function httpErrorMessage(res: Any, body: unknown) {
|
|
160
|
+
const details = typeof body === 'string' ? ` (${sliceWithEllipsis(body, 100)})` : ''
|
|
100
161
|
const statusMessage = res.statusMessage ? ` ${res.statusMessage}` : ''
|
|
101
|
-
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}`
|
|
162
|
+
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`
|
|
102
163
|
}
|
|
103
164
|
|
|
104
165
|
function stringifyBody(body: Any, res: Any) {
|
|
@@ -107,6 +168,10 @@ function stringifyBody(body: Any, res: Any) {
|
|
|
107
168
|
return isJson ? JSON.stringify(body, null, 2) : body
|
|
108
169
|
}
|
|
109
170
|
|
|
171
|
+
function sliceWithEllipsis(str: string, max: number) {
|
|
172
|
+
return str.length > max ? `${str.slice(0, max)}…` : str
|
|
173
|
+
}
|
|
174
|
+
|
|
110
175
|
/** @public */
|
|
111
176
|
export class CorsOriginError extends Error {
|
|
112
177
|
projectId: string
|
package/src/http/request.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {getIt, type Middlewares, type Requester} from 'get-it'
|
|
1
|
+
import {getIt, type HttpContext, type Middlewares, type Requester} from 'get-it'
|
|
2
2
|
import {jsonRequest, jsonResponse, observable, progress, retry} from 'get-it/middleware'
|
|
3
3
|
import {Observable} from 'rxjs'
|
|
4
4
|
|
|
@@ -6,11 +6,11 @@ import type {Any} from '../types'
|
|
|
6
6
|
import {ClientError, ServerError} from './errors'
|
|
7
7
|
|
|
8
8
|
const httpError = {
|
|
9
|
-
onResponse: (res: Any) => {
|
|
9
|
+
onResponse: (res: Any, context: HttpContext) => {
|
|
10
10
|
if (res.statusCode >= 500) {
|
|
11
11
|
throw new ServerError(res)
|
|
12
12
|
} else if (res.statusCode >= 400) {
|
|
13
|
-
throw new ClientError(res)
|
|
13
|
+
throw new ClientError(res, context)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
return res
|
|
@@ -7,6 +7,10 @@ const projectHeader = 'X-Sanity-Project-ID'
|
|
|
7
7
|
export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOptions, 'url'> {
|
|
8
8
|
const headers: Any = {}
|
|
9
9
|
|
|
10
|
+
if (config.headers) {
|
|
11
|
+
Object.assign(headers, config.headers)
|
|
12
|
+
}
|
|
13
|
+
|
|
10
14
|
const token = overrides.token || config.token
|
|
11
15
|
if (token) {
|
|
12
16
|
headers.Authorization = `Bearer ${token}`
|
|
@@ -37,7 +37,9 @@ export class ObservableProjectsClient {
|
|
|
37
37
|
*/
|
|
38
38
|
getById(projectId: string): Observable<SanityProject> {
|
|
39
39
|
validate.resourceGuard('projects', this.#client.config())
|
|
40
|
-
return _request<SanityProject>(this.#client.config(), this.#httpRequest, {
|
|
40
|
+
return _request<SanityProject>(this.#client.config(), this.#httpRequest, {
|
|
41
|
+
uri: `/projects/${projectId}`,
|
|
42
|
+
})
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -72,7 +74,9 @@ export class ProjectsClient {
|
|
|
72
74
|
getById(projectId: string): Promise<SanityProject> {
|
|
73
75
|
validate.resourceGuard('projects', this.#client.config())
|
|
74
76
|
return lastValueFrom(
|
|
75
|
-
_request<SanityProject>(this.#client.config(), this.#httpRequest, {
|
|
77
|
+
_request<SanityProject>(this.#client.config(), this.#httpRequest, {
|
|
78
|
+
uri: `/projects/${projectId}`,
|
|
79
|
+
}),
|
|
76
80
|
)
|
|
77
81
|
}
|
|
78
82
|
}
|