@sanity/client 4.0.1 → 5.0.0-esm.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/index.browser.cjs +1335 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.js +1312 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.cjs +1344 -0
- package/dist/index.cjs.js +15 -0
- package/dist/index.cjs.map +1 -0
- package/{sanityClient.d.ts → dist/index.d.ts} +991 -1117
- package/dist/index.js +1321 -0
- package/dist/index.js.map +1 -0
- package/package.json +81 -73
- package/src/SanityClient.ts +1261 -0
- package/src/assets/AssetsClient.ts +164 -0
- package/src/auth/AuthClient.ts +43 -0
- package/src/config.ts +95 -0
- package/src/data/dataMethods.ts +328 -0
- package/src/data/encodeQueryString.ts +28 -0
- package/src/data/listen.ts +195 -0
- package/src/data/patch.ts +353 -0
- package/src/data/transaction.ts +352 -0
- package/src/datasets/DatasetsClient.ts +97 -0
- package/src/generateHelpUrl.ts +5 -0
- package/src/http/browserMiddleware.ts +1 -0
- package/src/http/errors.ts +68 -0
- package/src/http/nodeMiddleware.ts +11 -0
- package/src/http/request.ts +50 -0
- package/src/http/requestOptions.ts +31 -0
- package/src/index.browser.ts +18 -0
- package/src/index.ts +57 -0
- package/src/projects/ProjectsClient.ts +45 -0
- package/src/types.ts +502 -0
- package/src/users/UsersClient.ts +46 -0
- package/src/util/defaults.ts +8 -0
- package/src/util/getSelection.ts +21 -0
- package/src/util/once.ts +12 -0
- package/src/util/pick.ts +9 -0
- package/src/validators.ts +76 -0
- package/src/warnings.ts +25 -0
- package/umd/sanityClient.js +5199 -5302
- package/umd/sanityClient.min.js +13 -13
- package/dist/sanityClient.browser.mjs +0 -2806
- package/dist/sanityClient.browser.mjs.map +0 -7
- package/index.js +0 -7
- package/lib/assets/assetsClient.js +0 -145
- package/lib/auth/authClient.js +0 -26
- package/lib/config.js +0 -88
- package/lib/data/dataMethods.js +0 -205
- package/lib/data/encodeQueryString.js +0 -31
- package/lib/data/listen.js +0 -164
- package/lib/data/patch.js +0 -121
- package/lib/data/transaction.js +0 -117
- package/lib/datasets/datasetsClient.js +0 -41
- package/lib/generateHelpUrl.js +0 -11
- package/lib/http/browserMiddleware.js +0 -9
- package/lib/http/errors.js +0 -56
- package/lib/http/nodeMiddleware.js +0 -22
- package/lib/http/queryString.js +0 -17
- package/lib/http/request.js +0 -52
- package/lib/http/requestOptions.js +0 -30
- package/lib/projects/projectsClient.js +0 -25
- package/lib/sanityClient.js +0 -118
- package/lib/users/usersClient.js +0 -20
- package/lib/util/defaults.js +0 -14
- package/lib/util/getSelection.js +0 -24
- package/lib/util/once.js +0 -20
- package/lib/util/pick.js +0 -17
- package/lib/validators.js +0 -76
- package/lib/warnings.js +0 -27
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const VALID_ASSET_TYPES = ['image', 'file']
|
|
2
|
+
const VALID_INSERT_LOCATIONS = ['before', 'after', 'replace']
|
|
3
|
+
|
|
4
|
+
export const dataset = (name: string) => {
|
|
5
|
+
if (!/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/.test(name)) {
|
|
6
|
+
throw new Error(
|
|
7
|
+
'Datasets can only contain lowercase characters, numbers, underscores and dashes, and start with tilde, and be maximum 64 characters'
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const projectId = (id: any) => {
|
|
13
|
+
if (!/^[-a-z0-9]+$/i.test(id)) {
|
|
14
|
+
throw new Error('`projectId` can only contain only a-z, 0-9 and dashes')
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const validateAssetType = (type: string) => {
|
|
19
|
+
if (VALID_ASSET_TYPES.indexOf(type) === -1) {
|
|
20
|
+
throw new Error(`Invalid asset type: ${type}. Must be one of ${VALID_ASSET_TYPES.join(', ')}`)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const validateObject = (op: string, val: any) => {
|
|
25
|
+
if (val === null || typeof val !== 'object' || Array.isArray(val)) {
|
|
26
|
+
throw new Error(`${op}() takes an object of properties`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const validateDocumentId = (op: string, id: string) => {
|
|
31
|
+
if (typeof id !== 'string' || !/^[a-z0-9_.-]+$/i.test(id)) {
|
|
32
|
+
throw new Error(`${op}(): "${id}" is not a valid document ID`)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const requireDocumentId = (op: string, doc: Record<string, any>) => {
|
|
37
|
+
if (!doc._id) {
|
|
38
|
+
throw new Error(`${op}() requires that the document contains an ID ("_id" property)`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
validateDocumentId(op, doc._id)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const validateInsert = (at: string, selector: string, items: any[]) => {
|
|
45
|
+
const signature = 'insert(at, selector, items)'
|
|
46
|
+
if (VALID_INSERT_LOCATIONS.indexOf(at) === -1) {
|
|
47
|
+
const valid = VALID_INSERT_LOCATIONS.map((loc) => `"${loc}"`).join(', ')
|
|
48
|
+
throw new Error(`${signature} takes an "at"-argument which is one of: ${valid}`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof selector !== 'string') {
|
|
52
|
+
throw new Error(`${signature} takes a "selector"-argument which must be a string`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!Array.isArray(items)) {
|
|
56
|
+
throw new Error(`${signature} takes an "items"-argument which must be an array`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const hasDataset = (config: any): string => {
|
|
61
|
+
if (!config.dataset) {
|
|
62
|
+
throw new Error('`dataset` must be provided to perform queries')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return config.dataset || ''
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const requestTag = (tag: string) => {
|
|
69
|
+
if (typeof tag !== 'string' || !/^[a-z0-9._-]{1,75}$/i.test(tag)) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Tag can only contain alphanumeric characters, underscores, dashes and dots, and be between one and 75 characters long.`
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return tag
|
|
76
|
+
}
|
package/src/warnings.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import generateHelpUrl from './generateHelpUrl'
|
|
2
|
+
import once from './util/once'
|
|
3
|
+
|
|
4
|
+
const createWarningPrinter = (message: string[]) =>
|
|
5
|
+
// eslint-disable-next-line no-console
|
|
6
|
+
once((...args: any[]) => console.warn(message.join(' '), ...args))
|
|
7
|
+
|
|
8
|
+
export const printCdnWarning = createWarningPrinter([
|
|
9
|
+
'You are not using the Sanity CDN. That means your data is always fresh, but the CDN is faster and',
|
|
10
|
+
`cheaper. Think about it! For more info, see ${generateHelpUrl('js-client-cdn-configuration')}.`,
|
|
11
|
+
'To hide this warning, please set the `useCdn` option to either `true` or `false` when creating',
|
|
12
|
+
'the client.',
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
export const printBrowserTokenWarning = createWarningPrinter([
|
|
16
|
+
'You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.',
|
|
17
|
+
`See ${generateHelpUrl(
|
|
18
|
+
'js-client-browser-token'
|
|
19
|
+
)} for more information and how to hide this warning.`,
|
|
20
|
+
])
|
|
21
|
+
|
|
22
|
+
export const printNoApiVersionSpecifiedWarning = createWarningPrinter([
|
|
23
|
+
'Using the Sanity client without specifying an API version is deprecated.',
|
|
24
|
+
`See ${generateHelpUrl('js-client-api-version')}`,
|
|
25
|
+
])
|