@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.
Files changed (80) hide show
  1. package/README.md +810 -40
  2. package/dist/_chunks-cjs/config.cjs +14 -0
  3. package/dist/_chunks-cjs/config.cjs.map +1 -1
  4. package/dist/_chunks-cjs/dataMethods.cjs +197 -32
  5. package/dist/_chunks-cjs/dataMethods.cjs.map +1 -1
  6. package/dist/_chunks-cjs/isRecord.cjs +6 -0
  7. package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
  8. package/dist/_chunks-cjs/resolveEditInfo.cjs +3 -5
  9. package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
  10. package/dist/_chunks-cjs/stegaClean.cjs +4 -0
  11. package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
  12. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +2 -5
  13. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
  14. package/dist/_chunks-es/config.js +15 -1
  15. package/dist/_chunks-es/config.js.map +1 -1
  16. package/dist/_chunks-es/dataMethods.js +200 -33
  17. package/dist/_chunks-es/dataMethods.js.map +1 -1
  18. package/dist/_chunks-es/isRecord.js +7 -0
  19. package/dist/_chunks-es/isRecord.js.map +1 -0
  20. package/dist/_chunks-es/resolveEditInfo.js +1 -3
  21. package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
  22. package/dist/_chunks-es/stegaClean.js +4 -0
  23. package/dist/_chunks-es/stegaClean.js.map +1 -1
  24. package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -4
  25. package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
  26. package/dist/index.browser.cjs +1019 -59
  27. package/dist/index.browser.cjs.map +1 -1
  28. package/dist/index.browser.d.cts +1950 -149
  29. package/dist/index.browser.d.ts +1950 -149
  30. package/dist/index.browser.js +1021 -60
  31. package/dist/index.browser.js.map +1 -1
  32. package/dist/index.cjs +825 -29
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +1950 -149
  35. package/dist/index.d.ts +1950 -149
  36. package/dist/index.js +826 -31
  37. package/dist/index.js.map +1 -1
  38. package/dist/stega.browser.d.cts +1950 -149
  39. package/dist/stega.browser.d.ts +1950 -149
  40. package/dist/stega.d.cts +1950 -149
  41. package/dist/stega.d.ts +1950 -149
  42. package/dist/views.cjs +21 -5
  43. package/dist/views.cjs.map +1 -1
  44. package/dist/views.d.cts +54 -36
  45. package/dist/views.d.ts +54 -36
  46. package/dist/views.js +22 -5
  47. package/dist/views.js.map +1 -1
  48. package/package.json +2 -1
  49. package/src/SanityClient.ts +652 -12
  50. package/src/agent/actions/AgentActionsClient.ts +29 -2
  51. package/src/agent/actions/commonTypes.ts +57 -17
  52. package/src/agent/actions/generate.ts +36 -2
  53. package/src/agent/actions/patch.ts +136 -0
  54. package/src/agent/actions/prompt.ts +145 -0
  55. package/src/agent/actions/transform.ts +105 -7
  56. package/src/agent/actions/translate.ts +5 -2
  57. package/src/config.ts +3 -1
  58. package/src/csm/walkMap.ts +1 -1
  59. package/src/data/dataMethods.ts +170 -12
  60. package/src/data/encodeQueryString.ts +1 -1
  61. package/src/data/eventsource.ts +16 -7
  62. package/src/data/listen.ts +10 -4
  63. package/src/data/live.ts +13 -5
  64. package/src/datasets/DatasetsClient.ts +4 -1
  65. package/src/defineCreateClient.ts +7 -1
  66. package/src/http/errors.ts +92 -27
  67. package/src/http/request.ts +3 -3
  68. package/src/http/requestOptions.ts +4 -0
  69. package/src/projects/ProjectsClient.ts +6 -2
  70. package/src/releases/ReleasesClient.ts +693 -0
  71. package/src/releases/createRelease.ts +53 -0
  72. package/src/types.ts +293 -10
  73. package/src/users/UsersClient.ts +7 -3
  74. package/src/util/codeFrame.ts +174 -0
  75. package/src/util/createVersionId.ts +79 -0
  76. package/src/{csm → util}/isRecord.ts +1 -1
  77. package/src/validators.ts +23 -1
  78. package/src/views/index.ts +65 -15
  79. package/umd/sanityClient.js +1067 -61
  80. package/umd/sanityClient.min.js +2 -2
@@ -1,4 +1,8 @@
1
- import type {ActionError, Any, ErrorProps, MutationError} from '../types'
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 (body.error && body.message) {
45
- props.message = `${body.error} - ${body.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(body) || isActionError(body)) {
51
- const allItems = body.error.items || []
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 = `${body.error.description}${itemsStr}`
84
+ props.message = `${error.description}${itemsStr}`
61
85
  props.details = body.error
62
86
  return props
63
87
  }
64
88
 
65
- // Query/database errors ({error: {description, other, arb, props}})
66
- if (body.error && body.error.description) {
67
- props.message = body.error.description
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 = body.error || body.message || httpErrorMessage(res)
105
+ props.message = httpErrorMessage(res, body)
74
106
  return props
75
107
  }
76
108
 
77
- function isMutationError(body: Any): body is MutationError {
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
- isPlainObject(body) &&
80
- isPlainObject(body.error) &&
81
- body.error.type === 'mutationError' &&
82
- typeof body.error.description === 'string'
120
+ 'type' in error &&
121
+ error.type === 'actionError' &&
122
+ 'description' in error &&
123
+ typeof error.description === 'string'
83
124
  )
84
125
  }
85
126
 
86
- function isActionError(body: Any): body is ActionError {
127
+ /** @internal */
128
+ export function isQueryParseError(error: object): error is QueryParseError {
87
129
  return (
88
- isPlainObject(body) &&
89
- isPlainObject(body.error) &&
90
- body.error.type === 'actionError' &&
91
- typeof body.error.description === 'string'
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
- function isPlainObject(obj: Any): obj is Record<string, unknown> {
96
- return typeof obj === 'object' && obj !== null && !Array.isArray(obj)
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
@@ -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, {uri: `/projects/${projectId}`})
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, {uri: `/projects/${projectId}`}),
77
+ _request<SanityProject>(this.#client.config(), this.#httpRequest, {
78
+ uri: `/projects/${projectId}`,
79
+ }),
76
80
  )
77
81
  }
78
82
  }