@@ -62,7 +62,7 @@ type KeyMap = Map<
6262 }
6363> ;
6464
65- const buildKeyMap = ( fields : FieldsConfig , map ?: KeyMap ) : KeyMap => {
65+ function buildKeyMap ( fields : FieldsConfig , map ?: KeyMap ) : KeyMap {
6666 if ( ! map ) {
6767 map = new Map ( ) ;
6868 }
@@ -85,17 +85,18 @@ const buildKeyMap = (fields: FieldsConfig, map?: KeyMap): KeyMap => {
8585 }
8686
8787 return map ;
88- } ;
88+ }
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- const stripEmptySlots = ( params : Params ) => {
97+ function stripEmptySlots ( params : Params ) : void {
9898 for ( const [ slot , value ] of Object . entries ( params ) ) {
99+ if ( slot === "body" ) continue ;
99100 if (
100101 value &&
101102 typeof value === "object" &&
@@ -105,21 +106,29 @@ const stripEmptySlots = (params: Params) => {
105106 delete params [ slot as Slot ] ;
106107 }
107108 }
108- } ;
109+ }
109110
110- export const buildClientParams = (
111+ export function buildClientParams (
111112 args : ReadonlyArray < unknown > ,
112113 fields : FieldsConfig ,
113- ) => {
114+ ) : Params {
114115 const params : Params = {
115- body : Object . create ( null ) ,
116116 headers : Object . create ( null ) ,
117117 path : Object . create ( null ) ,
118118 query : Object . create ( null ) ,
119119 } ;
120120
121121 const map = buildKeyMap ( fields ) ;
122122
123+ function writeSlot ( slot : Slot , key : string , value : unknown ) : void {
124+ let record = params [ slot ] as Record < string , unknown > | undefined ;
125+ if ( record === undefined ) {
126+ record = Object . create ( null ) as Record < string , unknown > ;
127+ params [ slot ] = record ;
128+ }
129+ record [ key ] = value ;
130+ }
131+
123132 let config : FieldsConfig [ number ] | undefined ;
124133
125134 for ( const [ index , arg ] of args . entries ( ) ) {
@@ -136,7 +145,7 @@ export const buildClientParams = (
136145 const field = map . get ( config . key ) ! ;
137146 const name = field . map || config . key ;
138147 if ( field . in ) {
139- ( params [ field . in ] as Record < string , unknown > ) [ name ] = arg ;
148+ writeSlot ( field . in , name , arg ) ;
140149 }
141150 } else {
142151 params . body = arg ;
@@ -148,7 +157,7 @@ export const buildClientParams = (
148157 if ( field ) {
149158 if ( field . in ) {
150159 const name = field . map || key ;
151- ( params [ field . in ] as Record < string , unknown > ) [ name ] = value ;
160+ writeSlot ( field . in , name , value ) ;
152161 } else {
153162 params [ field . map ] = value ;
154163 }
@@ -159,13 +168,11 @@ export const buildClientParams = (
159168
160169 if ( extra ) {
161170 const [ prefix , slot ] = extra ;
162- ( params [ slot ] as Record < string , unknown > ) [
163- key . slice ( prefix . length )
164- ] = value ;
171+ writeSlot ( slot , key . slice ( prefix . length ) , value ) ;
165172 } else if ( "allowExtra" in config && config . allowExtra ) {
166173 for ( const [ slot , allowed ] of Object . entries ( config . allowExtra ) ) {
167174 if ( allowed ) {
168- ( params [ slot as Slot ] as Record < string , unknown > ) [ key ] = value ;
175+ writeSlot ( slot as Slot , key , value ) ;
169176 break ;
170177 }
171178 }
@@ -178,4 +185,4 @@ export const buildClientParams = (
178185 stripEmptySlots ( params ) ;
179186
180187 return params ;
181- } ;
188+ }
0 commit comments