import * as t from 'io-ts' ; import { omitBy , isPlainObject , isEmpty } from 'lodash' ; import { isLeft } from 'fp-ts/lib/Either' ; import { PathReporter } from 'io-ts/lib/PathReporter' ; import Boom from '@hapi/boom' ; import { strictKeysRt } from '@kbn/io-ts-utils/strict_keys_rt' ; import { RouteParamsRT } from './typings' ; interface KibanaRequestParams { body : unknown ; query : unknown ; params : unknown ; } export function decodeRequestParams < T extends RouteParamsRT > ( params , paramsRt ) { const paramMap = omitBy ( { path : params . params , body : params . body , query : params . query , } , ( val ) => val === null || val === undefined || ( isPlainObject ( val ) && isEmpty ( val ) ) ) ; const result = strictKeysRt ( paramsRt ) . decode ( paramMap ) ; if ( isLeft ( result ) ) { throw Boom . badRequest ( PathReporter . report ( result ) [ 0 ] ) ; } return result . right ; }