| 103 | } |
| 104 | |
| 105 | export const buildClientParams = (args: ReadonlyArray<unknown>, fields: FieldsConfig) => { |
| 106 | const params: Params = { |
| 107 | body: {}, |
| 108 | headers: {}, |
| 109 | path: {}, |
| 110 | query: {}, |
| 111 | } |
| 112 | |
| 113 | const map = buildKeyMap(fields) |
| 114 | |
| 115 | let config: FieldsConfig[number] | undefined |
| 116 | |
| 117 | for (const [index, arg] of args.entries()) { |
| 118 | if (fields[index]) { |
| 119 | config = fields[index] |
| 120 | } |
| 121 | |
| 122 | if (!config) { |
| 123 | continue |
| 124 | } |
| 125 | |
| 126 | if ("in" in config) { |
| 127 | if (config.key) { |
| 128 | const field = map.get(config.key)! |
| 129 | const name = field.map || config.key |
| 130 | if (field.in) { |
| 131 | ;(params[field.in] as Record<string, unknown>)[name] = arg |
| 132 | } |
| 133 | } else { |
| 134 | params.body = arg |
| 135 | } |
| 136 | } else { |
| 137 | for (const [key, value] of Object.entries(arg ?? {})) { |
| 138 | const field = map.get(key) |
| 139 | |
| 140 | if (field) { |
| 141 | if (field.in) { |
| 142 | const name = field.map || key |
| 143 | ;(params[field.in] as Record<string, unknown>)[name] = value |
| 144 | } else { |
| 145 | params[field.map] = value |
| 146 | } |
| 147 | } else { |
| 148 | const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix)) |
| 149 | |
| 150 | if (extra) { |
| 151 | const [prefix, slot] = extra |
| 152 | ;(params[slot] as Record<string, unknown>)[key.slice(prefix.length)] = value |
| 153 | } else if ("allowExtra" in config && config.allowExtra) { |
| 154 | for (const [slot, allowed] of Object.entries(config.allowExtra)) { |
| 155 | if (allowed) { |
| 156 | ;(params[slot as Slot] as Record<string, unknown>)[key] = value |
| 157 | break |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |