import { createContextToken , createReader } from '@marblejs/core' ; import * as O from 'fp-ts/lib/Option' ; import { constVoid , pipe } from 'fp-ts/lib/function' ; import { getHttpRequestMetadataIdHeader } from '../../+internal/metadata.util' ; import { HttpEffectResponse } from '../../effects/http.effects.interface' ; import { HttpRequestMetadata , WithHttpRequest } from '../../http.interface' ; export type HttpRequestMetadataStorage = Readonly < ReturnType < typeof HttpRequestMetadataStorage > > ; export const HttpRequestMetadataStorageToken = createContextToken < HttpRequestMetadataStorage > ( 'HttpRequestMetadataStorage' ) ; export const HttpRequestMetadataStorage = createReader ( ( ) => { const storage = new Map < string , HttpRequestMetadata > ( ) ; const set = ( key , metadata ) : void => { if ( key && metadata ) storage . set ( key , metadata ) ; } ; const get = ( key ) : HttpRequestMetadata | undefined => { const metadata = storage . get ( key ) ; storage . delete ( key ) ; return metadata ; } ; const size = ( ) => storage . size ; const collect = ( response ) : void => pipe ( getHttpRequestMetadataIdHeader ( response . request . headers ) , O . fold ( constVoid , requestId => set ( requestId , response . request . meta ) ) ) ; return { param key returns get , param key param metadata returns set , returns size , param response returns collect , } ; } ) ;