@@ -88,32 +88,39 @@ function buildKeyMap(fields: FieldsConfig, map?: KeyMap): KeyMap {
8888}
8989
9090interface Params {
91- body : unknown ;
91+ body ? : unknown ;
9292 headers : Record < string , unknown > ;
9393 path : Record < string , unknown > ;
9494 query : Record < string , unknown > ;
9595}
9696
97- type ParamsSlotMap = Record < Slot , unknown > ;
98-
99- function stripEmptySlots ( params : ParamsSlotMap ) : void {
97+ function stripEmptySlots ( params : Params ) : void {
10098 for ( const [ slot , value ] of Object . entries ( params ) ) {
99+ if ( slot === 'body' ) continue ;
101100 if ( value && typeof value === 'object' && ! Array . isArray ( value ) && ! Object . keys ( value ) . length ) {
102101 delete params [ slot as Slot ] ;
103102 }
104103 }
105104}
106105
107106export function buildClientParams ( args : ReadonlyArray < unknown > , fields : FieldsConfig ) : Params {
108- const params : ParamsSlotMap = {
109- body : Object . create ( null ) ,
107+ const params : Params = {
110108 headers : Object . create ( null ) ,
111109 path : Object . create ( null ) ,
112110 query : Object . create ( null ) ,
113111 } ;
114112
115113 const map = buildKeyMap ( fields ) ;
116114
115+ function writeSlot ( slot : Slot , key : string , value : unknown ) : void {
116+ let record = params [ slot ] as Record < string , unknown > | undefined ;
117+ if ( record === undefined ) {
118+ record = Object . create ( null ) as Record < string , unknown > ;
119+ params [ slot ] = record ;
120+ }
121+ record [ key ] = value ;
122+ }
123+
117124 let config : FieldsConfig [ number ] | undefined ;
118125
119126 for ( const [ index , arg ] of args . entries ( ) ) {
@@ -130,7 +137,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
130137 const field = map . get ( config . key ) ! ;
131138 const name = field . map || config . key ;
132139 if ( field . in ) {
133- ( params [ field . in ] as Record < string , unknown > ) [ name ] = arg ;
140+ writeSlot ( field . in , name , arg ) ;
134141 }
135142 } else {
136143 params . body = arg ;
@@ -142,7 +149,7 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
142149 if ( field ) {
143150 if ( field . in ) {
144151 const name = field . map || key ;
145- ( params [ field . in ] as Record < string , unknown > ) [ name ] = value ;
152+ writeSlot ( field . in , name , value ) ;
146153 } else {
147154 params [ field . map ] = value ;
148155 }
@@ -151,11 +158,11 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
151158
152159 if ( extra ) {
153160 const [ prefix , slot ] = extra ;
154- ( params [ slot ] as Record < string , unknown > ) [ key . slice ( prefix . length ) ] = value ;
161+ writeSlot ( slot , key . slice ( prefix . length ) , value ) ;
155162 } else if ( 'allowExtra' in config && config . allowExtra ) {
156163 for ( const [ slot , allowed ] of Object . entries ( config . allowExtra ) ) {
157164 if ( allowed ) {
158- ( params [ slot as Slot ] as Record < string , unknown > ) [ key ] = value ;
165+ writeSlot ( slot as Slot , key , value ) ;
159166 break ;
160167 }
161168 }
@@ -167,5 +174,5 @@ export function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsCo
167174
168175 stripEmptySlots ( params ) ;
169176
170- return params as Params ;
177+ return params ;
171178}
0 commit comments