@@ -35,6 +35,8 @@ export type BaseQuery<R extends BaseRequest = BaseRequest, Q = unknown> = {
35
35
} ;
36
36
37
37
export type BaseSettings < S extends RecursiveRecord = RecursiveRecord > = S & {
38
+ /** The domain name (i.e. https://api.domain.ext) */
39
+ endpoint : string ;
38
40
/** A cors proxy endpoint to use for requests when in a browser */
39
41
corsProxy ?: string ;
40
42
/** A cors prefix to use for requests when in a browser */
@@ -118,8 +120,10 @@ export class ClientEndpoint<
118
120
opts : Option ;
119
121
body ?: BaseBody < string | keyof Parameter > ;
120
122
init ?: BaseInit ;
123
+ transform ?: ( param : Parameter ) => Parameter ;
121
124
validate ?: ( param : Parameter ) => boolean ;
122
125
cached : Cache extends true ? Omit < this, 'cached' > & ClientEndpointCache < Parameter , Response > : never ;
126
+ resolve : ( param ?: Parameter ) => URL ;
123
127
124
128
constructor ( template : BaseTemplate < Parameter , Option > ) {
125
129
this . method = template . method ;
@@ -129,7 +133,11 @@ export class ClientEndpoint<
129
133
this . body = template . body ;
130
134
131
135
this . validate = template . validate ;
136
+ this . transform = template . transform ;
132
137
this . cached = ( template . opts ?. cache ? this : null ) as never ;
138
+ this . resolve = ( ) => {
139
+ throw new Error ( 'Not implemented' ) ;
140
+ } ;
133
141
}
134
142
}
135
143
@@ -172,7 +180,7 @@ export abstract class BaseClient<
172
180
private _authentication : ObservableState < AuthenticationType > ;
173
181
private _callListeners : Observable < QueryType > ;
174
182
175
- protected get settings ( ) {
183
+ get settings ( ) {
176
184
if ( this . _settings . corsProxy ) return { ...this . _settings , endpoint : this . _settings . corsProxy } ;
177
185
return this . _settings ;
178
186
}
@@ -282,9 +290,18 @@ export abstract class BaseClient<
282
290
}
283
291
} ;
284
292
293
+ const parseUrl = ( param : Record < string , unknown > = { } ) => {
294
+ const _params = template . transform ?.( param ) ?? param ;
295
+ template . validate ?.( _params ) ;
296
+ return this . _parseUrl ( template , _params ) ;
297
+ } ;
298
+
285
299
Object . entries ( client [ endpoint ] ) . forEach ( ( [ key , value ] ) => {
286
300
if ( key === 'cached' ) {
287
301
if ( template . opts ?. cache ) Object . defineProperty ( fn , 'cached' , { value : cachedFn } ) ;
302
+ } else if ( key === 'resolve' ) {
303
+ Object . defineProperty ( fn , 'resolve' , { value : parseUrl } ) ;
304
+ if ( template . opts ?. cache ) Object . defineProperty ( cachedFn , 'resolve' , { value : parseUrl } ) ;
288
305
} else {
289
306
Object . defineProperty ( fn , key , { value } ) ;
290
307
if ( template . opts ?. cache ) Object . defineProperty ( cachedFn , key , { value } ) ;
0 commit comments