@@ -29,7 +29,10 @@ function isSanityClientLike(
2929 return client && 'clientConfig' in client ? typeof client . clientConfig === 'object' : false
3030}
3131
32- function rewriteSpecName ( key : string ) {
32+ /**
33+ * @internal
34+ */
35+ export function rewriteSpecName ( key : string ) {
3336 const specs = SPEC_NAME_TO_URL_NAME_MAPPINGS
3437 for ( const entry of specs ) {
3538 const [ specName , param ] = entry
@@ -42,42 +45,57 @@ function rewriteSpecName(key: string) {
4245}
4346
4447/**
45- * @public
48+ * @internal
4649 */
47- export function urlBuilder (
48- options ?: SanityClientLike | SanityProjectDetails | SanityModernClientLike
49- ) {
50- // Did we get a modernish client?
51- if ( isSanityModernClientLike ( options ) ) {
50+ export function createBuilder < C extends typeof ImageUrlBuilder > (
51+ Builder : C ,
52+ _options ?: SanityClientLike | SanityProjectDetails | SanityModernClientLike
53+ ) : InstanceType < C > {
54+ let options : ConstructorParameters < C > [ 1 ] = { }
55+
56+ if ( isSanityModernClientLike ( _options ) ) {
5257 // Inherit config from client
53- const { apiHost : apiUrl , projectId, dataset} = options . config ( )
58+ const { apiHost : apiUrl , projectId, dataset} = _options . config ( )
5459 const apiHost = apiUrl || 'https://api.sanity.io'
55- return new ImageUrlBuilder ( null , {
60+ options = {
5661 baseUrl : apiHost . replace ( / ^ h t t p s : \/ \/ a p i \. / , 'https://cdn.' ) ,
5762 projectId,
5863 dataset,
59- } )
64+ }
6065 }
6166
6267 // Did we get a SanityClient?
63- if ( isSanityClientLike ( options ) ) {
68+ else if ( isSanityClientLike ( _options ) ) {
6469 // Inherit config from client
65- const { apiHost : apiUrl , projectId, dataset} = options . clientConfig
70+ const { apiHost : apiUrl , projectId, dataset} = _options . clientConfig
6671 const apiHost = apiUrl || 'https://api.sanity.io'
67- return new ImageUrlBuilder ( null , {
72+ options = {
6873 baseUrl : apiHost . replace ( / ^ h t t p s : \/ \/ a p i \. / , 'https://cdn.' ) ,
6974 projectId,
7075 dataset,
71- } )
76+ }
7277 }
7378
7479 // Or just accept the options as given
75- return new ImageUrlBuilder ( null , options || { } )
80+ else {
81+ options = _options || { }
82+ }
83+
84+ return new Builder ( null , options ) as InstanceType < C >
7685}
7786
7887/**
7988 * @internal
8089 */
90+ export function urlBuilder (
91+ options ?: SanityClientLike | SanityProjectDetails | SanityModernClientLike
92+ ) {
93+ return createBuilder ( ImageUrlBuilder , options )
94+ }
95+
96+ /**
97+ * @public
98+ */
8199export class ImageUrlBuilder {
82100 public options : ImageUrlBuilderOptions
83101
@@ -87,7 +105,7 @@ export class ImageUrlBuilder {
87105 : { ...( options || { } ) } // Copy options
88106 }
89107
90- withOptions ( options : Partial < ImageUrlBuilderOptionsWithAliases > ) {
108+ protected constructNewOptions ( options : Partial < ImageUrlBuilderOptionsWithAliases > ) {
91109 const baseUrl = options . baseUrl || this . options . baseUrl
92110
93111 const newOptions : { [ key : string ] : any } = { baseUrl}
@@ -97,8 +115,12 @@ export class ImageUrlBuilder {
97115 newOptions [ specKey ] = options [ key ]
98116 }
99117 }
118+ return { baseUrl, ...newOptions }
119+ }
100120
101- return new ImageUrlBuilder ( this , { baseUrl, ...newOptions } )
121+ withOptions ( options : Partial < ImageUrlBuilderOptionsWithAliases > ) : this {
122+ const newOptions = this . constructNewOptions ( options )
123+ return new ImageUrlBuilder ( this , newOptions ) as this
102124 }
103125
104126 // The image to be represented. Accepts a Sanity 'image'-document, 'asset'-document or
0 commit comments