@sanity/client 5.0.0-esm.0 → 5.0.0-esm.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "5.0.0-esm.0",
3
+ "version": "5.0.0-esm.2",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,7 +1,7 @@
1
1
  import {lastValueFrom, Observable} from 'rxjs'
2
2
 
3
3
  import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
4
- import {AuthClient, BaseAuthClient, ObservableAuthClient} from './auth/AuthClient'
4
+ import {AuthClient, ObservableAuthClient} from './auth/AuthClient'
5
5
  import {initConfig} from './config'
6
6
  import * as dataMethods from './data/dataMethods'
7
7
  import {_listen} from './data/listen'
@@ -47,7 +47,6 @@ export type {
47
47
  _listen,
48
48
  AssetsClient,
49
49
  AuthClient,
50
- BaseAuthClient,
51
50
  BaseDatasetsClient,
52
51
  BaseProjectsClient,
53
52
  BaseUsersClient,
@@ -61,68 +60,7 @@ export type {
61
60
  UsersClient,
62
61
  }
63
62
 
64
- export class BaseSanityClient {
65
- #clientConfig: InitializedClientConfig
66
-
67
- constructor(config: ClientConfig) {
68
- this.config(config)
69
- }
70
-
71
- /** @internal */
72
- protected config(newConfig?: Partial<ClientConfig>): ClientConfig | this {
73
- if (newConfig === undefined) {
74
- return {...this.#clientConfig}
75
- }
76
-
77
- if (this.#clientConfig && this.#clientConfig.allowReconfigure === false) {
78
- throw new Error(
79
- 'Existing client instance cannot be reconfigured - use `withConfig(newConfig)` to return a new client'
80
- )
81
- }
82
-
83
- this.#clientConfig = initConfig(newConfig, this.#clientConfig || {})
84
- return this
85
- }
86
-
87
- /**
88
- * Returns whether or not this client is using the Promise API (otherwise it is using observables)
89
- * @internal
90
- * @deprecated Internals, should not be used externally
91
- */
92
- // @TODO check if this can be deleted
93
- isPromiseAPI(): this is SanityClient {
94
- return this instanceof SanityClient
95
- }
96
-
97
- /**
98
- * DEPRECATED: Get a Sanity API URL for the URI provided
99
- *
100
- * @deprecated Should be an internal concern
101
- * @param uri URI/path to build URL for
102
- * @param canUseCdn Whether or not to allow using the API CDN for this route
103
- */
104
- getUrl(uri: string, canUseCdn = false) {
105
- const base = canUseCdn ? this.#clientConfig.cdnUrl : this.#clientConfig.url
106
- return `${base}/${uri.replace(/^\//, '')}`
107
- }
108
-
109
- /**
110
- * DEPRECATED: Get a Sanity API URL for the data operation and path provided
111
- *
112
- * @deprecated Should be an internal concern
113
- * @param operation Data operation
114
- * @param path Path to append
115
- */
116
- getDataUrl(operation: string, path?: string): string {
117
- const config = this.config()
118
- const catalog = validators.hasDataset(config)
119
- const baseUri = `/${operation}/${catalog}`
120
- const uri = path ? `${baseUri}/${path}` : baseUri
121
- return `/data${uri}`.replace(/\/($|\?)/, '$1')
122
- }
123
- }
124
-
125
- export class ObservableSanityClient extends BaseSanityClient {
63
+ export class ObservableSanityClient {
126
64
  assets: ObservableAssetsClient
127
65
  auth: ObservableAuthClient
128
66
  datasets: ObservableDatasetsClient
@@ -132,15 +70,16 @@ export class ObservableSanityClient extends BaseSanityClient {
132
70
  /**
133
71
  * Private properties
134
72
  */
73
+ #clientConfig: InitializedClientConfig
135
74
  #httpRequest: HttpRequest
136
75
 
137
76
  constructor(httpRequest: HttpRequest, config: ClientConfig) {
138
- super(config)
77
+ this.config(config)
139
78
 
140
79
  this.#httpRequest = httpRequest
141
80
 
142
81
  this.assets = new ObservableAssetsClient(this, this.#httpRequest)
143
- this.auth = new ObservableAuthClient(this)
82
+ this.auth = new ObservableAuthClient(this, this.#httpRequest)
144
83
  this.datasets = new ObservableDatasetsClient(this)
145
84
  this.projects = new ObservableProjectsClient(this)
146
85
  this.users = new ObservableUsersClient(this)
@@ -162,7 +101,18 @@ export class ObservableSanityClient extends BaseSanityClient {
162
101
  */
163
102
  config(newConfig?: Partial<ClientConfig>): this
164
103
  config(newConfig?: Partial<ClientConfig>): ClientConfig | this {
165
- return super.config(newConfig)
104
+ if (newConfig === undefined) {
105
+ return {...this.#clientConfig}
106
+ }
107
+
108
+ if (this.#clientConfig && this.#clientConfig.allowReconfigure === false) {
109
+ throw new Error(
110
+ 'Existing client instance cannot be reconfigured - use `withConfig(newConfig)` to return a new client'
111
+ )
112
+ }
113
+
114
+ this.#clientConfig = initConfig(newConfig, this.#clientConfig || {})
115
+ return this
166
116
  }
167
117
 
168
118
  /**
@@ -672,9 +622,36 @@ export class ObservableSanityClient extends BaseSanityClient {
672
622
  request<R = any>(options: RawRequestOptions): Observable<R> {
673
623
  return dataMethods._request(this, this.#httpRequest, options)
674
624
  }
625
+
626
+ /**
627
+ * DEPRECATED: Get a Sanity API URL for the URI provided
628
+ *
629
+ * @deprecated Should be an internal concern
630
+ * @param uri URI/path to build URL for
631
+ * @param canUseCdn Whether or not to allow using the API CDN for this route
632
+ */
633
+ getUrl(uri: string, canUseCdn = false) {
634
+ const base = canUseCdn ? this.#clientConfig.cdnUrl : this.#clientConfig.url
635
+ return `${base}/${uri.replace(/^\//, '')}`
636
+ }
637
+
638
+ /**
639
+ * DEPRECATED: Get a Sanity API URL for the data operation and path provided
640
+ *
641
+ * @deprecated Should be an internal concern
642
+ * @param operation Data operation
643
+ * @param path Path to append
644
+ */
645
+ getDataUrl(operation: string, path?: string): string {
646
+ const config = this.config()
647
+ const catalog = validators.hasDataset(config)
648
+ const baseUri = `/${operation}/${catalog}`
649
+ const uri = path ? `${baseUri}/${path}` : baseUri
650
+ return `/data${uri}`.replace(/\/($|\?)/, '$1')
651
+ }
675
652
  }
676
653
 
677
- export class SanityClient extends BaseSanityClient {
654
+ export class SanityClient {
678
655
  assets: AssetsClient
679
656
  auth: AuthClient
680
657
  datasets: DatasetsClient
@@ -689,6 +666,7 @@ export class SanityClient extends BaseSanityClient {
689
666
  /**
690
667
  * Private properties
691
668
  */
669
+ #clientConfig: InitializedClientConfig
692
670
  #httpRequest: HttpRequest
693
671
 
694
672
  /**
@@ -697,17 +675,17 @@ export class SanityClient extends BaseSanityClient {
697
675
  listen = _listen
698
676
 
699
677
  constructor(httpRequest: HttpRequest, config: ClientConfig) {
700
- super(config)
678
+ this.observable = new ObservableSanityClient(httpRequest, config)
679
+
680
+ this.config(config)
701
681
 
702
682
  this.#httpRequest = httpRequest
703
683
 
704
684
  this.assets = new AssetsClient(this, this.#httpRequest)
705
- this.auth = new AuthClient(this)
685
+ this.auth = new AuthClient(this, this.#httpRequest)
706
686
  this.datasets = new DatasetsClient(this)
707
687
  this.projects = new ProjectsClient(this)
708
688
  this.users = new UsersClient(this)
709
-
710
- this.observable = new ObservableSanityClient(httpRequest, this.config())
711
689
  }
712
690
 
713
691
  /**
@@ -726,11 +704,20 @@ export class SanityClient extends BaseSanityClient {
726
704
  */
727
705
  config(newConfig?: Partial<ClientConfig>): this
728
706
  config(newConfig?: Partial<ClientConfig>): ClientConfig | this {
729
- if (newConfig) {
730
- this.observable.config(newConfig)
707
+ if (newConfig === undefined) {
708
+ return {...this.#clientConfig}
731
709
  }
732
710
 
733
- return super.config(newConfig)
711
+ if (this.#clientConfig && this.#clientConfig.allowReconfigure === false) {
712
+ throw new Error(
713
+ 'Existing client instance cannot be reconfigured - use `withConfig(newConfig)` to return a new client'
714
+ )
715
+ }
716
+
717
+ this.observable.config(newConfig)
718
+
719
+ this.#clientConfig = initConfig(newConfig, this.#clientConfig || {})
720
+ return this
734
721
  }
735
722
 
736
723
  /**
@@ -1258,4 +1245,31 @@ export class SanityClient extends BaseSanityClient {
1258
1245
  dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<any> {
1259
1246
  return lastValueFrom(dataMethods._dataRequest(this, this.#httpRequest, endpoint, body, options))
1260
1247
  }
1248
+
1249
+ /**
1250
+ * DEPRECATED: Get a Sanity API URL for the URI provided
1251
+ *
1252
+ * @deprecated Should be an internal concern
1253
+ * @param uri URI/path to build URL for
1254
+ * @param canUseCdn Whether or not to allow using the API CDN for this route
1255
+ */
1256
+ getUrl(uri: string, canUseCdn = false) {
1257
+ const base = canUseCdn ? this.#clientConfig.cdnUrl : this.#clientConfig.url
1258
+ return `${base}/${uri.replace(/^\//, '')}`
1259
+ }
1260
+
1261
+ /**
1262
+ * DEPRECATED: Get a Sanity API URL for the data operation and path provided
1263
+ *
1264
+ * @deprecated Should be an internal concern
1265
+ * @param operation Data operation
1266
+ * @param path Path to append
1267
+ */
1268
+ getDataUrl(operation: string, path?: string): string {
1269
+ const config = this.config()
1270
+ const catalog = validators.hasDataset(config)
1271
+ const baseUri = `/${operation}/${catalog}`
1272
+ const uri = path ? `${baseUri}/${path}` : baseUri
1273
+ return `/data${uri}`.replace(/\/($|\?)/, '$1')
1274
+ }
1261
1275
  }
@@ -1,43 +1,61 @@
1
- import {type Observable} from 'rxjs'
1
+ import {type Observable, lastValueFrom} from 'rxjs'
2
2
 
3
+ import * as dataMethods from '../data/dataMethods'
3
4
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
4
- import type {AuthProviderResponse} from '../types'
5
+ import type {AuthProviderResponse, HttpRequest} from '../types'
5
6
 
6
- export class BaseAuthClient {
7
- client: SanityClient | ObservableSanityClient
7
+ export class ObservableAuthClient {
8
+ #client: ObservableSanityClient
9
+ #httpRequest: HttpRequest
10
+ constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
11
+ this.#client = client
12
+ this.#httpRequest = httpRequest
13
+ }
8
14
 
9
15
  /**
10
16
  * Fetch available login providers
11
17
  */
12
- getLoginProviders(this: AuthClient): Promise<AuthProviderResponse>
13
- getLoginProviders(this: ObservableAuthClient): Observable<AuthProviderResponse>
14
- getLoginProviders(): Promise<AuthProviderResponse> | Observable<AuthProviderResponse> {
15
- return this.client.request({uri: '/auth/providers'})
18
+ getLoginProviders<R = AuthProviderResponse>(): Observable<R> {
19
+ return dataMethods._request<R>(this.#client, this.#httpRequest, {uri: '/auth/providers'})
16
20
  }
17
21
 
18
22
  /**
19
23
  * Revoke the configured session/token
20
24
  */
21
- logout(this: AuthClient): Promise<any>
22
- logout(this: ObservableAuthClient): Observable<any>
23
- logout(): Promise<any> | Observable<any> {
24
- return this.client.request({uri: '/auth/logout', method: 'POST'})
25
+ logout<R = any>(): Observable<R> {
26
+ return dataMethods._request<R>(this.#client, this.#httpRequest, {
27
+ uri: '/auth/logout',
28
+ method: 'POST',
29
+ })
25
30
  }
26
31
  }
27
32
 
28
- export class ObservableAuthClient extends BaseAuthClient {
29
- client: ObservableSanityClient
33
+ export class AuthClient {
34
+ #client: SanityClient
35
+ #httpRequest: HttpRequest
36
+ constructor(client: SanityClient, httpRequest: HttpRequest) {
37
+ this.#client = client
38
+ this.#httpRequest = httpRequest
39
+ }
30
40
 
31
- constructor(client: ObservableSanityClient) {
32
- super()
33
- this.client = client
41
+ /**
42
+ * Fetch available login providers
43
+ */
44
+ getLoginProviders<R = AuthProviderResponse>(): Promise<R> {
45
+ return lastValueFrom(
46
+ dataMethods._request<R>(this.#client, this.#httpRequest, {uri: '/auth/providers'})
47
+ )
34
48
  }
35
- }
36
49
 
37
- export class AuthClient extends BaseAuthClient {
38
- client: SanityClient
39
- constructor(client: SanityClient) {
40
- super()
41
- this.client = client
50
+ /**
51
+ * Revoke the configured session/token
52
+ */
53
+ logout<R = any>(): Promise<R> {
54
+ return lastValueFrom(
55
+ dataMethods._request<R>(this.#client, this.#httpRequest, {
56
+ uri: '/auth/logout',
57
+ method: 'POST',
58
+ })
59
+ )
42
60
  }
43
61
  }