1- import { parseUrl } from '@hey-api/shared' ;
1+ import { getBaseUrl } from '@hey-api/shared' ;
22
33import { getTypedConfig } from '../../../config/utils' ;
44import { clientFolderAbsolutePath } from '../../../generate/client' ;
55import { $ } from '../../../ts-dsl' ;
66import type { PluginHandler } from './types' ;
77import { getClientBaseUrlKey } from './utils' ;
88
9- const resolveBaseUrlString = ( { plugin } : Parameters < PluginHandler > [ 0 ] ) : string | undefined => {
10- const { baseUrl } = plugin . config ;
11-
12- if ( baseUrl === false ) {
13- return ;
14- }
15-
16- if ( typeof baseUrl === 'string' ) {
17- return baseUrl ;
18- }
19-
20- const { servers } = plugin . context . ir ;
21-
22- if ( ! servers ) {
23- return ;
24- }
25-
26- return servers [ typeof baseUrl === 'number' ? baseUrl : 0 ] ?. url ;
27- } ;
28-
299export const createClient : PluginHandler = ( { plugin } ) => {
3010 const clientModule = clientFolderAbsolutePath ( getTypedConfig ( plugin ) ) ;
3111 const symbolCreateClient = plugin . symbol ( 'createClient' , {
@@ -47,26 +27,13 @@ export const createClient: PluginHandler = ({ plugin }) => {
4727 } )
4828 : undefined ;
4929
50- const defaultVals = $ . object ( ) ;
30+ const baseUrl = getBaseUrl ( plugin . config . baseUrl ?? true , plugin . context . ir ) ;
5131
52- const resolvedBaseUrl = resolveBaseUrlString ( {
53- plugin : plugin as any ,
54- } ) ;
55- if ( resolvedBaseUrl ) {
56- const url = parseUrl ( resolvedBaseUrl ) ;
57- if ( url . protocol && url . host && ! resolvedBaseUrl . includes ( '{' ) ) {
58- defaultVals . prop ( getClientBaseUrlKey ( getTypedConfig ( plugin ) ) , $ . literal ( resolvedBaseUrl ) ) ;
59- } else if ( resolvedBaseUrl !== '/' && resolvedBaseUrl . startsWith ( '/' ) ) {
60- const baseUrl = resolvedBaseUrl . endsWith ( '/' )
61- ? resolvedBaseUrl . slice ( 0 , - 1 )
62- : resolvedBaseUrl ;
63- defaultVals . prop ( getClientBaseUrlKey ( getTypedConfig ( plugin ) ) , $ . literal ( baseUrl ) ) ;
64- }
65- }
66-
67- if ( 'throwOnError' in plugin . config && plugin . config . throwOnError ) {
68- defaultVals . prop ( 'throwOnError' , $ . literal ( true ) ) ;
69- }
32+ const defaultVals = $ . object ( )
33+ . $if ( baseUrl , ( o , v ) => o . prop ( getClientBaseUrlKey ( getTypedConfig ( plugin ) ) , $ . literal ( v ) ) )
34+ . $if ( 'throwOnError' in plugin . config && plugin . config . throwOnError , ( o ) =>
35+ o . prop ( 'throwOnError' , $ . literal ( true ) ) ,
36+ ) ;
7037
7138 const createConfigParameters = [
7239 $ ( symbolCreateConfig )
0 commit comments