@@ -29,7 +29,10 @@ function isSanityClientLike(
29
29
return client && 'clientConfig' in client ? typeof client . clientConfig === 'object' : false
30
30
}
31
31
32
- function rewriteSpecName ( key : string ) {
32
+ /**
33
+ * @internal
34
+ */
35
+ export function rewriteSpecName ( key : string ) {
33
36
const specs = SPEC_NAME_TO_URL_NAME_MAPPINGS
34
37
for ( const entry of specs ) {
35
38
const [ specName , param ] = entry
@@ -44,35 +47,50 @@ function rewriteSpecName(key: string) {
44
47
/**
45
48
* @public
46
49
*/
47
- export default 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 ) ) {
52
57
// Inherit config from client
53
- const { apiHost : apiUrl , projectId, dataset} = options . config ( )
58
+ const { apiHost : apiUrl , projectId, dataset} = _options . config ( )
54
59
const apiHost = apiUrl || 'https://api.sanity.io'
55
- return new ImageUrlBuilder ( null , {
60
+ options = {
56
61
baseUrl : apiHost . replace ( / ^ h t t p s : \/ \/ a p i \. / , 'https://cdn.' ) ,
57
62
projectId,
58
63
dataset,
59
- } )
64
+ }
60
65
}
61
66
62
67
// Did we get a SanityClient?
63
- if ( isSanityClientLike ( options ) ) {
68
+ else if ( isSanityClientLike ( _options ) ) {
64
69
// Inherit config from client
65
- const { apiHost : apiUrl , projectId, dataset} = options . clientConfig
70
+ const { apiHost : apiUrl , projectId, dataset} = _options . clientConfig
66
71
const apiHost = apiUrl || 'https://api.sanity.io'
67
- return new ImageUrlBuilder ( null , {
72
+ options = {
68
73
baseUrl : apiHost . replace ( / ^ h t t p s : \/ \/ a p i \. / , 'https://cdn.' ) ,
69
74
projectId,
70
75
dataset,
71
- } )
76
+ }
72
77
}
73
78
74
79
// 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 >
85
+ }
86
+
87
+ /**
88
+ * @public
89
+ */
90
+ export default function urlBuilder (
91
+ options ?: SanityClientLike | SanityProjectDetails | SanityModernClientLike
92
+ ) {
93
+ return createBuilder ( ImageUrlBuilder , options )
76
94
}
77
95
78
96
/**
@@ -87,7 +105,7 @@ export class ImageUrlBuilder {
87
105
: { ...( options || { } ) } // Copy options
88
106
}
89
107
90
- withOptions ( options : Partial < ImageUrlBuilderOptionsWithAliases > ) {
108
+ protected constructNewOptions ( options : Partial < ImageUrlBuilderOptionsWithAliases > ) {
91
109
const baseUrl = options . baseUrl || this . options . baseUrl
92
110
93
111
const newOptions : { [ key : string ] : any } = { baseUrl}
@@ -97,8 +115,12 @@ export class ImageUrlBuilder {
97
115
newOptions [ specKey ] = options [ key ]
98
116
}
99
117
}
118
+ return { baseUrl, ...newOptions }
119
+ }
100
120
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
102
124
}
103
125
104
126
// The image to be represented. Accepts a Sanity 'image'-document, 'asset'-document or
0 commit comments