1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import { Baggage , baggageEntryMetadataFromString } from '@opentelemetry/api' ;
16+ import { Baggage , BaggageEntryMetadata , baggageEntryMetadataFromString } from '@opentelemetry/api' ;
1717import {
1818 BAGGAGE_ITEMS_SEPARATOR ,
1919 BAGGAGE_PROPERTIES_SEPARATOR ,
2020 BAGGAGE_KEY_PAIR_SEPARATOR ,
2121 BAGGAGE_MAX_TOTAL_LENGTH ,
2222} from './constants' ;
2323
24- export const serializeKeyPairs = ( keyPairs : string [ ] ) => {
24+ type ParsedBaggageKeyValue = { key : string , value : string , metadata : BaggageEntryMetadata | undefined } ;
25+
26+ export function serializeKeyPairs ( keyPairs : string [ ] ) : string {
2527 return keyPairs . reduce ( ( hValue : string , current : string ) => {
26- const value = `${ hValue } ${
27- hValue !== '' ? BAGGAGE_ITEMS_SEPARATOR : ''
28- } ${ current } `;
28+ const value = `${ hValue } ${ hValue !== '' ? BAGGAGE_ITEMS_SEPARATOR : ''
29+ } ${ current } `;
2930 return value . length > BAGGAGE_MAX_TOTAL_LENGTH ? hValue : value ;
3031 } , '' ) ;
31- } ;
32+ }
3233
33- export const getKeyPairs = ( baggage : Baggage ) : string [ ] => {
34+ export function getKeyPairs ( baggage : Baggage ) : string [ ] {
3435 return baggage
3536 . getAllEntries ( )
3637 . map (
3738 ( [ key , value ] ) =>
3839 `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value . value ) } `
3940 ) ;
40- } ;
41+ }
4142
42- export const parsePairKeyValue = ( entry : string ) => {
43+ export function parsePairKeyValue ( entry : string ) : ParsedBaggageKeyValue | undefined {
4344 const valueProps = entry . split ( BAGGAGE_PROPERTIES_SEPARATOR ) ;
4445 if ( valueProps . length <= 0 ) return ;
4546 const keyPairPart = valueProps . shift ( ) ;
@@ -55,13 +56,13 @@ export const parsePairKeyValue = (entry: string) => {
5556 ) ;
5657 }
5758 return { key, value, metadata } ;
58- } ;
59+ }
5960
6061/**
6162 * Parse a string serialized in the baggage HTTP Format (without metadata):
6263 * https://github.com/w3c/baggage/blob/master/baggage/HTTP_HEADER_FORMAT.md
6364 */
64- export const parseKeyPairsIntoRecord = ( value ?: string ) = > {
65+ export function parseKeyPairsIntoRecord ( value ?: string ) : Record < string , string > {
6566 if ( typeof value !== 'string' || value . length === 0 ) return { } ;
6667 return value
6768 . split ( BAGGAGE_ITEMS_SEPARATOR )
@@ -70,7 +71,8 @@ export const parseKeyPairsIntoRecord = (value?: string) => {
7071 } )
7172 . filter ( keyPair => keyPair !== undefined && keyPair . value . length > 0 )
7273 . reduce < Record < string , string > > ( ( headers , keyPair ) => {
74+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7375 headers [ keyPair ! . key ] = keyPair ! . value ;
7476 return headers ;
7577 } , { } ) ;
76- } ;
78+ }
0 commit comments