import { IncomingHttpHeaders } from 'http' ; import { pick } from '@kbn/std' ; /**
 * Converts an object type to a new object type where each string
 * key is copied to the values of the object, and non string keys are
 * given a `never` value. This allows us to map over the values and
 * get the list of all string keys on a type in `KnownKeys<T>`
 */ type StringKeysAsVals < T > = { [ K in keyof T ] : string extends K ? never : number extends K ? never : K ; } ; example type KnownKeys < T > = StringKeysAsVals < T > extends { [ _ in keyof T ] : infer U ; } ? U : never ; public export type KnownHeaders = KnownKeys < IncomingHttpHeaders > ; public export type Headers = { [ header in KnownHeaders ] ? : string | string [ ] | undefined ; } & { [ header ] : string | string [ ] | undefined ; } ; public export type ResponseHeaders = Record < KnownHeaders , string | string [ ] > | Record < string , string | string [ ] > ; const normalizeHeaderField = ( field ) => field . trim ( ) . toLowerCase ( ) ; export function filterHeaders ( headers , fieldsToKeep , fieldsToExclude = [ ] ) { const fieldsToExcludeNormalized = fieldsToExclude . map ( normalizeHeaderField ) ; const fieldsToKeepNormalized = fieldsToKeep . map ( normalizeHeaderField ) . filter ( ( name ) => ! fieldsToExcludeNormalized . includes ( name ) ) ; return pick ( headers , fieldsToKeepNormalized ) ; }