@sanity/client 5.0.0-esm.2 → 5.0.0-esm.4
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 +442 -17
- package/dist/index.browser.cjs +252 -176
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +252 -176
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +253 -177
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +425 -367
- package/dist/index.js +253 -177
- package/dist/index.js.map +1 -1
- package/package.json +32 -29
- package/src/SanityClient.ts +181 -242
- package/src/assets/AssetsClient.ts +14 -12
- package/src/auth/AuthClient.ts +15 -9
- package/src/config.ts +2 -2
- package/src/data/dataMethods.ts +32 -4
- package/src/data/listen.ts +10 -9
- package/src/data/patch.ts +34 -31
- package/src/data/transaction.ts +5 -0
- package/src/datasets/DatasetsClient.ts +75 -57
- package/src/http/errors.ts +2 -0
- package/src/http/request.ts +1 -3
- package/src/index.browser.ts +3 -3
- package/src/index.ts +3 -42
- package/src/projects/ProjectsClient.ts +39 -23
- package/src/types.ts +60 -0
- package/src/users/UsersClient.ts +37 -28
- package/src/validators.ts +4 -2
- package/umd/sanityClient.js +5449 -5396
- package/umd/sanityClient.min.js +12 -12
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import {type Observable} from 'rxjs'
|
|
1
|
+
import {type Observable, lastValueFrom} from 'rxjs'
|
|
2
2
|
|
|
3
|
+
import {_request} from '../data/dataMethods'
|
|
3
4
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
4
|
-
import type {DatasetAclMode, DatasetResponse, DatasetsResponse} from '../types'
|
|
5
|
+
import type {DatasetAclMode, DatasetResponse, DatasetsResponse, HttpRequest} from '../types'
|
|
5
6
|
import * as validate from '../validators'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
/** @internal */
|
|
9
|
+
export class ObservableDatasetsClient {
|
|
10
|
+
#client: ObservableSanityClient
|
|
11
|
+
#httpRequest: HttpRequest
|
|
12
|
+
constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
|
|
13
|
+
this.#client = client
|
|
14
|
+
this.#httpRequest = httpRequest
|
|
15
|
+
}
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
18
|
* Create a new dataset with the given name
|
|
@@ -13,21 +20,8 @@ export class BaseDatasetsClient {
|
|
|
13
20
|
* @param name - Name of the dataset to create
|
|
14
21
|
* @param options - Options for the dataset
|
|
15
22
|
*/
|
|
16
|
-
create(
|
|
17
|
-
this
|
|
18
|
-
name: string,
|
|
19
|
-
options?: {aclMode?: DatasetAclMode}
|
|
20
|
-
): Promise<DatasetResponse>
|
|
21
|
-
create(
|
|
22
|
-
this: ObservableDatasetsClient,
|
|
23
|
-
name: string,
|
|
24
|
-
options?: {aclMode?: DatasetAclMode}
|
|
25
|
-
): Observable<DatasetResponse>
|
|
26
|
-
create(
|
|
27
|
-
name: string,
|
|
28
|
-
options?: {aclMode?: DatasetAclMode}
|
|
29
|
-
): Promise<DatasetResponse> | Observable<DatasetResponse> {
|
|
30
|
-
return this.#_modify('PUT', name, options)
|
|
23
|
+
create(name: string, options?: {aclMode?: DatasetAclMode}): Observable<DatasetResponse> {
|
|
24
|
+
return _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PUT', name, options)
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
/**
|
|
@@ -36,21 +30,8 @@ export class BaseDatasetsClient {
|
|
|
36
30
|
* @param name - Name of the dataset to edit
|
|
37
31
|
* @param options - New options for the dataset
|
|
38
32
|
*/
|
|
39
|
-
edit(
|
|
40
|
-
this
|
|
41
|
-
name: string,
|
|
42
|
-
options?: {aclMode?: DatasetAclMode}
|
|
43
|
-
): Promise<DatasetResponse>
|
|
44
|
-
edit(
|
|
45
|
-
this: ObservableDatasetsClient,
|
|
46
|
-
name: string,
|
|
47
|
-
options?: {aclMode?: DatasetAclMode}
|
|
48
|
-
): Observable<DatasetResponse>
|
|
49
|
-
edit(
|
|
50
|
-
name: string,
|
|
51
|
-
options?: {aclMode?: DatasetAclMode}
|
|
52
|
-
): Promise<DatasetResponse> | Observable<DatasetResponse> {
|
|
53
|
-
return this.#_modify('PATCH', name, options)
|
|
33
|
+
edit(name: string, options?: {aclMode?: DatasetAclMode}): Observable<DatasetResponse> {
|
|
34
|
+
return _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PATCH', name, options)
|
|
54
35
|
}
|
|
55
36
|
|
|
56
37
|
/**
|
|
@@ -58,40 +39,77 @@ export class BaseDatasetsClient {
|
|
|
58
39
|
*
|
|
59
40
|
* @param name - Name of the dataset to delete
|
|
60
41
|
*/
|
|
61
|
-
delete(
|
|
62
|
-
|
|
63
|
-
delete(name: string): Promise<{deleted: true}> | Observable<{deleted: true}> {
|
|
64
|
-
return this.#_modify('DELETE', name)
|
|
42
|
+
delete(name: string): Observable<{deleted: true}> {
|
|
43
|
+
return _modify<{deleted: true}>(this.#client, this.#httpRequest, 'DELETE', name)
|
|
65
44
|
}
|
|
66
45
|
|
|
67
46
|
/**
|
|
68
47
|
* Fetch a list of datasets for the configured project
|
|
69
48
|
*/
|
|
70
|
-
list(
|
|
71
|
-
|
|
72
|
-
list(): Promise<DatasetsResponse> | Observable<DatasetsResponse> {
|
|
73
|
-
return this.client.request({uri: '/datasets'})
|
|
49
|
+
list(): Observable<DatasetsResponse> {
|
|
50
|
+
return _request<DatasetsResponse>(this.#client, this.#httpRequest, {uri: '/datasets'})
|
|
74
51
|
}
|
|
52
|
+
}
|
|
75
53
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
54
|
+
/** @internal */
|
|
55
|
+
export class DatasetsClient {
|
|
56
|
+
#client: SanityClient
|
|
57
|
+
#httpRequest: HttpRequest
|
|
58
|
+
constructor(client: SanityClient, httpRequest: HttpRequest) {
|
|
59
|
+
this.#client = client
|
|
60
|
+
this.#httpRequest = httpRequest
|
|
79
61
|
}
|
|
80
|
-
}
|
|
81
62
|
|
|
82
|
-
|
|
83
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Create a new dataset with the given name
|
|
65
|
+
*
|
|
66
|
+
* @param name - Name of the dataset to create
|
|
67
|
+
* @param options - Options for the dataset
|
|
68
|
+
*/
|
|
69
|
+
create(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
|
|
70
|
+
return lastValueFrom(
|
|
71
|
+
_modify<DatasetResponse>(this.#client, this.#httpRequest, 'PUT', name, options)
|
|
72
|
+
)
|
|
73
|
+
}
|
|
84
74
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Edit a dataset with the given name
|
|
77
|
+
*
|
|
78
|
+
* @param name - Name of the dataset to edit
|
|
79
|
+
* @param options - New options for the dataset
|
|
80
|
+
*/
|
|
81
|
+
edit(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
|
|
82
|
+
return lastValueFrom(
|
|
83
|
+
_modify<DatasetResponse>(this.#client, this.#httpRequest, 'PATCH', name, options)
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Delete a dataset with the given name
|
|
89
|
+
*
|
|
90
|
+
* @param name - Name of the dataset to delete
|
|
91
|
+
*/
|
|
92
|
+
delete(name: string): Promise<{deleted: true}> {
|
|
93
|
+
return lastValueFrom(_modify<{deleted: true}>(this.#client, this.#httpRequest, 'DELETE', name))
|
|
88
94
|
}
|
|
89
|
-
}
|
|
90
95
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Fetch a list of datasets for the configured project
|
|
98
|
+
*/
|
|
99
|
+
list(): Promise<DatasetsResponse> {
|
|
100
|
+
return lastValueFrom(
|
|
101
|
+
_request<DatasetsResponse>(this.#client, this.#httpRequest, {uri: '/datasets'})
|
|
102
|
+
)
|
|
96
103
|
}
|
|
97
104
|
}
|
|
105
|
+
|
|
106
|
+
function _modify<R = unknown>(
|
|
107
|
+
client: SanityClient | ObservableSanityClient,
|
|
108
|
+
httpRequest: HttpRequest,
|
|
109
|
+
method: 'DELETE' | 'PATCH' | 'PUT',
|
|
110
|
+
name: string,
|
|
111
|
+
options?: {aclMode?: DatasetAclMode}
|
|
112
|
+
) {
|
|
113
|
+
validate.dataset(name)
|
|
114
|
+
return _request<R>(client, httpRequest, {method, uri: `/datasets/${name}`, body: options})
|
|
115
|
+
}
|
package/src/http/errors.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {BaseError} from 'make-error'
|
|
|
2
2
|
|
|
3
3
|
import {ErrorProps} from '../types'
|
|
4
4
|
|
|
5
|
+
/** @public */
|
|
5
6
|
export class ClientError extends BaseError {
|
|
6
7
|
response: ErrorProps['response']
|
|
7
8
|
statusCode: ErrorProps['statusCode'] = 400
|
|
@@ -15,6 +16,7 @@ export class ClientError extends BaseError {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
/** @public */
|
|
18
20
|
export class ServerError extends BaseError {
|
|
19
21
|
response: ErrorProps['response']
|
|
20
22
|
statusCode: ErrorProps['statusCode'] = 500
|
package/src/http/request.ts
CHANGED
|
@@ -26,9 +26,7 @@ const printWarnings = {
|
|
|
26
26
|
},
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
/**
|
|
30
|
-
* @param envMiddleware Environment-specific middleware.
|
|
31
|
-
*/
|
|
29
|
+
/** @internal */
|
|
32
30
|
export function defineHttpRequest(envMiddleware: Middlewares): HttpRequest {
|
|
33
31
|
const request = getIt([
|
|
34
32
|
...envMiddleware,
|
package/src/index.browser.ts
CHANGED
|
@@ -11,8 +11,8 @@ export * from './types'
|
|
|
11
11
|
|
|
12
12
|
// Set the http client to use for requests, and its environment specific middleware
|
|
13
13
|
const httpRequest = defineHttpRequest(envMiddleware)
|
|
14
|
+
/** @public */
|
|
14
15
|
export const requester = httpRequest.defaultRequester
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
17
|
+
/** @public */
|
|
18
|
+
export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)
|
package/src/index.ts
CHANGED
|
@@ -11,47 +11,8 @@ export * from './types'
|
|
|
11
11
|
|
|
12
12
|
// Set the http client to use for requests, and its environment specific middleware
|
|
13
13
|
const httpRequest = defineHttpRequest(envMiddleware)
|
|
14
|
+
/** @public */
|
|
14
15
|
export const requester = httpRequest.defaultRequester
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/*
|
|
21
|
-
const client = createClient({})
|
|
22
|
-
|
|
23
|
-
client.auth.getLoginProviders().then
|
|
24
|
-
client.observable.auth.getLoginProviders().subscribe
|
|
25
|
-
|
|
26
|
-
client.create({_id: 'id', _type: 'document'}, {}).then()
|
|
27
|
-
client.observable.create({_id: 'id', _type: 'document'}, {}).subscribe()
|
|
28
|
-
|
|
29
|
-
client.delete('id', {returnFirst: true}).then
|
|
30
|
-
|
|
31
|
-
client.config({projectId: 'my-project-id'})
|
|
32
|
-
// @ts-expect-error -- isPromiseAPI isn't allowed
|
|
33
|
-
client.config({projectId: 'my-project-id', isPromiseAPI: false})
|
|
34
|
-
client.fetch('', {}).then
|
|
35
|
-
// @ts-expect-error -- observable is not allowed
|
|
36
|
-
client.fetch('', {}).subscribe
|
|
37
|
-
client.observable.fetch('', {}).subscribe
|
|
38
|
-
// @ts-expect-error -- promise is not allowed
|
|
39
|
-
client.observable.fetch('', {}).then
|
|
40
|
-
|
|
41
|
-
client.assets.upload('image', Buffer.from('')).then
|
|
42
|
-
client.observable.assets.upload('image', Buffer.from('')).subscribe
|
|
43
|
-
// @ts-expect-error -- promise not allowed
|
|
44
|
-
client.observable.assets.upload('image', Buffer.from('')).then
|
|
45
|
-
// @ts-expect-error -- protected property
|
|
46
|
-
client.assets.isPromiseAPI()
|
|
47
|
-
|
|
48
|
-
const observableClient = new ObservableSanityClient(httpRequest, {})
|
|
49
|
-
observableClient.config({projectId: 'my-project-id'})
|
|
50
|
-
// eslint-disable-next-line no-console
|
|
51
|
-
console.log(observableClient.isPromiseAPI() === false)
|
|
52
|
-
observableClient.fetch('', {}).subscribe
|
|
53
|
-
// @ts-expect-error -- promise is not allowed
|
|
54
|
-
observableClient.fetch('', {}).then
|
|
55
|
-
// @ts-expect-error -- observable is not allowed
|
|
56
|
-
observableClient.observable.fetch
|
|
57
|
-
// */
|
|
17
|
+
/** @public */
|
|
18
|
+
export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
import {type Observable} from 'rxjs'
|
|
1
|
+
import {type Observable, lastValueFrom} from 'rxjs'
|
|
2
2
|
|
|
3
|
+
import {_request} from '../data/dataMethods'
|
|
3
4
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
4
|
-
import type {SanityProject} from '../types'
|
|
5
|
+
import type {HttpRequest, SanityProject} from '../types'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
/** @internal */
|
|
8
|
+
export class ObservableProjectsClient {
|
|
9
|
+
#client: ObservableSanityClient
|
|
10
|
+
#httpRequest: HttpRequest
|
|
11
|
+
constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
|
|
12
|
+
this.#client = client
|
|
13
|
+
this.#httpRequest = httpRequest
|
|
14
|
+
}
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
17
|
* Fetch a list of projects the authenticated user has access to
|
|
11
18
|
*/
|
|
12
|
-
list(
|
|
13
|
-
|
|
14
|
-
list(): Promise<SanityProject[]> | Observable<SanityProject[]> {
|
|
15
|
-
return this.client.request({uri: '/projects'})
|
|
19
|
+
list(): Observable<SanityProject[]> {
|
|
20
|
+
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'})
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
/**
|
|
@@ -20,26 +25,37 @@ export class BaseProjectsClient {
|
|
|
20
25
|
*
|
|
21
26
|
* @param projectId - ID of the project to fetch
|
|
22
27
|
*/
|
|
23
|
-
getById(
|
|
24
|
-
|
|
25
|
-
getById(projectId: string): Promise<SanityProject> | Observable<SanityProject> {
|
|
26
|
-
return this.client.request({uri: `/projects/${projectId}`})
|
|
28
|
+
getById(projectId: string): Observable<SanityProject> {
|
|
29
|
+
return _request<SanityProject>(this.#client, this.#httpRequest, {uri: `/projects/${projectId}`})
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
/** @internal */
|
|
34
|
+
export class ProjectsClient {
|
|
35
|
+
#client: SanityClient
|
|
36
|
+
#httpRequest: HttpRequest
|
|
37
|
+
constructor(client: SanityClient, httpRequest: HttpRequest) {
|
|
38
|
+
this.#client = client
|
|
39
|
+
this.#httpRequest = httpRequest
|
|
40
|
+
}
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Fetch a list of projects the authenticated user has access to
|
|
44
|
+
*/
|
|
45
|
+
list(): Promise<SanityProject[]> {
|
|
46
|
+
return lastValueFrom(
|
|
47
|
+
_request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'})
|
|
48
|
+
)
|
|
36
49
|
}
|
|
37
|
-
}
|
|
38
50
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Fetch a project by project ID
|
|
53
|
+
*
|
|
54
|
+
* @param projectId - ID of the project to fetch
|
|
55
|
+
*/
|
|
56
|
+
getById(projectId: string): Promise<SanityProject> {
|
|
57
|
+
return lastValueFrom(
|
|
58
|
+
_request<SanityProject>(this.#client, this.#httpRequest, {uri: `/projects/${projectId}`})
|
|
59
|
+
)
|
|
44
60
|
}
|
|
45
61
|
}
|