@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.
@@ -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
- export class BaseDatasetsClient {
8
- client: SanityClient | ObservableSanityClient
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: DatasetsClient,
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: DatasetsClient,
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(this: DatasetsClient, name: string): Promise<{deleted: true}>
62
- delete(this: ObservableDatasetsClient, name: string): Observable<{deleted: true}>
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(this: DatasetsClient): Promise<DatasetsResponse>
71
- list(this: ObservableDatasetsClient): Observable<DatasetsResponse>
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
- #_modify(method: 'DELETE' | 'PATCH' | 'PUT', name: string, options?: {aclMode?: DatasetAclMode}) {
77
- validate.dataset(name)
78
- return this.client.request({method, uri: `/datasets/${name}`, body: options})
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
- export class ObservableDatasetsClient extends BaseDatasetsClient {
83
- client: ObservableSanityClient
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
- constructor(client: ObservableSanityClient) {
86
- super()
87
- this.client = client
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
- export class DatasetsClient extends BaseDatasetsClient {
92
- client: SanityClient
93
- constructor(client: SanityClient) {
94
- super()
95
- this.client = client
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
+ }
@@ -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
@@ -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,
@@ -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
- export function createClient(config: ClientConfig) {
17
- return new SanityClient(httpRequest, config)
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
- export function createClient(config: ClientConfig) {
17
- return new SanityClient(httpRequest, config)
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
- export class BaseProjectsClient {
7
- client: SanityClient | ObservableSanityClient
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(this: ProjectsClient): Promise<SanityProject[]>
13
- list(this: ObservableProjectsClient): Observable<SanityProject[]>
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(this: ProjectsClient, projectId: string): Promise<SanityProject>
24
- getById(this: ObservableProjectsClient, projectId: string): Observable<SanityProject>
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
- export class ObservableProjectsClient extends BaseProjectsClient {
31
- client: ObservableSanityClient
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
- constructor(client: ObservableSanityClient) {
34
- super()
35
- this.client = client
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
- export class ProjectsClient extends BaseProjectsClient {
40
- client: SanityClient
41
- constructor(client: SanityClient) {
42
- super()
43
- this.client = client
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
  }