( input: unknown, fields: TFields, )
| 243 | } |
| 244 | |
| 245 | export function readFieldInput<TFields extends CommandFieldMap>( |
| 246 | input: unknown, |
| 247 | fields: TFields, |
| 248 | ): InferCommandInput<TFields> { |
| 249 | const record = readInputRecord(input); |
| 250 | const commandOptions = Object.fromEntries( |
| 251 | Object.entries(fields).flatMap(([key, field]) => { |
| 252 | const value = field.read(record, key); |
| 253 | if (field.required && value === undefined) { |
| 254 | throw new Error(`Expected ${key} to be set.`); |
| 255 | } |
| 256 | return value === undefined ? [] : [[key, value]]; |
| 257 | }), |
| 258 | ); |
| 259 | const commonInput = readCommonInput(record, { |
| 260 | readTargetAlias: !Object.hasOwn(fields, 'target'), |
| 261 | }); |
| 262 | return compactRecord({ |
| 263 | ...commonInput, |
| 264 | ...commonToClientOptions(commonInput), |
| 265 | ...commandOptions, |
| 266 | }) as InferCommandInput<TFields>; |
| 267 | } |
| 268 | |
| 269 | export function readInputRecord(input: unknown): Record<string, unknown> { |
| 270 | if (input === undefined || input === null) return {}; |
no test coverage detected
searching dependent graphs…